-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split EPTree and EPTreeIterator to different files (#65)
- Loading branch information
Showing
4 changed files
with
113 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (C) 2024 koolkdev | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
*/ | ||
|
||
#include "eptree_iterator.h" | ||
|
||
#include "free_blocks_allocator.h" | ||
|
||
EPTreeIterator& EPTreeIterator::operator++() { | ||
assert(!is_end()); | ||
auto rnode = nodes_.rbegin(); | ||
while ((++rnode->iterator).is_end()) { | ||
if (++rnode == nodes_.rend()) { | ||
while (--rnode != nodes_.rbegin()) | ||
--rnode->iterator; | ||
return *this; // end | ||
} | ||
} | ||
uint32_t node_block_number = (*rnode->iterator).value; | ||
for (auto node = rnode.base(); node != nodes_.end(); ++node) { | ||
*node = {allocator_->LoadAllocatorBlock(node_block_number)}; | ||
node->iterator = node->node->begin(); | ||
node_block_number = (*node->iterator).value; | ||
} | ||
return *this; | ||
} | ||
|
||
EPTreeIterator& EPTreeIterator::operator--() { | ||
assert(!is_begin()); | ||
auto rnode = nodes_.rbegin(); | ||
for (; rnode->iterator.is_begin(); rnode++) { | ||
if (rnode == nodes_.rend()) | ||
return *this; // begin | ||
} | ||
uint32_t node_block_number = (*--rnode->iterator).value; | ||
for (auto node = rnode.base(); node != nodes_.end(); ++node) { | ||
*node = {allocator_->LoadAllocatorBlock(node_block_number)}; | ||
node->iterator = node->node->end(); | ||
node_block_number = (*--node->iterator).value; | ||
} | ||
return *this; | ||
} | ||
|
||
EPTreeIterator EPTreeIterator::operator++(int) { | ||
EPTreeIterator tmp(*this); | ||
++(*this); | ||
return tmp; | ||
} | ||
|
||
EPTreeIterator EPTreeIterator::operator--(int) { | ||
EPTreeIterator tmp(*this); | ||
--(*this); | ||
return tmp; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (C) 2024 koolkdev | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <iterator> | ||
#include <memory> | ||
#include <vector> | ||
|
||
#include "rtree.h" | ||
|
||
class FreeBlocksAllocator; | ||
|
||
class EPTreeIterator { | ||
public: | ||
using iterator_category = std::bidirectional_iterator_tag; | ||
using difference_type = int; | ||
using value_type = RTree::iterator::value_type; | ||
|
||
using reference = RTree::iterator::reference; | ||
|
||
using node_info = node_iterator_info<RTree>; | ||
|
||
EPTreeIterator() = default; | ||
|
||
EPTreeIterator(FreeBlocksAllocator* allocator, std::vector<node_info> nodes) | ||
: allocator_(allocator), nodes_(std::move(nodes)) {} | ||
|
||
reference operator*() const { return *nodes_.back().iterator; } | ||
|
||
EPTreeIterator& operator++(); | ||
EPTreeIterator& operator--(); | ||
EPTreeIterator operator++(int); | ||
EPTreeIterator operator--(int); | ||
|
||
bool operator==(const EPTreeIterator& other) const { return nodes_.back().iterator == other.nodes_.back().iterator; } | ||
|
||
std::vector<node_info>& nodes() { return nodes_; }; | ||
const std::vector<node_info>& nodes() const { return nodes_; }; | ||
|
||
bool is_begin() const { | ||
return std::ranges::all_of(nodes_, [](const node_info& node) { return node.iterator.is_begin(); }); | ||
} | ||
bool is_end() const { return nodes_.back().iterator.is_end(); } | ||
|
||
private: | ||
FreeBlocksAllocator* allocator_; | ||
|
||
std::vector<node_info> nodes_; | ||
}; | ||
static_assert(std::bidirectional_iterator<EPTreeIterator>); |