range_run.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*=============================================================================
  2. Copyright (c) 2001-2003 Joel de Guzman
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #ifndef BOOST_XPRESSIVE_SPIRIT_RANGE_RUN_HPP_EAN_10_04_2005
  9. #define BOOST_XPRESSIVE_SPIRIT_RANGE_RUN_HPP_EAN_10_04_2005
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include <vector>
  12. ///////////////////////////////////////////////////////////////////////////////
  13. namespace boost { namespace xpressive { namespace detail
  14. {
  15. ///////////////////////////////////////////////////////////////////////////
  16. //
  17. // range class
  18. //
  19. // Implements a closed range of values. This class is used in
  20. // the implementation of the range_run class.
  21. //
  22. // { Low level implementation detail }
  23. // { Not to be confused with spirit::range }
  24. //
  25. ///////////////////////////////////////////////////////////////////////////
  26. template<typename Char>
  27. struct range
  28. {
  29. range(Char first, Char last);
  30. bool is_valid() const;
  31. bool includes(Char v) const;
  32. bool includes(range const &r) const;
  33. bool overlaps(range const &r) const;
  34. void merge(range const &r);
  35. Char first_;
  36. Char last_;
  37. };
  38. //////////////////////////////////
  39. template<typename Char>
  40. struct range_compare
  41. {
  42. bool operator()(range<Char> const &x, range<Char> const &y) const
  43. {
  44. return x.first_ < y.first_;
  45. }
  46. };
  47. ///////////////////////////////////////////////////////////////////////////
  48. //
  49. // range_run
  50. //
  51. // An implementation of a sparse bit (boolean) set. The set uses
  52. // a sorted vector of disjoint ranges. This class implements the
  53. // bare minimum essentials from which the full range of set
  54. // operators can be implemented. The set is constructed from
  55. // ranges. Internally, adjacent or overlapping ranges are
  56. // coalesced.
  57. //
  58. // range_runs are very space-economical in situations where there
  59. // are lots of ranges and a few individual disjoint values.
  60. // Searching is O(log n) where n is the number of ranges.
  61. //
  62. // { Low level implementation detail }
  63. //
  64. ///////////////////////////////////////////////////////////////////////////
  65. template<typename Char>
  66. struct range_run
  67. {
  68. typedef range<Char> range_type;
  69. typedef std::vector<range_type> run_type;
  70. typedef typename run_type::iterator iterator;
  71. typedef typename run_type::const_iterator const_iterator;
  72. void swap(range_run& rr);
  73. bool empty() const;
  74. bool test(Char v) const;
  75. template<typename Traits>
  76. bool test(Char v, Traits const &tr) const;
  77. void set(range_type const &r);
  78. void clear(range_type const &r);
  79. void clear();
  80. const_iterator begin() const;
  81. const_iterator end() const;
  82. private:
  83. void merge(iterator iter, range_type const &r);
  84. run_type run_;
  85. };
  86. }}} // namespace boost::xpressive::detail
  87. #endif