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 <boost/detail/iterator.hpp>
16 #include <boost/foreach.hpp>
17 #include <boost/assert.hpp>
19 namespace boost { namespace spirit { namespace qi { namespace detail
21 // This file contains low level TST routines, not for
22 // public consumption.
24 template <typename Char, typename T>
28 : id(id_), data(0), lt(0), eq(0), gt(0)
32 template <typename Alloc>
34 destruct_node(tst_node* p, Alloc* alloc)
39 alloc->delete_data(p->data);
40 destruct_node(p->lt, alloc);
41 destruct_node(p->eq, alloc);
42 destruct_node(p->gt, alloc);
43 alloc->delete_node(p);
47 template <typename Alloc>
49 clone_node(tst_node* p, Alloc* alloc)
53 tst_node* clone = alloc->new_node(p->id);
55 clone->data = alloc->new_data(*p->data);
56 clone->lt = clone_node(p->lt, alloc);
57 clone->eq = clone_node(p->eq, alloc);
58 clone->gt = clone_node(p->gt, alloc);
64 template <typename Iterator, typename Filter>
66 find(tst_node* start, Iterator& first, Iterator last, Filter filter)
72 Iterator latest = first;
76 while (p && i != last)
79 boost::detail::iterator_traits<Iterator>::value_type
80 c = filter(*i); // filter only the input
103 first = ++latest; // one past the last matching char
107 template <typename Iterator, typename Alloc>
113 , typename boost::call_traits<T>::param_type val
119 tst_node** pp = &start;
123 boost::detail::iterator_traits<Iterator>::value_type
127 *pp = alloc->new_node(c);
135 p->data = alloc->new_data(val);
151 template <typename Iterator, typename Alloc>
153 remove(tst_node*& p, Iterator first, Iterator last, Alloc* alloc)
155 if (p == 0 || first == last)
159 boost::detail::iterator_traits<Iterator>::value_type
168 alloc->delete_data(p->data);
172 remove(p->eq, first, last, alloc);
176 remove(p->lt, first, last, alloc);
180 remove(p->gt, first, last, alloc);
183 if (p->data == 0 && p->lt == 0 && p->eq == 0 && p->gt == 0)
185 alloc->delete_node(p);
190 template <typename F>
192 for_each(tst_node* p, std::basic_string<Char> prefix, F f)
196 for_each(p->lt, prefix, f);
197 std::basic_string<Char> s = prefix + p->id;
198 for_each(p->eq, s, f);
201 for_each(p->gt, prefix, f);
205 Char id; // the node's identity character
206 T* data; // optional data
207 tst_node* lt; // left pointer
208 tst_node* eq; // middle pointer
209 tst_node* gt; // right pointer