tst.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*=============================================================================
  2. Copyright (c) 2001-2014 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #if !defined(BOOST_SPIRIT_X3_TST_MARCH_09_2007_0905AM)
  7. #define BOOST_SPIRIT_X3_TST_MARCH_09_2007_0905AM
  8. #include <boost/call_traits.hpp>
  9. #include <boost/assert.hpp>
  10. #include <string>
  11. namespace boost { namespace spirit { namespace x3 { namespace detail
  12. {
  13. // This file contains low level TST routines, not for
  14. // public consumption.
  15. template <typename Char, typename T>
  16. struct tst_node
  17. {
  18. tst_node(Char id)
  19. : id(id), data(0), lt(0), eq(0), gt(0)
  20. {
  21. }
  22. template <typename Alloc>
  23. static void
  24. destruct_node(tst_node* p, Alloc* alloc)
  25. {
  26. if (p)
  27. {
  28. if (p->data)
  29. alloc->delete_data(p->data);
  30. destruct_node(p->lt, alloc);
  31. destruct_node(p->eq, alloc);
  32. destruct_node(p->gt, alloc);
  33. alloc->delete_node(p);
  34. }
  35. }
  36. template <typename Alloc>
  37. static tst_node*
  38. clone_node(tst_node* p, Alloc* alloc)
  39. {
  40. if (p)
  41. {
  42. tst_node* clone = alloc->new_node(p->id);
  43. if (p->data)
  44. clone->data = alloc->new_data(*p->data);
  45. clone->lt = clone_node(p->lt, alloc);
  46. clone->eq = clone_node(p->eq, alloc);
  47. clone->gt = clone_node(p->gt, alloc);
  48. return clone;
  49. }
  50. return 0;
  51. }
  52. template <typename Iterator, typename CaseCompare>
  53. static T*
  54. find(tst_node* start, Iterator& first, Iterator last, CaseCompare comp)
  55. {
  56. if (first == last)
  57. return 0;
  58. Iterator i = first;
  59. Iterator latest = first;
  60. tst_node* p = start;
  61. T* found = 0;
  62. while (p && i != last)
  63. {
  64. int32_t c = comp(*i,p->id);
  65. if (c == 0)
  66. {
  67. if (p->data)
  68. {
  69. found = p->data;
  70. latest = i;
  71. }
  72. p = p->eq;
  73. i++;
  74. }
  75. else if (c < 0)
  76. {
  77. p = p->lt;
  78. }
  79. else
  80. {
  81. p = p->gt;
  82. }
  83. }
  84. if (found)
  85. first = ++latest; // one past the last matching char
  86. return found;
  87. }
  88. template <typename Iterator, typename Alloc>
  89. static T*
  90. add(
  91. tst_node*& start
  92. , Iterator first
  93. , Iterator last
  94. , typename boost::call_traits<T>::param_type val
  95. , Alloc* alloc)
  96. {
  97. if (first == last)
  98. return 0;
  99. tst_node** pp = &start;
  100. for (;;)
  101. {
  102. auto c = *first;
  103. if (*pp == 0)
  104. *pp = alloc->new_node(c);
  105. tst_node* p = *pp;
  106. if (c == p->id)
  107. {
  108. if (++first == last)
  109. {
  110. if (p->data == 0)
  111. p->data = alloc->new_data(val);
  112. return p->data;
  113. }
  114. pp = &p->eq;
  115. }
  116. else if (c < p->id)
  117. {
  118. pp = &p->lt;
  119. }
  120. else
  121. {
  122. pp = &p->gt;
  123. }
  124. }
  125. }
  126. template <typename Iterator, typename Alloc>
  127. static void
  128. remove(tst_node*& p, Iterator first, Iterator last, Alloc* alloc)
  129. {
  130. if (p == 0 || first == last)
  131. return;
  132. auto c = *first;
  133. if (c == p->id)
  134. {
  135. if (++first == last)
  136. {
  137. if (p->data)
  138. {
  139. alloc->delete_data(p->data);
  140. p->data = 0;
  141. }
  142. }
  143. remove(p->eq, first, last, alloc);
  144. }
  145. else if (c < p->id)
  146. {
  147. remove(p->lt, first, last, alloc);
  148. }
  149. else
  150. {
  151. remove(p->gt, first, last, alloc);
  152. }
  153. if (p->data == 0 && p->lt == 0 && p->eq == 0 && p->gt == 0)
  154. {
  155. alloc->delete_node(p);
  156. p = 0;
  157. }
  158. }
  159. template <typename F>
  160. static void
  161. for_each(tst_node* p, std::basic_string<Char> prefix, F f)
  162. {
  163. if (p)
  164. {
  165. for_each(p->lt, prefix, f);
  166. std::basic_string<Char> s = prefix + p->id;
  167. for_each(p->eq, s, f);
  168. if (p->data)
  169. f(s, *p->data);
  170. for_each(p->gt, prefix, f);
  171. }
  172. }
  173. Char id; // the node's identity character
  174. T* data; // optional data
  175. tst_node* lt; // left pointer
  176. tst_node* eq; // middle pointer
  177. tst_node* gt; // right pointer
  178. };
  179. }}}}
  180. #endif