diff --git a/xo-ordinaltree/include/xo/ordinaltree/RedBlackTree.hpp b/xo-ordinaltree/include/xo/ordinaltree/RedBlackTree.hpp index 066226fa..9683f487 100644 --- a/xo-ordinaltree/include/xo/ordinaltree/RedBlackTree.hpp +++ b/xo-ordinaltree/include/xo/ordinaltree/RedBlackTree.hpp @@ -57,9 +57,12 @@ namespace xo { template class RedBlackTree : public xo::gc::gc_allocator_traits::object_interface_type { static_assert(ordered_key); + static_assert(std::default_initializable); + static_assert(std::predicate); static_assert(valid_rbtree_reduce_functor); public: @@ -67,7 +70,7 @@ namespace xo { using mapped_type = Value; using value_type = std::pair; - // using key_compare = Compare // not yet + using key_compare = Compare; using allocator_type = Allocator; using allocator_traits = xo::gc::gc_allocator_traits; @@ -76,7 +79,7 @@ namespace xo { **/ using GcObjectInterface = allocator_traits::object_interface_type; using ReducedValue = typename Reduce::value_type; - using RbUtil = detail::RbTreeUtil; + using RbUtil = detail::RbTreeUtil; using RbNode = detail::Node; using RbTreeLhs = detail::RedBlackTreeLhs; using RbTreeConstLhs = detail::RedBlackTreeConstLhs; @@ -92,15 +95,21 @@ namespace xo { using Direction = detail::Direction; using size_type = std::size_t; using difference_type = std::ptrdiff_t; - using iterator = detail::Iterator; - using const_iterator = detail::ConstIterator; + using iterator = detail::Iterator; + using const_iterator = detail::ConstIterator; using Reflect = xo::reflect::Reflect; using TaggedPtr = xo::reflect::TaggedPtr; public: - explicit RedBlackTree(const allocator_type & alloc = allocator_type{}, + explicit RedBlackTree(const key_compare & compare = key_compare{}, + const allocator_type & alloc = allocator_type{}, bool debug_flag = false) : + compare_{compare}, node_alloc_{alloc}, debug_flag_{debug_flag} {} #ifdef NOT_YET // need copy_from @@ -126,7 +135,8 @@ namespace xo { {} #endif - static RedBlackTree * make(allocator_type kvpair_alloc = allocator_type{}, + static RedBlackTree * make(key_compare compare = key_compare{}, + allocator_type kvpair_alloc = allocator_type{}, bool debug_flag = false) { tree_allocator_type tree_alloc(kvpair_alloc); @@ -135,7 +145,7 @@ namespace xo { try { // placement new - tree_allocator_traits::construct(tree_alloc, tree, kvpair_alloc, debug_flag); + tree_allocator_traits::construct(tree_alloc, tree, compare, kvpair_alloc, debug_flag); return tree; } catch(...) { tree_allocator_traits::deallocate(tree_alloc, tree, 1); @@ -670,6 +680,8 @@ namespace xo { } private: + /** key comparison */ + key_compare compare_; /** allocator state **/ node_allocator_type node_alloc_; /** number of nodes in this tree. Each node holds one (key,value) pair **/ @@ -688,10 +700,11 @@ namespace xo { template inline std::ostream & operator<<(std::ostream & os, - RedBlackTree const & tree) + RedBlackTree const & tree) { tree.display(os); return os; @@ -700,11 +713,12 @@ namespace xo { template inline std::ostream & operator<<(std::ostream & os, - detail::IteratorBase const & iter) + detail::IteratorBase const & iter) { iter.print(os); return os; diff --git a/xo-ordinaltree/include/xo/ordinaltree/rbtree/Iterator.hpp b/xo-ordinaltree/include/xo/ordinaltree/rbtree/Iterator.hpp index 73252294..9d420776 100644 --- a/xo-ordinaltree/include/xo/ordinaltree/rbtree/Iterator.hpp +++ b/xo-ordinaltree/include/xo/ordinaltree/rbtree/Iterator.hpp @@ -51,11 +51,12 @@ namespace xo { template class IteratorBase { public: - using RbUtil = RbTreeUtil; + using RbUtil = RbTreeUtil; using RbNode = Node; using Traits = NodeTypeTraits; using ReducedValue = typename Reduce::value_type; @@ -238,16 +239,21 @@ namespace xo { template class Iterator : public IteratorBase { public: using iterator_concept = std::bidirectional_iterator_tag; - using RbIteratorBase = IteratorBase; + using RbIteratorBase = IteratorBase; using RbNode = typename RbIteratorBase::RbNode; using RbUtil = typename RbIteratorBase::RbUtil; using ReducedValue = typename Reduce::value_type; @@ -308,12 +314,17 @@ namespace xo { template - class ConstIterator : public IteratorBase { + class ConstIterator : public IteratorBase { public: using iterator_concept = std::bidirectional_iterator_tag; - using RbIteratorBase = IteratorBase; + using RbIteratorBase = IteratorBase; using RbNode = typename RbIteratorBase::RbNode; using RbUtil = typename RbIteratorBase::RbUtil; using ReducedValue = typename Reduce::value_type; diff --git a/xo-ordinaltree/include/xo/ordinaltree/rbtree/Node.hpp b/xo-ordinaltree/include/xo/ordinaltree/rbtree/Node.hpp index ed293cd2..2bbe2cd8 100644 --- a/xo-ordinaltree/include/xo/ordinaltree/rbtree/Node.hpp +++ b/xo-ordinaltree/include/xo/ordinaltree/rbtree/Node.hpp @@ -20,7 +20,9 @@ namespace xo { namespace tree { namespace detail { /** see xo/ordinaltree/rbtree/RbTreeUtil.hpp */ - template + template class RbTreeUtil; /* xo::tree::detail::Node diff --git a/xo-ordinaltree/include/xo/ordinaltree/rbtree/RbTreeUtil.hpp b/xo-ordinaltree/include/xo/ordinaltree/rbtree/RbTreeUtil.hpp index 6aab30e5..6c073f4b 100644 --- a/xo-ordinaltree/include/xo/ordinaltree/rbtree/RbTreeUtil.hpp +++ b/xo-ordinaltree/include/xo/ordinaltree/rbtree/RbTreeUtil.hpp @@ -19,6 +19,7 @@ namespace xo { template class RbTreeUtil { public: diff --git a/xo-ordinaltree/include/xo/ordinaltree/rbtree/RbTypes.hpp b/xo-ordinaltree/include/xo/ordinaltree/rbtree/RbTypes.hpp index 7963428c..0e3b3db1 100644 --- a/xo-ordinaltree/include/xo/ordinaltree/rbtree/RbTypes.hpp +++ b/xo-ordinaltree/include/xo/ordinaltree/rbtree/RbTypes.hpp @@ -21,7 +21,7 @@ namespace xo { template , - //typename Compare = std::less, + typename Compare = std::less, typename Allocator = std::allocator>> class RedBlackTree; diff --git a/xo-ordinaltree/utest/RedBlackTree-gc.test.cpp b/xo-ordinaltree/utest/RedBlackTree-gc.test.cpp index 8a30f455..cf716eb9 100644 --- a/xo-ordinaltree/utest/RedBlackTree-gc.test.cpp +++ b/xo-ordinaltree/utest/RedBlackTree-gc.test.cpp @@ -56,6 +56,7 @@ namespace xo { using RbTree = RedBlackTree, + std::less, xo::gc::allocator>>; constexpr bool c_debug_flag = false; @@ -124,6 +125,7 @@ namespace xo { using RbTree = RedBlackTree, + std::less, xo::gc::allocator>>; constexpr bool c_debug_flag = false; @@ -160,9 +162,10 @@ namespace xo { REQUIRE(gc->native_gc_statistics().n_gc() == 0); + RbTree::key_compare compare; xo::gc::allocator allocator(gc.get()); - gp rbtree = RbTree::make(allocator, c_debug_flag); + gp rbtree = RbTree::make(compare, allocator, c_debug_flag); gc->add_gc_root_dwim(&rbtree); @@ -197,7 +200,7 @@ namespace xo { } REQUIRE(rbtree->verify_ok(debug_flag)); - + } if (n > 0) { @@ -259,7 +262,7 @@ namespace xo { &rgen); REQUIRE(rbtree->verify_ok(debug_flag)); - + if (tc.do_extra_gc_) { REQUIRE(gc->gc_in_progress() == false); gc->request_gc(gc::generation::nursery); diff --git a/xo-ordinaltree/utest/redblacktree.cpp b/xo-ordinaltree/utest/redblacktree.cpp index fdc815e7..b32a40ce 100644 --- a/xo-ordinaltree/utest/redblacktree.cpp +++ b/xo-ordinaltree/utest/redblacktree.cpp @@ -127,7 +127,7 @@ namespace { TEST_CASE("rbtree", "[redblacktree]") { constexpr bool c_debug_flag = false; - RbTree rbtree{RbTree::allocator_type{}, c_debug_flag}; + RbTree rbtree{RbTree::key_compare{}, RbTree::allocator_type{}, c_debug_flag}; REQUIRE(rbtree.size() == 0);