[DEV] add condition on add
This commit is contained in:
parent
8e3c9b253d
commit
703eb273be
223
etk/FlatTree.hpp
223
etk/FlatTree.hpp
@ -13,209 +13,11 @@
|
|||||||
|
|
||||||
namespace etk {
|
namespace etk {
|
||||||
template<class ETK_TREENODE_TYPE> class FlatTree {
|
template<class ETK_TREENODE_TYPE> class FlatTree {
|
||||||
public:
|
|
||||||
# if 0
|
|
||||||
//static const size_t npos = size_t(-1);
|
|
||||||
class Iterator {
|
|
||||||
private:
|
|
||||||
size_t m_current; //!< current Id on the vector
|
|
||||||
FlatTree<ETK_TREENODE_TYPE>* m_tree; //!< Pointer on the current element of the vectorBin
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* @brief Basic iterator constructor with no link with an etk::Vector
|
|
||||||
*/
|
|
||||||
Iterator():
|
|
||||||
m_current(0),
|
|
||||||
m_tree(null) {
|
|
||||||
// nothing to do ...
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @brief Recopy constructor on a specific etkVector.
|
|
||||||
* @param[in] _obj The Iterator that might be copy
|
|
||||||
*/
|
|
||||||
Iterator(const Iterator & _obj):
|
|
||||||
m_current(_obj.m_current),
|
|
||||||
m_tree(_obj.m_tree) {
|
|
||||||
// nothing to do ...
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @brief Assignation operator.
|
|
||||||
* @param[in] _otherIterator The Iterator that might be copy
|
|
||||||
* @return reference on the current Iterator
|
|
||||||
*/
|
|
||||||
Iterator& operator=(const Iterator & _otherIterator) {
|
|
||||||
m_current = _otherIterator.m_current;
|
|
||||||
m_tree = _otherIterator.m_tree;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @brief Basic destructor
|
|
||||||
*/
|
|
||||||
~Iterator() {
|
|
||||||
m_current = 0;
|
|
||||||
m_tree = null;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @brief basic boolean cast
|
|
||||||
* @return true if the element is present in the etkVector size
|
|
||||||
*/
|
|
||||||
operator bool () {
|
|
||||||
return (m_current < m_tree->size());
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @brief != Comparaison operator
|
|
||||||
* @param[in] _obj Object to compare
|
|
||||||
* @return true if the element are different
|
|
||||||
*/
|
|
||||||
bool operator!= (const Iterator& _obj) {
|
|
||||||
if ( m_tree != _obj.m_tree
|
|
||||||
|| m_current != _obj.m_current) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @brief == Comparaison operator.
|
|
||||||
* @param[in] _obj Object to compare.
|
|
||||||
* @return true if the element are identical.
|
|
||||||
*/
|
|
||||||
bool operator== (const Iterator& _obj) {
|
|
||||||
if ( m_tree == _obj.m_tree
|
|
||||||
&& m_current == _obj.m_current) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @brief Incremental operator
|
|
||||||
* @return Reference on the current iterator increment
|
|
||||||
*/
|
|
||||||
Iterator& operator++ () {
|
|
||||||
if ( m_tree != null
|
|
||||||
&& m_current < m_tree->size() )
|
|
||||||
{
|
|
||||||
m_current++;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @brief Decremental operator
|
|
||||||
* @return Reference on the current iterator decrement
|
|
||||||
*/
|
|
||||||
Iterator& operator-- () {
|
|
||||||
if ( m_tree != null
|
|
||||||
&& m_current > 0) {
|
|
||||||
m_current--;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @brief Incremental operator
|
|
||||||
* @return Reference on a new iterator and increment the other one
|
|
||||||
*/
|
|
||||||
Iterator operator++ (int32_t) {
|
|
||||||
Iterator it(*this);
|
|
||||||
++(*this);
|
|
||||||
return it;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @brief Decremental operator
|
|
||||||
* @return Reference on a new iterator and decrement the other one
|
|
||||||
*/
|
|
||||||
Iterator operator-- (int32_t) {
|
|
||||||
Iterator it(*this);
|
|
||||||
--(*this);
|
|
||||||
return it;
|
|
||||||
}
|
|
||||||
Iterator& operator-= (size_t _offset) {
|
|
||||||
m_current -= _offset;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
Iterator operator- (size_t _offset) const {
|
|
||||||
Iterator tmp(*this);
|
|
||||||
tmp -= _offset;
|
|
||||||
return tmp;
|
|
||||||
}
|
|
||||||
Iterator& operator-= (int _offset) {
|
|
||||||
m_current -= _offset;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
Iterator operator- (int _offset) const {
|
|
||||||
Iterator tmp(*this);
|
|
||||||
tmp -= _offset;
|
|
||||||
return tmp;
|
|
||||||
}
|
|
||||||
Iterator& operator-= (int64_t _offset) {
|
|
||||||
m_current -= _offset;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
Iterator operator- (int64_t _offset) const {
|
|
||||||
Iterator tmp(*this);
|
|
||||||
tmp -= _offset;
|
|
||||||
return tmp;
|
|
||||||
}
|
|
||||||
Iterator& operator+= (size_t _offset) {
|
|
||||||
m_current += _offset;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
Iterator operator+ (size_t _offset) const {
|
|
||||||
Iterator tmp(*this);
|
|
||||||
tmp += _offset;
|
|
||||||
return tmp;
|
|
||||||
}
|
|
||||||
Iterator& operator+= (int _offset) {
|
|
||||||
m_current += _offset;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
Iterator operator+ (int _offset) const {
|
|
||||||
Iterator tmp(*this);
|
|
||||||
tmp += _offset;
|
|
||||||
return tmp;
|
|
||||||
}
|
|
||||||
Iterator& operator+= (int64_t _offset) {
|
|
||||||
m_current += _offset;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
Iterator operator+ (int64_t _offset) const {
|
|
||||||
Iterator tmp(*this);
|
|
||||||
tmp += _offset;
|
|
||||||
return tmp;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @brief Get reference on the current Element
|
|
||||||
* @return the reference on the current Element
|
|
||||||
*/
|
|
||||||
ETK_TREENODE_TYPE* operator-> () const {
|
|
||||||
return &m_tree->get(m_current);
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @brief Get reference on the current Element
|
|
||||||
* @return the reference on the current Element
|
|
||||||
*/
|
|
||||||
ETK_TREENODE_TYPE& operator* () const {
|
|
||||||
return m_tree->get(m_current);
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
Iterator(const FlatTree<ETK_TREENODE_TYPE> * _obj, int32_t _pos):
|
|
||||||
m_current(_pos),
|
|
||||||
m_tree(const_cast<FlatTree<ETK_TREENODE_TYPE> *>(_obj)) {
|
|
||||||
// nothing to do ...
|
|
||||||
}
|
|
||||||
size_t getCurrent() const {
|
|
||||||
return m_current;
|
|
||||||
}
|
|
||||||
friend class FlatTree;
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
private:
|
private:
|
||||||
etk::Vector<ememory::SharedPtr<etk::TreeNode<ETK_TREENODE_TYPE>>> m_data;
|
etk::Vector<ememory::SharedPtr<etk::TreeNode<ETK_TREENODE_TYPE>>> m_data;
|
||||||
public:
|
public:
|
||||||
FlatTree() {
|
FlatTree() {
|
||||||
|
|
||||||
}
|
|
||||||
void setRoot(const ememory::SharedPtr<etk::TreeNode<ETK_TREENODE_TYPE>>& _root) {
|
|
||||||
m_data.clear();
|
|
||||||
append(_root);
|
|
||||||
}
|
}
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return m_data.size();
|
return m_data.size();
|
||||||
@ -238,6 +40,16 @@ namespace etk {
|
|||||||
auto operator[] (size_t _pos) const {
|
auto operator[] (size_t _pos) const {
|
||||||
return m_data[_pos];
|
return m_data[_pos];
|
||||||
}
|
}
|
||||||
|
void setRoot(const ememory::SharedPtr<etk::TreeNode<ETK_TREENODE_TYPE>>& _root) {
|
||||||
|
m_data.clear();
|
||||||
|
append(_root);
|
||||||
|
}
|
||||||
|
void setRoot(const ememory::SharedPtr<etk::TreeNode<ETK_TREENODE_TYPE>>& _root,
|
||||||
|
const etk::Function<void(ETK_TREENODE_TYPE*)>& _needAdd,
|
||||||
|
const etk::Function<void(ETK_TREENODE_TYPE*)>& _needAddChild) {
|
||||||
|
m_data.clear();
|
||||||
|
append(_root, _needAdd, _needAddChild);
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
void append(const ememory::SharedPtr<etk::TreeNode<ETK_TREENODE_TYPE>>& _node) {
|
void append(const ememory::SharedPtr<etk::TreeNode<ETK_TREENODE_TYPE>>& _node) {
|
||||||
if (_node == null) {
|
if (_node == null) {
|
||||||
@ -248,5 +60,20 @@ namespace etk {
|
|||||||
append(it);
|
append(it);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void append(const ememory::SharedPtr<etk::TreeNode<ETK_TREENODE_TYPE>>& _node,
|
||||||
|
const etk::Function<void(ETK_TREENODE_TYPE*)>& _needAdd,
|
||||||
|
const etk::Function<void(ETK_TREENODE_TYPE*)>& _needAddChild) {
|
||||||
|
if (_node == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (_needAdd(&_node->getData()) == true) {
|
||||||
|
m_data.pushBack(_node);
|
||||||
|
}
|
||||||
|
if (_needAddChild(&_node->getData()) == true) {
|
||||||
|
for (auto& it:_node->getChilds()) {
|
||||||
|
append(it);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user