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
18 namespace boost { namespace spirit { namespace qi { namespace detail
20 // This file contains low level TST routines, not for
21 // public consumption.
23 template <typename Char, typename T>
27 : id(id_), data(0), lt(0), eq(0), gt(0)
31 template <typename Alloc>
33 destruct_node(tst_node* p, Alloc* alloc)
38 alloc->delete_data(p->data);
39 destruct_node(p->lt, alloc);
40 destruct_node(p->eq, alloc);
41 destruct_node(p->gt, alloc);
42 alloc->delete_node(p);
46 template <typename Alloc>
48 clone_node(tst_node* p, Alloc* alloc)
52 tst_node* clone = alloc->new_node(p->id);
54 clone->data = alloc->new_data(*p->data);
55 clone->lt = clone_node(p->lt, alloc);
56 clone->eq = clone_node(p->eq, alloc);
57 clone->gt = clone_node(p->gt, alloc);
63 template <typename Iterator, typename Filter>
65 find(tst_node* start, Iterator& first, Iterator last, Filter filter)
71 Iterator latest = first;
75 while (p && i != last)
78 std::iterator_traits<Iterator>::value_type
79 c = filter(*i); // filter only the input
102 first = ++latest; // one past the last matching char
106 template <typename Iterator, typename Alloc>
112 , typename boost::call_traits<T>::param_type val
118 tst_node** pp = &start;
122 std::iterator_traits<Iterator>::value_type
126 *pp = alloc->new_node(c);
134 p->data = alloc->new_data(val);
150 template <typename Iterator, typename Alloc>
152 remove(tst_node*& p, Iterator first, Iterator last, Alloc* alloc)
154 if (p == 0 || first == last)
158 std::iterator_traits<Iterator>::value_type
167 alloc->delete_data(p->data);
171 remove(p->eq, first, last, alloc);
175 remove(p->lt, first, last, alloc);
179 remove(p->gt, first, last, alloc);
182 if (p->data == 0 && p->lt == 0 && p->eq == 0 && p->gt == 0)
184 alloc->delete_node(p);
189 template <typename F>
191 for_each(tst_node* p, std::basic_string<Char> prefix, F f)
195 for_each(p->lt, prefix, f);
196 std::basic_string<Char> s = prefix + p->id;
197 for_each(p->eq, s, f);
200 for_each(p->gt, prefix, f);
204 Char id; // the node's identity character
205 T* data; // optional data
206 tst_node* lt; // left pointer
207 tst_node* eq; // middle pointer
208 tst_node* gt; // right pointer