tst_map.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*=============================================================================
  2. Copyright (c) 2001-2014 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #if !defined(BOOST_SPIRIT_X3_TST_MAP_JUNE_03_2007_1143AM)
  7. #define BOOST_SPIRIT_X3_TST_MAP_JUNE_03_2007_1143AM
  8. #include <boost/spirit/home/x3/string/detail/tst.hpp>
  9. #include <unordered_map>
  10. #include <boost/pool/object_pool.hpp>
  11. namespace boost { namespace spirit { namespace x3
  12. {
  13. struct tst_pass_through; // declared in tst.hpp
  14. template <typename Char, typename T>
  15. struct tst_map
  16. {
  17. typedef Char char_type; // the character type
  18. typedef T value_type; // the value associated with each entry
  19. typedef detail::tst_node<Char, T> node;
  20. tst_map()
  21. {
  22. }
  23. ~tst_map()
  24. {
  25. // Nothing to do here.
  26. // The pools do the right thing for us
  27. }
  28. tst_map(tst_map const& rhs)
  29. {
  30. copy(rhs);
  31. }
  32. tst_map& operator=(tst_map const& rhs)
  33. {
  34. return assign(rhs);
  35. }
  36. template <typename Iterator, typename Filter>
  37. T* find(Iterator& first, Iterator last, Filter filter) const
  38. {
  39. if (first != last)
  40. {
  41. Iterator save = first;
  42. typename map_type::const_iterator
  43. i = map.find(filter(*first++));
  44. if (i == map.end())
  45. {
  46. first = save;
  47. return 0;
  48. }
  49. if (T* p = node::find(i->second.root, first, last, filter))
  50. {
  51. return p;
  52. }
  53. return i->second.data;
  54. }
  55. return 0;
  56. }
  57. template <typename Iterator>
  58. T* find(Iterator& first, Iterator last) const
  59. {
  60. return find(first, last, tst_pass_through());
  61. }
  62. template <typename Iterator>
  63. bool add(
  64. Iterator first
  65. , Iterator last
  66. , typename boost::call_traits<T>::param_type val)
  67. {
  68. if (first != last)
  69. {
  70. map_data x = {0, 0};
  71. std::pair<typename map_type::iterator, bool>
  72. r = map.insert(std::pair<Char, map_data>(*first++, x));
  73. if (first != last)
  74. {
  75. return node::add(r.first->second.root
  76. , first, last, val, this) ? true : false;
  77. }
  78. else
  79. {
  80. if (r.first->second.data)
  81. return false;
  82. r.first->second.data = this->new_data(val);
  83. }
  84. return true;
  85. }
  86. return false;
  87. }
  88. template <typename Iterator>
  89. void remove(Iterator first, Iterator last)
  90. {
  91. if (first != last)
  92. {
  93. typename map_type::iterator i = map.find(*first++);
  94. if (i != map.end())
  95. {
  96. if (first != last)
  97. {
  98. node::remove(i->second.root, first, last, this);
  99. }
  100. else if (i->second.data)
  101. {
  102. this->delete_data(i->second.data);
  103. i->second.data = 0;
  104. }
  105. if (i->second.data == 0 && i->second.root == 0)
  106. {
  107. map.erase(i);
  108. }
  109. }
  110. }
  111. }
  112. void clear()
  113. {
  114. for (typename map_type::value_type& x : map)
  115. {
  116. node::destruct_node(x.second.root, this);
  117. if (x.second.data)
  118. this->delete_data(x.second.data);
  119. }
  120. map.clear();
  121. }
  122. template <typename F>
  123. void for_each(F f) const
  124. {
  125. for (typename map_type::value_type const& x : map)
  126. {
  127. std::basic_string<Char> s(1, x.first);
  128. node::for_each(x.second.root, s, f);
  129. if (x.second.data)
  130. f(s, *x.second.data);
  131. }
  132. }
  133. private:
  134. friend struct detail::tst_node<Char, T>;
  135. struct map_data
  136. {
  137. node* root;
  138. T* data;
  139. };
  140. typedef std::unordered_map<Char, map_data> map_type;
  141. void copy(tst_map const& rhs)
  142. {
  143. for (typename map_type::value_type const& x : rhs.map)
  144. {
  145. map_data xx = {node::clone_node(x.second.root, this), 0};
  146. if (x.second.data)
  147. xx.data = data_pool.construct(*x.second.data);
  148. map[x.first] = xx;
  149. }
  150. }
  151. tst_map& assign(tst_map const& rhs)
  152. {
  153. if (this != &rhs)
  154. {
  155. for (typename map_type::value_type& x : map)
  156. {
  157. node::destruct_node(x.second.root, this);
  158. }
  159. map.clear();
  160. copy(rhs);
  161. }
  162. return *this;
  163. }
  164. node* new_node(Char id)
  165. {
  166. return node_pool.construct(id);
  167. }
  168. T* new_data(typename boost::call_traits<T>::param_type val)
  169. {
  170. return data_pool.construct(val);
  171. }
  172. void delete_node(node* p)
  173. {
  174. node_pool.destroy(p);
  175. }
  176. void delete_data(T* p)
  177. {
  178. data_pool.destroy(p);
  179. }
  180. map_type map;
  181. object_pool<node> node_pool;
  182. object_pool<T> data_pool;
  183. };
  184. }}}
  185. #endif