Rename to conform to duckhandy and add some of the missing files.
This commit is contained in:
parent
74a4e4fb2e
commit
f085f3c3d2
6 changed files with 360 additions and 4 deletions
235
include/duckhandy/implem/tree_iterator.inl
Normal file
235
include/duckhandy/implem/tree_iterator.inl
Normal file
|
@ -0,0 +1,235 @@
|
|||
namespace duckmem {
|
||||
namespace Implem {
|
||||
///---------------------------------------------------------------------
|
||||
///---------------------------------------------------------------------
|
||||
template <typename P>
|
||||
bool TreeIterator_base<P>::Exhausted() const {
|
||||
return m_stack.empty();
|
||||
}
|
||||
|
||||
///---------------------------------------------------------------------
|
||||
///---------------------------------------------------------------------
|
||||
template <typename P>
|
||||
TreeIterator_base<P>::TreeIterator_base (const TreeIterator_base& parOther) :
|
||||
m_stack(parOther.m_stack)
|
||||
{
|
||||
}
|
||||
|
||||
///---------------------------------------------------------------------
|
||||
///---------------------------------------------------------------------
|
||||
template <typename P>
|
||||
template <typename P1>
|
||||
TreeIterator_base<P>::TreeIterator_base (const TreeIterator_base<P1>& parOther) {
|
||||
typename TreeIterator_base<P1>::StackType otherStackCopy(parOther.m_stack);
|
||||
std::vector<P> localCopy;
|
||||
localCopy.reserve(parOther.m_stack.size());
|
||||
while (not otherStackCopy.empty()) {
|
||||
P convertedItem = otherStackCopy.top();
|
||||
localCopy.push_back(convertedItem);
|
||||
otherStackCopy.pop();
|
||||
}
|
||||
m_stack.reserve(parOther.m_stack.capacity());
|
||||
for (typename std::vector<P>::reverse_iterator itRev = localCopy.rbegin(), itRevEnd = localCopy.rend(); itRev != itRevEnd; ++itRev) {
|
||||
Assert(m_stack.capacity() > m_stack.size());
|
||||
m_stack.push(*itRev);
|
||||
}
|
||||
}
|
||||
|
||||
///---------------------------------------------------------------------
|
||||
///---------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
typename TreeIterator_const_layer<T, N, false>::reference TreeIterator_const_layer<T, N, false>::operator* () {
|
||||
AssertRelease(not this->Exhausted());
|
||||
return this->m_stack.top()->content;
|
||||
}
|
||||
|
||||
///---------------------------------------------------------------------
|
||||
///---------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
typename TreeIterator_const_layer<T, N, false>::const_reference TreeIterator_const_layer<T, N, false>::operator* () const {
|
||||
AssertRelease(not this->Exhausted());
|
||||
return this->m_stack.top()->content;
|
||||
}
|
||||
|
||||
///---------------------------------------------------------------------
|
||||
///---------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
typename TreeIterator_const_layer<T, N, false>::pointer TreeIterator_const_layer<T, N, false>::operator-> () {
|
||||
AssertRelease(not this->Exhausted());
|
||||
return &this->m_stack.top()->content;
|
||||
}
|
||||
|
||||
///---------------------------------------------------------------------
|
||||
///---------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
typename TreeIterator_const_layer<T, N, false>::const_pointer TreeIterator_const_layer<T, N, false>::operator-> () const {
|
||||
AssertRelease(not this->Exhausted());
|
||||
return &this->m_stack.top()->content;
|
||||
}
|
||||
|
||||
///---------------------------------------------------------------------
|
||||
///---------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
N* TreeIterator_const_layer<T, N, false>::GetPointer() {
|
||||
AssertRelease(not this->Exhausted());
|
||||
return this->m_stack.top();
|
||||
}
|
||||
|
||||
///---------------------------------------------------------------------
|
||||
///---------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
const N* TreeIterator_const_layer<T, N, false>::GetPointer() const {
|
||||
AssertRelease(not this->Exhausted());
|
||||
return this->m_stack.top();
|
||||
}
|
||||
|
||||
///---------------------------------------------------------------------
|
||||
///---------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
typename TreeIterator_const_layer<T, N, true>::const_reference TreeIterator_const_layer<T, N, true>::operator* () const {
|
||||
AssertRelease(not this->Exhausted());
|
||||
return this->m_stack.top()->content;
|
||||
}
|
||||
|
||||
///---------------------------------------------------------------------
|
||||
///---------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
typename TreeIterator_const_layer<T, N, true>::const_pointer TreeIterator_const_layer<T, N, true>::operator-> () const {
|
||||
AssertRelease(not this->Exhausted());
|
||||
return &this->m_stack.top()->content;
|
||||
}
|
||||
|
||||
///---------------------------------------------------------------------
|
||||
///---------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
const N* TreeIterator_const_layer<T, N, true>::GetPointer() const {
|
||||
AssertRelease(not this->Exhausted());
|
||||
return this->m_stack.top();
|
||||
}
|
||||
} //namespace Implem
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
TreeIterator<T, N>::TreeIterator() {
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
TreeIterator<T, N>::TreeIterator (const TreeIterator& parOther) :
|
||||
parent_type(parOther)
|
||||
{
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
template <typename T1>
|
||||
TreeIterator<T, N>::TreeIterator (const TreeIterator<T1, N>& parOther) :
|
||||
parent_type(parOther)
|
||||
{
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
template <typename S>
|
||||
TreeIterator<T, N>::TreeIterator (S parCopyStackBottomUp, size_type parStackLen, size_type parMaxDepthHint) {
|
||||
AssertRelease(parStackLen > 0);
|
||||
this->m_stack.reserve(std::max(parStackLen, parMaxDepthHint));
|
||||
typename StackType::value_type prevNode = *parCopyStackBottomUp;
|
||||
++parCopyStackBottomUp;
|
||||
this->m_stack.push(prevNode);
|
||||
|
||||
for (size_type z = 1; z < parStackLen; ++z) {
|
||||
typename StackType::value_type currNode = *parCopyStackBottomUp;
|
||||
if (prevNode->left == currNode) {
|
||||
Assert(this->m_stack.capacity() > this->m_stack.size());
|
||||
this->m_stack.push(currNode);
|
||||
}
|
||||
else {
|
||||
//If you get this assertion make sure the iterator you are
|
||||
//passing in is reversed (ie: from leaf to root)
|
||||
AssertRelease(currNode == prevNode->right);
|
||||
}
|
||||
|
||||
prevNode = currNode;
|
||||
++parCopyStackBottomUp;
|
||||
}
|
||||
Assert(not this->Exhausted());
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
TreeIterator<T, N>::TreeIterator (NodeTypePointer parRoot, size_type parMaxDepthHint) {
|
||||
if (parMaxDepthHint > 0)
|
||||
this->m_stack.reserve(parMaxDepthHint);
|
||||
RecurseLeft(parRoot);
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
TreeIterator<T, N>::~TreeIterator() {
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///Post-increment
|
||||
///-------------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
TreeIterator<T, N> TreeIterator<T, N>::operator++ (int) {
|
||||
AssertRelease(not this->Exhausted());
|
||||
TreeIterator<T, N> retVal = *this;
|
||||
++(*this);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///Pre-increment
|
||||
///-------------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
TreeIterator<T, N>& TreeIterator<T, N>::operator++() {
|
||||
AssertRelease(not this->Exhausted());
|
||||
NodeTypePointer currNode = this->m_stack.top();
|
||||
#if defined(ASSERTIONSENABLED)
|
||||
const size_type stackCapacity = this->m_stack.capacity();
|
||||
#endif
|
||||
this->m_stack.pop();
|
||||
#if defined(ASSERTIONSENABLED)
|
||||
//It shouldn't normally happen, but it's just to make sure
|
||||
Assert(stackCapacity == this->m_stack.capacity());
|
||||
#endif
|
||||
RecurseLeft(currNode->right);
|
||||
return *this;
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
const TreeIterator<T, N>& TreeIterator<T, N>::operator= (const TreeIterator& parOther) {
|
||||
this->m_stack = parOther.m_stack;
|
||||
Assert(this->m_stack.capacity() >= parOther.m_stack.capacity());
|
||||
return *this;
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
bool TreeIterator<T, N>::operator== (const TreeIterator& parOther) const {
|
||||
return this->m_stack.size() == parOther.m_stack.size() and (this->m_stack.empty() or parOther.m_stack.top() == this->m_stack.top());
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
template <typename T, typename N>
|
||||
void TreeIterator<T, N>::RecurseLeft (NodeTypePointer parFrom) {
|
||||
NodeTypePointer currNode = parFrom;
|
||||
while (NULL != currNode) {
|
||||
Assert(this->m_stack.capacity() > this->m_stack.size());
|
||||
this->m_stack.push(currNode);
|
||||
currNode = currNode->left;
|
||||
}
|
||||
}
|
||||
} //namespace duckmem
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef idDD2D1A57ABEB4BEEA0F7E46C347D1637
|
||||
#define idDD2D1A57ABEB4BEEA0F7E46C347D1637
|
||||
|
||||
#include "ScapegoatTree.hpp"
|
||||
#include "scapegoat_tree.hpp"
|
||||
|
||||
namespace duckmem {
|
||||
template <typename K, typename V, typename Hasher>
|
||||
|
@ -29,6 +29,6 @@ namespace duckmem {
|
|||
};
|
||||
} //namespace duckmem
|
||||
|
||||
#include "ScapegoatMap.inl"
|
||||
#include "implem/scapegoat_map.inl"
|
||||
|
||||
#endif
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef id79CEDB2530B54204A6BEDCBE0B767EA1
|
||||
#define id79CEDB2530B54204A6BEDCBE0B767EA1
|
||||
|
||||
#include "TreeIterator.hpp"
|
||||
#include "tree_iterator.hpp"
|
||||
#include "IteratorOnPtr.hpp"
|
||||
#include "SmallObject.hpp"
|
||||
#include "DuckMaths/Functions.hpp"
|
||||
|
@ -126,6 +126,6 @@ namespace duckmem {
|
|||
};
|
||||
} //namespace duckmem
|
||||
|
||||
#include "ScapegoatTree.inl"
|
||||
#include "implem/scapegoat_tree.inl"
|
||||
|
||||
#endif
|
121
include/duckhandy/tree_iterator.hpp
Normal file
121
include/duckhandy/tree_iterator.hpp
Normal file
|
@ -0,0 +1,121 @@
|
|||
#ifndef id6109D5EDE99D43C4909F084A231BF2C2
|
||||
#define id6109D5EDE99D43C4909F084A231BF2C2
|
||||
|
||||
#include "DuckCore/Stack.hpp"
|
||||
#include "DuckCore/Helpers.hpp"
|
||||
#include <vector>
|
||||
#include <cstddef>
|
||||
|
||||
namespace duckmem {
|
||||
namespace Implem {
|
||||
template <typename P>
|
||||
class TreeIterator_base {
|
||||
template <typename P1> friend class TreeIterator_base;
|
||||
public:
|
||||
explicit TreeIterator_base ( void ) {}
|
||||
TreeIterator_base ( const TreeIterator_base& parOther );
|
||||
template <typename P1>
|
||||
explicit TreeIterator_base ( const TreeIterator_base<P1>& parOther );
|
||||
~TreeIterator_base ( void ) {}
|
||||
protected:
|
||||
typedef duckcore::Stack<std::vector<P> > StackType;
|
||||
typedef size_t size_type;
|
||||
typedef ptrdiff_t difference_type;
|
||||
bool Exhausted ( void ) const;
|
||||
StackType m_stack;
|
||||
};
|
||||
|
||||
template <typename T, typename N, bool Const>
|
||||
class TreeIterator_const_layer;
|
||||
template <typename T, typename N>
|
||||
class TreeIterator_const_layer<T, N, true> : protected TreeIterator_base<const N*> {
|
||||
template <typename T1, typename N1, bool Const1> friend class TreeIterator_const_layer;
|
||||
typedef TreeIterator_base<const N*> parent_type;
|
||||
public:
|
||||
typedef const T* pointer;
|
||||
typedef const T* const_pointer;
|
||||
typedef const T& reference;
|
||||
typedef const T& const_reference;
|
||||
typedef typename parent_type::size_type size_type;
|
||||
typedef typename parent_type::difference_type difference_type;
|
||||
typedef N NodeType;
|
||||
typedef const N* NodeTypePointer;
|
||||
enum { IS_CONST = 1 };
|
||||
TreeIterator_const_layer ( void ) {}
|
||||
TreeIterator_const_layer ( const TreeIterator_const_layer& parOther ) : parent_type(parOther) {}
|
||||
template <typename T1, bool C1>
|
||||
explicit TreeIterator_const_layer ( const TreeIterator_const_layer<T1, N, C1>& parOther ) : parent_type(parOther) {}
|
||||
const_reference operator* ( void ) const;
|
||||
const_pointer operator-> ( void ) const;
|
||||
const N* GetPointer ( void ) const;
|
||||
protected:
|
||||
typedef typename parent_type::StackType StackType;
|
||||
};
|
||||
template <typename T, typename N>
|
||||
class TreeIterator_const_layer<T, N, false> : protected TreeIterator_base<N*> {
|
||||
template <typename T1, typename N1, bool Const1> friend class TreeIterator_const_layer;
|
||||
typedef TreeIterator_base<N*> parent_type;
|
||||
public:
|
||||
typedef T* pointer;
|
||||
typedef const T* const_pointer;
|
||||
typedef T& reference;
|
||||
typedef const T& const_reference;
|
||||
typedef typename parent_type::size_type size_type;
|
||||
typedef typename parent_type::difference_type difference_type;
|
||||
typedef N NodeType;
|
||||
typedef N* NodeTypePointer;
|
||||
enum { IS_CONST = 0 };
|
||||
TreeIterator_const_layer ( void ) {}
|
||||
TreeIterator_const_layer ( const TreeIterator_const_layer& parOther ) : parent_type(parOther) {}
|
||||
template <typename T1, bool C1>
|
||||
explicit TreeIterator_const_layer ( const TreeIterator_const_layer<T1, N, C1>& parOther ) : parent_type(parOther) {}
|
||||
reference operator* ( void );
|
||||
const_reference operator* ( void ) const;
|
||||
pointer operator-> ( void );
|
||||
const_pointer operator-> ( void ) const;
|
||||
const N* GetPointer ( void ) const;
|
||||
N* GetPointer ( void );
|
||||
protected:
|
||||
typedef typename parent_type::StackType StackType;
|
||||
};
|
||||
} //namespace Implem
|
||||
|
||||
template <typename T, typename N>
|
||||
class TreeIterator : public Implem::TreeIterator_const_layer<T, N, Loki::TypeTraits<T>::isConst> {
|
||||
typedef Implem::TreeIterator_const_layer<T, N, Loki::TypeTraits<T>::isConst> parent_type;
|
||||
typedef typename parent_type::NodeTypePointer NodeTypePointer;
|
||||
typedef typename parent_type::NodeType NodeType;
|
||||
typedef typename parent_type::StackType StackType;
|
||||
public:
|
||||
typedef T value_type;
|
||||
typedef std::forward_iterator_tag iterator_category;
|
||||
typedef typename parent_type::difference_type difference_type;
|
||||
typedef typename parent_type::size_type size_type;
|
||||
typedef typename parent_type::pointer pointer;
|
||||
typedef typename parent_type::const_pointer const_pointer;
|
||||
typedef typename parent_type::reference reference;
|
||||
typedef typename parent_type::const_reference const_reference;
|
||||
|
||||
TreeIterator ( void );
|
||||
TreeIterator ( const TreeIterator& parOther );
|
||||
TreeIterator ( NodeTypePointer parRoot, size_type parMaxDepthHint );
|
||||
template <typename S>
|
||||
TreeIterator ( S parCopyStackBottomUp, size_type parStackLen, size_type parMaxDepthHint );
|
||||
template <typename T1>
|
||||
TreeIterator ( const TreeIterator<T1, N>& parOther );
|
||||
~TreeIterator ( void );
|
||||
|
||||
const TreeIterator& operator= ( const TreeIterator& parOther );
|
||||
bool operator== ( const TreeIterator& parOther ) const;
|
||||
bool operator!= ( const TreeIterator& parOther ) const { return not (*this == parOther); }
|
||||
TreeIterator& operator++ ( void ); //pre
|
||||
TreeIterator operator++ ( int ); //post
|
||||
|
||||
private:
|
||||
void RecurseLeft ( NodeTypePointer parFrom );
|
||||
};
|
||||
} //namespace duckmem
|
||||
|
||||
#include "implem/tree_iterator.inl"
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue