17 #ifndef __ccuty_stringset__
18 #define __ccuty_stringset__
22 #include <initializer_list>
29 #if !defined(NO_STRINGIFY) && !defined(STRINGIFY)
30 # define STRINGIFY(s) _STRINGIFY(s)
32 # define _STRINGIFY(s...) #s
36 #if !defined(NO_STRING_SWITCH) && !defined(STRING_SWITCH)
51 #define STRING_SWITCH(str, ...) \
53 static const ccuty::stringset _stringset_ ## __LINE__(#__VA_ARGS__); \
54 switch(_stringset_ ## __LINE__[str])
95 elem(
const std::string& str,
unsigned int id) : str(str), id(
id) {}
96 const std::string str;
97 const unsigned int id;
101 bool operator()(
const elem& left,
const elem& right)
const {
return left.str<right.str;}
104 using string_set = std::set<elem, string_comp>;
116 std::vector<std::string> v;
126 std::vector<std::string> v;
150 template <
typename T>
explicit stringset(
const T& tokens) {
156 return elements_.empty();
161 return elements_.size();
165 const string_set&
elements()
const {
return elements_;}
169 auto it = elements_.find(elem{token,0});
170 return it==elements_.end() ? -1 : it->id;
179 for (
auto& it : elements_) {
180 if (it.str.compare(0, str.length(), str) == 0) {
181 if (it.str.length() == str.length())
return it.id;
186 if (count == 0)
return -1;
187 else if (count == 1)
return value;
200 auto p = elements_.insert(elem{token, lastid_});
201 if (p.second) lastid_++;
209 auto p = elements_.insert(elem{token, lastid_});
210 if (p.second) lastid_++;
218 auto p = elements_.insert(e);
220 if (e.id >= lastid_) lastid_ = e.id + 1;
229 template <
typename T>
void append(
const T& cont) {
230 for (
auto&& it : cont)
insert(it);
234 unsigned int lastid_{0};
235 string_set elements_;
247 bool operator()(
const elem& left,
const elem& right)
const {
return left.id < right.id;}
249 using id_set = std::set<elem, comp_id>;
252 static const std::string&
none() {
253 static std::string
empty;
267 std::vector<std::string> v;
277 std::vector<std::string> v;
286 explicit istringset(std::initializer_list<const char*> tokens) {
308 const id_set&
ids()
const {
return ids_;}
317 auto it = ids_.find(elem{
"",
id});
318 if (it==ids_.end())
return none();
else return it->str;
331 auto p = elements_.insert(elem{token, lastid_});
334 ids_.insert(*p.first);
343 auto p = elements_.insert(elem{token, lastid_});
346 ids_.insert(*p.first);
355 auto p = elements_.insert(e);
357 if (e.id >= lastid_) lastid_ = e.id + 1;
359 ids_.insert(*p.first);
367 template <
typename T>
void append(
const T& cont) {
368 for (
auto& it : cont)
insert(it);