From 6fd6cc1778d6db74fb7b4b0c6f43e3c99a3dd23a Mon Sep 17 00:00:00 2001 From: Roland Denis Date: Thu, 31 Aug 2017 15:38:24 +0200 Subject: [PATCH 1/9] Fixing setStartRank and setSlotRank name in slop.hpp --- Zcode/tree/slot/slot.hpp | 8 ++++---- Zcode/tree/slot/slotCollection.hpp | 13 ++++--------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/Zcode/tree/slot/slot.hpp b/Zcode/tree/slot/slot.hpp index e6abd5c..b98682b 100644 --- a/Zcode/tree/slot/slot.hpp +++ b/Zcode/tree/slot/slot.hpp @@ -349,14 +349,14 @@ struct slot: private std::vector< Node > } //! return slotrank. - inline int Slotrank() const + inline int slotRank() const { return slotrank; } //! set the slot rank //! \param r - inline void setSlotrank(std::size_t r) + inline void setSlotRank(std::size_t r) { slotrank = r; } @@ -403,8 +403,8 @@ std::ostream& operator<<(std::ostream& os, const slot& sl) os << "s2: " << sl.s2 << "\n"; os << "size: " << sl.size() << "\n"; os << "capacity: " << sl.capacity() << "\n"; - os << "startrank: " << sl.Startrank() << "\n"; - os << "slotrank: " << sl.Slotrank() << "\n"; + os << "startrank: " << sl.startRank() << "\n"; + os << "slotrank: " << sl.slotRank() << "\n"; os << "hasvoidNodes: " << sl.hasvoidNodes() << "\n"; return os; } diff --git a/Zcode/tree/slot/slotCollection.hpp b/Zcode/tree/slot/slotCollection.hpp index 5c4fc0e..6398134 100644 --- a/Zcode/tree/slot/slotCollection.hpp +++ b/Zcode/tree/slot/slotCollection.hpp @@ -22,17 +22,12 @@ struct slotCollection : private std::vector< std::shared_ptr< slot>; using parent::operator[]; using parent::push_back; - using parent::insert; - using parent::erase; using parent::reserve; - using parent::resize; using parent::begin; using parent::end; using parent::cbegin; using parent::cend; using parent::size; - using parent::capacity; - using parent::shrink_to_fit; using level_count_type = std::array; @@ -270,13 +265,13 @@ struct slotCollection : private std::vector< std::shared_ptr< slotsetStartRank(0); - (*this)[0]->setSlotrank(0); + (*this)[0]->setSlotRank(0); for(std::size_t i=0; i 0) (*this)[i]->setStartRank((*this)[i-1]->startRank()+wmax); wmax = (*this)[i]->size(); - (*this)[i]->setSlotrank(i); + (*this)[i]->setSlotRank(i); } } @@ -285,13 +280,13 @@ struct slotCollection : private std::vector< std::shared_ptr< slotsetStartRank(0); - (*this)[0]->setSlotrank(0); + (*this)[0]->setSlotRank(0); for(std::size_t i=0; i0) (*this)[i]->setStartRank((*this)[i-1]->startRank()+wmax); wmax = (*this)[i]->size(); - (*this)[i]->setSlotrank(i); + (*this)[i]->setSlotRank(i); } } //!return maximum size of slots. From 9167a5433a406d286b7e7e21b80834526bf000d6 Mon Sep 17 00:00:00 2001 From: Roland Denis Date: Thu, 31 Aug 2017 15:49:03 +0200 Subject: [PATCH 2/9] Removing clone method in slotCollection --- Zcode/tree/slot/slotCollection.hpp | 12 +----------- test/test_slotCollection.cpp | 20 -------------------- 2 files changed, 1 insertion(+), 31 deletions(-) diff --git a/Zcode/tree/slot/slotCollection.hpp b/Zcode/tree/slot/slotCollection.hpp index 6398134..411f891 100644 --- a/Zcode/tree/slot/slotCollection.hpp +++ b/Zcode/tree/slot/slotCollection.hpp @@ -28,6 +28,7 @@ struct slotCollection : private std::vector< std::shared_ptr< slot; @@ -80,17 +81,6 @@ struct slotCollection : private std::vector< std::shared_ptr< slotcopyInArray(array.data()+sl->startRank()); }); } - //! make a "clone", ie copy all, but not the Nodes! - // \parameter C SlotCollection to be "cloned" - inline void clone(const slotCollection& C) - { - slot_min_size = C.slot_min_size; - slot_max_size = C.slot_max_size; - - for ( std::size_t i = 0; i < C.size(); ++i) - push_back(std::make_shared(C[i]->s1, C[i]->s2, C[i]->size()*node_type::treetype)); - } - /// Is a Node abscissa in the interval [s1,s2[ ? /// \param s1 /// \param s2 diff --git a/test/test_slotCollection.cpp b/test/test_slotCollection.cpp index 9fece1c..4e141e8 100644 --- a/test/test_slotCollection.cpp +++ b/test/test_slotCollection.cpp @@ -39,26 +39,6 @@ TYPED_TEST(SlotCollectionTest, constructor) EXPECT_EQ( SCcopy[0]->size(), 1 ); } -TYPED_TEST(SlotCollectionTest, clone) -{ - auto const dim = TestFixture::dim; - using value_type = typename TestFixture::value_type; - using node_type = Node; - - slotCollection SC{2, 10, 10, 10}; - EXPECT_EQ( SC.capacity(), 2 ); - EXPECT_EQ( SC.size(), 1 ); - node_type n{1}; - SC.insert(n); - - slotCollection SCclone{}; - SCclone.clone(SC); - EXPECT_EQ( SCclone.capacity(), 1 ); - EXPECT_EQ( SCclone.size(), 1 ); - EXPECT_EQ( SCclone[0]->capacity(), SC[0]->size()*node_type::treetype ); - EXPECT_EQ( SCclone[0]->size(), 0 ); -} - TYPED_TEST(SlotCollectionTest, swap) { auto const dim = TestFixture::dim; From a96e628ebcbf79a16bb4e102d3b7254bc5473e0d Mon Sep 17 00:00:00 2001 From: Roland Denis Date: Thu, 31 Aug 2017 15:50:47 +0200 Subject: [PATCH 3/9] Removing inInterval from slotCollection --- Zcode/tree/slot/slotCollection.hpp | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/Zcode/tree/slot/slotCollection.hpp b/Zcode/tree/slot/slotCollection.hpp index 411f891..826b538 100644 --- a/Zcode/tree/slot/slotCollection.hpp +++ b/Zcode/tree/slot/slotCollection.hpp @@ -81,17 +81,6 @@ struct slotCollection : private std::vector< std::shared_ptr< slotcopyInArray(array.data()+sl->startRank()); }); } - /// Is a Node abscissa in the interval [s1,s2[ ? - /// \param s1 - /// \param s2 - /// \param x Node to check. - /// \note x must be *not* hashed. - inline bool inInterval(node_type s1, node_type s2, node_type x) const - { - node_type xabs = x.hash()&node_type::maskpos; - return (s1<=xabs) && (s2>xabs); - } - /// Store one node using a cache. /// \param x the node to be inserted. /// \param cache an external Cache. From 56d7207975967ed24859a29324404f7e4a3486f9 Mon Sep 17 00:00:00 2001 From: Roland Denis Date: Thu, 31 Aug 2017 16:11:44 +0200 Subject: [PATCH 4/9] Cleaning ubound and ubound_hashed methods. --- Zcode/tree/slot/slotCollection.hpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Zcode/tree/slot/slotCollection.hpp b/Zcode/tree/slot/slotCollection.hpp index 826b538..c7795a9 100644 --- a/Zcode/tree/slot/slotCollection.hpp +++ b/Zcode/tree/slot/slotCollection.hpp @@ -137,15 +137,14 @@ struct slotCollection : private std::vector< std::shared_ptr< slot Date: Thu, 31 Aug 2017 16:52:10 +0200 Subject: [PATCH 5/9] forgetFreeBits -> clearFreeBits and testing slotCollection clearFreeBits --- Zcode/tree/node/node.hpp | 12 ++++++++++++ Zcode/tree/slot/slot.hpp | 6 +++--- Zcode/tree/slot/slotCollection.hpp | 10 +++++----- test/test_slot.cpp | 4 ++-- test/test_slotCollection.cpp | 19 +++++++++++++++++++ 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/Zcode/tree/node/node.hpp b/Zcode/tree/node/node.hpp index 5d57b2d..ef00bee 100644 --- a/Zcode/tree/node/node.hpp +++ b/Zcode/tree/node/node.hpp @@ -184,6 +184,18 @@ struct Node: public definitions return value&(XYZbit>>(dim*(level()+1))); } + //! Suppress all bits used to mark something. + inline void clearFreeBits() + { + value &= ~FreeBitsPart; + } + + //! Returns z-curve position of this node. + inline value_type pos() const + { + return value & maskpos; + } + inline Node operator<<(std::size_t i) const { return {static_cast(value< > return ret; } - //! suppress all bits used to mark something (except voidbit). - inline void forgetFreeBits() + //! Suppress all bits used to mark something. + inline void clearFreeBits() { - std::for_each(begin(), end(), [&](auto &n){n.value&=(~FreeBitsPart);}); + std::for_each(begin(), end(), [](auto &n){ n.clearFreeBits(); }); } //! suppress ex-aequo. diff --git a/Zcode/tree/slot/slotCollection.hpp b/Zcode/tree/slot/slotCollection.hpp index c7795a9..f47ebff 100644 --- a/Zcode/tree/slot/slotCollection.hpp +++ b/Zcode/tree/slot/slotCollection.hpp @@ -173,8 +173,8 @@ struct slotCollection : private std::vector< std::shared_ptr< slotcend() ? 0 : 1; } - //! remove all free bits from all nodes. - inline void forgetFreeBits() + //! Clear all free bits from all nodes. + inline void clearFreeBits() { - std::for_each(begin(), end(), [](auto& st){st->forgetFreeBits();}); + std::for_each(begin(), end(), [](auto& st){st->clearFreeBits();}); } //! suppress void Nodes, if any. diff --git a/test/test_slot.cpp b/test/test_slot.cpp index 0d71030..c68d47c 100644 --- a/test/test_slot.cpp +++ b/test/test_slot.cpp @@ -446,7 +446,7 @@ TYPED_TEST(SlotTest, cutdown) EXPECT_EQ( slot.cutdown(3), false ); } -TYPED_TEST(SlotTest, forgetFreeBits) +TYPED_TEST(SlotTest, clearFreeBits) { auto const dim = TestFixture::dim; using value_type = typename TestFixture::value_type; @@ -457,7 +457,7 @@ TYPED_TEST(SlotTest, forgetFreeBits) for (std::size_t i=0; i<10; ++i) nodes[i] = i + node_type::voidbit; slot.put(nodes); - slot.forgetFreeBits(); + slot.clearFreeBits(); for (std::size_t i=0; i<10; ++i) { node_type node{static_cast(i)}; diff --git a/test/test_slotCollection.cpp b/test/test_slotCollection.cpp index 4e141e8..7259b67 100644 --- a/test/test_slotCollection.cpp +++ b/test/test_slotCollection.cpp @@ -227,3 +227,22 @@ TYPED_TEST(SlotCollectionTest, copyInArray) EXPECT_EQ( array[1], n2.hash() ); EXPECT_EQ( array[2], n3.hash() ); } + +TYPED_TEST(SlotCollectionTest, clearFreeBits) +{ + auto const dim = TestFixture::dim; + using value_type = typename TestFixture::value_type; + using node_type = Node; + + slotCollection SC{2, 10, 10, 11}; + const node_type n1{1}; + const node_type n2(n1.value + node_type::voidbit); + + SC.insert(n2); + + EXPECT_NE( (*SC[0])[0].value, n1.hash().value ); + + SC.clearFreeBits(); + + EXPECT_EQ( (*SC[0])[0].value, n1.hash().value ); +} From 42bb257e2816b2c031147889a2ee58d770c8cbd9 Mon Sep 17 00:00:00 2001 From: Roland Denis Date: Thu, 31 Aug 2017 17:27:46 +0200 Subject: [PATCH 6/9] Adding pos method to Node and updating other files. --- Zcode/tree/node/family.hpp | 2 +- Zcode/tree/node/node.hpp | 14 ++++++++------ Zcode/tree/slot/slot.hpp | 11 +++++------ Zcode/tree/slot/slotCollection.hpp | 15 ++++++++------- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/Zcode/tree/node/family.hpp b/Zcode/tree/node/family.hpp index c41d1e0..cce9ce9 100644 --- a/Zcode/tree/node/family.hpp +++ b/Zcode/tree/node/family.hpp @@ -46,7 +46,7 @@ template inline bool isAncestor(Node_type A, Node_type X) { return (A.level()<=X.level()) - && (A.value&Node_type::maskpos) == (X.value&Node_type::AllOnes[A.level()]); + && (A.pos()) == (X.value&Node_type::AllOnes[A.level()]); } //! Do 2 Nodes share the same ancestor of a given level ? diff --git a/Zcode/tree/node/node.hpp b/Zcode/tree/node/node.hpp index ef00bee..31f1ecd 100644 --- a/Zcode/tree/node/node.hpp +++ b/Zcode/tree/node/node.hpp @@ -191,7 +191,7 @@ struct Node: public definitions } //! Returns z-curve position of this node. - inline value_type pos() const + inline Node pos() const { return value & maskpos; } @@ -224,10 +224,6 @@ struct Node: public definitions return *this; } - inline bool operator&(Node const& node) const - { - return value&node.value; - } }; @@ -246,7 +242,7 @@ std::ostream& operator<<(std::ostream &os, const Node &node) for( int i = size-1; i >= 0; i-- ) { - if(node&(IntOne< operator&(Node const& node, value_ return {static_cast(node.value&value)}; } +template +inline Node operator&( value_type const& value, Node const& node ) +{ + return {static_cast(value&node.value)}; +} + template inline bool operator<(Node const& node1, Node const& node2) { diff --git a/Zcode/tree/slot/slot.hpp b/Zcode/tree/slot/slot.hpp index 9453762..c6a4c8e 100644 --- a/Zcode/tree/slot/slot.hpp +++ b/Zcode/tree/slot/slot.hpp @@ -42,7 +42,6 @@ struct slot: private std::vector< Node > static const node_value_type FreeBitsPart = node_type::FreeBitsPart; static const node_value_type voidbit = node_type::voidbit; - static const node_value_type maskpos = node_type::maskpos; static const node_value_type decal = dim*node_type::nlevels; node_type s1{0}, s2{node_type::AllOnes[node_type::nlevels-1]}; @@ -251,13 +250,13 @@ struct slot: private std::vector< Node > for(std::size_t i=0; i > //! sort by hash function. inline void sort() { - std::sort(begin(), end(), [&](auto &n1, auto &n2){return (n1.value&maskpos)<(n2.value&maskpos);}); + std::sort(begin(), end(), [&](auto &n1, auto &n2){return n1.pos() < n2.pos();}); } //! reallocate to reduce size; diff --git a/Zcode/tree/slot/slotCollection.hpp b/Zcode/tree/slot/slotCollection.hpp index f47ebff..da65931 100644 --- a/Zcode/tree/slot/slotCollection.hpp +++ b/Zcode/tree/slot/slotCollection.hpp @@ -41,7 +41,7 @@ struct slotCollection : private std::vector< std::shared_ptr< slot& cache ) { - const node_type xh = x.hash(), xabs = xh&node_type::maskpos; + const node_type xh = x.hash(), xabs = xh.pos(); auto slot_ptr = cache.find(xabs); // Cache::find returns a shared_ptr. // std::shared_ptr convertion to bool returns true iff the shared_ptr is valid. @@ -103,7 +103,7 @@ struct slotCollection : private std::vector< std::shared_ptr< slotput(xh); } @@ -137,14 +137,15 @@ struct slotCollection : private std::vector< std::shared_ptr< slot& cache) const { - node_type xh = x.hash(), xabs = xh&node_type::maskpos; + node_type xh = x.hash(), xabs = xh.pos(); auto slot_ptr = cache.find(xabs); if( ! slot_ptr ) @@ -203,7 +204,7 @@ struct slotCollection : private std::vector< std::shared_ptr< slotfind(xh); return node_it == slot_ptr->cend() ? 0 : 1; From 11f7c6430988317c0a5af97084f286dfabd10097 Mon Sep 17 00:00:00 2001 From: Roland Denis Date: Thu, 31 Aug 2017 17:51:26 +0200 Subject: [PATCH 7/9] Compliting compress test. --- Zcode/tree/slot/slotCollection.hpp | 3 ++- test/test_slot.cpp | 3 +++ test/test_slotCollection.cpp | 14 ++++++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Zcode/tree/slot/slotCollection.hpp b/Zcode/tree/slot/slotCollection.hpp index da65931..419c950 100644 --- a/Zcode/tree/slot/slotCollection.hpp +++ b/Zcode/tree/slot/slotCollection.hpp @@ -216,12 +216,13 @@ struct slotCollection : private std::vector< std::shared_ptr< slotclearFreeBits();}); } - //! suppress void Nodes, if any. + //! Suppress void Nodes, if any. //! update the count of leaves. inline void compress(node_type val=node_type::voidbit) { std::for_each(begin(), end(), [&val](auto& st){st->compress(val);}); } + //! empty all the slots. inline void clear() { diff --git a/test/test_slot.cpp b/test/test_slot.cpp index c68d47c..56ee02b 100644 --- a/test/test_slot.cpp +++ b/test/test_slot.cpp @@ -188,9 +188,12 @@ TYPED_TEST(SlotTest, compress) } slot.put(nodes); slot.setMark(node_type::voidbit); + // remove all odd nodes + EXPECT_EQ( slot.size(), 10 ); slot.compress(); EXPECT_EQ( slot.size(), 5 ); + for (std::size_t i=0; i<5; ++i) { node_type node{static_cast(2*i)}; diff --git a/test/test_slotCollection.cpp b/test/test_slotCollection.cpp index 7259b67..bedf283 100644 --- a/test/test_slotCollection.cpp +++ b/test/test_slotCollection.cpp @@ -104,9 +104,19 @@ TYPED_TEST(SlotCollectionTest, compress) using slot_type = slot; slotCollection SC{2, 10, 10, 11}; - SC.push_back(std::make_shared(10)); - + + const node_type n1{1}; + const node_type n2(2 + node_type::voidbit); + + SC.insert(n1); + SC.insert(n2); + + EXPECT_EQ( SC.nbNodes(), 2 ); + + SC[0]->setMark( node_type::voidbit ); // TODO: mark update should be triggered in slotCollection. SC.compress(); + + EXPECT_EQ( SC.nbNodes(), 1 ); } TYPED_TEST(SlotCollectionTest, nbNodes) From 0418300baac12121026f450d550c0019837d08f6 Mon Sep 17 00:00:00 2001 From: Roland Denis Date: Thu, 31 Aug 2017 17:54:18 +0200 Subject: [PATCH 8/9] Adding test for slotCollection.clear --- test/test_slotCollection.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/test_slotCollection.cpp b/test/test_slotCollection.cpp index bedf283..dc09b3a 100644 --- a/test/test_slotCollection.cpp +++ b/test/test_slotCollection.cpp @@ -256,3 +256,25 @@ TYPED_TEST(SlotCollectionTest, clearFreeBits) EXPECT_EQ( (*SC[0])[0].value, n1.hash().value ); } + +TYPED_TEST(SlotCollectionTest, clear) +{ + auto const dim = TestFixture::dim; + using value_type = typename TestFixture::value_type; + using node_type = Node; + using slot_type = slot; + + slotCollection SC{2, 10, 10, 11}; + + const node_type n1{1}, n2{2}, n3{3}; + + SC.insert(n1); + SC.insert(n2); + SC.insert(n3); + + EXPECT_EQ( SC.nbNodes(), 3 ); + + SC.clear(); + + EXPECT_EQ( SC.nbNodes(), 0 ); +} From fd825c8385a1f4643da5a46ef7ac43dd8ec15d91 Mon Sep 17 00:00:00 2001 From: Roland Denis Date: Fri, 1 Sep 2017 13:43:53 +0200 Subject: [PATCH 9/9] Removing unused method makeExtern --- Zcode/tree/slot/slotCollection.hpp | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Zcode/tree/slot/slotCollection.hpp b/Zcode/tree/slot/slotCollection.hpp index 419c950..f198d0b 100644 --- a/Zcode/tree/slot/slotCollection.hpp +++ b/Zcode/tree/slot/slotCollection.hpp @@ -229,15 +229,6 @@ struct slotCollection : private std::vector< std::shared_ptr< slotempty();}); } - //! make a copy (in a set) of the Nodes. - //! \param setN the set. - inline void makeExtern(SetNode& setN) - { - for (auto&st: this) - for(std::size_t i=0; isize(); ++i) - setN.insert(st[i]); - } - //! finalize: compute cumulsize (to allow rank function to work), and maximum //! size of slots; inline void finalize() @@ -269,6 +260,7 @@ struct slotCollection : private std::vector< std::shared_ptr< slotsetSlotRank(i); } } + //!return maximum size of slots. inline std::size_t maxSlotSize() const {