36 typedef COMP key_compare;
37 typedef COMP value_compare;
38 typedef ALLOC allocator_type;
39 typedef const T &reference;
40 typedef const T &const_reference;
43 typedef std::list<T, ALLOC> list_type;
44 typedef typename list_type::iterator list_iterator;
52 typedef typename list_type::const_iterator iterator;
53 typedef typename list_type::const_iterator const_iterator;
54 typedef std::reverse_iterator<iterator> reverse_iterator;
55 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
60 bool operator()(
const T *a,
const T *b)
const {
return comp(*a, *b); }
62 using map_alloc =
typename std::allocator_traits<ALLOC>::template rebind_alloc<
63 std::pair<const T *const, list_iterator>>;
64 using map_type = std::map<const T *, list_iterator, mapcmp, map_alloc>;
66 void init_data_map() {
68 for (
auto it = data.begin(); it != data.end(); it++) data_map.emplace(&*it, it);
70 iterator tr_iter(
typename map_type::iterator i) {
71 if (i == data_map.end())
return data.end();
74 const_iterator tr_iter(
typename map_type::const_iterator i)
const {
75 if (i == data_map.end())
return data.end();
80 typedef typename map_type::size_type size_type;
81 class sorted_iterator :
public std::iterator<std::bidirectional_iterator_tag, T> {
83 typename map_type::const_iterator iter;
88 const T &operator*()
const {
return *iter->first; }
89 const T *operator->()
const {
return iter->first; }
108 bool operator==(
const sorted_iterator i)
const {
return iter == i.iter; }
109 bool operator!=(
const sorted_iterator i)
const {
return iter != i.iter; }
114 ordered_set(std::initializer_list<T> init) : data(init) { init_data_map(); }
115 template <
typename InputIt>
116 ordered_set(InputIt first, InputIt last) : data(first, last) {
119 ordered_set(ordered_set &&a) =
default;
120 ordered_set &operator=(
const ordered_set &a) {
125 ordered_set &operator=(ordered_set &&a) =
default;
126 bool operator==(
const ordered_set &a)
const {
return data == a.data; }
127 bool operator!=(
const ordered_set &a)
const {
return data != a.data; }
128 bool operator<(
const ordered_set &a)
const {
135 auto it = a.data_map.begin();
136 for (
auto &el : data_map) {
137 if (it == a.data_map.end())
return false;
138 if (mapcmp()(el.first, it->first))
return true;
139 if (mapcmp()(it->first, el.first))
return false;
142 return it != a.data_map.end();
147 iterator begin() noexcept {
return data.begin(); }
148 const_iterator begin() const noexcept {
return data.begin(); }
149 iterator end() noexcept {
return data.end(); }
150 const_iterator end() const noexcept {
return data.end(); }
151 reverse_iterator rbegin() noexcept {
return data.rbegin(); }
152 const_reverse_iterator rbegin() const noexcept {
return data.rbegin(); }
153 reverse_iterator rend() noexcept {
return data.rend(); }
154 const_reverse_iterator rend() const noexcept {
return data.rend(); }
155 const_iterator cbegin() const noexcept {
return data.cbegin(); }
156 const_iterator cend() const noexcept {
return data.cend(); }
157 const_reverse_iterator crbegin() const noexcept {
return data.crbegin(); }
158 const_reverse_iterator crend() const noexcept {
return data.crend(); }
159 sorted_iterator sorted_begin() const noexcept {
return data_map.begin(); }
160 sorted_iterator sorted_end() const noexcept {
return data_map.end(); }
162 reference front() const noexcept {
return *data.begin(); }
163 reference back() const noexcept {
return *data.rbegin(); }
165 bool empty() const noexcept {
return data.empty(); }
166 size_type size() const noexcept {
return data_map.size(); }
167 size_type max_size() const noexcept {
return data_map.max_size(); }
173 iterator find(
const T &a) {
return tr_iter(data_map.find(&a)); }
174 const_iterator find(
const T &a)
const {
return tr_iter(data_map.find(&a)); }
175 size_type count(
const T &a)
const {
return data_map.count(&a); }
176 iterator upper_bound(
const T &a) {
return tr_iter(data_map.upper_bound(&a)); }
177 const_iterator upper_bound(
const T &a)
const {
return tr_iter(data_map.upper_bound(&a)); }
178 iterator lower_bound(
const T &a) {
return tr_iter(data_map.lower_bound(&a)); }
179 const_iterator lower_bound(
const T &a)
const {
return tr_iter(data_map.lower_bound(&a)); }
181 std::pair<iterator, bool> insert(
const T &v) {
182 iterator it = find(v);
183 if (it == data.end()) {
184 list_iterator it = data.insert(data.end(), v);
185 data_map.emplace(&*it, it);
186 return std::make_pair(it,
true);
188 return std::make_pair(it,
false);
190 std::pair<iterator, bool> insert(T &&v) {
191 iterator it = find(v);
192 if (it == data.end()) {
193 list_iterator it = data.insert(data.end(), std::move(v));
194 data_map.emplace(&*it, it);
195 return std::make_pair(it,
true);
197 return std::make_pair(it,
false);
199 void insert(ordered_set::const_iterator begin, ordered_set::const_iterator end) {
200 for (
auto it = begin; it != end; ++it) insert(*it);
202 iterator insert(const_iterator pos,
const T &v) {
203 iterator it = find(v);
204 if (it == data.end()) {
205 list_iterator it = data.insert(pos, v);
206 data_map.emplace(&*it, it);
211 iterator insert(const_iterator pos, T &&v) {
212 iterator it = find(v);
213 if (it == data.end()) {
214 list_iterator it = data.insert(pos, std::move(v));
215 data_map.emplace(&*it, it);
221 void push_back(
const T &v) {
222 iterator it = find(v);
223 if (it == data.end()) {
224 list_iterator it = data.insert(data.end(), v);
225 data_map.emplace(&*it, it);
227 data.splice(data.end(), data, it);
230 void push_back(T &&v) {
231 iterator it = find(v);
232 if (it == data.end()) {
233 list_iterator it = data.insert(data.end(), std::move(v));
234 data_map.emplace(&*it, it);
236 data.splice(data.end(), data, it);
240 template <
class... Args>
241 std::pair<iterator, bool> emplace(Args &&...args) {
242 auto it = data.emplace(data.end(), std::forward<Args>(args)...);
243 auto old = find(*it);
244 if (old == data.end()) {
245 data_map.emplace(&*it, it);
246 return std::make_pair(it,
true);
249 return std::make_pair(old,
false);
253 template <
class... Args>
254 std::pair<iterator, bool> emplace_back(Args &&...args) {
255 auto it = data.emplace(data.end(), std::forward<Args>(args)...);
256 auto old = find(*it);
257 if (old != data.end()) {
260 data_map.emplace(&*it, it);
261 return std::make_pair(it,
true);
264 iterator erase(const_iterator pos) {
265 auto it = data_map.find(&*pos);
266 assert(it != data_map.end());
269 auto list_it = it->second;
271 return data.erase(list_it);
273 size_type erase(
const T &v) {
275 if (it != data.end()) {