1 /*=============================================================================
2 Copyright (c) 2001-2011 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_TST_MAP_JUNE_03_2007_1143AM)
8 #define BOOST_SPIRIT_TST_MAP_JUNE_03_2007_1143AM
14 #include <boost/spirit/home/qi/string/tst.hpp>
15 #include <boost/spirit/home/qi/string/detail/tst.hpp>
16 #include <boost/unordered_map.hpp>
17 #include <boost/pool/object_pool.hpp>
19 namespace boost { namespace spirit { namespace qi
21 template <typename Char, typename T>
24 typedef Char char_type; // the character type
25 typedef T value_type; // the value associated with each entry
26 typedef detail::tst_node<Char, T> node;
34 // Nothing to do here.
35 // The pools do the right thing for us
38 tst_map(tst_map const& rhs)
43 tst_map& operator=(tst_map const& rhs)
48 template <typename Iterator, typename Filter>
49 T* find(Iterator& first, Iterator last, Filter filter) const
53 Iterator save = first;
54 typename map_type::const_iterator
55 i = map.find(filter(*first++));
59 if (T* p = node::find(i->second.root, first, last, filter))
66 return i->second.data;
74 template <typename Iterator>
75 T* find(Iterator& first, Iterator last) const
77 return find(first, last, tst_pass_through());
80 template <typename Iterator>
84 , typename boost::call_traits<T>::param_type val)
89 std::pair<typename map_type::iterator, bool>
90 r = map.insert(std::pair<Char, map_data>(*first++, x));
94 return node::add(r.first->second.root
95 , first, last, val, this) ? true : false;
99 if (r.first->second.data)
101 r.first->second.data = this->new_data(val);
108 template <typename Iterator>
109 void remove(Iterator first, Iterator last)
113 typename map_type::iterator i = map.find(*first++);
118 node::remove(i->second.root, first, last, this);
120 else if (i->second.data)
122 this->delete_data(i->second.data);
125 if (i->second.data == 0 && i->second.root == 0)
135 BOOST_FOREACH(typename map_type::value_type& x, map)
137 node::destruct_node(x.second.root, this);
139 this->delete_data(x.second.data);
144 template <typename F>
145 void for_each(F f) const
147 BOOST_FOREACH(typename map_type::value_type const& x, map)
149 std::basic_string<Char> s(1, x.first);
150 node::for_each(x.second.root, s, f);
152 f(s, *x.second.data);
158 friend struct detail::tst_node<Char, T>;
166 typedef unordered_map<Char, map_data> map_type;
168 void copy(tst_map const& rhs)
170 BOOST_FOREACH(typename map_type::value_type const& x, rhs.map)
172 map_data xx = {node::clone_node(x.second.root, this), 0};
174 xx.data = data_pool.construct(*x.second.data);
179 tst_map& assign(tst_map const& rhs)
183 BOOST_FOREACH(typename map_type::value_type& x, map)
185 node::destruct_node(x.second.root, this);
193 node* new_node(Char id)
195 return node_pool.construct(id);
198 T* new_data(typename boost::call_traits<T>::param_type val)
200 return data_pool.construct(val);
203 void delete_node(node* p)
205 node_pool.destroy(p);
208 void delete_data(T* p)
210 data_pool.destroy(p);
214 object_pool<node> node_pool;
215 object_pool<T> data_pool;