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/detail/iterator.hpp>
12 #include <boost/assert.hpp>
14 namespace boost { namespace spirit { namespace x3 { namespace detail
16 // This file contains low level TST routines, not for
17 // public consumption.
19 template <typename Char, typename T>
23 : id(id), data(0), lt(0), eq(0), gt(0)
27 template <typename Alloc>
29 destruct_node(tst_node* p, Alloc* alloc)
34 alloc->delete_data(p->data);
35 destruct_node(p->lt, alloc);
36 destruct_node(p->eq, alloc);
37 destruct_node(p->gt, alloc);
38 alloc->delete_node(p);
42 template <typename Alloc>
44 clone_node(tst_node* p, Alloc* alloc)
48 tst_node* clone = alloc->new_node(p->id);
50 clone->data = alloc->new_data(*p->data);
51 clone->lt = clone_node(p->lt, alloc);
52 clone->eq = clone_node(p->eq, alloc);
53 clone->gt = clone_node(p->gt, alloc);
59 template <typename Iterator, typename CaseCompare>
61 find(tst_node* start, Iterator& first, Iterator last, CaseCompare comp)
67 Iterator latest = first;
71 while (p && i != last)
73 int32_t c = comp(*i,p->id);
95 first = ++latest; // one past the last matching char
99 template <typename Iterator, typename Alloc>
105 , typename boost::call_traits<T>::param_type val
111 tst_node** pp = &start;
115 boost::detail::iterator_traits<Iterator>::value_type
119 *pp = alloc->new_node(c);
127 p->data = alloc->new_data(val);
143 template <typename Iterator, typename Alloc>
145 remove(tst_node*& p, Iterator first, Iterator last, Alloc* alloc)
147 if (p == 0 || first == last)
151 boost::detail::iterator_traits<Iterator>::value_type
160 alloc->delete_data(p->data);
164 remove(p->eq, first, last, alloc);
168 remove(p->lt, first, last, alloc);
172 remove(p->gt, first, last, alloc);
175 if (p->data == 0 && p->lt == 0 && p->eq == 0 && p->gt == 0)
177 alloc->delete_node(p);
182 template <typename F>
184 for_each(tst_node* p, std::basic_string<Char> prefix, F f)
188 for_each(p->lt, prefix, f);
189 std::basic_string<Char> s = prefix + p->id;
190 for_each(p->eq, s, f);
193 for_each(p->gt, prefix, f);
197 Char id; // the node's identity character
198 T* data; // optional data
199 tst_node* lt; // left pointer
200 tst_node* eq; // middle pointer
201 tst_node* gt; // right pointer