9
3

range_run.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*=============================================================================
  2. Copyright (c) 2001-2003 Joel de Guzman
  3. http://spirit.sourceforge.net/
  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. #ifndef BOOST_SPIRIT_RANGE_RUN_HPP
  8. #define BOOST_SPIRIT_RANGE_RUN_HPP
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include <vector>
  11. #include <boost/spirit/home/classic/namespace.hpp>
  12. ///////////////////////////////////////////////////////////////////////////////
  13. namespace boost { namespace spirit {
  14. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  15. namespace utility { namespace impl {
  16. ///////////////////////////////////////////////////////////////////////////
  17. //
  18. // range class
  19. //
  20. // Implements a closed range of values. This class is used in
  21. // the implementation of the range_run class.
  22. //
  23. // { Low level implementation detail }
  24. // { Not to be confused with BOOST_SPIRIT_CLASSIC_NS::range }
  25. //
  26. ///////////////////////////////////////////////////////////////////////////
  27. template <typename CharT>
  28. struct range {
  29. range(CharT first, CharT last);
  30. bool is_valid() const;
  31. bool includes(CharT v) const;
  32. bool includes(range const& r) const;
  33. bool overlaps(range const& r) const;
  34. void merge(range const& r);
  35. CharT first;
  36. CharT last;
  37. };
  38. //////////////////////////////////
  39. template <typename CharT>
  40. struct range_char_compare {
  41. bool operator()(range<CharT> const& x, const CharT y) const
  42. { return x.first < y; }
  43. bool operator()(const CharT x, range<CharT> const& y) const
  44. { return x < y.first; }
  45. // This additional operator is required for the checked STL shipped
  46. // with VC8 testing the ordering of the iterators passed to the
  47. // std::lower_bound algo this range_char_compare<> predicate is passed
  48. // to.
  49. bool operator()(range<CharT> const& x, range<CharT> const& y) const
  50. { return x.first < y.first; }
  51. };
  52. //////////////////////////////////
  53. template <typename CharT>
  54. struct range_compare {
  55. bool operator()(range<CharT> const& x, range<CharT> const& y) const
  56. { return x.first < y.first; }
  57. };
  58. ///////////////////////////////////////////////////////////////////////////
  59. //
  60. // range_run
  61. //
  62. // An implementation of a sparse bit (boolean) set. The set uses
  63. // a sorted vector of disjoint ranges. This class implements the
  64. // bare minimum essentials from which the full range of set
  65. // operators can be implemented. The set is constructed from
  66. // ranges. Internally, adjacent or overlapping ranges are
  67. // coalesced.
  68. //
  69. // range_runs are very space-economical in situations where there
  70. // are lots of ranges and a few individual disjoint values.
  71. // Searching is O(log n) where n is the number of ranges.
  72. //
  73. // { Low level implementation detail }
  74. //
  75. ///////////////////////////////////////////////////////////////////////////
  76. template <typename CharT>
  77. class range_run {
  78. public:
  79. typedef range<CharT> range_t;
  80. typedef std::vector<range_t> run_t;
  81. typedef typename run_t::iterator iterator;
  82. typedef typename run_t::const_iterator const_iterator;
  83. void swap(range_run& rr);
  84. bool test(CharT v) const;
  85. void set(range_t const& r);
  86. void clear(range_t const& r);
  87. void clear();
  88. const_iterator begin() const;
  89. const_iterator end() const;
  90. private:
  91. void merge(iterator iter, range_t const& r);
  92. run_t run;
  93. };
  94. }}
  95. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  96. }} // namespace BOOST_SPIRIT_CLASSIC_NS::utility::impl
  97. #endif
  98. #include <boost/spirit/home/classic/utility/impl/chset/range_run.ipp>