1 /*=============================================================================
2 Copyright (c) 2001-2003 Joel de Guzman
3 Copyright (c) 2011 Daniel James
4 http://spirit.sourceforge.net/
6 Use, modification and distribution is subject to the Boost Software
7 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
10 #ifndef QUICKBOOK_SYMBOLS_IPP
11 #define QUICKBOOK_SYMBOLS_IPP
13 ///////////////////////////////////////////////////////////////////////////////
15 #include <boost/spirit/home/classic/symbols.hpp>
16 #include <boost/intrusive_ptr.hpp>
17 #include <boost/scoped_ptr.hpp>
19 ///////////////////////////////////////////////////////////////////////////////
23 ///////////////////////////////////////////////////////////////////////////////
27 // This it the Ternary Search Tree from
28 // <boost/spirit/home/classic/symbols/impl/tst.ipp> adapted to be cheap
31 // Ternary Search Tree implementation. The data structure is faster than
32 // hashing for many typical search problems especially when the search
33 // interface is iterator based. Searching for a string of length k in a
34 // ternary search tree with n strings will require at most O(log n+k)
35 // character comparisons. TSTs are many times faster than hash tables
36 // for unsuccessful searches since mismatches are discovered earlier
37 // after examining only a few characters. Hash tables always examine an
38 // entire key when searching.
40 // For details see http://www.cs.princeton.edu/~rs/strings/.
42 // *** This is a low level class and is
43 // not meant for public consumption ***
45 ///////////////////////////////////////////////////////////////////////////////
47 template <typename T, typename CharT>
50 tst_node(CharT value_)
60 tst_node(tst_node const& other)
63 , middle(other.middle)
65 , data(other.data ? new T(*other.data) : 0)
70 // If you fancy a slight improvement in memory use,
71 // reference_count + value could probably be packed
72 // in the space for a single int.
74 boost::intrusive_ptr<tst_node> left;
75 boost::intrusive_ptr<tst_node> middle;
76 boost::intrusive_ptr<tst_node> right;
77 boost::scoped_ptr<T> data;
80 tst_node& operator=(tst_node const&);
83 template <typename T, typename CharT>
84 void intrusive_ptr_add_ref(tst_node<T, CharT>* ptr)
85 { ++ptr->reference_count; }
87 template <typename T, typename CharT>
88 void intrusive_ptr_release(tst_node<T, CharT>* ptr)
89 { if(--ptr->reference_count == 0) delete ptr; }
92 ///////////////////////////////////////////////////////////////////////////
93 template <typename T, typename CharT>
96 typedef tst_node<T, CharT> node_t;
97 typedef boost::intrusive_ptr<node_t> node_ptr;
108 void swap(tst& other)
110 root.swap(other.root);
113 // Adds symbol to ternary search tree.
114 // If it already exists, then replace it with new value.
116 // pre: first != last
117 template <typename IteratorT>
118 T* add(IteratorT first, IteratorT const& last, T const& data)
120 assert (first != last);
122 node_ptr* np = &root;
129 *np = new node_t(ch);
131 else if ((*np)->reference_count > 1)
133 *np = new node_t(**np);
136 if (ch < (*np)->value)
140 else if (ch == (*np)->value)
143 if (first == last) break;
153 boost::scoped_ptr<T> new_data(new T(data));
154 boost::swap((*np)->data, new_data);
155 return (*np)->data.get();
158 template <typename ScannerT>
159 search_info find(ScannerT const& scan) const
161 search_info result = { 0, 0 };
166 typedef typename ScannerT::iterator_t iterator_t;
169 iterator_t latest = scan.first;
170 std::size_t length = 0;
174 if (ch < np->value) // => go left!
178 else if (ch == np->value) // => go middle!
183 // Found a potential match.
186 result.data = np->data.get();
187 result.length = length;
191 if (scan.at_end()) break;
195 else // (ch > np->value) => go right!
206 typedef boost::spirit::classic::symbols<
209 quickbook::tst<std::string, char>
211 } // namespace quickbook