]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/spirit/home/qi/string/tst_map.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / spirit / home / qi / string / tst_map.hpp
1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3
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
9
10 #if defined(_MSC_VER)
11 #pragma once
12 #endif
13
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>
18
19 namespace boost { namespace spirit { namespace qi
20 {
21 template <typename Char, typename T>
22 struct tst_map
23 {
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;
27
28 tst_map()
29 {
30 }
31
32 ~tst_map()
33 {
34 // Nothing to do here.
35 // The pools do the right thing for us
36 }
37
38 tst_map(tst_map const& rhs)
39 {
40 copy(rhs);
41 }
42
43 tst_map& operator=(tst_map const& rhs)
44 {
45 return assign(rhs);
46 }
47
48 template <typename Iterator, typename Filter>
49 T* find(Iterator& first, Iterator last, Filter filter) const
50 {
51 if (first != last)
52 {
53 Iterator save = first;
54 typename map_type::const_iterator
55 i = map.find(filter(*first++));
56
57 if (i != map.end())
58 {
59 if (T* p = node::find(i->second.root, first, last, filter))
60 {
61 return p;
62 }
63
64 if (i->second.data)
65 {
66 return i->second.data;
67 }
68 }
69 first = save;
70 }
71 return 0;
72 }
73
74 template <typename Iterator>
75 T* find(Iterator& first, Iterator last) const
76 {
77 return find(first, last, tst_pass_through());
78 }
79
80 template <typename Iterator>
81 bool add(
82 Iterator first
83 , Iterator last
84 , typename boost::call_traits<T>::param_type val)
85 {
86 if (first != last)
87 {
88 map_data x = {0, 0};
89 std::pair<typename map_type::iterator, bool>
90 r = map.insert(std::pair<Char, map_data>(*first++, x));
91
92 if (first != last)
93 {
94 return node::add(r.first->second.root
95 , first, last, val, this) ? true : false;
96 }
97 else
98 {
99 if (r.first->second.data)
100 return false;
101 r.first->second.data = this->new_data(val);
102 }
103 return true;
104 }
105 return false;
106 }
107
108 template <typename Iterator>
109 void remove(Iterator first, Iterator last)
110 {
111 if (first != last)
112 {
113 typename map_type::iterator i = map.find(*first++);
114 if (i != map.end())
115 {
116 if (first != last)
117 {
118 node::remove(i->second.root, first, last, this);
119 }
120 else if (i->second.data)
121 {
122 this->delete_data(i->second.data);
123 i->second.data = 0;
124 }
125 if (i->second.data == 0 && i->second.root == 0)
126 {
127 map.erase(i);
128 }
129 }
130 }
131 }
132
133 void clear()
134 {
135 typedef typename map_type::iterator iter_t;
136 for (iter_t it = map.begin(), end = map.end(); it != end; ++it)
137 {
138 node::destruct_node(it->second.root, this);
139 if (it->second.data)
140 this->delete_data(it->second.data);
141 }
142 map.clear();
143 }
144
145 template <typename F>
146 void for_each(F f) const
147 {
148 typedef typename map_type::const_iterator iter_t;
149 for (iter_t it = map.begin(), end = map.end(); it != end; ++it)
150 {
151 std::basic_string<Char> s(1, it->first);
152 node::for_each(it->second.root, s, f);
153 if (it->second.data)
154 f(s, *it->second.data);
155 }
156 }
157
158 private:
159
160 friend struct detail::tst_node<Char, T>;
161
162 struct map_data
163 {
164 node* root;
165 T* data;
166 };
167
168 typedef unordered_map<Char, map_data> map_type;
169
170 void copy(tst_map const& rhs)
171 {
172 typedef typename map_type::const_iterator iter_t;
173 for (iter_t it = rhs.map.begin(), end = rhs.map.end(); it != end; ++it)
174 {
175 map_data xx = {node::clone_node(it->second.root, this), 0};
176 if (it->second.data)
177 xx.data = data_pool.construct(*it->second.data);
178 map[it->first] = xx;
179 }
180 }
181
182 tst_map& assign(tst_map const& rhs)
183 {
184 if (this != &rhs)
185 {
186 typedef typename map_type::const_iterator iter_t;
187 for (iter_t it = map.begin(), end = map.end(); it != end; ++it)
188 {
189 node::destruct_node(it->second.root, this);
190 }
191 map.clear();
192 copy(rhs);
193 }
194 return *this;
195 }
196
197 node* new_node(Char id)
198 {
199 return node_pool.construct(id);
200 }
201
202 T* new_data(typename boost::call_traits<T>::param_type val)
203 {
204 return data_pool.construct(val);
205 }
206
207 void delete_node(node* p)
208 {
209 node_pool.destroy(p);
210 }
211
212 void delete_data(T* p)
213 {
214 data_pool.destroy(p);
215 }
216
217 map_type map;
218 object_pool<node> node_pool;
219 object_pool<T> data_pool;
220 };
221 }}}
222
223 #endif