1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 #if !defined(BOOST_SPIRIT_X3_TST_MAP_JUNE_03_2007_1143AM)
8 #define BOOST_SPIRIT_X3_TST_MAP_JUNE_03_2007_1143AM
14 #include <boost/spirit/home/x3/string/detail/tst.hpp>
15 #include <unordered_map>
16 #include <boost/pool/object_pool.hpp>
18 namespace boost { namespace spirit { namespace x3
20 struct tst_pass_through; // declared in tst.hpp
22 template <typename Char, typename T>
25 typedef Char char_type; // the character type
26 typedef T value_type; // the value associated with each entry
27 typedef detail::tst_node<Char, T> node;
35 // Nothing to do here.
36 // The pools do the right thing for us
39 tst_map(tst_map const& rhs)
44 tst_map& operator=(tst_map const& rhs)
49 template <typename Iterator, typename Filter>
50 T* find(Iterator& first, Iterator last, Filter filter) const
54 Iterator save = first;
55 typename map_type::const_iterator
56 i = map.find(filter(*first++));
62 if (T* p = node::find(i->second.root, first, last, filter))
66 return i->second.data;
71 template <typename Iterator>
72 T* find(Iterator& first, Iterator last) const
74 return find(first, last, tst_pass_through());
77 template <typename Iterator>
81 , typename boost::call_traits<T>::param_type val)
86 std::pair<typename map_type::iterator, bool>
87 r = map.insert(std::pair<Char, map_data>(*first++, x));
91 return node::add(r.first->second.root
92 , first, last, val, this) ? true : false;
96 if (r.first->second.data)
98 r.first->second.data = this->new_data(val);
105 template <typename Iterator>
106 void remove(Iterator first, Iterator last)
110 typename map_type::iterator i = map.find(*first++);
115 node::remove(i->second.root, first, last, this);
117 else if (i->second.data)
119 this->delete_data(i->second.data);
122 if (i->second.data == 0 && i->second.root == 0)
132 BOOST_FOREACH(typename map_type::value_type& x, map)
134 node::destruct_node(x.second.root, this);
136 this->delete_data(x.second.data);
141 template <typename F>
142 void for_each(F f) const
144 BOOST_FOREACH(typename map_type::value_type const& x, map)
146 std::basic_string<Char> s(1, x.first);
147 node::for_each(x.second.root, s, f);
149 f(s, *x.second.data);
155 friend struct detail::tst_node<Char, T>;
163 typedef std::unordered_map<Char, map_data> map_type;
165 void copy(tst_map const& rhs)
167 BOOST_FOREACH(typename map_type::value_type const& x, rhs.map)
169 map_data xx = {node::clone_node(x.second.root, this), 0};
171 xx.data = data_pool.construct(*x.second.data);
176 tst_map& assign(tst_map const& rhs)
180 BOOST_FOREACH(typename map_type::value_type& x, map)
182 node::destruct_node(x.second.root, this);
190 node* new_node(Char id)
192 return node_pool.construct(id);
195 T* new_data(typename boost::call_traits<T>::param_type val)
197 return data_pool.construct(val);
200 void delete_node(node* p)
202 node_pool.destroy(p);
205 void delete_data(T* p)
207 data_pool.destroy(p);
211 object_pool<node> node_pool;
212 object_pool<T> data_pool;