trie_map.hpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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_MAP_HPP
  7. #define BOOST_PARSER_DETAIL_TEXT_TRIE_MAP_HPP
  8. #include <boost/parser/detail/text/trie.hpp>
  9. #include <boost/parser/detail/stl_interfaces/reverse_iterator.hpp>
  10. namespace boost::parser::detail { namespace text {
  11. template<typename Key, typename Value>
  12. struct trie_map_iterator;
  13. template<typename Key, typename Value>
  14. struct const_trie_map_iterator;
  15. template<typename Key, typename Value>
  16. using reverse_trie_map_iterator =
  17. stl_interfaces::reverse_iterator<trie_map_iterator<Key, Value>>;
  18. template<typename Key, typename Value>
  19. using const_reverse_trie_map_iterator =
  20. stl_interfaces::reverse_iterator<const_trie_map_iterator<Key, Value>>;
  21. /** A range type returned by certain operations on a trie_map or
  22. trie_set. */
  23. template<typename Iter>
  24. struct trie_range
  25. {
  26. using iterator = Iter;
  27. iterator first;
  28. iterator last;
  29. iterator begin() const { return first; }
  30. iterator end() const { return last; }
  31. friend bool operator==(trie_range const & lhs, trie_range const & rhs)
  32. {
  33. return lhs.first == rhs.first && lhs.last == rhs.last;
  34. }
  35. friend bool operator!=(trie_range const & lhs, trie_range const & rhs)
  36. {
  37. return !(lhs == rhs);
  38. }
  39. };
  40. /** A constant range type returned by certain operations on a trie_map or
  41. trie_set. */
  42. template<typename Iter>
  43. struct const_trie_range
  44. {
  45. using const_iterator = Iter;
  46. const_iterator first;
  47. const_iterator last;
  48. const_iterator begin() const { return first; }
  49. const_iterator end() const { return last; }
  50. friend bool
  51. operator==(const_trie_range const & lhs, const_trie_range const & rhs)
  52. {
  53. return lhs.first == rhs.first && lhs.last == rhs.last;
  54. }
  55. friend bool
  56. operator!=(const_trie_range const & lhs, const_trie_range const & rhs)
  57. {
  58. return !(lhs == rhs);
  59. }
  60. };
  61. /** The result type of insert() operations on a trie_map or trie_set. */
  62. template<typename Iter>
  63. struct trie_insert_result
  64. {
  65. using iterator = Iter;
  66. trie_insert_result() : iter(), inserted(false) {}
  67. trie_insert_result(iterator it, bool ins) : iter(it), inserted(ins) {}
  68. iterator iter;
  69. bool inserted;
  70. friend bool operator==(
  71. trie_insert_result const & lhs, trie_insert_result const & rhs)
  72. {
  73. return lhs.iter == rhs.iter && lhs.inserted == rhs.inserted;
  74. }
  75. friend bool operator!=(
  76. trie_insert_result const & lhs, trie_insert_result const & rhs)
  77. {
  78. return !(lhs == rhs);
  79. }
  80. };
  81. namespace detail {
  82. struct index_within_parent_t
  83. {
  84. index_within_parent_t() : value_(-1) {}
  85. std::size_t value() const { return value_; }
  86. template<
  87. typename Key,
  88. typename Value,
  89. typename Iter,
  90. std::size_t KeySize>
  91. void insert_at(
  92. std::unique_ptr<trie_node_t<
  93. index_within_parent_t,
  94. Key,
  95. Value,
  96. KeySize>> const & child,
  97. std::ptrdiff_t offset,
  98. Iter it,
  99. Iter end)
  100. {
  101. child->index_within_parent_.value_ = offset;
  102. for (; it != end; ++it) {
  103. ++(*it)->index_within_parent_.value_;
  104. }
  105. }
  106. template<typename Key, typename Value, std::size_t KeySize>
  107. void insert_ptr(std::unique_ptr<trie_node_t<
  108. index_within_parent_t,
  109. Key,
  110. Value,
  111. KeySize>> const & child)
  112. {
  113. child->index_within_parent_.value_ = 0;
  114. }
  115. template<typename Iter>
  116. void erase(Iter it, Iter end)
  117. {
  118. for (; it != end; ++it) {
  119. --(*it)->index_within_parent_.value_;
  120. }
  121. }
  122. private:
  123. std::size_t value_;
  124. };
  125. template<typename Key, typename Value, std::size_t KeySize = 0>
  126. struct trie_iterator_state_t
  127. {
  128. trie_node_t<index_within_parent_t, Key, Value, KeySize> const *
  129. parent_;
  130. std::size_t index_;
  131. };
  132. template<typename Key, typename Value, std::size_t KeySize>
  133. trie_iterator_state_t<Key, Value, KeySize>
  134. parent_state(trie_iterator_state_t<Key, Value, KeySize> state)
  135. {
  136. return {state.parent_->parent(),
  137. state.parent_->index_within_parent()};
  138. }
  139. template<typename Key, typename Value, std::size_t KeySize>
  140. Key reconstruct_key(trie_iterator_state_t<Key, Value, KeySize> state)
  141. {
  142. Key retval;
  143. while (state.parent_->parent()) {
  144. retval.insert(retval.end(), state.parent_->key(state.index_));
  145. state = parent_state(state);
  146. }
  147. std::reverse(retval.begin(), retval.end());
  148. return retval;
  149. }
  150. template<typename Key, typename Value, std::size_t KeySize>
  151. trie_node_t<index_within_parent_t, Key, Value, KeySize> const *
  152. to_node(trie_iterator_state_t<Key, Value, KeySize> state)
  153. {
  154. if (state.index_ < state.parent_->size())
  155. return state.parent_->child(state.index_);
  156. return nullptr;
  157. }
  158. }
  159. /** An associative container similar to std::map, built upon a trie, or
  160. prefix-tree. A trie_map contains a set of keys, each of which is a
  161. sequence, and a set of values, each associated with some key. Each
  162. node in the trie_map represents some prefix found in at least one
  163. member of the set of values contained in the trie_map. If a certain
  164. node represents the end of one of the keys, it has an associated
  165. value. Such a node may or may not have children.
  166. Complexity of lookups is always O(M), where M is the size of the Key
  167. being lookep up. Note that this implies that lookup complexity is
  168. independent of the size of the trie_map.
  169. \param Key The key-type; must be a sequence of values comparable via
  170. Compare()(x, y).
  171. \param Value The value-type.
  172. \param Compare The type of the comparison object used to compare
  173. elements of the key-type.
  174. */
  175. template<typename Key, typename Value, typename Compare>
  176. struct trie_map
  177. {
  178. private:
  179. using node_t =
  180. detail::trie_node_t<detail::index_within_parent_t, Key, Value>;
  181. using iter_state_t = detail::trie_iterator_state_t<Key, Value>;
  182. public:
  183. using key_type = Key;
  184. using mapped_type = Value;
  185. using value_type = trie_element<Key, Value>;
  186. using key_compare = Compare;
  187. using key_element_type = typename Key::value_type;
  188. using reference = value_type &;
  189. using const_reference = value_type const &;
  190. using iterator = trie_map_iterator<key_type, mapped_type>;
  191. using const_iterator = const_trie_map_iterator<key_type, mapped_type>;
  192. using reverse_iterator =
  193. reverse_trie_map_iterator<key_type, mapped_type>;
  194. using const_reverse_iterator =
  195. const_reverse_trie_map_iterator<key_type, mapped_type>;
  196. using size_type = std::ptrdiff_t;
  197. using difference_type = std::ptrdiff_t;
  198. using range = trie_range<iterator>;
  199. using const_range = const_trie_range<const_iterator>;
  200. using insert_result = trie_insert_result<iterator>;
  201. using match_result = trie_match_result;
  202. trie_map() : size_(0) {}
  203. trie_map(Compare const & comp) : size_(0), comp_(comp) {}
  204. template<typename Iter, typename Sentinel>
  205. trie_map(Iter first, Sentinel last, Compare const & comp = Compare()) :
  206. size_(0),
  207. comp_(comp)
  208. {
  209. insert(first, last);
  210. }
  211. template<typename Range>
  212. explicit trie_map(Range r, Compare const & comp = Compare()) :
  213. size_(0),
  214. comp_(comp)
  215. {
  216. insert(detail::begin(r), detail::end(r));
  217. }
  218. template<std::size_t KeySize>
  219. explicit trie_map(
  220. parser::detail::text::trie<Key, Value, Compare, KeySize> const &
  221. trie) :
  222. size_(0)
  223. {
  224. Key key;
  225. from_trie_impl(trie.header_, key);
  226. }
  227. trie_map(std::initializer_list<value_type> il) : size_(0)
  228. {
  229. insert(il);
  230. }
  231. trie_map & operator=(std::initializer_list<value_type> il)
  232. {
  233. clear();
  234. for (auto const & x : il) {
  235. insert(x);
  236. }
  237. return *this;
  238. }
  239. bool empty() const { return size_ == 0; }
  240. size_type size() const { return size_; }
  241. size_type max_size() const { return PTRDIFF_MAX; }
  242. const_iterator begin() const
  243. {
  244. iter_state_t state{&header_, 0};
  245. if (size_) {
  246. while (!state.parent_->min_value()) {
  247. state.parent_ = state.parent_->min_child();
  248. }
  249. }
  250. return const_iterator(state);
  251. }
  252. const_iterator end() const
  253. {
  254. iter_state_t state{&header_, 0};
  255. if (size_) {
  256. node_t const * node = nullptr;
  257. while ((node = to_node(state))) {
  258. state.parent_ = node;
  259. state.index_ = state.parent_->size() - 1;
  260. }
  261. state.parent_ = state.parent_->parent();
  262. state.index_ = state.parent_->size();
  263. }
  264. return const_iterator(state);
  265. }
  266. const_iterator cbegin() const { return begin(); }
  267. const_iterator cend() const { return end(); }
  268. const_reverse_iterator rbegin() const
  269. {
  270. return const_reverse_iterator{end()};
  271. }
  272. const_reverse_iterator rend() const
  273. {
  274. return const_reverse_iterator{begin()};
  275. }
  276. const_reverse_iterator crbegin() const { return rbegin(); }
  277. const_reverse_iterator crend() const { return rend(); }
  278. #ifndef BOOST_PARSER_DOXYGEN
  279. #define BOOST_TRIE_MAP_C_STR_OVERLOAD(rtype, func) \
  280. template<typename Char, std::size_t N> \
  281. rtype func(Char const(&chars)[N]) \
  282. { \
  283. static_assert( \
  284. std::is_same<Char, key_element_type>::value, \
  285. "Only well-formed when Char is Key::value_type."); \
  286. return func(detail::char_range<Char const>{chars, chars + N - 1}); \
  287. }
  288. #define BOOST_TRIE_MAP_C_STR_OVERLOAD_QUALS(rtype, func, quals) \
  289. template<typename Char, std::size_t N> \
  290. rtype func(Char const(&chars)[N]) quals \
  291. { \
  292. static_assert( \
  293. std::is_same<Char, key_element_type>::value, \
  294. "Only well-formed when Char is Key::value_type."); \
  295. return func(detail::char_range<Char const>{chars, chars + N - 1}); \
  296. }
  297. #endif
  298. /** Returns true if `key` is found in *this. */
  299. template<typename KeyRange>
  300. bool contains(KeyRange const & key) const
  301. {
  302. return find(key) != end();
  303. }
  304. #ifndef BOOST_PARSER_DOXYGEN
  305. BOOST_TRIE_MAP_C_STR_OVERLOAD_QUALS(bool, contains, const)
  306. #endif
  307. /** Returns the iterator pointing to the key/value pair associated
  308. with `key`, if `key` is found in *this. Returns end()
  309. otherwise. */
  310. template<typename KeyRange>
  311. const_iterator find(KeyRange const & key) const
  312. {
  313. auto first = detail::begin(key);
  314. auto const last = detail::end(key);
  315. auto match = longest_match_impl(first, last);
  316. if (first == last && match.match) {
  317. return const_iterator(iter_state_t{
  318. to_node_ptr(match.node)->parent(),
  319. to_node_ptr(match.node)->index_within_parent()});
  320. }
  321. return this->end();
  322. }
  323. #ifndef BOOST_PARSER_DOXYGEN
  324. BOOST_TRIE_MAP_C_STR_OVERLOAD_QUALS(const_iterator, find, const)
  325. #endif
  326. /** Returns the iterator pointing to the first key/value pair whose
  327. key is not less than `key`. Returns end() if no such key can be
  328. found. */
  329. template<typename KeyRange>
  330. const_iterator lower_bound(KeyRange const & key) const
  331. {
  332. return bound_impl<true>(key);
  333. }
  334. #ifndef BOOST_PARSER_DOXYGEN
  335. BOOST_TRIE_MAP_C_STR_OVERLOAD_QUALS(const_iterator, lower_bound, const)
  336. #endif
  337. /** Returns the iterator pointing to the first key/value pair whose
  338. key is not greater than `key`. Returns end() if no such key can
  339. be found. */
  340. template<typename KeyRange>
  341. const_iterator upper_bound(KeyRange const & key) const
  342. {
  343. return bound_impl<false>(key);
  344. }
  345. #ifndef BOOST_PARSER_DOXYGEN
  346. BOOST_TRIE_MAP_C_STR_OVERLOAD_QUALS(const_iterator, upper_bound, const)
  347. #endif
  348. /** Returns the `const_range(lower_bound(key), upper_bound(key))`.*/
  349. template<typename KeyRange>
  350. const_range equal_range(KeyRange const & key) const
  351. {
  352. return {lower_bound(key), upper_bound(key)};
  353. }
  354. #ifndef BOOST_PARSER_DOXYGEN
  355. BOOST_TRIE_MAP_C_STR_OVERLOAD_QUALS(const_range, equal_range, const)
  356. #endif
  357. /** Returns the longest subsequence of `[first, last)` found in *this,
  358. whether or not it is a match. */
  359. template<typename KeyIter, typename Sentinel>
  360. match_result longest_subsequence(KeyIter first, Sentinel last) const
  361. {
  362. return longest_match_impl(first, last);
  363. }
  364. /** Returns the longest subsequence of `key` found in *this, whether
  365. or not it is a match. */
  366. template<typename KeyRange>
  367. match_result longest_subsequence(KeyRange const & key) const
  368. {
  369. return longest_subsequence(detail::begin(key), detail::end(key));
  370. }
  371. #ifndef BOOST_PARSER_DOXYGEN
  372. BOOST_TRIE_MAP_C_STR_OVERLOAD_QUALS(
  373. match_result, longest_subsequence, const)
  374. #endif
  375. /** Returns the longest matching subsequence of `[first, last)` found
  376. in *this. */
  377. template<typename KeyIter, typename Sentinel>
  378. match_result longest_match(KeyIter first, Sentinel last) const
  379. {
  380. auto const retval = longest_match_impl(first, last);
  381. return back_up_to_match(retval);
  382. }
  383. /** Returns the longest matching subsequence of `key` found in
  384. *this. */
  385. template<typename KeyRange>
  386. match_result longest_match(KeyRange const & key) const
  387. {
  388. return longest_match(detail::begin(key), detail::end(key));
  389. }
  390. #ifndef BOOST_PARSER_DOXYGEN
  391. BOOST_TRIE_MAP_C_STR_OVERLOAD_QUALS(match_result, longest_match, const)
  392. #endif
  393. /** Returns the result of extending `prev` by one element, `e`. */
  394. template<typename KeyElementT>
  395. match_result extend_subsequence(match_result prev, KeyElementT e) const
  396. {
  397. auto e_ptr = &e;
  398. return extend_subsequence_impl(prev, e_ptr, e_ptr + 1);
  399. }
  400. /** Returns the result of extending `prev` by the longest subsequence
  401. of `[first, last)` found in *this. */
  402. template<typename KeyIter, typename Sentinel>
  403. match_result extend_subsequence(
  404. match_result prev, KeyIter first, Sentinel last) const
  405. {
  406. return extend_subsequence_impl(prev, first, last);
  407. }
  408. /** Returns the result of extending `prev` by one element, `e`, if
  409. that would form a match, and `prev` otherwise. `prev` must be a
  410. match. */
  411. template<typename KeyElementT>
  412. match_result extend_match(match_result prev, KeyElementT e) const
  413. {
  414. BOOST_PARSER_ASSERT(prev.match);
  415. auto e_ptr = &e;
  416. auto const retval = extend_subsequence_impl(prev, e_ptr, e_ptr + 1);
  417. return back_up_to_match(retval);
  418. }
  419. /** Returns the result of extending `prev` by the longest subsequence
  420. of `[first, last)` found in *this, if that would form a match, and
  421. `prev` otherwise. `prev` must be a match. */
  422. template<typename KeyIter, typename Sentinel>
  423. match_result
  424. extend_match(match_result prev, KeyIter first, Sentinel last) const
  425. {
  426. BOOST_PARSER_ASSERT(prev.match);
  427. auto const retval = extend_subsequence_impl(prev, first, last);
  428. return back_up_to_match(retval);
  429. }
  430. /** Writes the sequence of elements that would advance `prev` by one
  431. element to `out`, and returns the final value of `out` after the
  432. writes. */
  433. template<typename OutIter>
  434. OutIter copy_next_key_elements(match_result prev, OutIter out) const
  435. {
  436. auto node = to_node_ptr(prev.node);
  437. return std::copy(node->key_begin(), node->key_end(), out);
  438. }
  439. /** Returns an optional reference to the const value associated with
  440. `key` in *this (if any). */
  441. template<typename KeyRange>
  442. optional_ref<mapped_type const> operator[](KeyRange const & key) const
  443. {
  444. auto it = find(key);
  445. if (it == end())
  446. return {};
  447. return it->value;
  448. }
  449. #ifndef BOOST_PARSER_DOXYGEN
  450. BOOST_TRIE_MAP_C_STR_OVERLOAD_QUALS(
  451. optional_ref<mapped_type const>, operator[], const)
  452. #endif
  453. /** Returns an optional reference to the const value associated with
  454. `match` in *this (if any). */
  455. optional_ref<mapped_type const> operator[](match_result match) const
  456. {
  457. if (!match.match)
  458. return {};
  459. return *to_node_ptr(match.node)->value();
  460. }
  461. iterator begin() { return iterator(const_this()->begin()); }
  462. iterator end() { return iterator(const_this()->end()); }
  463. reverse_iterator rbegin() { return reverse_iterator{end()}; }
  464. reverse_iterator rend() { return reverse_iterator{begin()}; }
  465. void clear()
  466. {
  467. header_ = node_t();
  468. size_ = 0;
  469. }
  470. /** Returns the iterator pointing to the key/value pair associated
  471. with `key`, if `key` is found in *this. Returns end()
  472. otherwise. */
  473. template<typename KeyRange>
  474. iterator find(KeyRange const & key)
  475. {
  476. return iterator(const_this()->find(key));
  477. }
  478. #ifndef BOOST_PARSER_DOXYGEN
  479. BOOST_TRIE_MAP_C_STR_OVERLOAD(iterator, find)
  480. #endif
  481. /** Returns the iterator pointing to the first key/value pair whose
  482. key is not less than `key`. Returns end() if no such key can be
  483. found. */
  484. template<typename KeyRange>
  485. iterator lower_bound(KeyRange const & key)
  486. {
  487. return iterator(const_this()->lower_bound(key));
  488. }
  489. #ifndef BOOST_PARSER_DOXYGEN
  490. BOOST_TRIE_MAP_C_STR_OVERLOAD(iterator, lower_bound)
  491. #endif
  492. /** Returns the iterator pointing to the first key/value pair whose
  493. key is not greater than `key`. Returns end() if no such key can
  494. be found. */
  495. template<typename KeyRange>
  496. iterator upper_bound(KeyRange const & key)
  497. {
  498. return iterator(const_this()->upper_bound(key));
  499. }
  500. #ifndef BOOST_PARSER_DOXYGEN
  501. BOOST_TRIE_MAP_C_STR_OVERLOAD(iterator, upper_bound)
  502. #endif
  503. /** Returns the `const_range(lower_bound(key), upper_bound(key))`.*/
  504. template<typename KeyRange>
  505. range equal_range(KeyRange const & key)
  506. {
  507. return {lower_bound(key), upper_bound(key)};
  508. }
  509. #ifndef BOOST_PARSER_DOXYGEN
  510. BOOST_TRIE_MAP_C_STR_OVERLOAD(range, equal_range)
  511. #endif
  512. /** Returns an optional reference to the value associated with `key`
  513. in *this (if any). */
  514. template<typename KeyRange>
  515. optional_ref<mapped_type> operator[](KeyRange const & key)
  516. {
  517. auto it = find(key);
  518. if (it == end())
  519. return {};
  520. return it->value;
  521. }
  522. #ifndef BOOST_PARSER_DOXYGEN
  523. BOOST_TRIE_MAP_C_STR_OVERLOAD(
  524. optional_ref<mapped_type>, operator[])
  525. #endif
  526. /** Returns an optional reference to the value associated with `match`
  527. in *this (if any). */
  528. optional_ref<mapped_type> operator[](match_result match)
  529. {
  530. if (!match.match)
  531. return {};
  532. return *const_cast<node_t *>(to_node_ptr(match.node))->value();
  533. }
  534. /** Inserts the key/value pair `[first, last)`, `value` into *this.
  535. The `inserted` field of the result will be true if the operation
  536. resulted in a new insertion, or false otherwise. */
  537. template<typename KeyIter, typename Sentinel>
  538. insert_result insert(KeyIter first, Sentinel last, Value value)
  539. {
  540. if (empty()) {
  541. std::unique_ptr<node_t> new_node(new node_t(&header_));
  542. header_.insert(std::move(new_node));
  543. }
  544. auto match = longest_match_impl(first, last);
  545. if (first == last && match.match) {
  546. return {iterator(iter_state_t{
  547. to_node_ptr(match.node)->parent(),
  548. to_node_ptr(match.node)->index_within_parent()}),
  549. false};
  550. }
  551. auto node = create_children(
  552. const_cast<node_t *>(to_node_ptr(match.node)), first, last);
  553. node->value() = std::move(value);
  554. ++size_;
  555. return {iterator(iter_state_t{node->parent(), 0}), true};
  556. }
  557. /** Inserts the key/value pair `key`, `value` into *this. The
  558. `inserted` field of the result will be true if the operation
  559. resulted in a new insertion, or false otherwise. */
  560. template<typename KeyRange>
  561. insert_result insert(KeyRange const & key, Value value)
  562. {
  563. return insert(detail::begin(key), detail::end(key), std::move(value));
  564. }
  565. template<typename Char, std::size_t N>
  566. insert_result insert(Char const (&chars)[N], Value value)
  567. {
  568. static_assert(
  569. std::is_same<Char, key_element_type>::value,
  570. "Only well-formed when Char is Key::value_type.");
  571. return insert(
  572. detail::char_range<Char const>{chars, chars + N - 1},
  573. std::move(value));
  574. }
  575. /** Inserts the key/value pair `e` into *this. The `inserted` field
  576. of the result will be true if the operation resulted in a new
  577. insertion, or false otherwise. */
  578. insert_result insert(value_type e)
  579. {
  580. return insert(
  581. detail::begin(e.key), detail::end(e.key), std::move(e.value));
  582. }
  583. /** Inserts the sequence of key/value pairs `[first, last)` into
  584. *this. The `inserted` field of the result will be true if the
  585. operation resulted in a new insertion, or false otherwise. */
  586. template<typename Iter, typename Sentinel>
  587. void insert(Iter first, Sentinel last)
  588. {
  589. for (; first != last; ++first) {
  590. insert(first->key, first->value);
  591. }
  592. }
  593. /** Inserts the sequence of key/value pairs `r` into *this. The
  594. `inserted` field of the result will be true if the operation
  595. resulted in a new insertion, or false otherwise. */
  596. template<typename Range>
  597. insert_result insert(Range const & r)
  598. {
  599. return insert(detail::begin(r), detail::end(r));
  600. }
  601. /** Inserts the sequence of key/value pairs `il` into this. The
  602. `inserted` field of the result will be true if the operation
  603. resulted in a new insertion, or false otherwise. */
  604. void insert(std::initializer_list<value_type> il)
  605. {
  606. for (auto const & x : il) {
  607. insert(x);
  608. }
  609. }
  610. /** Inserts the key/value pair `[first, last)`, `value` into *this, or
  611. assigns `value` over the existing value associated with `[first,
  612. last)`, if this key is already found in *this. The `inserted`
  613. field of the result will be true if the operation resulted in a
  614. new insertion, or false otherwise. */
  615. template<typename KeyIter, typename Sentinel>
  616. insert_result
  617. insert_or_assign(KeyIter first, Sentinel last, Value value)
  618. {
  619. auto it = first;
  620. auto match = longest_match_impl(it, last);
  621. if (it == last && match.match) {
  622. const_cast<Value &>(*to_node_ptr(match.node)->value()) =
  623. std::move(value);
  624. return insert_result{
  625. iterator(iter_state_t{
  626. to_node_ptr(match.node)->parent(),
  627. to_node_ptr(match.node)->index_within_parent()}),
  628. false};
  629. }
  630. return insert(first, last, std::move(value));
  631. }
  632. /** Inserts the key/value pair `key`, `value` into *this, or assigns
  633. `value` over the existing value associated with `key`, if `key` is
  634. already found in *this. The `inserted` field of the result will
  635. be true if the operation resulted in a new insertion, or false
  636. otherwise. */
  637. template<typename KeyRange>
  638. insert_result insert_or_assign(KeyRange const & key, Value value)
  639. {
  640. return insert_or_assign(
  641. detail::begin(key), detail::end(key), std::move(value));
  642. }
  643. template<typename Char, std::size_t N>
  644. insert_result insert_or_assign(Char const (&chars)[N], Value value)
  645. {
  646. static_assert(
  647. std::is_same<Char, key_element_type>::value,
  648. "Only well-formed when Char is Key::value_type.");
  649. return insert_or_assign(
  650. detail::char_range<Char const>{chars, chars + N - 1},
  651. std::move(value));
  652. }
  653. /** Erases the key/value pair associated with `key` from
  654. *this. Returns true if the key is found in *this, false
  655. otherwise. */
  656. template<typename KeyRange>
  657. bool erase(KeyRange const & key)
  658. {
  659. auto it = find(key);
  660. if (it == end())
  661. return false;
  662. erase(it);
  663. return true;
  664. }
  665. #ifndef BOOST_PARSER_DOXYGEN
  666. BOOST_TRIE_MAP_C_STR_OVERLOAD(bool, erase)
  667. #endif
  668. /** Erases the key/value pair pointed to by `it` from *this. Returns
  669. an iterator to the next key/value pair in this. */
  670. iterator erase(iterator it)
  671. {
  672. auto state = it.it_.state_;
  673. --size_;
  674. auto node =
  675. const_cast<node_t *>(state.parent_->child(state.index_));
  676. if (!node->empty()) {
  677. // node has a value, but also children. Remove the value and
  678. // return the next-iterator.
  679. ++it;
  680. node->value() = std::optional<Value>();
  681. return it;
  682. }
  683. // node has a value, *and* no children. Remove it and all its
  684. // singular predecessors.
  685. const_cast<node_t *>(state.parent_)->erase(state.index_);
  686. while (state.parent_->parent() && state.parent_->empty() &&
  687. !state.parent_->value()) {
  688. state = parent_state(state);
  689. const_cast<node_t *>(state.parent_)->erase(state.index_);
  690. }
  691. if (state.parent_->parent())
  692. state = parent_state(state);
  693. auto retval = iterator(state);
  694. if (!empty())
  695. ++retval;
  696. return retval;
  697. }
  698. /** Erases the sequence of key/value pairs pointed to by `[first,
  699. last)` from *this. Returns an iterator to the next key/value pair
  700. in *this. */
  701. iterator erase(iterator first, iterator last)
  702. {
  703. auto retval = last;
  704. if (first == last)
  705. return retval;
  706. --last;
  707. while (last != first) {
  708. erase(last--);
  709. }
  710. erase(last);
  711. return retval;
  712. }
  713. void swap(trie_map & other)
  714. {
  715. header_.swap(other.header_);
  716. std::swap(size_, other.size_);
  717. std::swap(comp_, other.comp_);
  718. }
  719. friend bool operator==(trie_map const & lhs, trie_map const & rhs)
  720. {
  721. return lhs.size() == rhs.size() &&
  722. std::equal(lhs.begin(), lhs.end(), rhs.begin());
  723. }
  724. friend bool operator!=(trie_map const & lhs, trie_map const & rhs)
  725. {
  726. return !(lhs == rhs);
  727. }
  728. #ifndef BOOST_PARSER_DOXYGEN
  729. private:
  730. trie_map const * const_this()
  731. {
  732. return const_cast<trie_map const *>(this);
  733. }
  734. static node_t const * to_node_ptr(void const * ptr)
  735. {
  736. return static_cast<node_t const *>(ptr);
  737. }
  738. template<std::size_t KeySize>
  739. void from_trie_impl(
  740. detail::trie_node_t<
  741. detail::no_index_within_parent_t,
  742. Key,
  743. Value,
  744. KeySize> const & node,
  745. Key & key,
  746. bool root = true)
  747. {
  748. // TODO: Use an iterative approach instead?
  749. if (!!node.value()) {
  750. insert(key, *node.value());
  751. }
  752. std::vector<key_element_type> next_elements;
  753. node.copy_next_key_elements(std::back_inserter(next_elements));
  754. for (auto const & e : next_elements) {
  755. auto const * n = node.child(e, comp_);
  756. if (!root)
  757. key.insert(key.end(), e);
  758. from_trie_impl(*n, key, false);
  759. if (!root)
  760. key.erase(std::prev(key.end()));
  761. }
  762. }
  763. template<typename KeyIter, typename Sentinel>
  764. match_result longest_match_impl(KeyIter & first, Sentinel last) const
  765. {
  766. return extend_subsequence_impl(
  767. match_result{&header_, 0, false, true}, first, last);
  768. }
  769. template<typename KeyIter, typename Sentinel>
  770. match_result extend_subsequence_impl(
  771. match_result prev, KeyIter & first, Sentinel last) const
  772. {
  773. if (to_node_ptr(prev.node) == &header_) {
  774. if (header_.empty())
  775. return prev;
  776. prev.node = header_.child(0);
  777. }
  778. if (first == last) {
  779. prev.match = !!to_node_ptr(prev.node)->value();
  780. prev.leaf = to_node_ptr(prev.node)->empty();
  781. return prev;
  782. }
  783. node_t const * node = to_node_ptr(prev.node);
  784. size_type size = prev.size;
  785. while (first != last) {
  786. auto const it = node->find(*first, comp_);
  787. if (it == node->end())
  788. break;
  789. ++first;
  790. ++size;
  791. node = it->get();
  792. }
  793. return match_result{node, size, !!node->value(), node->empty()};
  794. }
  795. static match_result back_up_to_match(match_result retval)
  796. {
  797. auto node = to_node_ptr(retval.node);
  798. while (node->parent() && !node->value()) {
  799. retval.node = node = node->parent();
  800. --retval.size;
  801. }
  802. if (!!node->value())
  803. retval.match = true;
  804. return retval;
  805. }
  806. template<typename KeyIter, typename Sentinel>
  807. node_t * create_children(node_t * node, KeyIter first, Sentinel last)
  808. {
  809. auto retval = node;
  810. for (; first != last; ++first) {
  811. std::unique_ptr<node_t> new_node(new node_t(retval));
  812. retval =
  813. retval->insert(*first, comp_, std::move(new_node))->get();
  814. }
  815. return retval;
  816. }
  817. template<bool LowerBound, typename KeyRange>
  818. const_iterator bound_impl(KeyRange const & key) const
  819. {
  820. auto first = detail::begin(key);
  821. auto const last = detail::end(key);
  822. auto match = longest_match_impl(first, last);
  823. if (first == last && match.match) {
  824. auto retval = const_iterator(iter_state_t{
  825. to_node_ptr(match.node)->parent(),
  826. to_node_ptr(match.node)->index_within_parent()});
  827. if (!LowerBound)
  828. ++retval;
  829. return retval;
  830. }
  831. auto node = to_node_ptr(match.node);
  832. if (node->before_child_subtree(*first)) {
  833. // If the next element of the key would be before this node's
  834. // children, use the successor of this node; let
  835. // const_iterator::operator++() figure out for us which node
  836. // that is.
  837. return ++const_iterator(
  838. iter_state_t{node->parent(), node->index_within_parent()});
  839. }
  840. auto const it = node->lower_bound(*first, comp_);
  841. if (it == node->end()) {
  842. // Find the max value in this subtree, and go one past that.
  843. do {
  844. node = to_node_ptr(node->max_child());
  845. } while (!node->value());
  846. return ++const_iterator(
  847. iter_state_t{node->parent(), node->parent()->size() - 1});
  848. }
  849. // Otherwise, find the min value within the child found above.
  850. std::size_t parent_index = it - node->begin();
  851. node = to_node_ptr(it->get());
  852. while (!node->value()) {
  853. node = to_node_ptr(node->min_child());
  854. parent_index = 0;
  855. }
  856. return const_iterator(iter_state_t{node->parent(), parent_index});
  857. }
  858. node_t header_;
  859. size_type size_;
  860. key_compare comp_;
  861. #endif
  862. };
  863. template<typename Key, typename Compare>
  864. struct trie_set;
  865. template<typename Key>
  866. struct const_trie_set_iterator;
  867. template<typename Key, typename Value>
  868. struct const_trie_map_iterator : stl_interfaces::iterator_interface<
  869. const_trie_map_iterator<Key, Value>,
  870. std::bidirectional_iterator_tag,
  871. trie_element<Key, Value>,
  872. trie_element<Key, Value const &>>
  873. {
  874. private:
  875. using state_t = detail::trie_iterator_state_t<Key, Value>;
  876. state_t state_;
  877. using ref_type = trie_element<Key, Value const &>;
  878. using ptr_type = stl_interfaces::proxy_arrow_result<ref_type>;
  879. public:
  880. const_trie_map_iterator() : state_{nullptr, 0} {}
  881. const_trie_map_iterator(trie_match_result match_result)
  882. {
  883. BOOST_PARSER_ASSERT(match_result.node);
  884. BOOST_PARSER_ASSERT(match_result.match);
  885. auto node = static_cast<detail::trie_node_t<
  886. detail::index_within_parent_t,
  887. Key,
  888. Value> const *>(match_result.node);
  889. state_.parent_ = node->parent();
  890. state_.index_ = node->index_within_parent();
  891. }
  892. ref_type operator*() const
  893. {
  894. return ref_type{detail::reconstruct_key(state_),
  895. state_.parent_->child_value(state_.index_)};
  896. }
  897. ptr_type operator->() const
  898. {
  899. ref_type && deref_result = **this;
  900. return ptr_type(
  901. ref_type{std::move(deref_result.key), deref_result.value});
  902. }
  903. const_trie_map_iterator & operator++()
  904. {
  905. auto node = to_node(state_);
  906. if (node && !node->empty()) {
  907. state_.parent_ = node;
  908. state_.index_ = 0;
  909. } else {
  910. // Try the next sibling node.
  911. ++state_.index_;
  912. auto const first_state = state_;
  913. while (state_.parent_->parent() &&
  914. state_.parent_->parent()->parent() &&
  915. state_.parent_->size() <= state_.index_) {
  916. state_ = parent_state(state_);
  917. ++state_.index_;
  918. }
  919. // If we went all the way up, incrementing indices, and they
  920. // were all at size() for each node, the first increment above
  921. // must have taken us to the end; use that.
  922. if ((!state_.parent_->parent() ||
  923. !state_.parent_->parent()->parent()) &&
  924. state_.parent_->size() <= state_.index_) {
  925. state_ = first_state;
  926. return *this;
  927. }
  928. }
  929. node = state_.parent_->child(state_.index_);
  930. while (!node->value()) {
  931. auto i = 0u;
  932. node = node->child(i);
  933. state_ = state_t{node->parent(), i};
  934. }
  935. return *this;
  936. }
  937. const_trie_map_iterator & operator--()
  938. {
  939. // Decrement-from-end case.
  940. if (state_.index_ == state_.parent_->size()) {
  941. --state_.index_;
  942. return *this;
  943. }
  944. // Back up one node at a time until we find an ancestor with a
  945. // value or a previous sibling.
  946. while (state_.parent_->parent() && state_.index_ == 0) {
  947. state_ = parent_state(state_);
  948. if (state_.parent_->child(state_.index_)->value())
  949. return *this;
  950. }
  951. // If we get found no value above, go down the maximum subtree of
  952. // the previous sibling.
  953. if (state_.index_)
  954. --state_.index_;
  955. auto node = state_.parent_->child(state_.index_);
  956. while (!node->empty()) {
  957. auto i = node->size() - 1;
  958. node = node->child(i);
  959. state_ = state_t{node->parent(), i};
  960. }
  961. return *this;
  962. }
  963. friend bool operator==(
  964. const_trie_map_iterator lhs, const_trie_map_iterator rhs)
  965. {
  966. return lhs.state_.parent_ == rhs.state_.parent_ &&
  967. lhs.state_.index_ == rhs.state_.index_;
  968. }
  969. using base_type = stl_interfaces::iterator_interface<
  970. const_trie_map_iterator<Key, Value>,
  971. std::bidirectional_iterator_tag,
  972. trie_element<Key, Value>,
  973. trie_element<Key, Value const &>>;
  974. using base_type::operator++;
  975. using base_type::operator--;
  976. #ifndef BOOST_PARSER_DOXYGEN
  977. private:
  978. explicit const_trie_map_iterator(state_t state) : state_(state) {}
  979. template<typename KeyT, typename ValueT, typename Compare>
  980. friend struct trie_map;
  981. template<typename KeyT, typename Compare>
  982. friend struct trie_set;
  983. template<typename KeyT, typename ValueT>
  984. friend struct trie_map_iterator;
  985. template<typename KeyT>
  986. friend struct const_trie_set_iterator;
  987. #endif
  988. };
  989. template<typename Key, typename Value>
  990. struct trie_map_iterator : stl_interfaces::iterator_interface<
  991. trie_map_iterator<Key, Value>,
  992. std::bidirectional_iterator_tag,
  993. trie_element<Key, Value>,
  994. trie_element<Key, Value &>>
  995. {
  996. private:
  997. const_trie_map_iterator<Key, Value> it_;
  998. using ref_type = trie_element<Key, Value &>;
  999. using ptr_type = stl_interfaces::proxy_arrow_result<ref_type>;
  1000. public:
  1001. trie_map_iterator() {}
  1002. trie_map_iterator(trie_match_result match_result) :
  1003. trie_map_iterator(const_trie_map_iterator<Key, Value>(match_result))
  1004. {}
  1005. ref_type operator*() const
  1006. {
  1007. return ref_type{detail::reconstruct_key(it_.state_),
  1008. it_.state_.parent_->child_value(it_.state_.index_)};
  1009. };
  1010. ptr_type operator->() const
  1011. {
  1012. ref_type && deref_result = **this;
  1013. return ptr_type(
  1014. ref_type{std::move(deref_result.key), deref_result.value});
  1015. }
  1016. trie_map_iterator & operator++()
  1017. {
  1018. ++it_;
  1019. return *this;
  1020. }
  1021. trie_map_iterator & operator--()
  1022. {
  1023. --it_;
  1024. return *this;
  1025. }
  1026. friend bool
  1027. operator==(trie_map_iterator lhs, trie_map_iterator rhs)
  1028. {
  1029. return lhs.it_ == rhs.it_;
  1030. }
  1031. using base_type = stl_interfaces::iterator_interface<
  1032. trie_map_iterator<Key, Value>,
  1033. std::bidirectional_iterator_tag,
  1034. trie_element<Key, Value>,
  1035. trie_element<Key, Value &>>;
  1036. using base_type::operator++;
  1037. using base_type::operator--;
  1038. #ifndef BOOST_PARSER_DOXYGEN
  1039. private:
  1040. explicit trie_map_iterator(
  1041. detail::trie_iterator_state_t<Key, Value> state) :
  1042. it_(state)
  1043. {}
  1044. explicit trie_map_iterator(const_trie_map_iterator<Key, Value> it) :
  1045. it_(it)
  1046. {}
  1047. template<typename KeyT, typename ValueT, typename Compare>
  1048. friend struct trie_map;
  1049. template<typename KeyT, typename Compare>
  1050. friend struct trie_set;
  1051. #endif
  1052. };
  1053. }}
  1054. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  1055. namespace std::ranges {
  1056. template<typename Iter>
  1057. inline constexpr bool
  1058. enable_borrowed_range<boost::parser::detail::text::trie_range<Iter>> =
  1059. true;
  1060. template<typename Iter>
  1061. inline constexpr bool enable_borrowed_range<
  1062. boost::parser::detail::text::const_trie_range<Iter>> = true;
  1063. }
  1064. #endif
  1065. #endif