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
10 #include <boost/spirit/home/x3/string/detail/tst.hpp>
11 #include <unordered_map>
12 #include <boost/pool/object_pool.hpp>
14 namespace boost { namespace spirit { namespace x3
16 struct tst_pass_through; // declared in tst.hpp
18 template <typename Char, typename T>
21 typedef Char char_type; // the character type
22 typedef T value_type; // the value associated with each entry
23 typedef detail::tst_node<Char, T> node;
31 // Nothing to do here.
32 // The pools do the right thing for us
35 tst_map(tst_map const& rhs)
40 tst_map& operator=(tst_map const& rhs)
45 template <typename Iterator, typename Filter>
46 T* find(Iterator& first, Iterator last, Filter filter) const
50 Iterator save = first;
51 typename map_type::const_iterator
52 i = map.find(filter(*first++));
58 if (T* p = node::find(i->second.root, first, last, filter))
62 return i->second.data;
67 template <typename Iterator>
68 T* find(Iterator& first, Iterator last) const
70 return find(first, last, tst_pass_through());
73 template <typename Iterator>
77 , typename boost::call_traits<T>::param_type val)
82 std::pair<typename map_type::iterator, bool>
83 r = map.insert(std::pair<Char, map_data>(*first++, x));
87 return node::add(r.first->second.root
88 , first, last, val, this) ? true : false;
92 if (r.first->second.data)
94 r.first->second.data = this->new_data(val);
101 template <typename Iterator>
102 void remove(Iterator first, Iterator last)
106 typename map_type::iterator i = map.find(*first++);
111 node::remove(i->second.root, first, last, this);
113 else if (i->second.data)
115 this->delete_data(i->second.data);
118 if (i->second.data == 0 && i->second.root == 0)
128 for (typename map_type::value_type& x : map)
130 node::destruct_node(x.second.root, this);
132 this->delete_data(x.second.data);
137 template <typename F>
138 void for_each(F f) const
140 for (typename map_type::value_type const& x : map)
142 std::basic_string<Char> s(1, x.first);
143 node::for_each(x.second.root, s, f);
145 f(s, *x.second.data);
151 friend struct detail::tst_node<Char, T>;
159 typedef std::unordered_map<Char, map_data> map_type;
161 void copy(tst_map const& rhs)
163 for (typename map_type::value_type const& x : rhs.map)
165 map_data xx = {node::clone_node(x.second.root, this), 0};
167 xx.data = data_pool.construct(*x.second.data);
172 tst_map& assign(tst_map const& rhs)
176 for (typename map_type::value_type& x : map)
178 node::destruct_node(x.second.root, this);
186 node* new_node(Char id)
188 return node_pool.construct(id);
191 T* new_data(typename boost::call_traits<T>::param_type val)
193 return data_pool.construct(val);
196 void delete_node(node* p)
198 node_pool.destroy(p);
201 void delete_data(T* p)
203 data_pool.destroy(p);
207 object_pool<node> node_pool;
208 object_pool<T> data_pool;