27 typedef COMP key_compare;
28 typedef COMP value_compare;
29 typedef ALLOC allocator_type;
30 typedef const T &reference;
31 typedef const T &const_reference;
34 typedef std::list<T, ALLOC> list_type;
35 typedef typename list_type::iterator list_iterator;
43 typedef typename list_type::const_iterator iterator;
44 typedef typename list_type::const_iterator const_iterator;
45 typedef std::reverse_iterator<iterator> reverse_iterator;
46 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
51 bool operator()(
const T *a,
const T *b)
const {
return comp(*a, *b); }
53 using map_alloc =
typename std::allocator_traits<ALLOC>::template rebind_alloc<
54 std::pair<const T *const, list_iterator>>;
55 using map_type = std::map<const T *, list_iterator, mapcmp, map_alloc>;
57 void init_data_map() {
59 for (
auto it = data.begin(); it != data.end(); it++) data_map.emplace(&*it, it);
61 iterator tr_iter(
typename map_type::iterator i) {
62 if (i == data_map.end())
return data.end();
65 const_iterator tr_iter(
typename map_type::const_iterator i)
const {
66 if (i == data_map.end())
return data.end();
71 typedef typename map_type::size_type size_type;
72 class sorted_iterator :
public std::iterator<std::bidirectional_iterator_tag, T> {
73 friend class ordered_set;
74 typename map_type::const_iterator iter;
75 sorted_iterator(
typename map_type::const_iterator it)
79 const T &operator*()
const {
return *iter->first; }
80 const T *operator->()
const {
return iter->first; }
81 sorted_iterator operator++() {
85 sorted_iterator operator--() {
89 sorted_iterator operator++(
int) {
94 sorted_iterator operator--(
int) {
99 bool operator==(
const sorted_iterator i)
const {
return iter == i.iter; }
100 bool operator!=(
const sorted_iterator i)
const {
return iter != i.iter; }
105 ordered_set(std::initializer_list<T> init) : data(init) { init_data_map(); }
106 template <
typename InputIt>
107 ordered_set(InputIt first, InputIt last) : data(first, last) {
110 ordered_set(ordered_set &&a) =
default;
111 ordered_set &operator=(
const ordered_set &a) {
116 ordered_set &operator=(ordered_set &&a) =
default;
117 bool operator==(
const ordered_set &a)
const {
return data == a.data; }
118 bool operator!=(
const ordered_set &a)
const {
return data != a.data; }
119 bool operator<(
const ordered_set &a)
const {
126 auto it = a.data_map.begin();
127 for (
auto &el : data_map) {
128 if (it == a.data_map.end())
return false;
129 if (mapcmp()(el.first, it->first))
return true;
130 if (mapcmp()(it->first, el.first))
return false;
133 return it != a.data_map.end();
138 iterator begin() noexcept {
return data.begin(); }
139 const_iterator begin() const noexcept {
return data.begin(); }
140 iterator end() noexcept {
return data.end(); }
141 const_iterator end() const noexcept {
return data.end(); }
142 reverse_iterator rbegin() noexcept {
return data.rbegin(); }
143 const_reverse_iterator rbegin() const noexcept {
return data.rbegin(); }
144 reverse_iterator rend() noexcept {
return data.rend(); }
145 const_reverse_iterator rend() const noexcept {
return data.rend(); }
146 const_iterator cbegin() const noexcept {
return data.cbegin(); }
147 const_iterator cend() const noexcept {
return data.cend(); }
148 const_reverse_iterator crbegin() const noexcept {
return data.crbegin(); }
149 const_reverse_iterator crend() const noexcept {
return data.crend(); }
150 sorted_iterator sorted_begin() const noexcept {
return data_map.begin(); }
153 reference front() const noexcept {
return *data.begin(); }
154 reference back() const noexcept {
return *data.rbegin(); }
156 bool empty() const noexcept {
return data.empty(); }
157 size_type size() const noexcept {
return data_map.size(); }
158 size_type max_size() const noexcept {
return data_map.max_size(); }
164 iterator find(
const T &a) {
return tr_iter(data_map.find(&a)); }
165 const_iterator find(
const T &a)
const {
return tr_iter(data_map.find(&a)); }
166 size_type count(
const T &a)
const {
return data_map.count(&a); }
167 iterator upper_bound(
const T &a) {
return tr_iter(data_map.upper_bound(&a)); }
168 const_iterator upper_bound(
const T &a)
const {
return tr_iter(data_map.upper_bound(&a)); }
169 iterator lower_bound(
const T &a) {
return tr_iter(data_map.lower_bound(&a)); }
170 const_iterator lower_bound(
const T &a)
const {
return tr_iter(data_map.lower_bound(&a)); }
172 std::pair<iterator, bool> insert(
const T &v) {
173 iterator it = find(v);
174 if (it == data.end()) {
175 list_iterator it = data.insert(data.end(), v);
176 data_map.emplace(&*it, it);
177 return std::make_pair(it,
true);
179 return std::make_pair(it,
false);
181 std::pair<iterator, bool> insert(T &&v) {
182 iterator it = find(v);
183 if (it == data.end()) {
184 list_iterator it = data.insert(data.end(), std::move(v));
185 data_map.emplace(&*it, it);
186 return std::make_pair(it,
true);
188 return std::make_pair(it,
false);
190 void insert(ordered_set::const_iterator begin, ordered_set::const_iterator end) {
191 for (
auto it = begin; it != end; ++it) insert(*it);
193 iterator insert(const_iterator pos,
const T &v) {
194 iterator it = find(v);
195 if (it == data.end()) {
196 list_iterator it = data.insert(pos, v);
197 data_map.emplace(&*it, it);
202 iterator insert(const_iterator pos, T &&v) {
203 iterator it = find(v);
204 if (it == data.end()) {
205 list_iterator it = data.insert(pos, std::move(v));
206 data_map.emplace(&*it, it);
212 void push_back(
const T &v) {
213 iterator it = find(v);
214 if (it == data.end()) {
215 list_iterator it = data.insert(data.end(), v);
216 data_map.emplace(&*it, it);
218 data.splice(data.end(), data, it);
221 void push_back(T &&v) {
222 iterator it = find(v);
223 if (it == data.end()) {
224 list_iterator it = data.insert(data.end(), std::move(v));
225 data_map.emplace(&*it, it);
227 data.splice(data.end(), data, it);
231 template <
class... Args>
232 std::pair<iterator, bool> emplace(Args &&...args) {
233 auto it = data.emplace(data.end(), std::forward<Args>(args)...);
234 auto old = find(*it);
235 if (old == data.end()) {
236 data_map.emplace(&*it, it);
237 return std::make_pair(it,
true);
240 return std::make_pair(old,
false);
244 template <
class... Args>
245 std::pair<iterator, bool> emplace_back(Args &&...args) {
246 auto it = data.emplace(data.end(), std::forward<Args>(args)...);
247 auto old = find(*it);
248 if (old != data.end()) {
251 data_map.emplace(&*it, it);
252 return std::make_pair(it,
true);
255 iterator erase(const_iterator pos) {
256 auto it = data_map.find(&*pos);
257 assert(it != data_map.end());
260 auto list_it = it->second;
262 return data.erase(list_it);
264 size_type erase(
const T &v) {
266 if (it != data.end()) {