diff --git a/include/boost/graph/detail/bitset.hpp b/include/boost/graph/detail/bitset.hpp index eebadcee..588ae656 100644 --- a/include/boost/graph/detail/bitset.hpp +++ b/include/boost/graph/detail/bitset.hpp @@ -23,6 +23,7 @@ #include #include #include +#include // This provides versions of std::bitset with both static and dynamic size. @@ -57,6 +58,8 @@ namespace boost { //========================================================================= template class bitset_base + : public bitset_adaptor< SizeType, + bitset_base > { // private: public: @@ -314,6 +317,12 @@ namespace boost { return static_cast(*this); } + // this wasn't working, why? + int compare_3way(const Derived& x) const { + return std::lexicographical_compare_3way + (data(), data() + num_words(), x.data(), x.data() + x.num_words()); + } + // less-than compare bool operator<(const Derived& x) const { return std::lexicographical_compare @@ -380,6 +389,14 @@ namespace boost { return result; } + template + inline int compare_3way(const bitset_base& x, + const bitset_base& y) { + return std::lexicographical_compare_3way + (x.data(), x.data() + x.num_words(), + y.data(), y.data() + y.num_words()); + } + template std::istream& @@ -468,7 +485,7 @@ namespace boost { (const basic_string& s, std::size_t pos = 0, std::size_t n = size_t(basic_string::npos), - const Allocator& alloc = Allocator()) + const Allocator& alloc = Allocator()) : m_data(alloc.allocate((n + word_size - 1) / word_size)), m_size(n), m_num_words((n + word_size - 1) / word_size), m_alloc(alloc) @@ -480,8 +497,8 @@ namespace boost { template explicit dyn_size_bitset (InputIterator first, InputIterator last, - size_type n, // size of the set's "universe" - const Allocator& alloc = Allocator()) + size_type n, // size of the set's "universe" + const Allocator& alloc = Allocator()) : m_data(alloc.allocate((n + word_size - 1) / word_size)), m_size(N), m_num_words((n + word_size - 1) / word_size), m_alloc(alloc) diff --git a/include/boost/graph/detail/bitset_adaptor.hpp b/include/boost/graph/detail/bitset_adaptor.hpp new file mode 100644 index 00000000..84cba947 --- /dev/null +++ b/include/boost/graph/detail/bitset_adaptor.hpp @@ -0,0 +1,81 @@ +#ifndef BOOST_BITSET_ADAPTOR_HPP +#define BOOST_BITSET_ADAPTOR_HPP + + template + struct bitset_adaptor { + Derived& derived() { return static_cast(*this); } + const Derived& derived() const { + return static_cast(*this); + } + }; + + template + bool set_contains(const bitset_adaptor& s, const V& x) { + return s.derived().test(x); + } + + template + bool set_equal(const bitset_adaptor& x, + const bitset_adaptor& y) { + return x.derived() == y.derived(); + } + + template + int set_lex_order(const bitset_adaptor& x, + const bitset_adaptor& y) { + return compare_3way(x.derived(), y.derived()); + } + + template + void set_clear(bitset_adaptor& x) { + x.derived().reset(); + } + + template + bool set_empty(const bitset_adaptor& x) { + return x.derived().none(); + } + + template + void set_insert(bitset_adaptor& x, const V& a) { + x.derived().set(a); + } + + template + void set_remove(bitset_adaptor& x, const V& a) { + x.derived().set(a, false); + } + + template + void set_intersect(const bitset_adaptor& x, + const bitset_adaptor& y, + bitset_adaptor& z) + { + z.derived() = x.derived() & y.derived(); + } + + template + void set_union(const bitset_adaptor& x, + const bitset_adaptor& y, + bitset_adaptor& z) + { + z.derived() = x.derived() | y.derived(); + } + + template + void set_difference(const bitset_adaptor& x, + const bitset_adaptor& y, + bitset_adaptor& z) + { + z.derived() = x.derived() - y.derived(); + } + + template + void set_compliment(const bitset_adaptor& x, + bitset_adaptor& z) + { + z.derived() = x.derived(); + z.derived().flip(); + } + +#endif // BOOST_BITSET_ADAPTOR_HPP diff --git a/include/boost/graph/detail/set_adaptor.hpp b/include/boost/graph/detail/set_adaptor.hpp new file mode 100644 index 00000000..f6d2491b --- /dev/null +++ b/include/boost/graph/detail/set_adaptor.hpp @@ -0,0 +1,103 @@ +#ifndef BOOST_SET_ADAPTOR_HPP +#define BOOST_SET_ADAPTOR_HPP + +#include + + template + bool set_contains(const std::set& s, const T& x) { + return s.find(x) != s.end(); + } + + template + bool set_equal(const std::set& x, + const std::set& y) + { + return x == y; + } + + // Not the same as lexicographical_compare_3way applied to std::set. + // this is equivalent semantically to bitset::operator<() + template + int set_lex_order(const std::set& x, + const std::set& y) + { + typename std::set::iterator + xi = x.begin(), yi = y.begin(), xend = x.end(), yend = y.end(); + for (; xi != xend && yi != yend; ++xi, ++yi) { + if (*xi < *yi) + return 1; + else if (*yi < *xi) + return -1; + } + if (xi == xend) + return (yi == yend) ? 0 : -1; + else + return 1; + } + + template + void set_clear(std::set& x) { + x.clear(); + } + + template + bool set_empty(const std::set& x) { + return x.empty(); + } + + template + void set_insert(std::set& x, const T& a) { + x.insert(a); + } + + template + void set_remove(std::set& x, const T& a) { + x.remove(a); + } + + template + void set_intersect(const std::set& x, + const std::set& y, + std::set& z) + { + z.clear(); + std::set_intersection(x.begin(), x.end(), + y.begin(), y.end(), + std::inserter(z)); + } + + template + void set_union(const std::set& x, + const std::set& y, + std::set& z) + { + z.clear(); + std::set_union(x.begin(), x.end(), + y.begin(), y.end(), + std::inserter(z)); + } + + template + void set_difference(const std::set& x, + const std::set& y, + std::set& z) + { + z.clear(); + std::set_difference(x.begin(), x.end(), + y.begin(), y.end(), + std::inserter(z)); + } + + // Shit, can't implement this without knowing the size of the + // universe. + template + void set_compliment(const std::set& x, + std::set& z) + { + z.clear(); + + } + + + +#endif // BOOST_SET_ADAPTOR_HPP