ordered_vector.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Filename: ordered_vector.h
  2. // Created by: drose (20Feb02)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://www.panda3d.org/license.txt .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #ifndef ORDERED_VECTOR_H
  19. #define ORDERED_VECTOR_H
  20. #include "pandabase.h"
  21. #include "config_util.h"
  22. #include "pvector.h"
  23. #include "pset.h"
  24. #include <algorithm>
  25. ////////////////////////////////////////////////////////////////////
  26. // Class : ordered_vector
  27. // Description : This template class presents an interface similar to
  28. // the STL set or multiset (and ov_set and ov_multiset
  29. // are implemented specifically, below), but it is
  30. // implemented using a vector that is kept always in
  31. // sorted order.
  32. //
  33. // In most cases, an ov_set or ov_multiset may be
  34. // dropped in transparently in place of a set or
  35. // multiset, but the implementation difference has a few
  36. // implications:
  37. //
  38. // (1) The ov_multiset will maintain stability of order
  39. // between elements that sort equally: they are stored
  40. // in the order in which they were added, from back to
  41. // front.
  42. //
  43. // (2) Insert and erase operations into the middle of
  44. // the set can be slow, just as inserting into the
  45. // middle of a vector can be slow. In fact, building up
  46. // an ov_set by inserting elements one at a time is an
  47. // n^2 operation. On the other hand, building up an
  48. // ov_set by adding elements to the end, one at time, is
  49. // somewhat faster than building up a traditional set;
  50. // and you can even add unsorted elements with
  51. // push_back() and then call sort() when you're done,
  52. // for a log(n) operation.
  53. //
  54. // (3) Iterators may not be valid for the life of the
  55. // ordered_vector. If the vector reallocates itself,
  56. // all iterators are invalidated.
  57. //
  58. // (4) Random access into the set is easy with the []
  59. // operator.
  60. ////////////////////////////////////////////////////////////////////
  61. template<class Key, class Compare = less<Key> >
  62. class ordered_vector {
  63. private:
  64. typedef pvector<Key> Vector;
  65. public:
  66. // Typedefs
  67. typedef Key key_type;
  68. typedef Key value_type;
  69. typedef Key &reference;
  70. typedef const Key &const_reference;
  71. typedef Compare key_compare;
  72. typedef Compare value_compare;
  73. // Be careful when using the non-const iterators that you do not
  74. // disturb the sorted order of the vector, or that if you do, you
  75. // call sort() when you are done.
  76. typedef Vector::iterator iterator;
  77. typedef Vector::const_iterator const_iterator;
  78. typedef Vector::reverse_iterator reverse_iterator;
  79. typedef Vector::const_reverse_iterator const_reverse_iterator;
  80. typedef Vector::difference_type difference_type;
  81. typedef Vector::size_type size_type;
  82. public:
  83. // Constructors. We don't implement the whole slew of STL
  84. // constructors here yet.
  85. INLINE ordered_vector(const Compare &compare = Compare());
  86. INLINE ordered_vector(const ordered_vector<Key, Compare> &copy);
  87. INLINE ordered_vector<Key, Compare> &operator = (const ordered_vector<Key, Compare> &copy);
  88. INLINE ~ordered_vector();
  89. // Iterator access.
  90. INLINE iterator begin();
  91. INLINE iterator end();
  92. INLINE reverse_iterator rbegin();
  93. INLINE reverse_iterator rend();
  94. INLINE const_iterator begin() const;
  95. INLINE const_iterator end() const;
  96. INLINE const_reverse_iterator rbegin() const;
  97. INLINE const_reverse_iterator rend() const;
  98. // Random access.
  99. INLINE reference operator [] (size_type n);
  100. INLINE const_reference operator [] (size_type n) const;
  101. // Size information.
  102. INLINE size_type size() const;
  103. INLINE size_type max_size() const;
  104. INLINE bool empty() const;
  105. // Equivalence and lexicographical comparisons.
  106. INLINE bool operator == (const ordered_vector<Key, Compare> &other) const;
  107. INLINE bool operator != (const ordered_vector<Key, Compare> &other) const;
  108. INLINE bool operator < (const ordered_vector<Key, Compare> &other) const;
  109. INLINE bool operator > (const ordered_vector<Key, Compare> &other) const;
  110. INLINE bool operator <= (const ordered_vector<Key, Compare> &other) const;
  111. INLINE bool operator >= (const ordered_vector<Key, Compare> &other) const;
  112. // Insert operations.
  113. iterator insert_unique(iterator position, const value_type &key);
  114. iterator insert_nonunique(iterator position, const value_type &key);
  115. INLINE pair<iterator, bool> insert_unique(const value_type &key);
  116. INLINE iterator insert_nonunique(const value_type &key);
  117. // Erase operations.
  118. INLINE iterator erase(iterator position);
  119. INLINE size_type erase(const key_type &key);
  120. INLINE void erase(iterator first, iterator last);
  121. INLINE void clear();
  122. // Find operations.
  123. INLINE iterator find(const key_type &key);
  124. INLINE const_iterator find(const key_type &key) const;
  125. INLINE iterator find_particular(const key_type &key);
  126. INLINE const_iterator find_particular(const key_type &key) const;
  127. INLINE size_type count(const key_type &key) const;
  128. INLINE iterator lower_bound(const key_type &key);
  129. INLINE const_iterator lower_bound(const key_type &key) const;
  130. INLINE iterator upper_bound(const key_type &key);
  131. INLINE const_iterator upper_bound(const key_type &key) const;
  132. INLINE pair<iterator, iterator> equal_range(const key_type &key);
  133. INLINE pair<const_iterator, const_iterator> equal_range(const key_type &key) const;
  134. // Special operations.
  135. INLINE void swap(ordered_vector<Key, Compare> &other);
  136. INLINE void reserve(size_type n);
  137. INLINE void sort_unique();
  138. INLINE void sort_nonunique();
  139. INLINE void push_back(const value_type &key);
  140. private:
  141. INLINE iterator nci(const_iterator iterator);
  142. INLINE iterator find_insert_position(iterator first, iterator last,
  143. const key_type &key);
  144. iterator r_find_insert_position(iterator first, iterator last,
  145. const key_type &key);
  146. const_iterator r_find(const_iterator first, const_iterator last,
  147. const_iterator not_found,
  148. const key_type &key) const;
  149. const_iterator r_find_particular(const_iterator first, const_iterator last,
  150. const_iterator not_found,
  151. const key_type &key) const;
  152. size_type r_count(const_iterator first, const_iterator last,
  153. const key_type &key) const;
  154. const_iterator r_lower_bound(const_iterator first, const_iterator last,
  155. const key_type &key) const;
  156. const_iterator r_upper_bound(const_iterator first, const_iterator last,
  157. const key_type &key) const;
  158. pair<const_iterator, const_iterator>
  159. r_equal_range(const_iterator first, const_iterator last,
  160. const key_type &key) const;
  161. INLINE bool verify_list();
  162. #ifndef NDEBUG
  163. bool verify_list_impl(iterator first, iterator last);
  164. #endif
  165. // This function object is used in sort_unique(). It returns true
  166. // if two consecutive sorted elements are equivalent.
  167. class EquivalentTest {
  168. public:
  169. INLINE EquivalentTest(const Compare &compare);
  170. INLINE bool operator () (const key_type &a, const key_type &b);
  171. Compare _compare;
  172. };
  173. Compare _compare;
  174. Vector _vector;
  175. };
  176. ////////////////////////////////////////////////////////////////////
  177. // Class : ov_set
  178. // Description : A specialization of ordered_vector that emulates a
  179. // standard STL set: one copy of each element is
  180. // allowed.
  181. ////////////////////////////////////////////////////////////////////
  182. template<class Key, class Compare = less<Key> >
  183. class ov_set : public ordered_vector<Key, Compare> {
  184. public:
  185. INLINE ov_set(const Compare &compare = Compare());
  186. INLINE ov_set(const ov_set<Key, Compare> &copy);
  187. INLINE ov_set<Key, Compare> &operator = (const ov_set<Key, Compare> &copy);
  188. INLINE iterator insert(iterator position, const value_type &key);
  189. INLINE pair<iterator, bool> insert(const value_type &key);
  190. INLINE void sort();
  191. };
  192. ////////////////////////////////////////////////////////////////////
  193. // Class : ov_multiset
  194. // Description : A specialization of ordered_vector that emulates a
  195. // standard STL set: many copies of each element are
  196. // allowed.
  197. ////////////////////////////////////////////////////////////////////
  198. template<class Key, class Compare = less<Key> >
  199. class ov_multiset : public ordered_vector<Key, Compare> {
  200. public:
  201. INLINE ov_multiset(const Compare &compare = Compare());
  202. INLINE ov_multiset(const ov_multiset<Key, Compare> &copy);
  203. INLINE ov_multiset<Key, Compare> &operator = (const ov_multiset<Key, Compare> &copy);
  204. INLINE iterator insert(iterator position, const value_type &key);
  205. INLINE iterator insert(const value_type &key);
  206. INLINE void sort();
  207. };
  208. #include "ordered_vector.I"
  209. #include "ordered_vector.T"
  210. #endif