Remove NodeStack:

*  This gets all parts of SHAMap using a single type of stack.
*  This paves the way for more code sharing.
This commit is contained in:
Howard Hinnant
2016-01-12 15:23:22 -05:00
committed by Nik Bougalis
parent 278f679bb1
commit 2f9f217c11
2 changed files with 29 additions and 30 deletions

View File

@@ -79,9 +79,6 @@ using MissingNodeHandler = std::function <void (std::uint32_t refNum)>;
class SHAMap
{
private:
using NodeStack = std::stack<std::pair<SHAMapAbstractNode*, SHAMapNodeID>,
std::vector<std::pair<SHAMapAbstractNode*, SHAMapNodeID>>>;
Family& f_;
beast::Journal journal_;
std::uint32_t seq_;
@@ -243,7 +240,7 @@ private:
/** Walk towards the specified id, returning the node. Caller must check
if the return is nullptr, and if not, if the node->peekItem()->key() == id */
SHAMapTreeNode*
walkTowardsKey(uint256 const& id, NodeStack* stack = nullptr) const;
walkTowardsKey(uint256 const& id, SharedPtrNodeStack* stack = nullptr) const;
/** Return nullptr if key not found */
SHAMapTreeNode*
findKey(uint256 const& id) const;
@@ -263,7 +260,8 @@ private:
writeNode(NodeObjectType t, std::uint32_t seq,
std::shared_ptr<SHAMapAbstractNode> node) const;
SHAMapTreeNode* firstBelow (SHAMapAbstractNode*, NodeStack& stack) const;
SHAMapTreeNode* firstBelow (std::shared_ptr<SHAMapAbstractNode>,
SharedPtrNodeStack& stack) const;
// Simple descent
// Get a child of the specified node
@@ -291,8 +289,8 @@ private:
bool hasInnerNode (SHAMapNodeID const& nodeID, SHAMapHash const& hash) const;
bool hasLeafNode (uint256 const& tag, SHAMapHash const& hash) const;
SHAMapItem const* peekFirstItem(NodeStack& stack) const;
SHAMapItem const* peekNextItem(uint256 const& id, NodeStack& stack) const;
SHAMapItem const* peekFirstItem(SharedPtrNodeStack& stack) const;
SHAMapItem const* peekNextItem(uint256 const& id, SharedPtrNodeStack& stack) const;
bool walkBranch (SHAMapAbstractNode* node,
std::shared_ptr<SHAMapItem const> const& otherMapItem,
bool isFirstMap, Delta & differences, int & maxCount) const;
@@ -361,9 +359,9 @@ public:
using pointer = value_type const*;
private:
NodeStack stack_;
SHAMap const* map_ = nullptr;
pointer item_ = nullptr;
SharedPtrNodeStack stack_;
SHAMap const* map_ = nullptr;
pointer item_ = nullptr;
public:
const_iterator() = default;
@@ -377,7 +375,7 @@ public:
private:
explicit const_iterator(SHAMap const* map);
const_iterator(SHAMap const* map, pointer item);
const_iterator(SHAMap const* map, pointer item, NodeStack&& stack);
const_iterator(SHAMap const* map, pointer item, SharedPtrNodeStack&& stack);
friend bool operator==(const_iterator const& x, const_iterator const& y);
friend class SHAMap;
@@ -399,7 +397,7 @@ SHAMap::const_iterator::const_iterator(SHAMap const* map, pointer item)
inline
SHAMap::const_iterator::const_iterator(SHAMap const* map, pointer item,
NodeStack&& stack)
SharedPtrNodeStack&& stack)
: stack_(std::move(stack))
, map_(map)
, item_(item)

View File

@@ -145,10 +145,10 @@ SHAMap::dirtyUp (SharedPtrNodeStack& stack,
}
SHAMapTreeNode*
SHAMap::walkTowardsKey(uint256 const& id, NodeStack* stack) const
SHAMap::walkTowardsKey(uint256 const& id, SharedPtrNodeStack* stack) const
{
assert(stack == nullptr || stack->empty());
auto inNode = root_.get();
auto inNode = root_;
SHAMapNodeID nodeID;
if (stack != nullptr)
stack->push({inNode, nodeID});
@@ -156,7 +156,7 @@ SHAMap::walkTowardsKey(uint256 const& id, NodeStack* stack) const
while (inNode->isInner())
{
auto const branch = nodeID.selectBranch (id);
auto const inner = static_cast<SHAMapInnerNode*>(inNode);
auto const inner = std::static_pointer_cast<SHAMapInnerNode>(inNode);
if (inner->isEmptyBranch (branch))
return nullptr;
@@ -165,7 +165,7 @@ SHAMap::walkTowardsKey(uint256 const& id, NodeStack* stack) const
if (stack != nullptr)
stack->push({inNode, nodeID});
}
return static_cast<SHAMapTreeNode*>(inNode);
return static_cast<SHAMapTreeNode*>(inNode.get());
}
SHAMapTreeNode*
@@ -430,12 +430,13 @@ SHAMap::unshareNode (std::shared_ptr<Node> node, SHAMapNodeID const& nodeID)
}
SHAMapTreeNode*
SHAMap::firstBelow(SHAMapAbstractNode* node, NodeStack& stack) const
SHAMap::firstBelow(std::shared_ptr<SHAMapAbstractNode> node,
SharedPtrNodeStack& stack) const
{
// Return the first item at or below this node
if (node->isLeaf())
return static_cast<SHAMapTreeNode*>(node);
auto inner = static_cast<SHAMapInnerNode*>(node);
return static_cast<SHAMapTreeNode*>(node.get());
auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
for (int i = 0; i < 16;)
{
if (!inner->isEmptyBranch(i))
@@ -444,8 +445,8 @@ SHAMap::firstBelow(SHAMapAbstractNode* node, NodeStack& stack) const
assert(!stack.empty());
stack.push({node, stack.top().second.getChildNodeID(i)});
if (node->isLeaf())
return static_cast<SHAMapTreeNode*>(node);
inner = static_cast<SHAMapInnerNode*>(node);
return static_cast<SHAMapTreeNode*>(node.get());
inner = std::static_pointer_cast<SHAMapInnerNode>(node);
i = 0; // scan all 16 branches of this new node
}
else
@@ -497,11 +498,11 @@ static std::shared_ptr<
SHAMapItem const> const nullConstSHAMapItem;
SHAMapItem const*
SHAMap::peekFirstItem(NodeStack& stack) const
SHAMap::peekFirstItem(SharedPtrNodeStack& stack) const
{
assert(stack.empty());
stack.push({root_.get(), SHAMapNodeID{}});
SHAMapTreeNode* node = firstBelow(root_.get(), stack);
stack.push({root_, SHAMapNodeID{}});
SHAMapTreeNode* node = firstBelow(root_, stack);
if (!node)
{
while (!stack.empty())
@@ -512,7 +513,7 @@ SHAMap::peekFirstItem(NodeStack& stack) const
}
SHAMapItem const*
SHAMap::peekNextItem(uint256 const& id, NodeStack& stack) const
SHAMap::peekNextItem(uint256 const& id, SharedPtrNodeStack& stack) const
{
assert(!stack.empty());
assert(stack.top().first->isLeaf());
@@ -522,7 +523,7 @@ SHAMap::peekNextItem(uint256 const& id, NodeStack& stack) const
auto node = stack.top().first;
auto nodeID = stack.top().second;
assert(!node->isLeaf());
auto inner = static_cast<SHAMapInnerNode*>(node);
auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
for (auto i = nodeID.selectBranch(id) + 1; i < 16; ++i)
{
if (!inner->isEmptyBranch(i))
@@ -583,22 +584,22 @@ SHAMap::upper_bound(uint256 const& id) const
{
// Get a const_iterator to the next item in the tree after a given item
// item need not be in tree
NodeStack stack;
SharedPtrNodeStack stack;
walkTowardsKey(id, &stack);
SHAMapAbstractNode* node;
std::shared_ptr<SHAMapAbstractNode> node;
SHAMapNodeID nodeID;
while (!stack.empty())
{
std::tie(node, nodeID) = stack.top();
if (node->isLeaf())
{
auto leaf = static_cast<SHAMapTreeNode*>(node);
auto leaf = static_cast<SHAMapTreeNode*>(node.get());
if (leaf->peekItem()->key() > id)
return const_iterator(this, leaf->peekItem().get(), std::move(stack));
}
else
{
auto inner = static_cast<SHAMapInnerNode*>(node);
auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
for (auto branch = nodeID.selectBranch(id) + 1; branch < 16; ++branch)
{
if (!inner->isEmptyBranch(branch))