AssocVector.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // The Loki Library
  3. // Copyright (c) 2001 by Andrei Alexandrescu
  4. // This code accompanies the book:
  5. // Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
  6. // Patterns Applied". Copyright (c) 2001. Addison-Wesley.
  7. // Permission to use, copy, modify, distribute and sell this software for any
  8. // purpose is hereby granted without fee, provided that the above copyright
  9. // notice appear in all copies and that both that copyright notice and this
  10. // permission notice appear in supporting documentation.
  11. // The author or Addison-Wesley Longman make no representations about the
  12. // suitability of this software for any purpose. It is provided "as is"
  13. // without express or implied warranty.
  14. ////////////////////////////////////////////////////////////////////////////////
  15. // Updated 2019 by Matthieu Dartiailh for C++11 compliancy
  16. ////////////////////////////////////////////////////////////////////////////////
  17. #pragma once
  18. // $Id: AssocVector.h 765 2006-10-18 13:55:32Z syntheticpp $
  19. #include <algorithm>
  20. #include <functional>
  21. #include <vector>
  22. #include <utility>
  23. namespace Loki
  24. {
  25. ////////////////////////////////////////////////////////////////////////////////
  26. // class template AssocVectorCompare
  27. // Used by AssocVector
  28. ////////////////////////////////////////////////////////////////////////////////
  29. namespace Private
  30. {
  31. template <class Value, class C>
  32. class AssocVectorCompare : public C
  33. {
  34. typedef std::pair<typename C::first_argument_type, Value>
  35. Data;
  36. typedef typename C::first_argument_type first_argument_type;
  37. public:
  38. AssocVectorCompare()
  39. {}
  40. AssocVectorCompare(const C& src) : C(src)
  41. {}
  42. bool operator()(const first_argument_type& lhs,
  43. const first_argument_type& rhs) const
  44. { return C::operator()(lhs, rhs); }
  45. bool operator()(const Data& lhs, const Data& rhs) const
  46. { return operator()(lhs.first, rhs.first); }
  47. bool operator()(const Data& lhs,
  48. const first_argument_type& rhs) const
  49. { return operator()(lhs.first, rhs); }
  50. bool operator()(const first_argument_type& lhs,
  51. const Data& rhs) const
  52. { return operator()(lhs, rhs.first); }
  53. };
  54. }
  55. ////////////////////////////////////////////////////////////////////////////////
  56. // class template AssocVector
  57. // An associative vector built as a syntactic drop-in replacement for std::map
  58. // BEWARE: AssocVector doesn't respect all map's guarantees, the most important
  59. // being:
  60. // * iterators are invalidated by insert and erase operations
  61. // * the complexity of insert/erase is O(N) not O(log N)
  62. // * value_type is std::pair<K, V> not std::pair<const K, V>
  63. // * iterators are random
  64. ////////////////////////////////////////////////////////////////////////////////
  65. template
  66. <
  67. class K,
  68. class V,
  69. class C = std::less<K>,
  70. class A = std::allocator< std::pair<K, V> >
  71. >
  72. class AssocVector
  73. : private std::vector< std::pair<K, V>, A >
  74. , private Private::AssocVectorCompare<V, C>
  75. {
  76. typedef std::vector<std::pair<K, V>, A> Base;
  77. typedef Private::AssocVectorCompare<V, C> MyCompare;
  78. public:
  79. typedef K key_type;
  80. typedef V mapped_type;
  81. typedef typename Base::value_type value_type;
  82. typedef C key_compare;
  83. typedef A allocator_type;
  84. typedef typename A::reference reference;
  85. typedef typename A::const_reference const_reference;
  86. typedef typename Base::iterator iterator;
  87. typedef typename Base::const_iterator const_iterator;
  88. typedef typename Base::size_type size_type;
  89. typedef typename Base::difference_type difference_type;
  90. typedef typename A::pointer pointer;
  91. typedef typename A::const_pointer const_pointer;
  92. typedef typename Base::reverse_iterator reverse_iterator;
  93. typedef typename Base::const_reverse_iterator const_reverse_iterator;
  94. class value_compare
  95. : public std::function<bool(value_type, value_type)>
  96. , private key_compare
  97. {
  98. friend class AssocVector;
  99. protected:
  100. value_compare(key_compare pred) : key_compare(pred)
  101. {}
  102. public:
  103. bool operator()(const value_type& lhs, const value_type& rhs) const
  104. { return key_compare::operator()(lhs.first, rhs.first); }
  105. };
  106. // 23.3.1.1 construct/copy/destroy
  107. explicit AssocVector(const key_compare& comp = key_compare(),
  108. const A& alloc = A())
  109. : Base(alloc), MyCompare(comp)
  110. {}
  111. template <class InputIterator>
  112. AssocVector(InputIterator first, InputIterator last,
  113. const key_compare& comp = key_compare(),
  114. const A& alloc = A())
  115. : Base(first, last, alloc), MyCompare(comp)
  116. {
  117. MyCompare& me = *this;
  118. std::sort(begin(), end(), me);
  119. }
  120. AssocVector& operator=(const AssocVector& rhs)
  121. {
  122. AssocVector(rhs).swap(*this);
  123. return *this;
  124. }
  125. // iterators:
  126. // The following are here because MWCW gets 'using' wrong
  127. iterator begin() { return Base::begin(); }
  128. const_iterator begin() const { return Base::begin(); }
  129. iterator end() { return Base::end(); }
  130. const_iterator end() const { return Base::end(); }
  131. reverse_iterator rbegin() { return Base::rbegin(); }
  132. const_reverse_iterator rbegin() const { return Base::rbegin(); }
  133. reverse_iterator rend() { return Base::rend(); }
  134. const_reverse_iterator rend() const { return Base::rend(); }
  135. // capacity:
  136. bool empty() const { return Base::empty(); }
  137. size_type size() const { return Base::size(); }
  138. size_type max_size() { return Base::max_size(); }
  139. // 23.3.1.2 element access:
  140. mapped_type& operator[](const key_type& key)
  141. { return insert(value_type(key, mapped_type())).first->second; }
  142. // modifiers:
  143. std::pair<iterator, bool> insert(const value_type& val)
  144. {
  145. bool found(true);
  146. iterator i(lower_bound(val.first));
  147. if (i == end() || this->operator()(val.first, i->first))
  148. {
  149. i = Base::insert(i, val);
  150. found = false;
  151. }
  152. return std::make_pair(i, !found);
  153. }
  154. //Section [23.1.2], Table 69
  155. //http://developer.apple.com/documentation/DeveloperTools/gcc-3.3/libstdc++/23_containers/howto.html#4
  156. iterator insert(iterator pos, const value_type& val)
  157. {
  158. if( (pos == begin() || this->operator()(*(pos-1),val)) &&
  159. (pos == end() || this->operator()(val, *pos)) )
  160. {
  161. return Base::insert(pos, val);
  162. }
  163. return insert(val).first;
  164. }
  165. template <class InputIterator>
  166. void insert(InputIterator first, InputIterator last)
  167. { for (; first != last; ++first) insert(*first); }
  168. void erase(iterator pos)
  169. { Base::erase(pos); }
  170. size_type erase(const key_type& k)
  171. {
  172. iterator i(find(k));
  173. if (i == end()) return 0;
  174. erase(i);
  175. return 1;
  176. }
  177. void erase(iterator first, iterator last)
  178. { Base::erase(first, last); }
  179. void swap(AssocVector& other)
  180. {
  181. Base::swap(other);
  182. MyCompare& me = *this;
  183. MyCompare& rhs = other;
  184. std::swap(me, rhs);
  185. }
  186. void clear()
  187. { Base::clear(); }
  188. // observers:
  189. key_compare key_comp() const
  190. { return *this; }
  191. value_compare value_comp() const
  192. {
  193. const key_compare& comp = *this;
  194. return value_compare(comp);
  195. }
  196. // 23.3.1.3 map operations:
  197. iterator find(const key_type& k)
  198. {
  199. iterator i(lower_bound(k));
  200. if (i != end() && this->operator()(k, i->first))
  201. {
  202. i = end();
  203. }
  204. return i;
  205. }
  206. const_iterator find(const key_type& k) const
  207. {
  208. const_iterator i(lower_bound(k));
  209. if (i != end() && this->operator()(k, i->first))
  210. {
  211. i = end();
  212. }
  213. return i;
  214. }
  215. size_type count(const key_type& k) const
  216. { return find(k) != end(); }
  217. iterator lower_bound(const key_type& k)
  218. {
  219. MyCompare& me = *this;
  220. return std::lower_bound(begin(), end(), k, me);
  221. }
  222. const_iterator lower_bound(const key_type& k) const
  223. {
  224. const MyCompare& me = *this;
  225. return std::lower_bound(begin(), end(), k, me);
  226. }
  227. iterator upper_bound(const key_type& k)
  228. {
  229. MyCompare& me = *this;
  230. return std::upper_bound(begin(), end(), k, me);
  231. }
  232. const_iterator upper_bound(const key_type& k) const
  233. {
  234. const MyCompare& me = *this;
  235. return std::upper_bound(begin(), end(), k, me);
  236. }
  237. std::pair<iterator, iterator> equal_range(const key_type& k)
  238. {
  239. MyCompare& me = *this;
  240. return std::equal_range(begin(), end(), k, me);
  241. }
  242. std::pair<const_iterator, const_iterator> equal_range(
  243. const key_type& k) const
  244. {
  245. const MyCompare& me = *this;
  246. return std::equal_range(begin(), end(), k, me);
  247. }
  248. template <class K1, class V1, class C1, class A1>
  249. friend bool operator==(const AssocVector<K1, V1, C1, A1>& lhs,
  250. const AssocVector<K1, V1, C1, A1>& rhs);
  251. bool operator<(const AssocVector& rhs) const
  252. {
  253. const Base& me = *this;
  254. const Base& yo = rhs;
  255. return me < yo;
  256. }
  257. template <class K1, class V1, class C1, class A1>
  258. friend bool operator!=(const AssocVector<K1, V1, C1, A1>& lhs,
  259. const AssocVector<K1, V1, C1, A1>& rhs);
  260. template <class K1, class V1, class C1, class A1>
  261. friend bool operator>(const AssocVector<K1, V1, C1, A1>& lhs,
  262. const AssocVector<K1, V1, C1, A1>& rhs);
  263. template <class K1, class V1, class C1, class A1>
  264. friend bool operator>=(const AssocVector<K1, V1, C1, A1>& lhs,
  265. const AssocVector<K1, V1, C1, A1>& rhs);
  266. template <class K1, class V1, class C1, class A1>
  267. friend bool operator<=(const AssocVector<K1, V1, C1, A1>& lhs,
  268. const AssocVector<K1, V1, C1, A1>& rhs);
  269. };
  270. template <class K, class V, class C, class A>
  271. inline bool operator==(const AssocVector<K, V, C, A>& lhs,
  272. const AssocVector<K, V, C, A>& rhs)
  273. {
  274. const std::vector<std::pair<K, V>, A>& me = lhs;
  275. return me == rhs;
  276. }
  277. template <class K, class V, class C, class A>
  278. inline bool operator!=(const AssocVector<K, V, C, A>& lhs,
  279. const AssocVector<K, V, C, A>& rhs)
  280. { return !(lhs == rhs); }
  281. template <class K, class V, class C, class A>
  282. inline bool operator>(const AssocVector<K, V, C, A>& lhs,
  283. const AssocVector<K, V, C, A>& rhs)
  284. { return rhs < lhs; }
  285. template <class K, class V, class C, class A>
  286. inline bool operator>=(const AssocVector<K, V, C, A>& lhs,
  287. const AssocVector<K, V, C, A>& rhs)
  288. { return !(lhs < rhs); }
  289. template <class K, class V, class C, class A>
  290. inline bool operator<=(const AssocVector<K, V, C, A>& lhs,
  291. const AssocVector<K, V, C, A>& rhs)
  292. { return !(rhs < lhs); }
  293. // specialized algorithms:
  294. template <class K, class V, class C, class A>
  295. void swap(AssocVector<K, V, C, A>& lhs, AssocVector<K, V, C, A>& rhs)
  296. { lhs.swap(rhs); }
  297. } // namespace Loki