Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Zcode/tree/node/family.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ template<typename Node_type>
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 ?
Expand Down
24 changes: 19 additions & 5 deletions Zcode/tree/node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ struct Node: public definitions<Dim, Value>
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 Node pos() const
{
return value & maskpos;
}

inline Node operator<<(std::size_t i) const
{
return {static_cast<value_type>(value<<i)};
Expand Down Expand Up @@ -212,10 +224,6 @@ struct Node: public definitions<Dim, Value>
return *this;
}

inline bool operator&(Node<dim, value_type> const& node) const
{
return value&node.value;
}

};

Expand All @@ -234,7 +242,7 @@ std::ostream& operator<<(std::ostream &os, const Node<dim, value_type> &node)

for( int i = size-1; i >= 0; i-- )
{
if(node&(IntOne<<i))
if(node.value&(IntOne<<i).value)
s+='1';
else
s+='0';
Expand Down Expand Up @@ -270,6 +278,12 @@ inline Node<dim, value_type> operator&(Node<dim, value_type> const& node, value_
return {static_cast<value_type>(node.value&value)};
}

template <std::size_t dim, typename value_type>
inline Node<dim, value_type> operator&( value_type const& value, Node<dim, value_type> const& node )
{
return {static_cast<value_type>(value&node.value)};
}

template <std::size_t dim, typename value_type>
inline bool operator<(Node<dim, value_type> const& node1, Node<dim, value_type> const& node2)
{
Expand Down
25 changes: 12 additions & 13 deletions Zcode/tree/slot/slot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ struct slot: private std::vector< Node<dim, node_value_type> >

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]};
Expand Down Expand Up @@ -251,13 +250,13 @@ struct slot: private std::vector< Node<dim, node_value_type> >
for(std::size_t i=0; i<nc-1; ++i)
{
slarray[i].insert(slarray[i].begin(), begin()+i*sizec, begin()+(i+1)*sizec);
slarray[i].s1 = (*this)[i*sizec].value&maskpos;
slarray[i].s2 = (*this)[(i+1)*sizec-1].value&maskpos;
slarray[i].s1 = (*this)[i*sizec].pos();
slarray[i].s2 = (*this)[(i+1)*sizec-1].pos();
}
std::size_t i = nc-1;
slarray[i].insert(slarray[i].begin(), begin()+i*sizec, end());
slarray[i].s1 = (*this)[i*sizec].value&maskpos;
slarray[i].s2 = (*this)[size()-1].value&maskpos;
slarray[i].s1 = (*this)[i*sizec].pos();
slarray[i].s2 = (*this)[size()-1].pos();
return slarray;
}

Expand Down Expand Up @@ -289,7 +288,7 @@ struct slot: private std::vector< Node<dim, node_value_type> >
//! 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;
Expand All @@ -307,10 +306,10 @@ struct slot: private std::vector< Node<dim, node_value_type> >
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.
Expand Down Expand Up @@ -349,14 +348,14 @@ struct slot: private std::vector< Node<dim, node_value_type> >
}

//! 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;
}
Expand Down Expand Up @@ -403,8 +402,8 @@ std::ostream& operator<<(std::ostream& os, const slot<dim, node_value_type>& 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;
}
75 changes: 21 additions & 54 deletions Zcode/tree/slot/slotCollection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@ struct slotCollection : private std::vector< std::shared_ptr< slot<dim, node_val
using parent = std::vector<std::shared_ptr<slot_type>>;
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<std::size_t, definition::nlevels+1>;

Expand All @@ -45,7 +41,7 @@ struct slotCollection : private std::vector< std::shared_ptr< slot<dim, node_val
{
inline bool operator()(const node_type n1,const node_type n2) const
{
return (n1&node_type::maskpos)<(n2&node_type::maskpos);
return n1.pos() < n2.pos();
}
};

Expand Down Expand Up @@ -85,34 +81,12 @@ struct slotCollection : private std::vector< std::shared_ptr< slot<dim, node_val
tbb::parallel_for_each(cbegin(), cend(), [&array](auto &sl){ sl->copyInArray(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<slot_type>(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
/// \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.
inline void insert( node_type x, Cache<dim, node_value_type>& 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.
Expand All @@ -129,7 +103,7 @@ struct slotCollection : private std::vector< std::shared_ptr< slot<dim, node_val
/// \param x the node to be inserted.
inline void insert( node_type x )
{
const node_type xh = x.hash(), xabs = xh&node_type::maskpos;
const node_type xh = x.hash(), xabs = xh.pos();
const auto slot_ptr = (*this)[findSlot(xabs, 0, size()-1)];
slot_ptr->put(xh);
}
Expand Down Expand Up @@ -163,15 +137,15 @@ struct slotCollection : private std::vector< std::shared_ptr< slot<dim, node_val
//! \param x Node *not* *hashed*
inline auto ubound(node_type x) const
{
typename node_type::type maskpos = node_type::maskpos;
return (*this)[findSlot(x.hash()&maskpos, 0, size()-1)];
return (*this)[findSlot(x.hash().pos(), 0, size()-1)];
}

//! return a pointer to a slot which *possibly* contains a Node.
//! \param x Node *hashed*
inline auto ubound_hashed(node_type x) const
{
typename node_type::type maskpos = node_type::maskpos;
return (*this)[findSlot(x&maskpos, 0, size()-1)];
assert( x.isHashed() );
return (*this)[findSlot(x.pos(), 0, size()-1)];
}

//! Given a Node, find his slot.
Expand Down Expand Up @@ -200,14 +174,14 @@ struct slotCollection : private std::vector< std::shared_ptr< slot<dim, node_val
}

/// Test if a Node exists within this slotCollection, using a cache.
/// \param x Node non hashed.
/// \param cach cache updated.
/// \param x Node non hashed.
/// \param[in,out] cache Used cache.
/// \returns the number of corresponding found node (either 0 or 1).
/// \note we do not directly check if the Node is really non hashed, but
/// this is checked in "xh=hash(x)".
inline std::size_t count(node_type x, Cache<dim, node_value_type>& 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 )
Expand All @@ -230,53 +204,45 @@ struct slotCollection : private std::vector< std::shared_ptr< slot<dim, node_val
/// this is checked in "xh=hash(x)".
inline std::size_t count(node_type x) const
{
node_type xh = x.hash(), xabs = xh&node_type::maskpos;
node_type xh = x.hash(), xabs = xh.pos();
const auto slot_ptr = (*this)[findSlot(xabs, 0, size()-1)];
const auto node_it = slot_ptr->find(xh);
return node_it == slot_ptr->cend() ? 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.
//! 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()
{
std::for_each(begin(), end(), [](auto& st){st->empty();});
}

//! 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; i<st->size(); ++i)
setN.insert(st[i]);
}

//! finalize: compute cumulsize (to allow rank function to work), and maximum
//! size of slots;
inline void finalize()
{
std::size_t wmax=0;

(*this)[0]->setStartRank(0);
(*this)[0]->setSlotrank(0);
(*this)[0]->setSlotRank(0);
for(std::size_t i=0; i<size(); ++i)
{
if (i > 0)
(*this)[i]->setStartRank((*this)[i-1]->startRank()+wmax);
wmax = (*this)[i]->size();
(*this)[i]->setSlotrank(i);
(*this)[i]->setSlotRank(i);
}
}

Expand All @@ -285,15 +251,16 @@ struct slotCollection : private std::vector< std::shared_ptr< slot<dim, node_val
{
std::size_t wmax=0;
(*this)[0]->setStartRank(0);
(*this)[0]->setSlotrank(0);
(*this)[0]->setSlotRank(0);
for(std::size_t i=0; i<size(); ++i)
{
if(i>0)
(*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.
inline std::size_t maxSlotSize() const
{
Expand Down
7 changes: 5 additions & 2 deletions test/test_slot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<value_type>(2*i)};
Expand Down Expand Up @@ -446,7 +449,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;
Expand All @@ -457,7 +460,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<value_type>(i)};
Expand Down
Loading