competitive programming scripts on Linux
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

861 lines
29 KiB

3 years ago
  1. // -*- C++ -*-
  2. // Copyright (C) 2005-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the terms
  6. // of the GNU General Public License as published by the Free Software
  7. // Foundation; either version 3, or (at your option) any later
  8. // version.
  9. // This library is distributed in the hope that it will be useful, but
  10. // WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
  21. // Permission to use, copy, modify, sell, and distribute this software
  22. // is hereby granted without fee, provided that the above copyright
  23. // notice appears in all copies, and that both that copyright notice
  24. // and this permission notice appear in supporting documentation. None
  25. // of the above authors, nor IBM Haifa Research Laboratories, make any
  26. // representation about the suitability of this software for any
  27. // purpose. It is provided "as is" without express or implied
  28. // warranty.
  29. /**
  30. * @file assoc_container.hpp
  31. * Contains associative containers.
  32. */
  33. #ifndef PB_DS_ASSOC_CNTNR_HPP
  34. #define PB_DS_ASSOC_CNTNR_HPP
  35. #include <bits/c++config.h>
  36. #include <ext/typelist.h>
  37. #include <ext/pb_ds/tag_and_trait.hpp>
  38. #include <ext/pb_ds/detail/standard_policies.hpp>
  39. #include <ext/pb_ds/detail/container_base_dispatch.hpp>
  40. #include <ext/pb_ds/detail/branch_policy/traits.hpp>
  41. namespace __gnu_pbds
  42. {
  43. /**
  44. * @defgroup containers-pbds Containers
  45. * @ingroup pbds
  46. * @{
  47. */
  48. /**
  49. * @defgroup hash-based Hash-Based
  50. * @ingroup containers-pbds
  51. * @{
  52. */
  53. #define PB_DS_HASH_BASE \
  54. detail::container_base_dispatch<Key, Mapped, _Alloc, Tag, \
  55. typename __gnu_cxx::typelist::append< \
  56. typename __gnu_cxx::typelist::create4<Hash_Fn, Eq_Fn, Resize_Policy, \
  57. detail::integral_constant<int, Store_Hash> >::type, Policy_Tl>::type>::type
  58. /**
  59. * @defgroup hash-detail Base and Policy Classes
  60. * @ingroup hash-based
  61. */
  62. /**
  63. * A hashed container abstraction.
  64. *
  65. * @tparam Key Key type.
  66. * @tparam Mapped Map type.
  67. * @tparam Hash_Fn Hashing functor.
  68. * @tparam Eq_Fn Equal functor.
  69. * @tparam Resize_Policy Resizes hash.
  70. * @tparam Store_Hash Indicates whether the hash value
  71. * will be stored along with each key.
  72. * @tparam Tag Instantiating data structure type,
  73. * see container_tag.
  74. * @tparam Policy_TL Policy typelist.
  75. * @tparam _Alloc Allocator type.
  76. *
  77. * Base is dispatched at compile time via Tag, from the following
  78. * choices: cc_hash_tag, gp_hash_tag, and descendants of basic_hash_tag.
  79. *
  80. * Base choices are: detail::cc_ht_map, detail::gp_ht_map
  81. */
  82. template<typename Key,
  83. typename Mapped,
  84. typename Hash_Fn,
  85. typename Eq_Fn,
  86. typename Resize_Policy,
  87. bool Store_Hash,
  88. typename Tag,
  89. typename Policy_Tl,
  90. typename _Alloc>
  91. class basic_hash_table : public PB_DS_HASH_BASE
  92. {
  93. private:
  94. typedef typename PB_DS_HASH_BASE base_type;
  95. public:
  96. virtual
  97. ~basic_hash_table() { }
  98. protected:
  99. basic_hash_table() { }
  100. basic_hash_table(const basic_hash_table& other)
  101. : base_type((const base_type&)other) { }
  102. template<typename T0>
  103. basic_hash_table(T0 t0) : base_type(t0) { }
  104. template<typename T0, typename T1>
  105. basic_hash_table(T0 t0, T1 t1) : base_type(t0, t1) { }
  106. template<typename T0, typename T1, typename T2>
  107. basic_hash_table(T0 t0, T1 t1, T2 t2) : base_type(t0, t1, t2) { }
  108. template<typename T0, typename T1, typename T2, typename T3>
  109. basic_hash_table(T0 t0, T1 t1, T2 t2, T3 t3)
  110. : base_type(t0, t1, t2, t3) { }
  111. template<typename T0, typename T1, typename T2, typename T3, typename T4>
  112. basic_hash_table(T0 t0, T1 t1, T2 t2, T3 t3, T4 t4)
  113. : base_type(t0, t1, t2, t3, t4) { }
  114. template<typename T0, typename T1, typename T2, typename T3, typename T4,
  115. typename T5>
  116. basic_hash_table(T0 t0, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5)
  117. : base_type(t0, t1, t2, t3, t4, t5) { }
  118. template<typename T0, typename T1, typename T2, typename T3, typename T4,
  119. typename T5, typename T6>
  120. basic_hash_table(T0 t0, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6)
  121. : base_type(t0, t1, t2, t3, t4, t5, t6) { }
  122. template<typename T0, typename T1, typename T2, typename T3, typename T4,
  123. typename T5, typename T6, typename T7>
  124. basic_hash_table(T0 t0, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7)
  125. : base_type(t0, t1, t2, t3, t4, t5, t6, t7) { }
  126. template<typename T0, typename T1, typename T2, typename T3, typename T4,
  127. typename T5, typename T6, typename T7, typename T8>
  128. basic_hash_table(T0 t0, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6,
  129. T7 t7, T8 t8)
  130. : base_type(t0, t1, t2, t3, t4, t5, t6, t7, t8)
  131. { }
  132. private:
  133. basic_hash_table&
  134. operator=(const base_type&);
  135. };
  136. #undef PB_DS_HASH_BASE
  137. #define PB_DS_CC_HASH_BASE \
  138. basic_hash_table<Key, Mapped, Hash_Fn, Eq_Fn, Resize_Policy, Store_Hash, \
  139. cc_hash_tag, \
  140. typename __gnu_cxx::typelist::create1<Comb_Hash_Fn>::type, _Alloc>
  141. /**
  142. * A collision-chaining hash-based associative container.
  143. *
  144. * @tparam Key Key type.
  145. * @tparam Mapped Map type.
  146. * @tparam Hash_Fn Hashing functor.
  147. * @tparam Eq_Fn Equal functor.
  148. * @tparam Comb_Hash_Fn Combining hash functor.
  149. * If Hash_Fn is not null_type, then this
  150. * is the ranged-hash functor; otherwise,
  151. * this is the range-hashing functor.
  152. * XXX(See Design::Hash-Based Containers::Hash Policies.)
  153. * @tparam Resize_Policy Resizes hash.
  154. * @tparam Store_Hash Indicates whether the hash value
  155. * will be stored along with each key.
  156. * If Hash_Fn is null_type, then the
  157. * container will not compile if this
  158. * value is true
  159. * @tparam _Alloc Allocator type.
  160. *
  161. * Base tag choices are: cc_hash_tag.
  162. *
  163. * Base is basic_hash_table.
  164. */
  165. template<typename Key,
  166. typename Mapped,
  167. typename Hash_Fn = typename detail::default_hash_fn<Key>::type,
  168. typename Eq_Fn = typename detail::default_eq_fn<Key>::type,
  169. typename Comb_Hash_Fn = detail::default_comb_hash_fn::type,
  170. typename Resize_Policy = typename detail::default_resize_policy<Comb_Hash_Fn>::type,
  171. bool Store_Hash = detail::default_store_hash,
  172. typename _Alloc = std::allocator<char> >
  173. class cc_hash_table : public PB_DS_CC_HASH_BASE
  174. {
  175. private:
  176. typedef PB_DS_CC_HASH_BASE base_type;
  177. public:
  178. typedef cc_hash_tag container_category;
  179. typedef Hash_Fn hash_fn;
  180. typedef Eq_Fn eq_fn;
  181. typedef Resize_Policy resize_policy;
  182. typedef Comb_Hash_Fn comb_hash_fn;
  183. /// Default constructor.
  184. cc_hash_table() { }
  185. /// Constructor taking some policy objects. r_hash_fn will be
  186. /// copied by the Hash_Fn object of the container object.
  187. cc_hash_table(const hash_fn& h)
  188. : base_type(h) { }
  189. /// Constructor taking some policy objects. r_hash_fn will be
  190. /// copied by the hash_fn object of the container object, and
  191. /// r_eq_fn will be copied by the eq_fn object of the container
  192. /// object.
  193. cc_hash_table(const hash_fn& h, const eq_fn& e)
  194. : base_type(h, e) { }
  195. /// Constructor taking some policy objects. r_hash_fn will be
  196. /// copied by the hash_fn object of the container object, r_eq_fn
  197. /// will be copied by the eq_fn object of the container object,
  198. /// and r_comb_hash_fn will be copied by the comb_hash_fn object
  199. /// of the container object.
  200. cc_hash_table(const hash_fn& h, const eq_fn& e, const comb_hash_fn& ch)
  201. : base_type(h, e, ch) { }
  202. /// Constructor taking some policy objects. r_hash_fn will be
  203. /// copied by the hash_fn object of the container object, r_eq_fn
  204. /// will be copied by the eq_fn object of the container object,
  205. /// r_comb_hash_fn will be copied by the comb_hash_fn object of
  206. /// the container object, and r_resize_policy will be copied by
  207. /// the resize_policy object of the container object.
  208. cc_hash_table(const hash_fn& h, const eq_fn& e, const comb_hash_fn& ch,
  209. const resize_policy& rp)
  210. : base_type(h, e, ch, rp) { }
  211. /// Constructor taking __iterators to a range of value_types. The
  212. /// value_types between first_it and last_it will be inserted into
  213. /// the container object.
  214. template<typename It>
  215. cc_hash_table(It first, It last)
  216. { base_type::copy_from_range(first, last); }
  217. /// Constructor taking __iterators to a range of value_types and
  218. /// some policy objects. The value_types between first_it and
  219. /// last_it will be inserted into the container object.
  220. template<typename It>
  221. cc_hash_table(It first, It last, const hash_fn& h)
  222. : base_type(h)
  223. { this->copy_from_range(first, last); }
  224. /// Constructor taking __iterators to a range of value_types and
  225. /// some policy objects The value_types between first_it and
  226. /// last_it will be inserted into the container object. r_hash_fn
  227. /// will be copied by the hash_fn object of the container object,
  228. /// and r_eq_fn will be copied by the eq_fn object of the
  229. /// container object.
  230. template<typename It>
  231. cc_hash_table(It first, It last, const hash_fn& h, const eq_fn& e)
  232. : base_type(h, e)
  233. { this->copy_from_range(first, last); }
  234. /// Constructor taking __iterators to a range of value_types and
  235. /// some policy objects The value_types between first_it and
  236. /// last_it will be inserted into the container object. r_hash_fn
  237. /// will be copied by the hash_fn object of the container object,
  238. /// r_eq_fn will be copied by the eq_fn object of the container
  239. /// object, and r_comb_hash_fn will be copied by the comb_hash_fn
  240. /// object of the container object.
  241. template<typename It>
  242. cc_hash_table(It first, It last, const hash_fn& h, const eq_fn& e,
  243. const comb_hash_fn& ch)
  244. : base_type(h, e, ch)
  245. { this->copy_from_range(first, last); }
  246. /// Constructor taking __iterators to a range of value_types and
  247. /// some policy objects The value_types between first_it and
  248. /// last_it will be inserted into the container object. r_hash_fn
  249. /// will be copied by the hash_fn object of the container object,
  250. /// r_eq_fn will be copied by the eq_fn object of the container
  251. /// object, r_comb_hash_fn will be copied by the comb_hash_fn
  252. /// object of the container object, and r_resize_policy will be
  253. /// copied by the resize_policy object of the container object.
  254. template<typename It>
  255. cc_hash_table(It first, It last, const hash_fn& h, const eq_fn& e,
  256. const comb_hash_fn& ch, const resize_policy& rp)
  257. : base_type(h, e, ch, rp)
  258. { this->copy_from_range(first, last); }
  259. cc_hash_table(const cc_hash_table& other)
  260. : base_type((const base_type&)other)
  261. { }
  262. virtual
  263. ~cc_hash_table() { }
  264. cc_hash_table&
  265. operator=(const cc_hash_table& other)
  266. {
  267. if (this != &other)
  268. {
  269. cc_hash_table tmp(other);
  270. swap(tmp);
  271. }
  272. return *this;
  273. }
  274. void
  275. swap(cc_hash_table& other)
  276. { base_type::swap(other); }
  277. };
  278. #undef PB_DS_CC_HASH_BASE
  279. #define PB_DS_GP_HASH_BASE \
  280. basic_hash_table<Key, Mapped, Hash_Fn, Eq_Fn, Resize_Policy, Store_Hash, \
  281. gp_hash_tag, \
  282. typename __gnu_cxx::typelist::create2<Comb_Probe_Fn, Probe_Fn>::type, _Alloc>
  283. /**
  284. * A general-probing hash-based associative container.
  285. *
  286. * @tparam Key Key type.
  287. * @tparam Mapped Map type.
  288. * @tparam Hash_Fn Hashing functor.
  289. * @tparam Eq_Fn Equal functor.
  290. * @tparam Comb_Probe_Fn Combining probe functor.
  291. * If Hash_Fn is not null_type, then this
  292. * is the ranged-probe functor; otherwise,
  293. * this is the range-hashing functor.
  294. * XXX See Design::Hash-Based Containers::Hash Policies.
  295. * @tparam Probe_Fn Probe functor.
  296. * @tparam Resize_Policy Resizes hash.
  297. * @tparam Store_Hash Indicates whether the hash value
  298. * will be stored along with each key.
  299. * If Hash_Fn is null_type, then the
  300. * container will not compile if this
  301. * value is true
  302. * @tparam _Alloc Allocator type.
  303. *
  304. * Base tag choices are: gp_hash_tag.
  305. *
  306. * Base is basic_hash_table.
  307. */
  308. template<typename Key,
  309. typename Mapped,
  310. typename Hash_Fn = typename detail::default_hash_fn<Key>::type,
  311. typename Eq_Fn = typename detail::default_eq_fn<Key>::type,
  312. typename Comb_Probe_Fn = detail::default_comb_hash_fn::type,
  313. typename Probe_Fn = typename detail::default_probe_fn<Comb_Probe_Fn>::type,
  314. typename Resize_Policy = typename detail::default_resize_policy<Comb_Probe_Fn>::type,
  315. bool Store_Hash = detail::default_store_hash,
  316. typename _Alloc = std::allocator<char> >
  317. class gp_hash_table : public PB_DS_GP_HASH_BASE
  318. {
  319. private:
  320. typedef PB_DS_GP_HASH_BASE base_type;
  321. public:
  322. typedef gp_hash_tag container_category;
  323. typedef Hash_Fn hash_fn;
  324. typedef Eq_Fn eq_fn;
  325. typedef Comb_Probe_Fn comb_probe_fn;
  326. typedef Probe_Fn probe_fn;
  327. typedef Resize_Policy resize_policy;
  328. /// Default constructor.
  329. gp_hash_table() { }
  330. /// Constructor taking some policy objects. r_hash_fn will be
  331. /// copied by the hash_fn object of the container object.
  332. gp_hash_table(const hash_fn& h)
  333. : base_type(h) { }
  334. /// Constructor taking some policy objects. r_hash_fn will be
  335. /// copied by the hash_fn object of the container object, and
  336. /// r_eq_fn will be copied by the eq_fn object of the container
  337. /// object.
  338. gp_hash_table(const hash_fn& h, const eq_fn& e)
  339. : base_type(h, e) { }
  340. /// Constructor taking some policy objects. r_hash_fn will be
  341. /// copied by the hash_fn object of the container object, r_eq_fn
  342. /// will be copied by the eq_fn object of the container object,
  343. /// and r_comb_probe_fn will be copied by the comb_probe_fn object
  344. /// of the container object.
  345. gp_hash_table(const hash_fn& h, const eq_fn& e, const comb_probe_fn& cp)
  346. : base_type(h, e, cp) { }
  347. /// Constructor taking some policy objects. r_hash_fn will be
  348. /// copied by the hash_fn object of the container object, r_eq_fn
  349. /// will be copied by the eq_fn object of the container object,
  350. /// r_comb_probe_fn will be copied by the comb_probe_fn object of
  351. /// the container object, and r_probe_fn will be copied by the
  352. /// probe_fn object of the container object.
  353. gp_hash_table(const hash_fn& h, const eq_fn& e, const comb_probe_fn& cp,
  354. const probe_fn& p)
  355. : base_type(h, e, cp, p) { }
  356. /// Constructor taking some policy objects. r_hash_fn will be
  357. /// copied by the hash_fn object of the container object, r_eq_fn
  358. /// will be copied by the eq_fn object of the container object,
  359. /// r_comb_probe_fn will be copied by the comb_probe_fn object of
  360. /// the container object, r_probe_fn will be copied by the
  361. /// probe_fn object of the container object, and r_resize_policy
  362. /// will be copied by the Resize_Policy object of the container
  363. /// object.
  364. gp_hash_table(const hash_fn& h, const eq_fn& e, const comb_probe_fn& cp,
  365. const probe_fn& p, const resize_policy& rp)
  366. : base_type(h, e, cp, p, rp) { }
  367. /// Constructor taking __iterators to a range of value_types. The
  368. /// value_types between first_it and last_it will be inserted into
  369. /// the container object.
  370. template<typename It>
  371. gp_hash_table(It first, It last)
  372. { base_type::copy_from_range(first, last); }
  373. /// Constructor taking __iterators to a range of value_types and
  374. /// some policy objects. The value_types between first_it and
  375. /// last_it will be inserted into the container object. r_hash_fn
  376. /// will be copied by the hash_fn object of the container object.
  377. template<typename It>
  378. gp_hash_table(It first, It last, const hash_fn& h)
  379. : base_type(h)
  380. { base_type::copy_from_range(first, last); }
  381. /// Constructor taking __iterators to a range of value_types and
  382. /// some policy objects. The value_types between first_it and
  383. /// last_it will be inserted into the container object. r_hash_fn
  384. /// will be copied by the hash_fn object of the container object,
  385. /// and r_eq_fn will be copied by the eq_fn object of the
  386. /// container object.
  387. template<typename It>
  388. gp_hash_table(It first, It last, const hash_fn& h, const eq_fn& e)
  389. : base_type(h, e)
  390. { base_type::copy_from_range(first, last); }
  391. /// Constructor taking __iterators to a range of value_types and
  392. /// some policy objects. The value_types between first_it and
  393. /// last_it will be inserted into the container object. r_hash_fn
  394. /// will be copied by the hash_fn object of the container object,
  395. /// r_eq_fn will be copied by the eq_fn object of the container
  396. /// object, and r_comb_probe_fn will be copied by the
  397. /// comb_probe_fn object of the container object.
  398. template<typename It>
  399. gp_hash_table(It first, It last, const hash_fn& h, const eq_fn& e,
  400. const comb_probe_fn& cp)
  401. : base_type(h, e, cp)
  402. { base_type::copy_from_range(first, last); }
  403. /// Constructor taking __iterators to a range of value_types and
  404. /// some policy objects. The value_types between first_it and
  405. /// last_it will be inserted into the container object. r_hash_fn
  406. /// will be copied by the hash_fn object of the container object,
  407. /// r_eq_fn will be copied by the eq_fn object of the container
  408. /// object, r_comb_probe_fn will be copied by the comb_probe_fn
  409. /// object of the container object, and r_probe_fn will be copied
  410. /// by the probe_fn object of the container object.
  411. template<typename It>
  412. gp_hash_table(It first, It last, const hash_fn& h, const eq_fn& e,
  413. const comb_probe_fn& cp, const probe_fn& p)
  414. : base_type(h, e, cp, p)
  415. { base_type::copy_from_range(first, last); }
  416. /// Constructor taking __iterators to a range of value_types and
  417. /// some policy objects. The value_types between first_it and
  418. /// last_it will be inserted into the container object. r_hash_fn
  419. /// will be copied by the hash_fn object of the container object,
  420. /// r_eq_fn will be copied by the eq_fn object of the container
  421. /// object, r_comb_probe_fn will be copied by the comb_probe_fn
  422. /// object of the container object, r_probe_fn will be copied by
  423. /// the probe_fn object of the container object, and
  424. /// r_resize_policy will be copied by the resize_policy object of
  425. /// the container object.
  426. template<typename It>
  427. gp_hash_table(It first, It last, const hash_fn& h, const eq_fn& e,
  428. const comb_probe_fn& cp, const probe_fn& p,
  429. const resize_policy& rp)
  430. : base_type(h, e, cp, p, rp)
  431. { base_type::copy_from_range(first, last); }
  432. gp_hash_table(const gp_hash_table& other)
  433. : base_type((const base_type&)other)
  434. { }
  435. virtual
  436. ~gp_hash_table() { }
  437. gp_hash_table&
  438. operator=(const gp_hash_table& other)
  439. {
  440. if (this != &other)
  441. {
  442. gp_hash_table tmp(other);
  443. swap(tmp);
  444. }
  445. return *this;
  446. }
  447. void
  448. swap(gp_hash_table& other)
  449. { base_type::swap(other); }
  450. };
  451. //@} hash-based
  452. #undef PB_DS_GP_HASH_BASE
  453. /**
  454. * @defgroup branch-based Branch-Based
  455. * @ingroup containers-pbds
  456. * @{
  457. */
  458. #define PB_DS_BRANCH_BASE \
  459. detail::container_base_dispatch<Key, Mapped, _Alloc, Tag, Policy_Tl>::type
  460. /**
  461. * @defgroup branch-detail Base and Policy Classes
  462. * @ingroup branch-based
  463. */
  464. /**
  465. * A branched, tree-like (tree, trie) container abstraction.
  466. *
  467. * @tparam Key Key type.
  468. * @tparam Mapped Map type.
  469. * @tparam Tag Instantiating data structure type,
  470. * see container_tag.
  471. * @tparam Node_Update Updates nodes, restores invariants.
  472. * @tparam Policy_TL Policy typelist.
  473. * @tparam _Alloc Allocator type.
  474. *
  475. * Base is dispatched at compile time via Tag, from the following
  476. * choices: tree_tag, trie_tag, and their descendants.
  477. *
  478. * Base choices are: detail::ov_tree_map, detail::rb_tree_map,
  479. * detail::splay_tree_map, and detail::pat_trie_map.
  480. */
  481. template<typename Key, typename Mapped, typename Tag,
  482. typename Node_Update, typename Policy_Tl, typename _Alloc>
  483. class basic_branch : public PB_DS_BRANCH_BASE
  484. {
  485. private:
  486. typedef typename PB_DS_BRANCH_BASE base_type;
  487. public:
  488. typedef Node_Update node_update;
  489. virtual
  490. ~basic_branch() { }
  491. protected:
  492. basic_branch() { }
  493. basic_branch(const basic_branch& other)
  494. : base_type((const base_type&)other) { }
  495. template<typename T0>
  496. basic_branch(T0 t0) : base_type(t0) { }
  497. template<typename T0, typename T1>
  498. basic_branch(T0 t0, T1 t1) : base_type(t0, t1) { }
  499. template<typename T0, typename T1, typename T2>
  500. basic_branch(T0 t0, T1 t1, T2 t2) : base_type(t0, t1, t2) { }
  501. template<typename T0, typename T1, typename T2, typename T3>
  502. basic_branch(T0 t0, T1 t1, T2 t2, T3 t3)
  503. : base_type(t0, t1, t2, t3) { }
  504. template<typename T0, typename T1, typename T2, typename T3, typename T4>
  505. basic_branch(T0 t0, T1 t1, T2 t2, T3 t3, T4 t4)
  506. : base_type(t0, t1, t2, t3, t4) { }
  507. template<typename T0, typename T1, typename T2, typename T3, typename T4,
  508. typename T5>
  509. basic_branch(T0 t0, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5)
  510. : base_type(t0, t1, t2, t3, t4, t5) { }
  511. template<typename T0, typename T1, typename T2, typename T3, typename T4,
  512. typename T5, typename T6>
  513. basic_branch(T0 t0, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6)
  514. : base_type(t0, t1, t2, t3, t4, t5, t6) { }
  515. };
  516. #undef PB_DS_BRANCH_BASE
  517. #define PB_DS_TREE_NODE_AND_IT_TRAITS \
  518. detail::tree_traits<Key, Mapped,Cmp_Fn,Node_Update,Tag,_Alloc>
  519. #define PB_DS_TREE_BASE \
  520. basic_branch<Key,Mapped, Tag, \
  521. typename PB_DS_TREE_NODE_AND_IT_TRAITS::node_update, \
  522. typename __gnu_cxx::typelist::create2<Cmp_Fn, \
  523. PB_DS_TREE_NODE_AND_IT_TRAITS>::type, _Alloc>
  524. /**
  525. * A tree-based container.
  526. *
  527. * @tparam Key Key type.
  528. * @tparam Mapped Map type.
  529. * @tparam Cmp_Fn Comparison functor.
  530. * @tparam Tag Instantiating data structure type,
  531. * see container_tag.
  532. * @tparam Node_Update Updates tree internal-nodes,
  533. * restores invariants when invalidated.
  534. * XXX See design::tree-based-containers::node invariants.
  535. * @tparam _Alloc Allocator type.
  536. *
  537. * Base tag choices are: ov_tree_tag, rb_tree_tag, splay_tree_tag.
  538. *
  539. * Base is basic_branch.
  540. */
  541. template<typename Key, typename Mapped, typename Cmp_Fn = std::less<Key>,
  542. typename Tag = rb_tree_tag,
  543. template<typename Node_CItr, typename Node_Itr,
  544. typename Cmp_Fn_, typename _Alloc_>
  545. class Node_Update = null_node_update,
  546. typename _Alloc = std::allocator<char> >
  547. class tree : public PB_DS_TREE_BASE
  548. {
  549. private:
  550. typedef PB_DS_TREE_BASE base_type;
  551. public:
  552. /// Comparison functor type.
  553. typedef Cmp_Fn cmp_fn;
  554. tree() { }
  555. /// Constructor taking some policy objects. r_cmp_fn will be
  556. /// copied by the Cmp_Fn object of the container object.
  557. tree(const cmp_fn& c)
  558. : base_type(c) { }
  559. /// Constructor taking __iterators to a range of value_types. The
  560. /// value_types between first_it and last_it will be inserted into
  561. /// the container object.
  562. template<typename It>
  563. tree(It first, It last)
  564. { base_type::copy_from_range(first, last); }
  565. /// Constructor taking __iterators to a range of value_types and
  566. /// some policy objects The value_types between first_it and
  567. /// last_it will be inserted into the container object. r_cmp_fn
  568. /// will be copied by the cmp_fn object of the container object.
  569. template<typename It>
  570. tree(It first, It last, const cmp_fn& c)
  571. : base_type(c)
  572. { base_type::copy_from_range(first, last); }
  573. tree(const tree& other)
  574. : base_type((const base_type&)other) { }
  575. virtual
  576. ~tree() { }
  577. tree&
  578. operator=(const tree& other)
  579. {
  580. if (this != &other)
  581. {
  582. tree tmp(other);
  583. swap(tmp);
  584. }
  585. return *this;
  586. }
  587. void
  588. swap(tree& other)
  589. { base_type::swap(other); }
  590. };
  591. #undef PB_DS_TREE_BASE
  592. #undef PB_DS_TREE_NODE_AND_IT_TRAITS
  593. #define PB_DS_TRIE_NODE_AND_IT_TRAITS \
  594. detail::trie_traits<Key,Mapped,_ATraits,Node_Update,Tag,_Alloc>
  595. #define PB_DS_TRIE_BASE \
  596. basic_branch<Key,Mapped,Tag, \
  597. typename PB_DS_TRIE_NODE_AND_IT_TRAITS::node_update, \
  598. typename __gnu_cxx::typelist::create2<_ATraits, \
  599. PB_DS_TRIE_NODE_AND_IT_TRAITS >::type, _Alloc>
  600. /**
  601. * A trie-based container.
  602. *
  603. * @tparam Key Key type.
  604. * @tparam Mapped Map type.
  605. * @tparam _ATraits Element access traits.
  606. * @tparam Tag Instantiating data structure type,
  607. * see container_tag.
  608. * @tparam Node_Update Updates trie internal-nodes,
  609. * restores invariants when invalidated.
  610. * XXX See design::tree-based-containers::node invariants.
  611. * @tparam _Alloc Allocator type.
  612. *
  613. * Base tag choice is pat_trie_tag.
  614. *
  615. * Base is basic_branch.
  616. */
  617. template<typename Key,
  618. typename Mapped,
  619. typename _ATraits = \
  620. typename detail::default_trie_access_traits<Key>::type,
  621. typename Tag = pat_trie_tag,
  622. template<typename Node_CItr,
  623. typename Node_Itr,
  624. typename _ATraits_,
  625. typename _Alloc_>
  626. class Node_Update = null_node_update,
  627. typename _Alloc = std::allocator<char> >
  628. class trie : public PB_DS_TRIE_BASE
  629. {
  630. private:
  631. typedef PB_DS_TRIE_BASE base_type;
  632. public:
  633. /// Element access traits type.
  634. typedef _ATraits access_traits;
  635. trie() { }
  636. /// Constructor taking some policy objects. r_access_traits will
  637. /// be copied by the _ATraits object of the container object.
  638. trie(const access_traits& t)
  639. : base_type(t) { }
  640. /// Constructor taking __iterators to a range of value_types. The
  641. /// value_types between first_it and last_it will be inserted into
  642. /// the container object.
  643. template<typename It>
  644. trie(It first, It last)
  645. { base_type::copy_from_range(first, last); }
  646. /// Constructor taking __iterators to a range of value_types and
  647. /// some policy objects. The value_types between first_it and
  648. /// last_it will be inserted into the container object.
  649. template<typename It>
  650. trie(It first, It last, const access_traits& t)
  651. : base_type(t)
  652. { base_type::copy_from_range(first, last); }
  653. trie(const trie& other)
  654. : base_type((const base_type&)other) { }
  655. virtual
  656. ~trie() { }
  657. trie&
  658. operator=(const trie& other)
  659. {
  660. if (this != &other)
  661. {
  662. trie tmp(other);
  663. swap(tmp);
  664. }
  665. return *this;
  666. }
  667. void
  668. swap(trie& other)
  669. { base_type::swap(other); }
  670. };
  671. //@} branch-based
  672. #undef PB_DS_TRIE_BASE
  673. #undef PB_DS_TRIE_NODE_AND_IT_TRAITS
  674. /**
  675. * @defgroup list-based List-Based
  676. * @ingroup containers-pbds
  677. * @{
  678. */
  679. #define PB_DS_LU_BASE \
  680. detail::container_base_dispatch<Key, Mapped, _Alloc, list_update_tag, \
  681. typename __gnu_cxx::typelist::create2<Eq_Fn, Update_Policy>::type>::type
  682. /**
  683. * A list-update based associative container.
  684. *
  685. * @tparam Key Key type.
  686. * @tparam Mapped Map type.
  687. * @tparam Eq_Fn Equal functor.
  688. * @tparam Update_Policy Update policy, determines when an element
  689. * will be moved to the front of the list.
  690. * @tparam _Alloc Allocator type.
  691. *
  692. * Base is detail::lu_map.
  693. */
  694. template<typename Key,
  695. typename Mapped,
  696. class Eq_Fn = typename detail::default_eq_fn<Key>::type,
  697. class Update_Policy = detail::default_update_policy::type,
  698. class _Alloc = std::allocator<char> >
  699. class list_update : public PB_DS_LU_BASE
  700. {
  701. private:
  702. typedef typename PB_DS_LU_BASE base_type;
  703. public:
  704. typedef list_update_tag container_category;
  705. typedef Eq_Fn eq_fn;
  706. typedef Update_Policy update_policy;
  707. list_update() { }
  708. /// Constructor taking __iterators to a range of value_types. The
  709. /// value_types between first_it and last_it will be inserted into
  710. /// the container object.
  711. template<typename It>
  712. list_update(It first, It last)
  713. { base_type::copy_from_range(first, last); }
  714. list_update(const list_update& other)
  715. : base_type((const base_type&)other) { }
  716. virtual
  717. ~list_update() { }
  718. list_update&
  719. operator=(const list_update& other)
  720. {
  721. if (this !=& other)
  722. {
  723. list_update tmp(other);
  724. swap(tmp);
  725. }
  726. return *this;
  727. }
  728. void
  729. swap(list_update& other)
  730. { base_type::swap(other); }
  731. };
  732. //@} list-based
  733. #undef PB_DS_LU_BASE
  734. // @} group containers-pbds
  735. } // namespace __gnu_pbds
  736. #endif