-
Notifications
You must be signed in to change notification settings - Fork 9
/
vector_set
164 lines (125 loc) · 4.4 KB
/
vector_set
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
vector_set
An unordered set-type class implemented with std::vector for very fast
iteration and efficient memory storage for small(ish) data sets
ex: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0429r1.pdf
Qore Programming Language
Copyright (C) 2003 - 2022 Qore Technologies, s.r.o.
constants can only be defined when parsing
constants values will be substituted during the 2nd parse phase
reads and writes are (must be) wrapped under the program-level parse lock
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Note that the Qore library is released under a choice of three open-source
licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
information.
*/
#ifndef _QORE_VECTOR_SET_H
#define _QORE_VECTOR_SET_H
#include <vector>
#include <algorithm>
#include <iterator>
struct string_compare {
DLLLOCAL explicit string_compare(const char* const& baseline) : baseline(baseline) {
}
DLLLOCAL bool operator() (const char* const& arg) {
return !strcmp(arg, baseline);
}
const char* const baseline;
};
template <typename T>
class vector_set_t {
public:
typedef T value_type;
private:
typedef std::vector<value_type> vector_t;
public:
typedef typename vector_t::iterator iterator;
typedef typename vector_t::const_iterator const_iterator;
DLLLOCAL vector_set_t() {
}
DLLLOCAL vector_set_t(const T& v) {
insert(v);
}
DLLLOCAL iterator find(const T& v) {
return std::find(vector.begin(), vector.end(), v);
}
DLLLOCAL const_iterator find(const T& v) const {
return std::find(vector.begin(), vector.end(), v);
}
// same as find, just for map compatibility
DLLLOCAL iterator lower_bound(const T& v) {
return std::find(vector.begin(), vector.end(), v);
}
// same as find, just for map compatibility
DLLLOCAL const_iterator lower_bound(const T& v) const {
return std::find(vector.begin(), vector.end(), v);
}
DLLLOCAL iterator begin() {
return vector.begin();
}
DLLLOCAL const_iterator begin() const {
return vector.begin();
}
DLLLOCAL iterator end() {
return vector.end();
}
DLLLOCAL const_iterator end() const {
return vector.end();
}
DLLLOCAL bool empty() const {
return vector.empty();
}
DLLLOCAL void clear() {
vector.clear();
}
DLLLOCAL size_t size() const {
return vector.size();
}
DLLLOCAL void erase(const T& v) {
iterator i = find(v);
if (i != vector.end())
vector.erase(i);
}
DLLLOCAL void erase(iterator i) {
vector.erase(i);
}
// iterator arg ignored
DLLLOCAL iterator insert(iterator i, const T& v) {
bool created;
return find_create(v, created);
}
DLLLOCAL std::pair<iterator, bool> insert(const T& v) {
bool created;
iterator i = find_create(v, created);
return std::pair<iterator, bool>(i, created);
}
private:
DLLLOCAL iterator find_create(const T& v, bool& created) {
iterator i = find(v);
if (i == vector.end()) {
i = vector.insert(vector.end(), v);
created = true;
}
else {
created = false;
}
return i;
}
vector_t vector;
};
#endif