ordered_vector.I 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. // Filename: ordered_vector.I
  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. ////////////////////////////////////////////////////////////////////
  19. // Function: ordered_vector::Constructor
  20. // Access: Public
  21. // Description:
  22. ////////////////////////////////////////////////////////////////////
  23. template<class Key, class Compare>
  24. INLINE ordered_vector<Key, Compare>::
  25. ordered_vector(const Compare &compare) :
  26. _compare(compare)
  27. {
  28. }
  29. ////////////////////////////////////////////////////////////////////
  30. // Function: ordered_vector::Copy Constructor
  31. // Access: Public
  32. // Description:
  33. ////////////////////////////////////////////////////////////////////
  34. template<class Key, class Compare>
  35. INLINE ordered_vector<Key, Compare>::
  36. ordered_vector(const ordered_vector<Key, Compare> &copy) :
  37. _compare(copy._compare),
  38. _vector(copy._vector)
  39. {
  40. }
  41. ////////////////////////////////////////////////////////////////////
  42. // Function: ordered_vector::Copy Assignment Operator
  43. // Access: Public
  44. // Description:
  45. ////////////////////////////////////////////////////////////////////
  46. template<class Key, class Compare>
  47. INLINE ordered_vector<Key, Compare> &ordered_vector<Key, Compare>::
  48. operator = (const ordered_vector<Key, Compare> &copy) {
  49. _compare = copy._compare;
  50. _vector = copy._vector;
  51. return *this;
  52. }
  53. ////////////////////////////////////////////////////////////////////
  54. // Function: ordered_vector::Destructor
  55. // Access: Public
  56. // Description:
  57. ////////////////////////////////////////////////////////////////////
  58. template<class Key, class Compare>
  59. INLINE ordered_vector<Key, Compare>::
  60. ~ordered_vector() {
  61. }
  62. ////////////////////////////////////////////////////////////////////
  63. // Function: ordered_vector::begin
  64. // Access: Public
  65. // Description: Returns the iterator that marks the first element in
  66. // the ordered vector.
  67. ////////////////////////////////////////////////////////////////////
  68. template<class Key, class Compare>
  69. INLINE TYPENAME ordered_vector<Key, Compare>::ITERATOR ordered_vector<Key, Compare>::
  70. begin() {
  71. return _vector.begin();
  72. }
  73. ////////////////////////////////////////////////////////////////////
  74. // Function: ordered_vector::end
  75. // Access: Public
  76. // Description: Returns the iterator that marks the end of the
  77. // ordered vector.
  78. ////////////////////////////////////////////////////////////////////
  79. template<class Key, class Compare>
  80. INLINE TYPENAME ordered_vector<Key, Compare>::ITERATOR ordered_vector<Key, Compare>::
  81. end() {
  82. return _vector.end();
  83. }
  84. ////////////////////////////////////////////////////////////////////
  85. // Function: ordered_vector::rbegin
  86. // Access: Public
  87. // Description: Returns the iterator that marks the first element in
  88. // the ordered vector, when viewed in reverse order.
  89. ////////////////////////////////////////////////////////////////////
  90. template<class Key, class Compare>
  91. INLINE TYPENAME ordered_vector<Key, Compare>::REVERSE_ITERATOR ordered_vector<Key, Compare>::
  92. rbegin() {
  93. return _vector.rbegin();
  94. }
  95. ////////////////////////////////////////////////////////////////////
  96. // Function: ordered_vector::rend
  97. // Access: Public
  98. // Description: Returns the iterator that marks the end of the
  99. // ordered vector, when viewed in reverse order.
  100. ////////////////////////////////////////////////////////////////////
  101. template<class Key, class Compare>
  102. INLINE TYPENAME ordered_vector<Key, Compare>::REVERSE_ITERATOR ordered_vector<Key, Compare>::
  103. rend() {
  104. return _vector.rend();
  105. }
  106. ////////////////////////////////////////////////////////////////////
  107. // Function: ordered_vector::begin
  108. // Access: Public
  109. // Description: Returns the iterator that marks the first element in
  110. // the ordered vector.
  111. ////////////////////////////////////////////////////////////////////
  112. template<class Key, class Compare>
  113. INLINE TYPENAME ordered_vector<Key, Compare>::CONST_ITERATOR ordered_vector<Key, Compare>::
  114. begin() const {
  115. return _vector.begin();
  116. }
  117. ////////////////////////////////////////////////////////////////////
  118. // Function: ordered_vector::end
  119. // Access: Public
  120. // Description: Returns the iterator that marks the end of the
  121. // ordered vector.
  122. ////////////////////////////////////////////////////////////////////
  123. template<class Key, class Compare>
  124. INLINE TYPENAME ordered_vector<Key, Compare>::CONST_ITERATOR ordered_vector<Key, Compare>::
  125. end() const {
  126. return _vector.end();
  127. }
  128. ////////////////////////////////////////////////////////////////////
  129. // Function: ordered_vector::rbegin
  130. // Access: Public
  131. // Description: Returns the iterator that marks the first element in
  132. // the ordered vector, when viewed in reverse order.
  133. ////////////////////////////////////////////////////////////////////
  134. template<class Key, class Compare>
  135. INLINE TYPENAME ordered_vector<Key, Compare>::CONST_REVERSE_ITERATOR ordered_vector<Key, Compare>::
  136. rbegin() const {
  137. return _vector.rbegin();
  138. }
  139. ////////////////////////////////////////////////////////////////////
  140. // Function: ordered_vector::rend
  141. // Access: Public
  142. // Description: Returns the iterator that marks the end of the
  143. // ordered vector, when viewed in reverse order.
  144. ////////////////////////////////////////////////////////////////////
  145. template<class Key, class Compare>
  146. INLINE TYPENAME ordered_vector<Key, Compare>::CONST_REVERSE_ITERATOR ordered_vector<Key, Compare>::
  147. rend() const {
  148. return _vector.rend();
  149. }
  150. ////////////////////////////////////////////////////////////////////
  151. // Function: ordered_vector::operator []
  152. // Access: Public
  153. // Description: Returns the nth element.
  154. ////////////////////////////////////////////////////////////////////
  155. template<class Key, class Compare>
  156. INLINE TYPENAME ordered_vector<Key, Compare>::REFERENCE ordered_vector<Key, Compare>::
  157. operator [] (TYPENAME ordered_vector<Key, Compare>::SIZE_TYPE n) {
  158. return _vector[n];
  159. }
  160. ////////////////////////////////////////////////////////////////////
  161. // Function: ordered_vector::operator []
  162. // Access: Public
  163. // Description: Returns the nth element.
  164. ////////////////////////////////////////////////////////////////////
  165. template<class Key, class Compare>
  166. INLINE TYPENAME ordered_vector<Key, Compare>::CONST_REFERENCE ordered_vector<Key, Compare>::
  167. operator [] (TYPENAME ordered_vector<Key, Compare>::SIZE_TYPE n) const {
  168. return _vector[n];
  169. }
  170. ////////////////////////////////////////////////////////////////////
  171. // Function: ordered_vector::size
  172. // Access: Public
  173. // Description: Returns the number of elements in the ordered vector.
  174. ////////////////////////////////////////////////////////////////////
  175. template<class Key, class Compare>
  176. INLINE TYPENAME ordered_vector<Key, Compare>::SIZE_TYPE ordered_vector<Key, Compare>::
  177. size() const {
  178. return _vector.size();
  179. }
  180. ////////////////////////////////////////////////////////////////////
  181. // Function: ordered_vector::max_size
  182. // Access: Public
  183. // Description: Returns the maximum number of elements that can
  184. // possibly be stored in an ordered vector.
  185. ////////////////////////////////////////////////////////////////////
  186. template<class Key, class Compare>
  187. INLINE TYPENAME ordered_vector<Key, Compare>::SIZE_TYPE ordered_vector<Key, Compare>::
  188. max_size() const {
  189. return _vector.max_size();
  190. }
  191. ////////////////////////////////////////////////////////////////////
  192. // Function: ordered_vector::empty
  193. // Access: Public
  194. // Description: Returns true if the ordered vector is empty, false
  195. // otherwise.
  196. ////////////////////////////////////////////////////////////////////
  197. template<class Key, class Compare>
  198. INLINE bool ordered_vector<Key, Compare>::
  199. empty() const {
  200. return _vector.empty();
  201. }
  202. ////////////////////////////////////////////////////////////////////
  203. // Function: ordered_vector::operator ==
  204. // Access: Public
  205. // Description: Returns true if the two ordered vectors are
  206. // memberwise equivalent, false otherwise.
  207. ////////////////////////////////////////////////////////////////////
  208. template<class Key, class Compare>
  209. INLINE bool ordered_vector<Key, Compare>::
  210. operator == (const ordered_vector<Key, Compare> &other) const {
  211. return _vector == other._vector;
  212. }
  213. ////////////////////////////////////////////////////////////////////
  214. // Function: ordered_vector::operator !=
  215. // Access: Public
  216. // Description: Returns true if the two ordered vectors are not
  217. // memberwise equivalent, false if they are.
  218. ////////////////////////////////////////////////////////////////////
  219. template<class Key, class Compare>
  220. INLINE bool ordered_vector<Key, Compare>::
  221. operator != (const ordered_vector<Key, Compare> &other) const {
  222. return _vector != other._vector;
  223. }
  224. ////////////////////////////////////////////////////////////////////
  225. // Function: ordered_vector::operator <
  226. // Access: Public
  227. // Description: Returns true if this ordered vector sorts
  228. // lexicographically before the other one, false
  229. // otherwise.
  230. ////////////////////////////////////////////////////////////////////
  231. template<class Key, class Compare>
  232. INLINE bool ordered_vector<Key, Compare>::
  233. operator < (const ordered_vector<Key, Compare> &other) const {
  234. return _vector < other._vector;
  235. }
  236. ////////////////////////////////////////////////////////////////////
  237. // Function: ordered_vector::operator >
  238. // Access: Public
  239. // Description: Returns true if this ordered vector sorts
  240. // lexicographically after the other one, false
  241. // otherwise.
  242. ////////////////////////////////////////////////////////////////////
  243. template<class Key, class Compare>
  244. INLINE bool ordered_vector<Key, Compare>::
  245. operator > (const ordered_vector<Key, Compare> &other) const {
  246. return _vector > other._vector;
  247. }
  248. ////////////////////////////////////////////////////////////////////
  249. // Function: ordered_vector::operator <=
  250. // Access: Public
  251. // Description: Returns true if this ordered vector sorts
  252. // lexicographically before the other one or is
  253. // equivalent, false otherwise.
  254. ////////////////////////////////////////////////////////////////////
  255. template<class Key, class Compare>
  256. INLINE bool ordered_vector<Key, Compare>::
  257. operator <= (const ordered_vector<Key, Compare> &other) const {
  258. return _vector <= other._vector;
  259. }
  260. ////////////////////////////////////////////////////////////////////
  261. // Function: ordered_vector::operator >=
  262. // Access: Public
  263. // Description: Returns true if this ordered vector sorts
  264. // lexicographically after the other one or is
  265. // equivalent, false otherwise.
  266. ////////////////////////////////////////////////////////////////////
  267. template<class Key, class Compare>
  268. INLINE bool ordered_vector<Key, Compare>::
  269. operator >= (const ordered_vector<Key, Compare> &other) const {
  270. return _vector >= other._vector;
  271. }
  272. ////////////////////////////////////////////////////////////////////
  273. // Function: ordered_vector::insert_unique
  274. // Access: Public
  275. // Description: Inserts the indicated key into the ordered vector, at
  276. // the appropriate place. If there is already an element
  277. // sorting equivalent to the key in the vector, the new
  278. // key is not inserted.
  279. //
  280. // The return value is a pair, where the first component
  281. // is the iterator referencing the new element (or the
  282. // original element), and the second componet is true if
  283. // the insert operation has taken place.
  284. ////////////////////////////////////////////////////////////////////
  285. template<class Key, class Compare>
  286. INLINE pair<TYPENAME ordered_vector<Key, Compare>::ITERATOR, bool> ordered_vector<Key, Compare>::
  287. insert_unique(const TYPENAME ordered_vector<Key, Compare>::VALUE_TYPE &key) {
  288. ITERATOR position = find_insert_position(begin(), end(), key);
  289. #ifdef NDEBUG
  290. pair<ITERATOR, bool> bogus_result(end(), false);
  291. nassertr(position >= begin() && position <= end(), bogus_result);
  292. #endif
  293. // If there's already an equivalent key in the vector, it's at
  294. // *(position - 1).
  295. if (position != begin() && !_compare(*(position - 1), key)) {
  296. pair<ITERATOR, bool> result(position - 1, false);
  297. nassertr(!_compare(key, *(position - 1)), result);
  298. return result;
  299. }
  300. ITERATOR result = _vector.insert(position, key);
  301. return pair<ITERATOR, bool>(result, true);
  302. }
  303. ////////////////////////////////////////////////////////////////////
  304. // Function: ordered_vector::insert_nonunique
  305. // Access: Public
  306. // Description: Inserts the indicated key into the ordered vector, at
  307. // the appropriate place. If there are already elements
  308. // sorting equivalent to the key in the vector, the new
  309. // value is inserted following them.
  310. // The return value is the iterator referencing the new
  311. // element.
  312. ////////////////////////////////////////////////////////////////////
  313. template<class Key, class Compare>
  314. INLINE TYPENAME ordered_vector<Key, Compare>::ITERATOR ordered_vector<Key, Compare>::
  315. insert_nonunique(const TYPENAME ordered_vector<Key, Compare>::VALUE_TYPE &key) {
  316. ITERATOR position = find_insert_position(begin(), end(), key);
  317. nassertr(position >= begin() && position <= end(), end());
  318. ITERATOR result = _vector.insert(position, key);
  319. return result;
  320. }
  321. ////////////////////////////////////////////////////////////////////
  322. // Function: ordered_vector::erase, with iterator
  323. // Access: Public
  324. // Description: Removes the element indicated by the given iterator,
  325. // and returns the next sequential iterator.
  326. ////////////////////////////////////////////////////////////////////
  327. template<class Key, class Compare>
  328. INLINE TYPENAME ordered_vector<Key, Compare>::ITERATOR ordered_vector<Key, Compare>::
  329. erase(TYPENAME ordered_vector<Key, Compare>::ITERATOR position) {
  330. SIZE_TYPE count = position - begin();
  331. _vector.erase(position);
  332. return begin() + count;
  333. }
  334. ////////////////////////////////////////////////////////////////////
  335. // Function: ordered_vector::erase, with key
  336. // Access: Public
  337. // Description: Removes all elements matching the indicated key;
  338. // returns the number of elements removed.
  339. ////////////////////////////////////////////////////////////////////
  340. template<class Key, class Compare>
  341. INLINE TYPENAME ordered_vector<Key, Compare>::SIZE_TYPE ordered_vector<Key, Compare>::
  342. erase(const TYPENAME ordered_vector<Key, Compare>::KEY_TYPE &key) {
  343. pair<ITERATOR, ITERATOR> result = equal_range(key);
  344. SIZE_TYPE count = result.second - result.first;
  345. erase(result.first, result.second);
  346. return count;
  347. }
  348. ////////////////////////////////////////////////////////////////////
  349. // Function: ordered_vector::erase, a range
  350. // Access: Public
  351. // Description: Removes all elements indicated by the given iterator
  352. // range.
  353. ////////////////////////////////////////////////////////////////////
  354. template<class Key, class Compare>
  355. INLINE void ordered_vector<Key, Compare>::
  356. erase(TYPENAME ordered_vector<Key, Compare>::ITERATOR first,
  357. TYPENAME ordered_vector<Key, Compare>::ITERATOR last) {
  358. _vector.erase(first, last);
  359. }
  360. ////////////////////////////////////////////////////////////////////
  361. // Function: ordered_vector::clear
  362. // Access: Public
  363. // Description: Removes all elements from the ordered vector.
  364. ////////////////////////////////////////////////////////////////////
  365. template<class Key, class Compare>
  366. INLINE void ordered_vector<Key, Compare>::
  367. clear() {
  368. _vector.erase(_vector.begin(), _vector.end());
  369. }
  370. ////////////////////////////////////////////////////////////////////
  371. // Function: ordered_vector::find
  372. // Access: Public
  373. // Description: Searches for an element with the indicated key and
  374. // returns its iterator if it is found, or end() if it
  375. // is not. If there are multiple elements matching the
  376. // key, the particular iterator returned is not defined.
  377. ////////////////////////////////////////////////////////////////////
  378. template<class Key, class Compare>
  379. INLINE TYPENAME ordered_vector<Key, Compare>::ITERATOR ordered_vector<Key, Compare>::
  380. find(const TYPENAME ordered_vector<Key, Compare>::KEY_TYPE &key) {
  381. return nci(r_find(begin(), end(), end(), key));
  382. }
  383. ////////////////////////////////////////////////////////////////////
  384. // Function: ordered_vector::find
  385. // Access: Public
  386. // Description: Searches for an element with the indicated key and
  387. // returns its iterator if it is found, or end() if it
  388. // is not. If there are multiple elements matching the
  389. // key, the particular iterator returned is not defined.
  390. ////////////////////////////////////////////////////////////////////
  391. template<class Key, class Compare>
  392. INLINE TYPENAME ordered_vector<Key, Compare>::CONST_ITERATOR ordered_vector<Key, Compare>::
  393. find(const TYPENAME ordered_vector<Key, Compare>::KEY_TYPE &key) const {
  394. return r_find(begin(), end(), end(), key);
  395. }
  396. ////////////////////////////////////////////////////////////////////
  397. // Function: ordered_vector::find_particular
  398. // Access: Public
  399. // Description: Searches for a particular element and returns its
  400. // iterator if it is found, or end() if it is not.
  401. //
  402. // First, the Compare function is used to narrow down
  403. // the range of elements the element might be located
  404. // within; then the element is compared elementwise, via
  405. // ==, until the exact matching element is found. If
  406. // multiple matches exist within the vector, the
  407. // particular iterator returned is not defined.
  408. //
  409. // The assumption is that == implies !Compare(a, b) and
  410. // !Compare(b, a), but not necessarily the converse.
  411. ////////////////////////////////////////////////////////////////////
  412. template<class Key, class Compare>
  413. INLINE TYPENAME ordered_vector<Key, Compare>::ITERATOR ordered_vector<Key, Compare>::
  414. find_particular(const TYPENAME ordered_vector<Key, Compare>::KEY_TYPE &key) {
  415. return nci(r_find_particular(begin(), end(), end(), key));
  416. }
  417. ////////////////////////////////////////////////////////////////////
  418. // Function: ordered_vector::find_particular
  419. // Access: Public
  420. // Description: Searches for a particular element and returns its
  421. // iterator if it is found, or end() if it is not.
  422. //
  423. // First, the Compare function is used to narrow down
  424. // the range of elements the element might be located
  425. // within; then the element is compared elementwise, via
  426. // ==, until the exact matching element is found. If
  427. // multiple matches exist within the vector, the
  428. // particular iterator returned is not defined./
  429. ////////////////////////////////////////////////////////////////////
  430. template<class Key, class Compare>
  431. INLINE TYPENAME ordered_vector<Key, Compare>::CONST_ITERATOR ordered_vector<Key, Compare>::
  432. find_particular(const TYPENAME ordered_vector<Key, Compare>::KEY_TYPE &key) const {
  433. return r_find_particular(begin(), end(), end(), key);
  434. }
  435. ////////////////////////////////////////////////////////////////////
  436. // Function: ordered_vector::count
  437. // Access: Public
  438. // Description: Returns the number of elements that sort equivalent
  439. // to the key that are in the vector.
  440. ////////////////////////////////////////////////////////////////////
  441. template<class Key, class Compare>
  442. INLINE TYPENAME ordered_vector<Key, Compare>::SIZE_TYPE ordered_vector<Key, Compare>::
  443. count(const key_type &key) const {
  444. return r_count(begin(), end(), key);
  445. }
  446. ////////////////////////////////////////////////////////////////////
  447. // Function: ordered_vector::lower_bound
  448. // Access: Public
  449. // Description: Returns the iterator for the first element not less
  450. // than key, or end() if all elements are less than key.
  451. ////////////////////////////////////////////////////////////////////
  452. template<class Key, class Compare>
  453. INLINE TYPENAME ordered_vector<Key, Compare>::ITERATOR ordered_vector<Key, Compare>::
  454. lower_bound(const TYPENAME ordered_vector<Key, Compare>::KEY_TYPE &key) {
  455. return nci(r_lower_bound(begin(), end(), key));
  456. }
  457. ////////////////////////////////////////////////////////////////////
  458. // Function: ordered_vector::lower_bound
  459. // Access: Public
  460. // Description: Returns the iterator for the first element not less
  461. // than key, or end() if all elements are less than key.
  462. ////////////////////////////////////////////////////////////////////
  463. template<class Key, class Compare>
  464. INLINE TYPENAME ordered_vector<Key, Compare>::CONST_ITERATOR ordered_vector<Key, Compare>::
  465. lower_bound(const TYPENAME ordered_vector<Key, Compare>::KEY_TYPE &key) const {
  466. return r_lower_bound(begin(), end(), key);
  467. }
  468. ////////////////////////////////////////////////////////////////////
  469. // Function: ordered_vector::upper_bound
  470. // Access: Public
  471. // Description: Returns the iterator for the first element greater
  472. // than key, or end() if no element is greater than
  473. // key.
  474. ////////////////////////////////////////////////////////////////////
  475. template<class Key, class Compare>
  476. INLINE TYPENAME ordered_vector<Key, Compare>::ITERATOR ordered_vector<Key, Compare>::
  477. upper_bound(const TYPENAME ordered_vector<Key, Compare>::KEY_TYPE &key) {
  478. return nci(r_upper_bound(begin(), end(), key));
  479. }
  480. ////////////////////////////////////////////////////////////////////
  481. // Function: ordered_vector::upper_bound
  482. // Access: Public
  483. // Description: Returns the iterator for the first element greater
  484. // than key, or end() if no element is greater than
  485. // key.
  486. ////////////////////////////////////////////////////////////////////
  487. template<class Key, class Compare>
  488. INLINE TYPENAME ordered_vector<Key, Compare>::CONST_ITERATOR ordered_vector<Key, Compare>::
  489. upper_bound(const TYPENAME ordered_vector<Key, Compare>::KEY_TYPE &key) const {
  490. return r_upper_bound(begin(), end(), key);
  491. }
  492. ////////////////////////////////////////////////////////////////////
  493. // Function: ordered_vector::equal_range
  494. // Access: Public
  495. // Description: Returns the pair (lower_bound(key), upper_bound(key)).
  496. ////////////////////////////////////////////////////////////////////
  497. template<class Key, class Compare>
  498. INLINE pair<TYPENAME ordered_vector<Key, Compare>::ITERATOR, TYPENAME ordered_vector<Key, Compare>::ITERATOR> ordered_vector<Key, Compare>::
  499. equal_range(const TYPENAME ordered_vector<Key, Compare>::KEY_TYPE &key) {
  500. pair<TYPENAME ordered_vector<Key, Compare>::CONST_ITERATOR, TYPENAME ordered_vector<Key, Compare>::CONST_ITERATOR> result;
  501. result = r_equal_range(begin(), end(), key);
  502. return pair<TYPENAME ordered_vector<Key, Compare>::ITERATOR, TYPENAME ordered_vector<Key, Compare>::ITERATOR>(nci(result.first), nci(result.second));
  503. }
  504. ////////////////////////////////////////////////////////////////////
  505. // Function: ordered_vector::equal_range
  506. // Access: Public
  507. // Description: Returns the pair (lower_bound(key), upper_bound(key)).
  508. ////////////////////////////////////////////////////////////////////
  509. template<class Key, class Compare>
  510. INLINE pair<TYPENAME ordered_vector<Key, Compare>::CONST_ITERATOR, TYPENAME ordered_vector<Key, Compare>::CONST_ITERATOR> ordered_vector<Key, Compare>::
  511. equal_range(const TYPENAME ordered_vector<Key, Compare>::KEY_TYPE &key) const {
  512. return r_equal_range(begin(), end(), key);
  513. }
  514. ////////////////////////////////////////////////////////////////////
  515. // Function: ordered_vector::swap
  516. // Access: Public
  517. // Description: Exchanges the contents of this vector and the other
  518. // vector, in constant time (e.g., with a pointer swap).
  519. ////////////////////////////////////////////////////////////////////
  520. template<class Key, class Compare>
  521. INLINE void ordered_vector<Key, Compare>::
  522. swap(ordered_vector<Key, Compare> &copy) {
  523. _vector.swap(copy._vector);
  524. }
  525. ////////////////////////////////////////////////////////////////////
  526. // Function: ordered_vector::reserve
  527. // Access: Public
  528. // Description: Informs the vector of a planned change in size;
  529. // ensures that the capacity of the vector is greater
  530. // than or equal to n.
  531. ////////////////////////////////////////////////////////////////////
  532. template<class Key, class Compare>
  533. INLINE void ordered_vector<Key, Compare>::
  534. reserve(TYPENAME ordered_vector<Key, Compare>::SIZE_TYPE n) {
  535. _vector.reserve(n);
  536. }
  537. ////////////////////////////////////////////////////////////////////
  538. // Function: ordered_vector::sort_unique
  539. // Access: Public
  540. // Description: Ensures that the vector is properly sorted after a
  541. // potentially damaging operation. This should not
  542. // normally need to be called, unless the user has
  543. // written to the vector using the non-const iterators
  544. // or has called push_back().
  545. //
  546. // This flavor of sort also eliminates repeated
  547. // elements.
  548. ////////////////////////////////////////////////////////////////////
  549. template<class Key, class Compare>
  550. INLINE void ordered_vector<Key, Compare>::
  551. sort_unique() {
  552. sort(begin(), end(), _compare);
  553. iterator new_end = unique(begin(), end(), EquivalentTest(_compare));
  554. erase(new_end, end());
  555. }
  556. ////////////////////////////////////////////////////////////////////
  557. // Function: ordered_vector::sort_nonunique
  558. // Access: Public
  559. // Description: Ensures that the vector is properly sorted after a
  560. // potentially damaging operation. This should not
  561. // normally need to be called, unless the user has
  562. // written to the vector using the non-const iterators
  563. // or has called push_back().
  564. ////////////////////////////////////////////////////////////////////
  565. template<class Key, class Compare>
  566. INLINE void ordered_vector<Key, Compare>::
  567. sort_nonunique() {
  568. sort(begin(), end(), _compare);
  569. }
  570. ////////////////////////////////////////////////////////////////////
  571. // Function: ordered_vector::push_back
  572. // Access: Public
  573. // Description: Adds the new element to the end of the vector without
  574. // regard for proper sorting. This is a bad idea to do
  575. // except to populate the vector the first time; be sure
  576. // to call sort() after you have added all the elements.
  577. ////////////////////////////////////////////////////////////////////
  578. template<class Key, class Compare>
  579. INLINE void ordered_vector<Key, Compare>::
  580. push_back(const value_type &key) {
  581. _vector.push_back(key);
  582. }
  583. ////////////////////////////////////////////////////////////////////
  584. // Function: ordered_vector::nci
  585. // Access: Private
  586. // Description: I.e. "non-const iterator". This function is used to
  587. // typecast a const iterator to a non-const iterator for
  588. // easy definition of const vs. non-const flavors of
  589. // some of these methods.
  590. ////////////////////////////////////////////////////////////////////
  591. template<class Key, class Compare>
  592. INLINE TYPENAME ordered_vector<Key, Compare>::ITERATOR ordered_vector<Key, Compare>::
  593. nci(TYPENAME ordered_vector<Key, Compare>::CONST_ITERATOR i) {
  594. return begin() + (i - begin());
  595. }
  596. ////////////////////////////////////////////////////////////////////
  597. // Function: ordered_vector::find_insert_position
  598. // Access: Private
  599. // Description: Searches for the appropriate place in the ordered
  600. // vector to insert the indicated key, and returns the
  601. // corresponding iterator.
  602. ////////////////////////////////////////////////////////////////////
  603. template<class Key, class Compare>
  604. INLINE TYPENAME ordered_vector<Key, Compare>::ITERATOR ordered_vector<Key, Compare>::
  605. find_insert_position(TYPENAME ordered_vector<Key, Compare>::ITERATOR first,
  606. TYPENAME ordered_vector<Key, Compare>::ITERATOR last,
  607. const TYPENAME ordered_vector<Key, Compare>::KEY_TYPE &key) {
  608. ITERATOR result = r_find_insert_position(first, last, key);
  609. return result;
  610. }
  611. ////////////////////////////////////////////////////////////////////
  612. // Function: ov_set::Constructor
  613. // Access: Public
  614. // Description:
  615. ////////////////////////////////////////////////////////////////////
  616. template<class Key, class Compare>
  617. INLINE ov_set<Key, Compare>::
  618. ov_set(const Compare &compare) :
  619. ordered_vector<Key, Compare>(compare)
  620. {
  621. }
  622. ////////////////////////////////////////////////////////////////////
  623. // Function: ov_set::Copy Constructor
  624. // Access: Public
  625. // Description:
  626. ////////////////////////////////////////////////////////////////////
  627. template<class Key, class Compare>
  628. INLINE ov_set<Key, Compare>::
  629. ov_set(const ov_set<Key, Compare> &copy) :
  630. ordered_vector<Key, Compare>(copy)
  631. {
  632. }
  633. ////////////////////////////////////////////////////////////////////
  634. // Function: ov_set::Copy Assignment Operator
  635. // Access: Public
  636. // Description:
  637. ////////////////////////////////////////////////////////////////////
  638. template<class Key, class Compare>
  639. INLINE ov_set<Key, Compare> &ov_set<Key, Compare>::
  640. operator = (const ov_set<Key, Compare> &copy) {
  641. ordered_vector<Key, Compare>::operator = (copy);
  642. return *this;
  643. }
  644. ////////////////////////////////////////////////////////////////////
  645. // Function: ov_set::insert
  646. // Access: Public
  647. // Description: Maps to insert_unique().
  648. ////////////////////////////////////////////////////////////////////
  649. template<class Key, class Compare>
  650. TYPENAME ov_set<Key, Compare>::ITERATOR ov_set<Key, Compare>::
  651. insert(TYPENAME ov_set<Key, Compare>::ITERATOR position,
  652. const TYPENAME ov_set<Key, Compare>::VALUE_TYPE &key) {
  653. return ordered_vector<Key, Compare>::insert_unique(position, key);
  654. }
  655. ////////////////////////////////////////////////////////////////////
  656. // Function: ov_set::insert
  657. // Access: Public
  658. // Description: Maps to insert_unique().
  659. ////////////////////////////////////////////////////////////////////
  660. template<class Key, class Compare>
  661. INLINE pair<TYPENAME ov_set<Key, Compare>::ITERATOR, bool> ov_set<Key, Compare>::
  662. insert(const TYPENAME ov_set<Key, Compare>::VALUE_TYPE &key) {
  663. return ordered_vector<Key, Compare>::insert_unique(key);
  664. }
  665. ////////////////////////////////////////////////////////////////////
  666. // Function: ov_set::sort
  667. // Access: Public
  668. // Description: Maps to sort_unique().
  669. ////////////////////////////////////////////////////////////////////
  670. template<class Key, class Compare>
  671. INLINE void ov_set<Key, Compare>::
  672. sort() {
  673. ordered_vector<Key, Compare>::sort_unique();
  674. }
  675. ////////////////////////////////////////////////////////////////////
  676. // Function: ov_set::verify_list
  677. // Access: Public
  678. // Description: Maps to verify_list_unique().
  679. ////////////////////////////////////////////////////////////////////
  680. template<class Key, class Compare>
  681. INLINE bool ov_set<Key, Compare>::
  682. verify_list() const {
  683. return ordered_vector<Key, Compare>::verify_list_unique();
  684. }
  685. ////////////////////////////////////////////////////////////////////
  686. // Function: ov_multiset::Constructor
  687. // Access: Public
  688. // Description:
  689. ////////////////////////////////////////////////////////////////////
  690. template<class Key, class Compare>
  691. INLINE ov_multiset<Key, Compare>::
  692. ov_multiset(const Compare &compare) :
  693. ordered_vector<Key, Compare>(compare)
  694. {
  695. }
  696. ////////////////////////////////////////////////////////////////////
  697. // Function: ov_multiset::Copy Constructor
  698. // Access: Public
  699. // Description:
  700. ////////////////////////////////////////////////////////////////////
  701. template<class Key, class Compare>
  702. INLINE ov_multiset<Key, Compare>::
  703. ov_multiset(const ov_multiset<Key, Compare> &copy) :
  704. ordered_vector<Key, Compare>(copy)
  705. {
  706. }
  707. ////////////////////////////////////////////////////////////////////
  708. // Function: ov_multiset::Copy Assignment Operator
  709. // Access: Public
  710. // Description:
  711. ////////////////////////////////////////////////////////////////////
  712. template<class Key, class Compare>
  713. INLINE ov_multiset<Key, Compare> &ov_multiset<Key, Compare>::
  714. operator = (const ov_multiset<Key, Compare> &copy) {
  715. ordered_vector<Key, Compare>::operator = (copy);
  716. return *this;
  717. }
  718. ////////////////////////////////////////////////////////////////////
  719. // Function: ov_multiset::insert
  720. // Access: Public
  721. // Description: Maps to insert_nonunique().
  722. ////////////////////////////////////////////////////////////////////
  723. template<class Key, class Compare>
  724. TYPENAME ov_multiset<Key, Compare>::ITERATOR ov_multiset<Key, Compare>::
  725. insert(TYPENAME ov_multiset<Key, Compare>::ITERATOR position,
  726. const TYPENAME ov_multiset<Key, Compare>::VALUE_TYPE &key) {
  727. return ordered_vector<Key, Compare>::insert_nonunique(position, key);
  728. }
  729. ////////////////////////////////////////////////////////////////////
  730. // Function: ov_multiset::insert
  731. // Access: Public
  732. // Description: Maps to insert_nonunique().
  733. ////////////////////////////////////////////////////////////////////
  734. template<class Key, class Compare>
  735. INLINE TYPENAME ov_multiset<Key, Compare>::ITERATOR ov_multiset<Key, Compare>::
  736. insert(const TYPENAME ov_multiset<Key, Compare>::VALUE_TYPE &key) {
  737. return ordered_vector<Key, Compare>::insert_nonunique(key);
  738. }
  739. ////////////////////////////////////////////////////////////////////
  740. // Function: ov_multiset::sort
  741. // Access: Public
  742. // Description: Maps to sort_nonunique().
  743. ////////////////////////////////////////////////////////////////////
  744. template<class Key, class Compare>
  745. INLINE void ov_multiset<Key, Compare>::
  746. sort() {
  747. ordered_vector<Key, Compare>::sort_nonunique();
  748. }
  749. ////////////////////////////////////////////////////////////////////
  750. // Function: ov_multiset::verify_list
  751. // Access: Public
  752. // Description: Maps to verify_list_nonunique().
  753. ////////////////////////////////////////////////////////////////////
  754. template<class Key, class Compare>
  755. INLINE bool ov_multiset<Key, Compare>::
  756. verify_list() const {
  757. return ordered_vector<Key, Compare>::verify_list_nonunique();
  758. }