AssocVector.h 12 KB

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