Vita
category_set.cc
Go to the documentation of this file.
1
13#include <algorithm>
14#include <iterator>
15
17#include "utility/utility.h"
18
19namespace vita
20{
21
22const category_info category_info::null = {};
23
31 : columns_()
32{
33 category_t categories(0);
34
35 for (const auto &c : cols)
36 {
37 category_t id;
38 if (c.domain == d_void)
39 id = undefined_category;
40 else if (t == typing::strong || c.domain == d_string)
41 id = categories++;
42 else
43 {
44 const auto it(std::find_if(begin(), end(),
45 [target = c.domain](const auto &x)
46 {
47 return x.domain == target;
48 }));
49
50 if (it == columns_.end())
51 id = categories++;
52 else
53 id = it->category;
54 }
55
56 columns_.push_back({id, c.domain, c.name});
57 }
58}
59
65{
66 const auto it(std::find_if(begin(), end(),
67 [category](const auto &e)
68 {
69 return e.category == category;
70 }));
71 return it == columns_.end() ? category_info::null : *it;
72}
73
78const category_info &category_set::column(std::size_t i) const
79{
80 Expects(i < columns_.size());
81 return columns_[i];
82}
83
88const category_info &category_set::column(const std::string &name) const
89{
90 const auto it(std::find_if(begin(), end(),
91 [name](const auto &e)
92 {
93 return e.name == name;
94 }));
95 return it == columns_.end() ? category_info::null : *it;
96}
97
101std::set<category_t> category_set::used_categories() const
102{
103 std::set<category_t> ret;
104
105 std::transform(begin(), end(), std::inserter(ret, ret.end()),
106 [](const auto &c) { return c.category; });
107
108 return ret;
109}
110
115{
116 // Unique column name (when available).
117 for (std::size_t i(0); i < columns_.size(); ++i)
118 if (!columns_[i].name.empty())
119 for (std::size_t j(i + 1); j < columns_.size(); ++j)
120 if (columns_[j].name == columns_[i].name)
121 return false;
122
123 // Undefined category implies void domain.
124 for (std::size_t i(0); i < columns_.size(); ++i)
125 if (columns_[i].category == undefined_category
126 && columns_[i].domain != d_void)
127 return false;
128
129 // Same category implies same domain.
130 for (std::size_t i(0); i < columns_.size(); ++i)
131 {
132 const category_t category(columns_[i].category);
133 const domain_t domain(columns_[i].domain);
134
135 for (std::size_t j(i + 1); j < columns_.size(); ++j)
136 if (columns_[j].category == category && columns_[j].domain != domain)
137 return false;
138 }
139
140 return true;
141}
142
150std::ostream &operator<<(std::ostream &s, const category_info &c)
151{
152 return s << c.name << " (category " << c.category << ", domain " << c.domain;
153}
154
162bool operator==(const category_info &lhs, const category_info &rhs)
163{
164 return lhs.category == rhs.category && lhs.domain == rhs.domain
165 && lhs.name == rhs.name;
166}
167
168} // namespace vita
const category_info & category(category_t) const
Definition: category_set.cc:64
const category_info & column(std::size_t) const
Definition: category_set.cc:78
std::set< category_t > used_categories() const
bool is_valid() const
category_set(const dataframe::columns_info &, typing=typing::weak)
Builds a category_set extracting data from a columns_info object.
Definition: category_set.cc:30
Information about the collection of columns (type, name, output index).
Definition: dataframe.h:69
The main namespace for the project.
typing
Category/type management of the dataframe columns.
Definition: category_set.h:39
domain_t
In an environment where a symbol such as '+' may have many different meanings, it's useful to specify...
Definition: value.h:34
std::size_t category_t
A category provide operations which supplement or supersede those of the domain but which are restric...
Definition: common.h:44
std::ostream & operator<<(std::ostream &o, hash_t h)
Mainly useful for debugging / testing.
Definition: cache_hash.cc:56