From 1eaefde012ca2457bf7775467eda92b6375e90e5 Mon Sep 17 00:00:00 2001 From: Ivan Morozov Date: Fri, 16 Sep 2022 22:54:30 +0300 Subject: [PATCH] Added experimental red-black tree output iterator. Fixed compoundfile Delete() --- Common/cfcpp/RBTree/rbtree.cpp | 40 +++++++++++++++++++++++++++++----- Common/cfcpp/RBTree/rbtree.h | 20 ++++++++--------- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/Common/cfcpp/RBTree/rbtree.cpp b/Common/cfcpp/RBTree/rbtree.cpp index c3e0047f20..e4c193e05b 100644 --- a/Common/cfcpp/RBTree/rbtree.cpp +++ b/Common/cfcpp/RBTree/rbtree.cpp @@ -423,12 +423,40 @@ void RBTree::DoVisitTreeNodes(Action action, PIRBNode walker) } } -RBTree::iterator::iterator(RBTree &tree, bool end) +RBTree::iterator::iterator(RBTree *tree) : tree(tree) { - Action inserter = [&] (PIRBNode pNode) + if (tree == nullptr) + return; + + auto walker = tree->getRoot(); + while (walker != nullptr) { - heap.push_back(pNode); - }; - tree.VisitTreeNodes(inserter); - current = end ? heap.end() : heap.begin(); + current = walker; + walker = walker->getLeft(); + } +} + +RBTree::iterator &RBTree::iterator::operator++() +{ + if (current->getRight()) + { + current = current->getRight(); + auto walker = tree->getRoot(); + while (walker != nullptr) + { + current = walker; + walker = walker->getLeft(); + } + } + else if (current->getParent()) + { + current = current->getParent(); + return *this; + } + else + { + current.reset(); + } + + return *this; } diff --git a/Common/cfcpp/RBTree/rbtree.h b/Common/cfcpp/RBTree/rbtree.h index 7c8fb93f01..60aaf87b3e 100644 --- a/Common/cfcpp/RBTree/rbtree.h +++ b/Common/cfcpp/RBTree/rbtree.h @@ -52,23 +52,21 @@ private: void DoVisitTreeNodes(Action action, PIRBNode walker); public: - // todo this from C# and it's weak realization - class iterator : public std::iterator + + class iterator : public std::iterator { - std::list heap; - std::list::iterator current; + PIRBNode current; + RBTree* tree; public: - iterator(RBTree &tree, bool end = false); - inline iterator& operator++() {++current; return *this;} - inline iterator& operator--() {--current; return *this;} + iterator(RBTree *tree); + iterator& operator++(); inline bool operator==(const iterator &other) const {return current == other.current;} inline bool operator!=(const iterator &other) const {return current != other.current;} - inline PIRBNode operator*() {return current->lock();} - inline iterator end() {current = heap.end(); return *this;} + inline PIRBNode operator*() {return current;} }; - iterator&& begin() {return std::move(iterator(*this));} - iterator&& end() {return std::move(iterator(*this).end());} + iterator begin() {return iterator(this);} + iterator end() {return iterator(nullptr);} private: PIRBNode root;