1 /*=============================================================================
2 Copyright (c) 2001-2011 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_TST_MARCH_09_2007_0905AM)
8 #define BOOST_SPIRIT_TST_MARCH_09_2007_0905AM
14 #include <boost/call_traits.hpp>
15 #include <iterator> // for std::iterator_traits
17 namespace boost { namespace spirit { namespace qi { namespace detail
19 // This file contains low level TST routines, not for
20 // public consumption.
22 template <typename Char, typename T>
26 : id(id_), data(0), lt(0), eq(0), gt(0)
30 template <typename Alloc>
32 destruct_node(tst_node* p, Alloc* alloc)
37 alloc->delete_data(p->data);
38 destruct_node(p->lt, alloc);
39 destruct_node(p->eq, alloc);
40 destruct_node(p->gt, alloc);
41 alloc->delete_node(p);
45 template <typename Alloc>
47 clone_node(tst_node* p, Alloc* alloc)
51 tst_node* clone = alloc->new_node(p->id);
53 clone->data = alloc->new_data(*p->data);
54 clone->lt = clone_node(p->lt, alloc);
55 clone->eq = clone_node(p->eq, alloc);
56 clone->gt = clone_node(p->gt, alloc);
62 template <typename Iterator, typename Filter>
64 find(tst_node* start, Iterator& first, Iterator last, Filter filter)
70 Iterator latest = first;
74 while (p && i != last)
77 std::iterator_traits<Iterator>::value_type
78 c = filter(*i); // filter only the input
101 first = ++latest; // one past the last matching char
105 template <typename Iterator, typename Alloc>
111 , typename boost::call_traits<T>::param_type val
117 tst_node** pp = &start;
121 std::iterator_traits<Iterator>::value_type
125 *pp = alloc->new_node(c);
133 p->data = alloc->new_data(val);
149 template <typename Iterator, typename Alloc>
151 remove(tst_node*& p, Iterator first, Iterator last, Alloc* alloc)
153 if (p == 0 || first == last)
157 std::iterator_traits<Iterator>::value_type
166 alloc->delete_data(p->data);
170 remove(p->eq, first, last, alloc);
174 remove(p->lt, first, last, alloc);
178 remove(p->gt, first, last, alloc);
181 if (p->data == 0 && p->lt == 0 && p->eq == 0 && p->gt == 0)
183 alloc->delete_node(p);
188 template <typename F>
190 for_each(tst_node* p, std::basic_string<Char> prefix, F f)
194 for_each(p->lt, prefix, f);
195 std::basic_string<Char> s = prefix + p->id;
196 for_each(p->eq, s, f);
199 for_each(p->gt, prefix, f);
203 Char id; // the node's identity character
204 T* data; // optional data
205 tst_node* lt; // left pointer
206 tst_node* eq; // middle pointer
207 tst_node* gt; // right pointer