set_adaptor.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // (C) Copyright Jeremy Siek 2001.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_SET_ADAPTOR_HPP
  6. #define BOOST_SET_ADAPTOR_HPP
  7. #include <set>
  8. #include <boost/unordered_set.hpp>
  9. namespace boost
  10. {
  11. template < class K, class C, class A, class T >
  12. bool set_contains(const std::set< K, C, A >& s, const T& x)
  13. {
  14. return s.find(x) != s.end();
  15. }
  16. template < class K, class H, class C, class A, class T >
  17. bool set_contains(const boost::unordered_set< K, H, C, A >& s, const T& x)
  18. {
  19. return s.find(x) != s.end();
  20. }
  21. template < class K, class C, class A >
  22. bool set_equal(const std::set< K, C, A >& x, const std::set< K, C, A >& y)
  23. {
  24. return x == y;
  25. }
  26. // Not the same as lexicographical_compare_3way applied to std::set.
  27. // this is equivalent semantically to bitset::operator<()
  28. template < class K, class C, class A >
  29. int set_lex_order(const std::set< K, C, A >& x, const std::set< K, C, A >& y)
  30. {
  31. typename std::set< K, C, A >::iterator xi = x.begin(), yi = y.begin(),
  32. xend = x.end(), yend = y.end();
  33. for (; xi != xend && yi != yend; ++xi, ++yi)
  34. {
  35. if (*xi < *yi)
  36. return 1;
  37. else if (*yi < *xi)
  38. return -1;
  39. }
  40. if (xi == xend)
  41. return (yi == yend) ? 0 : -1;
  42. else
  43. return 1;
  44. }
  45. template < class K, class C, class A > void set_clear(std::set< K, C, A >& x)
  46. {
  47. x.clear();
  48. }
  49. template < class K, class C, class A >
  50. bool set_empty(const std::set< K, C, A >& x)
  51. {
  52. return x.empty();
  53. }
  54. template < class K, class C, class A, class T >
  55. void set_insert(std::set< K, C, A >& x, const T& a)
  56. {
  57. x.insert(a);
  58. }
  59. template < class K, class C, class A, class T >
  60. void set_remove(std::set< K, C, A >& x, const T& a)
  61. {
  62. x.erase(a);
  63. }
  64. template < class K, class C, class A >
  65. void set_intersect(const std::set< K, C, A >& x, const std::set< K, C, A >& y,
  66. std::set< K, C, A >& z)
  67. {
  68. z.clear();
  69. std::set_intersection(
  70. x.begin(), x.end(), y.begin(), y.end(), std::inserter(z));
  71. }
  72. template < class K, class C, class A >
  73. void set_union(const std::set< K, C, A >& x, const std::set< K, C, A >& y,
  74. std::set< K, C, A >& z)
  75. {
  76. z.clear();
  77. std::set_union(x.begin(), x.end(), y.begin(), y.end(), std::inserter(z));
  78. }
  79. template < class K, class C, class A >
  80. void set_difference(const std::set< K, C, A >& x, const std::set< K, C, A >& y,
  81. std::set< K, C, A >& z)
  82. {
  83. z.clear();
  84. std::set_difference(
  85. x.begin(), x.end(), y.begin(), y.end(), std::inserter(z, z.begin()));
  86. }
  87. template < class K, class C, class A >
  88. bool set_subset(const std::set< K, C, A >& x, const std::set< K, C, A >& y)
  89. {
  90. return std::includes(x.begin(), x.end(), y.begin(), y.end());
  91. }
  92. // Shit, can't implement this without knowing the size of the
  93. // universe.
  94. template < class K, class C, class A >
  95. void set_compliment(const std::set< K, C, A >& /*x*/, std::set< K, C, A >& z)
  96. {
  97. z.clear();
  98. }
  99. } // namespace boost
  100. #endif // BOOST_SET_ADAPTOR_HPP