trie.hpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. // Copyright (C) 2020 T. Zachary Laine
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_PARSER_DETAIL_TEXT_TRIE_HPP
  7. #define BOOST_PARSER_DETAIL_TEXT_TRIE_HPP
  8. #include <boost/parser/detail/text/config.hpp>
  9. #include <boost/parser/detail/text/trie_fwd.hpp>
  10. #include <algorithm>
  11. #include <memory>
  12. #include <type_traits>
  13. #include <vector>
  14. namespace boost::parser::detail { namespace text {
  15. /** An optional reference. Its optionality is testable, via the operator
  16. bool() members, and it is implicitly convertible to the underlying
  17. value type. */
  18. template<typename T, bool Const = std::is_const<T>::value>
  19. struct optional_ref
  20. {
  21. private:
  22. T * t_;
  23. public:
  24. optional_ref() : t_(nullptr) {}
  25. optional_ref(T & t) : t_(&t) {}
  26. template<typename U>
  27. auto operator=(U && u)
  28. -> decltype(*this->t_ = static_cast<U &&>(u), *this)
  29. {
  30. BOOST_PARSER_DEBUG_ASSERT(t_);
  31. *t_ = static_cast<U &&>(u);
  32. return *this;
  33. }
  34. explicit operator bool() const & { return t_ != nullptr; }
  35. explicit operator bool() & { return t_ != nullptr; }
  36. explicit operator bool() && { return t_ != nullptr; }
  37. T const & operator*() const
  38. {
  39. BOOST_PARSER_DEBUG_ASSERT(t_);
  40. return *t_;
  41. }
  42. T const * operator->() const
  43. {
  44. BOOST_PARSER_DEBUG_ASSERT(t_);
  45. return t_;
  46. }
  47. operator T const &() const &
  48. {
  49. BOOST_PARSER_DEBUG_ASSERT(t_);
  50. return *t_;
  51. }
  52. operator T const &() const &&
  53. {
  54. BOOST_PARSER_DEBUG_ASSERT(t_);
  55. return *t_;
  56. }
  57. T & operator*()
  58. {
  59. BOOST_PARSER_DEBUG_ASSERT(t_);
  60. return *t_;
  61. }
  62. T * operator->()
  63. {
  64. BOOST_PARSER_DEBUG_ASSERT(t_);
  65. return t_;
  66. }
  67. operator T &() &
  68. {
  69. BOOST_PARSER_DEBUG_ASSERT(t_);
  70. return *t_;
  71. }
  72. operator T &() &&
  73. {
  74. BOOST_PARSER_DEBUG_ASSERT(t_);
  75. return *t_;
  76. }
  77. };
  78. template<typename T>
  79. struct optional_ref<T, true>
  80. {
  81. private:
  82. T * t_;
  83. public:
  84. optional_ref() : t_(nullptr) {}
  85. optional_ref(T & t) : t_(&t) {}
  86. explicit operator bool() const & { return t_ != nullptr; }
  87. explicit operator bool() && { return t_ != nullptr; }
  88. T & operator*() const
  89. {
  90. BOOST_PARSER_DEBUG_ASSERT(t_);
  91. return *t_;
  92. }
  93. T * operator->() const
  94. {
  95. BOOST_PARSER_DEBUG_ASSERT(t_);
  96. return t_;
  97. }
  98. operator T &() const &
  99. {
  100. BOOST_PARSER_DEBUG_ASSERT(t_);
  101. return *t_;
  102. }
  103. operator T &() const &&
  104. {
  105. BOOST_PARSER_DEBUG_ASSERT(t_);
  106. return *t_;
  107. }
  108. };
  109. template<>
  110. struct optional_ref<bool, false>
  111. {
  112. private:
  113. bool * t_;
  114. public:
  115. optional_ref() : t_(nullptr) {}
  116. optional_ref(bool & t) : t_(&t) {}
  117. template<typename U>
  118. auto operator=(U && u)
  119. -> decltype(*this->t_ = static_cast<U &&>(u), *this)
  120. {
  121. BOOST_PARSER_DEBUG_ASSERT(t_);
  122. *t_ = static_cast<U &&>(u);
  123. return *this;
  124. }
  125. explicit operator bool() const & { return t_ != nullptr; }
  126. explicit operator bool() && { return t_ != nullptr; }
  127. bool const & operator*() const
  128. {
  129. BOOST_PARSER_DEBUG_ASSERT(t_);
  130. return *t_;
  131. }
  132. bool const * operator->() const
  133. {
  134. BOOST_PARSER_DEBUG_ASSERT(t_);
  135. return t_;
  136. }
  137. bool & operator*()
  138. {
  139. BOOST_PARSER_DEBUG_ASSERT(t_);
  140. return *t_;
  141. }
  142. bool * operator->()
  143. {
  144. BOOST_PARSER_DEBUG_ASSERT(t_);
  145. return t_;
  146. }
  147. };
  148. template<>
  149. struct optional_ref<bool const, true>
  150. {
  151. private:
  152. bool const * t_;
  153. public:
  154. optional_ref() : t_(nullptr) {}
  155. optional_ref(bool const & t) : t_(&t) {}
  156. explicit operator bool() const & { return t_ != nullptr; }
  157. explicit operator bool() && { return t_ != nullptr; }
  158. bool const & operator*() const
  159. {
  160. BOOST_PARSER_DEBUG_ASSERT(t_);
  161. return *t_;
  162. }
  163. bool const * operator->() const
  164. {
  165. BOOST_PARSER_DEBUG_ASSERT(t_);
  166. return t_;
  167. }
  168. };
  169. namespace detail {
  170. template<
  171. typename ParentIndexing,
  172. typename Key,
  173. typename Value,
  174. std::size_t KeySize = 0>
  175. struct trie_node_t;
  176. struct no_index_within_parent_t
  177. {
  178. std::size_t value() const
  179. {
  180. BOOST_PARSER_DEBUG_ASSERT(!"This should never be called.");
  181. return 0;
  182. }
  183. template<
  184. typename Key,
  185. typename Value,
  186. typename Iter,
  187. std::size_t KeySize>
  188. void insert_at(
  189. std::unique_ptr<trie_node_t<
  190. no_index_within_parent_t,
  191. Key,
  192. Value,
  193. KeySize>> const & child,
  194. std::ptrdiff_t offset,
  195. Iter it,
  196. Iter end)
  197. {}
  198. template<typename Key, typename Value, std::size_t KeySize>
  199. void insert_ptr(std::unique_ptr<trie_node_t<
  200. no_index_within_parent_t,
  201. Key,
  202. Value,
  203. KeySize>> const & child)
  204. {}
  205. template<typename Iter>
  206. void erase(Iter it, Iter end)
  207. {}
  208. };
  209. template<typename Char>
  210. struct char_range
  211. {
  212. Char * first_;
  213. Char * last_;
  214. Char * begin() const { return first_; }
  215. Char * end() const { return last_; }
  216. };
  217. struct void_type
  218. {};
  219. }
  220. /** A key/value pair found in a trie or trie_map. */
  221. template<typename Key, typename Value>
  222. struct trie_element
  223. {
  224. trie_element() {}
  225. trie_element(Key k, Value v) : key(k), value(v) {}
  226. template<typename KeyT, typename ValueT>
  227. trie_element(trie_element<KeyT, ValueT> const & rhs) :
  228. key(rhs.key), value(rhs.value)
  229. {}
  230. template<typename KeyT, typename ValueT>
  231. trie_element(trie_element<KeyT, ValueT> && rhs) :
  232. key(std::move(rhs.key)), value(std::move(rhs.value))
  233. {}
  234. template<typename KeyT, typename ValueT>
  235. trie_element & operator=(trie_element<KeyT, ValueT> const & rhs)
  236. {
  237. key = rhs.key;
  238. value = rhs.value;
  239. return *this;
  240. }
  241. template<typename KeyT, typename ValueT>
  242. trie_element & operator=(trie_element<KeyT, ValueT> && rhs)
  243. {
  244. key = std::move(rhs.key);
  245. value = std::move(rhs.value);
  246. return *this;
  247. }
  248. Key key;
  249. Value value;
  250. friend bool
  251. operator==(trie_element const & lhs, trie_element const & rhs)
  252. {
  253. return lhs.key == rhs.key && lhs.value == rhs.value;
  254. }
  255. friend bool
  256. operator!=(trie_element const & lhs, trie_element const & rhs)
  257. {
  258. return !(lhs == rhs);
  259. }
  260. };
  261. /** The result type for trie operations that produce a matching
  262. subsequence. */
  263. struct trie_match_result
  264. {
  265. trie_match_result() : node(nullptr), size(0), match(false), leaf(false)
  266. {}
  267. trie_match_result(void const * n, std::ptrdiff_t s, bool m, bool l) :
  268. node(n),
  269. size(s),
  270. match(m),
  271. leaf(l)
  272. {}
  273. /** An opaque pointer to the underlying trie node. */
  274. void const * node;
  275. /** The size/length of the match, from the root through `node`,
  276. inclusive. */
  277. std::ptrdiff_t size;
  278. /** True iff this result represents a match. Stated another way,
  279. `match` is true iff `node` represents an element in the trie
  280. (whether or not `node` has children). */
  281. bool match;
  282. /** True iff `node` is a leaf (that is, that `node` hs no
  283. children). */
  284. bool leaf;
  285. friend bool
  286. operator==(trie_match_result const & lhs, trie_match_result const & rhs)
  287. {
  288. return lhs.node == rhs.node && lhs.size == rhs.size &&
  289. lhs.match == rhs.match && lhs.leaf == rhs.leaf;
  290. }
  291. friend bool
  292. operator!=(trie_match_result const & lhs, trie_match_result const & rhs)
  293. {
  294. return !(lhs == rhs);
  295. }
  296. };
  297. /** A trie, or prefix-tree. A trie contains a set of keys, each of which
  298. is a sequence, and a set of values, each associated with some key.
  299. Each node in the trie represents some prefix found in at least one
  300. member of the set of values contained in the trie. If a certain node
  301. represents the end of one of the keys, it has an associated value.
  302. Such a node may or may not have children.
  303. Complexity of lookups is always O(M), where M is the size of the Key
  304. being lookep up. Note that this implies that lookup complexity is
  305. independent of the size of the trie.
  306. \param Key The key-type; must be a sequence of values comparable via
  307. Compare()(x, y).
  308. \param Value The value-type.
  309. \param Compare The type of the comparison object used to compare
  310. elements of the key-type.
  311. \note trie is not a C++ Container, according to the definition in the
  312. C++ standard, in part because it does not have any iterators. This
  313. allows trie to do slightly less work when it does insertions and
  314. erasures. If you need iterators, use trie_map or trie_set. */
  315. template<
  316. typename Key,
  317. typename Value,
  318. typename Compare,
  319. std::size_t KeySize>
  320. struct trie
  321. {
  322. private:
  323. using node_t = detail::
  324. trie_node_t<detail::no_index_within_parent_t, Key, Value, KeySize>;
  325. public:
  326. using key_type = Key;
  327. using value_type = Value;
  328. using key_compare = Compare;
  329. using key_element_type = typename Key::value_type;
  330. using reference = value_type &;
  331. using const_reference = value_type const &;
  332. using size_type = std::ptrdiff_t;
  333. using difference_type = std::ptrdiff_t;
  334. using match_result = trie_match_result;
  335. trie() : size_(0) {}
  336. trie(Compare const & comp) : size_(0), comp_(comp) {}
  337. template<typename Iter, typename Sentinel>
  338. trie(Iter first, Sentinel last, Compare const & comp = Compare()) :
  339. size_(0), comp_(comp)
  340. {
  341. insert(first, last);
  342. }
  343. template<typename Range>
  344. explicit trie(Range r, Compare const & comp = Compare()) :
  345. size_(0), comp_(comp)
  346. {
  347. insert(detail::begin(r), detail::end(r));
  348. }
  349. trie(std::initializer_list<trie_element<key_type, value_type>> il) :
  350. size_(0)
  351. {
  352. insert(il);
  353. }
  354. trie &
  355. operator=(std::initializer_list<trie_element<key_type, value_type>> il)
  356. {
  357. clear();
  358. for (auto const & x : il) {
  359. insert(x);
  360. }
  361. return *this;
  362. }
  363. bool empty() const { return size_ == 0; }
  364. size_type size() const { return size_; }
  365. #ifndef BOOST_TEXT_DOXYGEN
  366. #define BOOST_TRIE_C_STR_OVERLOAD(rtype, func) \
  367. template<typename Char, std::size_t N> \
  368. rtype func(Char const(&chars)[N]) \
  369. { \
  370. static_assert( \
  371. std::is_same<Char, key_element_type>::value, \
  372. "Only well-formed when Char is Key::value_type."); \
  373. return func(detail::char_range<Char const>{chars, chars + N - 1}); \
  374. }
  375. #define BOOST_TRIE_C_STR_OVERLOAD_QUALS(rtype, func, quals) \
  376. template<typename Char, std::size_t N> \
  377. rtype func(Char const(&chars)[N]) quals \
  378. { \
  379. static_assert( \
  380. std::is_same<Char, key_element_type>::value, \
  381. "Only well-formed when Char is Key::value_type."); \
  382. return func(detail::char_range<Char const>{chars, chars + N - 1}); \
  383. }
  384. #endif
  385. /** Returns true if `key` is found in *this. */
  386. template<typename KeyRange>
  387. bool contains(KeyRange const & key) const
  388. {
  389. auto first = detail::begin(key);
  390. auto const last = detail::end(key);
  391. auto match = longest_match_impl<false>(first, last);
  392. return first == last && match.match;
  393. }
  394. #ifndef BOOST_TEXT_DOXYGEN
  395. BOOST_TRIE_C_STR_OVERLOAD_QUALS(bool, contains, const)
  396. #endif
  397. /** Returns the longest subsequence of `[first, last)` found in *this,
  398. whether or not it is a match. */
  399. template<typename KeyIter, typename Sentinel>
  400. match_result longest_subsequence(KeyIter first, Sentinel last) const
  401. {
  402. return longest_match_impl<false>(first, last);
  403. }
  404. /** Returns the longest subsequence of `key` found in *this, whether
  405. or not it is a match. */
  406. template<typename KeyRange>
  407. match_result longest_subsequence(KeyRange const & key) const
  408. {
  409. return longest_subsequence(detail::begin(key), detail::end(key));
  410. }
  411. #ifndef BOOST_TEXT_DOXYGEN
  412. BOOST_TRIE_C_STR_OVERLOAD_QUALS(match_result, longest_subsequence, const)
  413. #endif
  414. /** Returns the longest matching subsequence of `[first, last)` found
  415. in *this. */
  416. template<typename KeyIter, typename Sentinel>
  417. match_result longest_match(KeyIter first, Sentinel last) const
  418. {
  419. return longest_match_impl<true>(first, last);
  420. }
  421. /** Returns the longest matching subsequence of `key` found in
  422. *this. */
  423. template<typename KeyRange>
  424. match_result longest_match(KeyRange const & key) const
  425. {
  426. return longest_match(detail::begin(key), detail::end(key));
  427. }
  428. #ifndef BOOST_TEXT_DOXYGEN
  429. BOOST_TRIE_C_STR_OVERLOAD_QUALS(match_result, longest_match, const)
  430. #endif
  431. /** Returns the result of extending `prev` by one element, `e`. */
  432. template<typename KeyElementT>
  433. match_result extend_subsequence(match_result prev, KeyElementT e) const
  434. {
  435. auto e_ptr = &e;
  436. return extend_subsequence_impl<false>(
  437. match_result{prev}, e_ptr, e_ptr + 1);
  438. }
  439. /** Returns the result of extending `prev` by the longest subsequence
  440. of `[first, last)` found in *this. */
  441. template<typename KeyIter, typename Sentinel>
  442. match_result extend_subsequence(
  443. match_result prev, KeyIter first, Sentinel last) const
  444. {
  445. return extend_subsequence_impl<false>(
  446. match_result{prev}, first, last);
  447. }
  448. /** Returns the result of extending `prev` by one element, `e`, if
  449. that would form a match, and `prev` otherwise. `prev` must be a
  450. match. */
  451. template<typename KeyElementT>
  452. match_result extend_match(match_result prev, KeyElementT e) const
  453. {
  454. BOOST_PARSER_DEBUG_ASSERT(prev.match);
  455. auto e_ptr = &e;
  456. return extend_subsequence_impl<true>(prev, e_ptr, e_ptr + 1);
  457. }
  458. /** Returns the result of extending `prev` by the longest subsequence
  459. of `[first, last)` found in *this, if that would form a match, and
  460. `prev` otherwise. `prev` must be a match. */
  461. template<typename KeyIter, typename Sentinel>
  462. match_result
  463. extend_match(match_result prev, KeyIter first, Sentinel last) const
  464. {
  465. BOOST_PARSER_DEBUG_ASSERT(prev.match);
  466. return extend_subsequence_impl<true>(prev, first, last);
  467. }
  468. /** Writes the sequence of elements that would advance `prev` by one
  469. element to `out`, and returns the final value of `out` after the
  470. writes. */
  471. template<typename OutIter>
  472. OutIter copy_next_key_elements(match_result prev, OutIter out) const
  473. {
  474. auto node = to_node_ptr(prev.node);
  475. return node->copy_next_key_elements(out);
  476. }
  477. /** Returns an optional reference to the const value associated with
  478. `key` in *this (if any). */
  479. template<typename KeyRange>
  480. optional_ref<value_type const> operator[](KeyRange const & key) const
  481. {
  482. auto first = detail::begin(key);
  483. auto const last = detail::end(key);
  484. auto match = longest_match_impl<false>(first, last);
  485. if (first != last || !match.match)
  486. return {};
  487. return *to_node_ptr(match.node)->value();
  488. }
  489. #ifndef BOOST_TEXT_DOXYGEN
  490. BOOST_TRIE_C_STR_OVERLOAD_QUALS(
  491. optional_ref<value_type const>, operator[], const)
  492. #endif
  493. /** Returns an optional reference to the const value associated with
  494. `match` in *this (if any). */
  495. optional_ref<value_type const> operator[](match_result match) const
  496. {
  497. if (!match.match)
  498. return {};
  499. return *to_node_ptr(match.node)->value();
  500. }
  501. void clear()
  502. {
  503. header_ = node_t();
  504. size_ = 0;
  505. }
  506. /** Returns an optional reference to the value associated with `key`
  507. in *this (if any). */
  508. template<typename KeyRange>
  509. optional_ref<value_type> operator[](KeyRange const & key)
  510. {
  511. auto first = detail::begin(key);
  512. auto const last = detail::end(key);
  513. auto match = longest_match_impl<false>(first, last);
  514. if (first != last || !match.match)
  515. return {};
  516. return *const_cast<node_t *>(to_node_ptr(match.node))->value();
  517. }
  518. #ifndef BOOST_TEXT_DOXYGEN
  519. BOOST_TRIE_C_STR_OVERLOAD(optional_ref<value_type>, operator[])
  520. #endif
  521. /** Returns an optional reference to the value associated with `match`
  522. in *this (if any). */
  523. optional_ref<value_type> operator[](match_result match)
  524. {
  525. if (!match.match)
  526. return {};
  527. return *const_cast<node_t *>(to_node_ptr(match.node))->value();
  528. }
  529. /** Inserts the key/value pair `[first, last)`, `value` into *this.
  530. Returns false if the key already exists in *this, true
  531. otherwise. */
  532. template<typename KeyIter, typename Sentinel>
  533. bool insert(KeyIter first, Sentinel last, Value value)
  534. {
  535. if (empty()) {
  536. std::unique_ptr<node_t> new_node(new node_t(&header_));
  537. header_.insert(std::move(new_node));
  538. }
  539. auto match = longest_match_impl<false>(first, last);
  540. if (first == last && match.match)
  541. return false;
  542. auto node = create_children(
  543. const_cast<node_t *>(to_node_ptr(match.node)), first, last);
  544. node->value() = std::move(value);
  545. ++size_;
  546. return true;
  547. }
  548. /** Inserts the key/value pair `key`, `value` into *this. Returns
  549. false if the key already exists in *this, true otherwise. */
  550. template<typename KeyRange>
  551. bool insert(KeyRange const & key, Value value)
  552. {
  553. return insert(
  554. detail::begin(key), detail::end(key), std::move(value));
  555. }
  556. template<typename Char, std::size_t N>
  557. bool insert(Char const (&chars)[N], Value value)
  558. {
  559. static_assert(
  560. std::is_same<Char, key_element_type>::value,
  561. "Only well-formed when Char is Key::value_type.");
  562. return insert(
  563. detail::char_range<Char const>{chars, chars + N - 1},
  564. std::move(value));
  565. }
  566. /** Inserts the sequence of key/value pairs `[first, last)` into
  567. *this. */
  568. template<typename Iter, typename Sentinel>
  569. void insert(Iter first, Sentinel last)
  570. {
  571. for (; first != last; ++first) {
  572. insert(first->key, first->value);
  573. }
  574. }
  575. /** Inserts the element e.key, e.value into *this. */
  576. bool insert(trie_element<key_type, value_type> const & e)
  577. {
  578. return insert(e.key, e.value);
  579. }
  580. /** Inserts the sequence of key/value pairs `r` into
  581. *this. */
  582. template<typename Range>
  583. bool insert(Range const & r)
  584. {
  585. return insert(detail::begin(r), detail::end(r));
  586. }
  587. /** Inserts the sequence of key/value pairs `il` into *this. */
  588. void
  589. insert(std::initializer_list<trie_element<key_type, value_type>> il)
  590. {
  591. for (auto const & x : il) {
  592. insert(x);
  593. }
  594. }
  595. /** Inserts the key/value pair `[first, last)`, `value` into *this, or
  596. assigns `value` over the existing value associated with `[first,
  597. last)`, if this key is already found in *this. The `inserted`
  598. field of the result will be true if the operation resulted in a
  599. new insertion, or false otherwise. */
  600. template<typename KeyIter, typename Sentinel>
  601. void insert_or_assign(KeyIter first, Sentinel last, Value value)
  602. {
  603. auto it = first;
  604. auto match = longest_match_impl<false>(it, last);
  605. if (it == last && match.match) {
  606. const_cast<Value &>(*to_node_ptr(match.node)->value()) =
  607. std::move(value);
  608. }
  609. insert(first, last, std::move(value));
  610. }
  611. /** Inserts the key/value pair `key`, `value` into *this, or assigns
  612. `value` over the existing value associated with `key`, if `key` is
  613. already found in *this. The `inserted` field of the result will
  614. be true if the operation resulted in a new insertion, or false
  615. otherwise. */
  616. template<typename KeyRange>
  617. void insert_or_assign(KeyRange const & key, Value value)
  618. {
  619. insert_or_assign(
  620. detail::begin(key), detail::end(key), std::move(value));
  621. }
  622. /** Erases the key/value pair associated with `key` from *this.
  623. Returns true if the key is found in *this, false otherwise. */
  624. template<typename KeyRange>
  625. bool erase(KeyRange const & key)
  626. {
  627. auto first = detail::begin(key);
  628. auto const last = detail::end(key);
  629. auto match = longest_match_impl<false>(first, last);
  630. if (first != last || !match.match)
  631. return false;
  632. return erase(match);
  633. }
  634. /** Erases the key/value pair associated with `match` from *this.
  635. Returns true if the key is found in *this, false otherwise. */
  636. bool erase(match_result match)
  637. {
  638. auto node = const_cast<node_t *>(to_node_ptr(match.node));
  639. if (node == &header_)
  640. return false;
  641. --size_;
  642. if (!node->empty()) {
  643. // node has a value, but also children. Remove the value and
  644. // return the next-iterator.
  645. node->value() = std::optional<Value>();
  646. return true;
  647. }
  648. // node has a value, *and* no children. Remove it and all its
  649. // singular predecessors.
  650. auto parent = const_cast<node_t *>(node->parent());
  651. parent->erase(node);
  652. while (parent->parent() && parent->empty() && !parent->value()) {
  653. node = parent;
  654. parent = const_cast<node_t *>(node->parent());
  655. parent->erase(node);
  656. }
  657. return true;
  658. }
  659. #ifndef BOOST_TEXT_DOXYGEN
  660. BOOST_TRIE_C_STR_OVERLOAD(bool, erase)
  661. #endif
  662. void swap(trie & other)
  663. {
  664. header_.swap(other.header_);
  665. std::swap(size_, other.size_);
  666. std::swap(comp_, other.comp_);
  667. }
  668. friend bool operator==(trie const & lhs, trie const & rhs)
  669. {
  670. if (lhs.size_ != rhs.size_)
  671. return false;
  672. return lhs.header_ == rhs.header_;
  673. }
  674. friend bool operator!=(trie const & lhs, trie const & rhs)
  675. {
  676. return !(lhs == rhs);
  677. }
  678. #ifndef BOOST_TEXT_DOXYGEN
  679. private:
  680. trie const * const_this() { return const_cast<trie const *>(this); }
  681. static node_t const * to_node_ptr(void const * ptr)
  682. {
  683. return static_cast<node_t const *>(ptr);
  684. }
  685. template<bool StopAtMatch, typename KeyIter, typename Sentinel>
  686. match_result
  687. longest_match_impl(KeyIter & first, Sentinel last) const
  688. {
  689. return extend_subsequence_impl<StopAtMatch>(
  690. match_result{&header_, 0, false, true}, first, last);
  691. }
  692. template<bool StopAtMatch, typename KeyIter, typename Sentinel>
  693. match_result extend_subsequence_impl(
  694. match_result prev, KeyIter & first, Sentinel last) const
  695. {
  696. if (to_node_ptr(prev.node) == &header_) {
  697. if (header_.empty())
  698. return prev;
  699. prev.node = header_.child(0);
  700. }
  701. if (first == last) {
  702. prev.match = !!to_node_ptr(prev.node)->value();
  703. prev.leaf = to_node_ptr(prev.node)->empty();
  704. return prev;
  705. }
  706. node_t const * node = to_node_ptr(prev.node);
  707. size_type size = prev.size;
  708. node_t const * last_match_node = nullptr;
  709. size_type last_match_size = 0;
  710. while (first != last) {
  711. auto const it = node->find(*first, comp_);
  712. if (it == node->end())
  713. break;
  714. ++first;
  715. ++size;
  716. node = it->get();
  717. if (StopAtMatch && !!node->value()) {
  718. last_match_node = node;
  719. last_match_size = size;
  720. }
  721. }
  722. if (StopAtMatch && last_match_node) {
  723. node = last_match_node;
  724. size = last_match_size;
  725. }
  726. return match_result{node, size, !!node->value(), node->empty()};
  727. }
  728. template<typename KeyIter, typename Sentinel>
  729. node_t * create_children(node_t * node, KeyIter first, Sentinel last)
  730. {
  731. auto retval = node;
  732. for (; first != last; ++first) {
  733. std::unique_ptr<node_t> new_node(new node_t(retval));
  734. retval =
  735. retval->insert(*first, comp_, std::move(new_node))->get();
  736. }
  737. return retval;
  738. }
  739. node_t header_;
  740. size_type size_;
  741. key_compare comp_;
  742. template<typename Key2, typename Value2, typename Compare2>
  743. friend struct trie_map;
  744. #endif
  745. };
  746. namespace detail {
  747. template<
  748. typename ParentIndexing,
  749. typename Key,
  750. typename Value,
  751. std::size_t KeySize>
  752. struct trie_node_t
  753. {
  754. using children_t = std::vector<std::unique_ptr<trie_node_t>>;
  755. using iterator = typename children_t::iterator;
  756. using const_iterator = typename children_t::const_iterator;
  757. using key_element = typename Key::value_type;
  758. static_assert(std::is_unsigned<key_element>::value, "");
  759. trie_node_t() : parent_(nullptr) {}
  760. trie_node_t(trie_node_t * parent) : parent_(parent) {}
  761. trie_node_t(trie_node_t const & other) :
  762. children_(other.children_.size()),
  763. value_(other.value_),
  764. parent_(other.parent_),
  765. index_within_parent_(other.index_within_parent_)
  766. {
  767. for (int i = 0, end = (int)children_.size(); i < end; ++i) {
  768. if (other.children_[i]) {
  769. children_[i].reset(
  770. new trie_node_t(*other.children_[i]));
  771. }
  772. }
  773. }
  774. trie_node_t(trie_node_t && other) : parent_(nullptr)
  775. {
  776. swap(other);
  777. }
  778. trie_node_t & operator=(trie_node_t const & rhs)
  779. {
  780. BOOST_PARSER_DEBUG_ASSERT(
  781. parent_ == nullptr &&
  782. "Assignment of trie_node_ts are defined only for the "
  783. "header node.");
  784. trie_node_t temp(rhs);
  785. temp.swap(*this);
  786. return *this;
  787. }
  788. trie_node_t & operator=(trie_node_t && rhs)
  789. {
  790. BOOST_PARSER_DEBUG_ASSERT(
  791. parent_ == nullptr &&
  792. "Move assignments of trie_node_ts are defined only for the "
  793. "header node.");
  794. trie_node_t temp(std::move(rhs));
  795. temp.swap(*this);
  796. return *this;
  797. }
  798. auto max_size() const { return KeySize; }
  799. std::optional<Value> const & value() const { return value_; }
  800. Value & child_value(std::size_t i) const
  801. {
  802. return *children_[i]->value_;
  803. }
  804. trie_node_t * parent() const { return parent_; }
  805. bool empty() const { return children_.size() == 0; }
  806. const_iterator end() const { return children_.end(); }
  807. std::size_t index_within_parent() const
  808. {
  809. return index_within_parent_.value();
  810. }
  811. bool before_child_subtree(key_element const & e) const
  812. {
  813. return e < key_element(0);
  814. }
  815. template<typename Compare>
  816. const_iterator
  817. lower_bound(key_element const & e, Compare const &) const
  818. {
  819. return children_.empty() ? children_.end() : children_.begin() + e;
  820. }
  821. template<typename Compare>
  822. const_iterator
  823. find(key_element const & e, Compare const & comp) const
  824. {
  825. auto const it = lower_bound(e, comp);
  826. if (children_.empty() || !*it)
  827. return children_.end();
  828. return it;
  829. }
  830. template<typename Compare>
  831. trie_node_t const *
  832. child(key_element const & e, Compare const &) const
  833. {
  834. return children_[e].get();
  835. }
  836. trie_node_t const * child(std::size_t i) const
  837. {
  838. return children_[i].get();
  839. }
  840. key_element const & key(std::size_t i) const
  841. {
  842. BOOST_PARSER_DEBUG_ASSERT(key_element(i) == i);
  843. return key_element(i);
  844. }
  845. template<typename OutIter>
  846. OutIter copy_next_key_elements(OutIter out) const
  847. {
  848. for (key_element i = 0, end = (key_element)children_.size();
  849. i < end;
  850. ++i) {
  851. if (children_[i]) {
  852. *out = i;
  853. ++out;
  854. }
  855. }
  856. return out;
  857. }
  858. void swap(trie_node_t & other)
  859. {
  860. BOOST_PARSER_DEBUG_ASSERT(
  861. parent_ == nullptr &&
  862. "Swaps of trie_node_ts are defined only for the header "
  863. "node.");
  864. children_.swap(other.children_);
  865. value_.swap(other.value_);
  866. for (auto const & node : children_) {
  867. if (node)
  868. node->parent_ = this;
  869. }
  870. for (auto const & node : other.children_) {
  871. if (node)
  872. node->parent_ = &other;
  873. }
  874. std::swap(index_within_parent_, other.index_within_parent_);
  875. }
  876. std::optional<Value> & value() { return value_; }
  877. template<typename Compare>
  878. iterator insert(
  879. key_element const & e,
  880. Compare const & comp,
  881. std::unique_ptr<trie_node_t> && child)
  882. {
  883. if (children_.empty())
  884. children_.resize(max_size());
  885. auto child_it = children_.begin() + e;
  886. index_within_parent_.insert_at(
  887. child, e, child_it, children_.end());
  888. children_[e] = std::move(child);
  889. return child_it;
  890. }
  891. iterator insert(std::unique_ptr<trie_node_t> && child)
  892. {
  893. if (children_.empty())
  894. children_.resize(max_size());
  895. index_within_parent_.insert_ptr(child);
  896. return children_.insert(children_.begin(), std::move(child));
  897. }
  898. void erase(std::size_t i)
  899. {
  900. auto it = children_.begin() + i;
  901. it->reset(nullptr);
  902. index_within_parent_.erase(it, children_.end());
  903. }
  904. void erase(trie_node_t const * child)
  905. {
  906. auto const it = std::find_if(
  907. children_.begin(),
  908. children_.end(),
  909. [child](std::unique_ptr<trie_node_t> const & ptr) {
  910. return child == ptr.get();
  911. });
  912. BOOST_PARSER_DEBUG_ASSERT(it != children_.end());
  913. erase(it - children_.begin());
  914. }
  915. template<typename Compare>
  916. iterator
  917. lower_bound(key_element const & e, Compare const &)
  918. {
  919. return children_.begin() + e;
  920. }
  921. template<typename Compare>
  922. iterator find(key_element const & e, Compare const & comp)
  923. {
  924. if (children_.empty())
  925. return children_.end();
  926. auto const it = lower_bound(e, comp);
  927. if (!*it)
  928. return children_.end();
  929. return it;
  930. }
  931. template<typename Compare>
  932. trie_node_t * child(key_element const & e, Compare const &)
  933. {
  934. return children_[e].get();
  935. }
  936. trie_node_t * child(std::size_t i)
  937. {
  938. return children_[i].get();
  939. }
  940. friend bool
  941. operator==(trie_node_t const & lhs, trie_node_t const & rhs)
  942. {
  943. if (lhs.value_ != rhs.value_)
  944. return false;
  945. return std::equal(
  946. lhs.children_.begin(),
  947. lhs.children_.end(),
  948. rhs.children_.begin(),
  949. rhs.children_.end(),
  950. [](auto const & l_ptr, auto const & r_ptr) {
  951. if (!l_ptr && !r_ptr)
  952. return true;
  953. if (!l_ptr || !r_ptr)
  954. return false;
  955. return *l_ptr == *r_ptr;
  956. });
  957. }
  958. friend bool
  959. operator!=(trie_node_t const & lhs, trie_node_t const & rhs)
  960. {
  961. return !(lhs == rhs);
  962. }
  963. private:
  964. trie_node_t const * const_this()
  965. {
  966. return const_cast<trie_node_t const *>(this);
  967. }
  968. key_element const & key(const_iterator it) const
  969. {
  970. return key_element(it - children_.begin());
  971. }
  972. children_t children_;
  973. std::optional<Value> value_;
  974. trie_node_t * parent_;
  975. ParentIndexing index_within_parent_;
  976. friend struct index_within_parent_t;
  977. };
  978. template<typename ParentIndexing, typename Key, typename Value>
  979. struct trie_node_t<ParentIndexing, Key, Value, 0>
  980. {
  981. using children_t = std::vector<std::unique_ptr<trie_node_t>>;
  982. using iterator = typename children_t::iterator;
  983. using const_iterator = typename children_t::const_iterator;
  984. using key_element = typename Key::value_type;
  985. using keys_t = std::vector<key_element>;
  986. using key_iterator = typename keys_t::const_iterator;
  987. trie_node_t() : parent_(nullptr) {}
  988. trie_node_t(trie_node_t * parent) : parent_(parent) {}
  989. trie_node_t(trie_node_t const & other) :
  990. keys_(other.keys_),
  991. value_(other.value_),
  992. parent_(other.parent_),
  993. index_within_parent_(other.index_within_parent_)
  994. {
  995. children_.reserve(other.children_.size());
  996. for (auto const & node : other.children_) {
  997. std::unique_ptr<trie_node_t> new_node(
  998. new trie_node_t(*node));
  999. children_.push_back(std::move(new_node));
  1000. }
  1001. }
  1002. trie_node_t(trie_node_t && other) : parent_(nullptr)
  1003. {
  1004. swap(other);
  1005. }
  1006. trie_node_t & operator=(trie_node_t const & rhs)
  1007. {
  1008. BOOST_PARSER_DEBUG_ASSERT(
  1009. parent_ == nullptr &&
  1010. "Assignment of trie_node_ts are defined only for the "
  1011. "header node.");
  1012. trie_node_t temp(rhs);
  1013. temp.swap(*this);
  1014. return *this;
  1015. }
  1016. trie_node_t & operator=(trie_node_t && rhs)
  1017. {
  1018. BOOST_PARSER_DEBUG_ASSERT(
  1019. parent_ == nullptr &&
  1020. "Move assignments of trie_node_ts are defined only for the "
  1021. "header node.");
  1022. trie_node_t temp(std::move(rhs));
  1023. temp.swap(*this);
  1024. return *this;
  1025. }
  1026. std::optional<Value> const & value() const { return value_; }
  1027. Value & child_value(std::size_t i) const
  1028. {
  1029. return *children_[i]->value_;
  1030. }
  1031. trie_node_t * parent() const { return parent_; }
  1032. trie_node_t * min_child() const
  1033. {
  1034. return children_.front().get();
  1035. }
  1036. trie_node_t * max_child() const
  1037. {
  1038. return children_.back().get();
  1039. }
  1040. bool empty() const { return children_.size() == 0; }
  1041. std::size_t size() const { return children_.size(); }
  1042. bool min_value() const
  1043. {
  1044. return !!children_.front()->value_;
  1045. }
  1046. bool max_value() const
  1047. {
  1048. return !!children_.back()->value_;
  1049. }
  1050. const_iterator begin() const { return children_.begin(); }
  1051. const_iterator end() const { return children_.end(); }
  1052. key_iterator key_begin() const { return keys_.begin(); }
  1053. key_iterator key_end() const { return keys_.end(); }
  1054. std::size_t index_within_parent() const
  1055. {
  1056. return index_within_parent_.value();
  1057. }
  1058. bool before_child_subtree(key_element const & e) const
  1059. {
  1060. return keys_.empty() || e < keys_.front();
  1061. }
  1062. template<typename Compare>
  1063. const_iterator
  1064. lower_bound(key_element const & e, Compare const & comp) const
  1065. {
  1066. auto const it =
  1067. std::lower_bound(keys_.begin(), keys_.end(), e, comp);
  1068. return children_.begin() + (it - keys_.begin());
  1069. }
  1070. template<typename Compare>
  1071. const_iterator
  1072. find(key_element const & e, Compare const & comp) const
  1073. {
  1074. auto const it = lower_bound(e, comp);
  1075. auto const end_ = end();
  1076. if (it != end_ && comp(e, key(it)))
  1077. return end_;
  1078. return it;
  1079. }
  1080. template<typename Compare>
  1081. trie_node_t const *
  1082. child(key_element const & e, Compare const & comp) const
  1083. {
  1084. auto const it = find(e, comp);
  1085. if (it == children_.end())
  1086. return nullptr;
  1087. return it->get();
  1088. }
  1089. trie_node_t const * child(std::size_t i) const
  1090. {
  1091. return children_[i].get();
  1092. }
  1093. key_element const & key(std::size_t i) const
  1094. {
  1095. return keys_[i];
  1096. }
  1097. template<typename OutIter>
  1098. OutIter copy_next_key_elements(OutIter out) const
  1099. {
  1100. return std::copy(key_begin(), key_end(), out);
  1101. }
  1102. void swap(trie_node_t & other)
  1103. {
  1104. BOOST_PARSER_DEBUG_ASSERT(
  1105. parent_ == nullptr &&
  1106. "Swaps of trie_node_ts are defined only for the header "
  1107. "node.");
  1108. keys_.swap(other.keys_);
  1109. children_.swap(other.children_);
  1110. value_.swap(other.value_);
  1111. for (auto const & node : children_) {
  1112. node->parent_ = this;
  1113. }
  1114. for (auto const & node : other.children_) {
  1115. node->parent_ = &other;
  1116. }
  1117. std::swap(index_within_parent_, other.index_within_parent_);
  1118. }
  1119. std::optional<Value> & value() { return value_; }
  1120. iterator begin() { return children_.begin(); }
  1121. iterator end() { return children_.end(); }
  1122. template<typename Compare>
  1123. iterator insert(
  1124. key_element const & e,
  1125. Compare const & comp,
  1126. std::unique_ptr<trie_node_t> && child)
  1127. {
  1128. BOOST_PARSER_DEBUG_ASSERT(child->empty());
  1129. auto it = std::lower_bound(keys_.begin(), keys_.end(), e, comp);
  1130. it = keys_.insert(it, e);
  1131. auto const offset = it - keys_.begin();
  1132. auto child_it = children_.begin() + offset;
  1133. index_within_parent_.insert_at(
  1134. child, offset, child_it, children_.end());
  1135. return children_.insert(child_it, std::move(child));
  1136. }
  1137. iterator insert(std::unique_ptr<trie_node_t> && child)
  1138. {
  1139. BOOST_PARSER_DEBUG_ASSERT(empty());
  1140. index_within_parent_.insert_ptr(child);
  1141. return children_.insert(children_.begin(), std::move(child));
  1142. }
  1143. void erase(std::size_t i)
  1144. {
  1145. // This empty-keys situation happens only in the header node.
  1146. if (!keys_.empty())
  1147. keys_.erase(keys_.begin() + i);
  1148. auto it = children_.erase(children_.begin() + i);
  1149. index_within_parent_.erase(it, children_.end());
  1150. }
  1151. void erase(trie_node_t const * child)
  1152. {
  1153. auto const it = std::find_if(
  1154. children_.begin(),
  1155. children_.end(),
  1156. [child](std::unique_ptr<trie_node_t> const & ptr) {
  1157. return child == ptr.get();
  1158. });
  1159. BOOST_PARSER_DEBUG_ASSERT(it != children_.end());
  1160. erase(it - children_.begin());
  1161. }
  1162. template<typename Compare>
  1163. iterator
  1164. lower_bound(key_element const & e, Compare const & comp)
  1165. {
  1166. auto const it = const_this()->lower_bound(e, comp);
  1167. return children_.begin() +
  1168. (it - const_iterator(children_.begin()));
  1169. }
  1170. template<typename Compare>
  1171. iterator find(key_element const & e, Compare const & comp)
  1172. {
  1173. auto const it = const_this()->find(e, comp);
  1174. return children_.begin() +
  1175. (it - const_iterator(children_.begin()));
  1176. }
  1177. template<typename Compare>
  1178. trie_node_t *
  1179. child(key_element const & e, Compare const & comp)
  1180. {
  1181. return const_cast<trie_node_t *>(const_this()->child(e, comp));
  1182. }
  1183. trie_node_t * child(std::size_t i)
  1184. {
  1185. return const_cast<trie_node_t *>(const_this()->child(i));
  1186. }
  1187. friend bool
  1188. operator==(trie_node_t const & lhs, trie_node_t const & rhs)
  1189. {
  1190. if (lhs.keys_ != rhs.keys_ || lhs.value_ != rhs.value_)
  1191. return false;
  1192. return std::equal(
  1193. lhs.children_.begin(),
  1194. lhs.children_.end(),
  1195. rhs.children_.begin(),
  1196. rhs.children_.end(),
  1197. [](auto const & l_ptr, auto const & r_ptr) {
  1198. if (!l_ptr && !r_ptr)
  1199. return true;
  1200. if (!l_ptr || !r_ptr)
  1201. return false;
  1202. return *l_ptr == *r_ptr;
  1203. });
  1204. }
  1205. friend bool
  1206. operator!=(trie_node_t const & lhs, trie_node_t const & rhs)
  1207. {
  1208. return !(lhs == rhs);
  1209. }
  1210. private:
  1211. trie_node_t const * const_this()
  1212. {
  1213. return const_cast<trie_node_t const *>(this);
  1214. }
  1215. key_element const & key(const_iterator it) const
  1216. {
  1217. return keys_[it - children_.begin()];
  1218. }
  1219. keys_t keys_;
  1220. children_t children_;
  1221. std::optional<Value> value_;
  1222. trie_node_t * parent_;
  1223. ParentIndexing index_within_parent_;
  1224. friend struct index_within_parent_t;
  1225. };
  1226. }
  1227. }}
  1228. #endif