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_MARCH_09_2007_0905AM)
8 #define BOOST_SPIRIT_X3_TST_MARCH_09_2007_0905AM
10 #include <boost/call_traits.hpp>
11 #include <boost/assert.hpp>
13 namespace boost { namespace spirit { namespace x3 { namespace detail
15 // This file contains low level TST routines, not for
16 // public consumption.
18 template <typename Char, typename T>
22 : id(id), data(0), lt(0), eq(0), gt(0)
26 template <typename Alloc>
28 destruct_node(tst_node* p, Alloc* alloc)
33 alloc->delete_data(p->data);
34 destruct_node(p->lt, alloc);
35 destruct_node(p->eq, alloc);
36 destruct_node(p->gt, alloc);
37 alloc->delete_node(p);
41 template <typename Alloc>
43 clone_node(tst_node* p, Alloc* alloc)
47 tst_node* clone = alloc->new_node(p->id);
49 clone->data = alloc->new_data(*p->data);
50 clone->lt = clone_node(p->lt, alloc);
51 clone->eq = clone_node(p->eq, alloc);
52 clone->gt = clone_node(p->gt, alloc);
58 template <typename Iterator, typename CaseCompare>
60 find(tst_node* start, Iterator& first, Iterator last, CaseCompare comp)
66 Iterator latest = first;
70 while (p && i != last)
72 int32_t c = comp(*i,p->id);
94 first = ++latest; // one past the last matching char
98 template <typename Iterator, typename Alloc>
104 , typename boost::call_traits<T>::param_type val
110 tst_node** pp = &start;
116 *pp = alloc->new_node(c);
124 p->data = alloc->new_data(val);
140 template <typename Iterator, typename Alloc>
142 remove(tst_node*& p, Iterator first, Iterator last, Alloc* alloc)
144 if (p == 0 || first == last)
155 alloc->delete_data(p->data);
159 remove(p->eq, first, last, alloc);
163 remove(p->lt, first, last, alloc);
167 remove(p->gt, first, last, alloc);
170 if (p->data == 0 && p->lt == 0 && p->eq == 0 && p->gt == 0)
172 alloc->delete_node(p);
177 template <typename F>
179 for_each(tst_node* p, std::basic_string<Char> prefix, F f)
183 for_each(p->lt, prefix, f);
184 std::basic_string<Char> s = prefix + p->id;
185 for_each(p->eq, s, f);
188 for_each(p->gt, prefix, f);
192 Char id; // the node's identity character
193 T* data; // optional data
194 tst_node* lt; // left pointer
195 tst_node* eq; // middle pointer
196 tst_node* gt; // right pointer