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>
15 namespace boost { namespace spirit { namespace x3 { namespace detail
17 // This file contains low level TST routines, not for
18 // public consumption.
20 template <typename Char, typename T>
24 : id(id), data(0), lt(0), eq(0), gt(0)
28 template <typename Alloc>
30 destruct_node(tst_node* p, Alloc* alloc)
35 alloc->delete_data(p->data);
36 destruct_node(p->lt, alloc);
37 destruct_node(p->eq, alloc);
38 destruct_node(p->gt, alloc);
39 alloc->delete_node(p);
43 template <typename Alloc>
45 clone_node(tst_node* p, Alloc* alloc)
49 tst_node* clone = alloc->new_node(p->id);
51 clone->data = alloc->new_data(*p->data);
52 clone->lt = clone_node(p->lt, alloc);
53 clone->eq = clone_node(p->eq, alloc);
54 clone->gt = clone_node(p->gt, alloc);
60 template <typename Iterator, typename CaseCompare>
62 find(tst_node* start, Iterator& first, Iterator last, CaseCompare comp)
68 Iterator latest = first;
72 while (p && i != last)
74 int32_t c = comp(*i,p->id);
96 first = ++latest; // one past the last matching char
100 template <typename Iterator, typename Alloc>
106 , typename boost::call_traits<T>::param_type val
112 tst_node** pp = &start;
118 *pp = alloc->new_node(c);
126 p->data = alloc->new_data(val);
142 template <typename Iterator, typename Alloc>
144 remove(tst_node*& p, Iterator first, Iterator last, Alloc* alloc)
146 if (p == 0 || first == last)
157 alloc->delete_data(p->data);
161 remove(p->eq, first, last, alloc);
165 remove(p->lt, first, last, alloc);
169 remove(p->gt, first, last, alloc);
172 if (p->data == 0 && p->lt == 0 && p->eq == 0 && p->gt == 0)
174 alloc->delete_node(p);
179 template <typename F>
181 for_each(tst_node* p, std::basic_string<Char> prefix, F f)
185 for_each(p->lt, prefix, f);
186 std::basic_string<Char> s = prefix + p->id;
187 for_each(p->eq, s, f);
190 for_each(p->gt, prefix, f);
194 Char id; // the node's identity character
195 T* data; // optional data
196 tst_node* lt; // left pointer
197 tst_node* eq; // middle pointer
198 tst_node* gt; // right pointer