CatapultServer  v0.5.0.1 (Elephant)
TreeNodePath.h
Go to the documentation of this file.
1 
21 #pragma once
23 #include <algorithm>
24 #include <iosfwd>
25 #include <vector>
26 #include <stdint.h>
27 
28 namespace catapult { namespace tree {
29 
31  class TreeNodePath {
32  public:
34  TreeNodePath();
35 
37  template<typename TKey>
38  explicit TreeNodePath(TKey key) : m_adjustment(0) {
39  if constexpr (utils::traits::is_scalar_v<TKey>) {
40  m_size = 2 * sizeof(TKey);
41  m_path.resize(sizeof(TKey));
42 
43  // copy in big endian byte order
44  const auto* pKeyData = reinterpret_cast<const uint8_t*>(&key);
45  std::reverse_copy(pKeyData, pKeyData + sizeof(TKey), m_path.begin());
46  } else {
47  m_size = 2 * key.size();
48  m_path.resize(key.size());
49  std::copy(key.cbegin(), key.cend(), m_path.begin());
50  }
51  }
52 
53  private:
54  TreeNodePath(const std::vector<uint8_t>& path, size_t offset, size_t size);
55 
56  public:
58  bool empty() const;
59 
61  size_t size() const;
62 
63  public:
65  uint8_t nibbleAt(size_t index) const;
66 
67  public:
69  bool operator==(const TreeNodePath& rhs) const;
70 
72  bool operator!=(const TreeNodePath& rhs) const;
73 
74  public:
76  TreeNodePath subpath(size_t offset) const;
77 
79  TreeNodePath subpath(size_t offset, size_t size) const;
80 
81  public:
83  static TreeNodePath Join(const TreeNodePath& lhs, const TreeNodePath& rhs);
84 
86  static TreeNodePath Join(const TreeNodePath& lhs, uint8_t nibble, const TreeNodePath& rhs);
87 
88  private:
89  size_t m_size;
90  size_t m_adjustment; // used to track odd / even starting nibble
91  std::vector<uint8_t> m_path;
92  };
93 
95  std::ostream& operator<<(std::ostream& out, const TreeNodePath& path);
96 
98  size_t FindFirstDifferenceIndex(const TreeNodePath& lhs, const TreeNodePath& rhs);
99 }}
catapult::tree::FindFirstDifferenceIndex
size_t FindFirstDifferenceIndex(const TreeNodePath &lhs, const TreeNodePath &rhs)
Compares two paths (lhs and rhs) and returns the index of the first non-equal nibble.
Definition: TreeNodePath.cpp:142
catapult::tree::TreeNodePath::empty
bool empty() const
Returns true if this path is empty.
Definition: TreeNodePath.cpp:54
catapult::tree::TreeNodePath::subpath
TreeNodePath subpath(size_t offset) const
Creates a subpath starting at nibble offset.
Definition: TreeNodePath.cpp:78
catapult::tree::TreeNodePath::operator!=
bool operator!=(const TreeNodePath &rhs) const
Returns true if this path is not equal to rhs.
Definition: TreeNodePath.cpp:74
catapult::tree::TreeNodePath::nibbleAt
uint8_t nibbleAt(size_t index) const
Gets the nibble at index.
Definition: TreeNodePath.cpp:62
catapult::tree::operator<<
std::ostream & operator<<(std::ostream &out, const TreeNodePath &path)
Insertion operator for outputting path to out.
Definition: TreeNodePath.cpp:133
catapult::tree::TreeNodePath::m_adjustment
size_t m_adjustment
Definition: TreeNodePath.h:90
catapult::utils::IntegralHexFormatter
Formatter for printing an integral hex number to a stream.
Definition: HexFormatter.h:71
HexFormatter.h
catapult::tree::TreeNodePath::TreeNodePath
TreeNodePath(TKey key)
Creates a path from key.
Definition: TreeNodePath.h:38
m_index
size_t m_index
Definition: TreeNodePath.cpp:111
m_path
std::vector< uint8_t > m_path
Definition: TreeNodePath.cpp:112
catapult::tree::TreeNodePath::m_path
std::vector< uint8_t > m_path
Definition: TreeNodePath.h:91
size
uint64_t size
Definition: MemoryCounters.cpp:65
Traits.h
catapult::tree::TreeNodePath::operator==
bool operator==(const TreeNodePath &rhs) const
Returns true if this path is equal to rhs.
Definition: TreeNodePath.cpp:70
catapult
Definition: AddressExtractionExtension.cpp:28
TreeNodePath.h
catapult::tree::TreeNodePath::size
size_t size() const
Gets the number of nibbles in this path.
Definition: TreeNodePath.cpp:58
catapult::tree::TreeNodePath::Join
static TreeNodePath Join(const TreeNodePath &lhs, const TreeNodePath &rhs)
Joins lhs and rhs into a new path.
Definition: TreeNodePath.cpp:116
catapult::tree::TreeNodePath::TreeNodePath
TreeNodePath()
Creates a default path.
Definition: TreeNodePath.cpp:39
catapult::tree::TreeNodePath::m_size
size_t m_size
Definition: TreeNodePath.h:89
catapult::tree::TreeNodePath
Represents a path in a tree.
Definition: TreeNodePath.h:31