range_functions.hpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 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_RANGE_FUNCTIONS_MAY_16_2006_0720_PM)
  7. #define BOOST_SPIRIT_RANGE_FUNCTIONS_MAY_16_2006_0720_PM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/integer_traits.hpp>
  12. namespace boost { namespace spirit { namespace support { namespace detail
  13. {
  14. template <typename Range>
  15. inline bool
  16. is_valid(Range const& range)
  17. {
  18. // test for valid ranges
  19. return range.first <= range.last;
  20. }
  21. template <typename Range>
  22. inline bool
  23. includes(Range const& range, Range const& other)
  24. {
  25. // see if two ranges intersect
  26. return (range.first <= other.first) && (range.last >= other.last);
  27. }
  28. template <typename Range>
  29. inline bool
  30. includes(Range const& range, typename Range::value_type val)
  31. {
  32. // see if val is in range
  33. return (range.first <= val) && (range.last >= val);
  34. }
  35. template <typename Range>
  36. inline bool
  37. can_merge(Range const& range, Range const& other)
  38. {
  39. // see if a 'range' overlaps, or is adjacent to
  40. // another range 'other', so we can merge them
  41. typedef typename Range::value_type value_type;
  42. typedef integer_traits<value_type> integer_traits;
  43. value_type decr_first =
  44. range.first == integer_traits::const_min
  45. ? range.first : range.first-1;
  46. value_type incr_last =
  47. range.last == integer_traits::const_max
  48. ? range.last : range.last+1;
  49. return (decr_first <= other.last) && (incr_last >= other.first);
  50. }
  51. template <typename Range>
  52. inline void
  53. merge(Range& result, Range const& other)
  54. {
  55. // merge two ranges
  56. if (result.first > other.first)
  57. result.first = other.first;
  58. if (result.last < other.last)
  59. result.last = other.last;
  60. }
  61. template <typename Range>
  62. struct range_compare
  63. {
  64. // compare functor with a value or another range
  65. typedef typename Range::value_type value_type;
  66. bool operator()(Range const& x, const value_type y) const
  67. {
  68. return x.first < y;
  69. }
  70. bool operator()(value_type const x, Range const& y) const
  71. {
  72. return x < y.first;
  73. }
  74. bool operator()(Range const& x, Range const& y) const
  75. {
  76. return x.first < y.first;
  77. }
  78. };
  79. }}}}
  80. #endif