CatapultServer  v0.5.0.1 (Elephant)
BaseSetDeltaIterationView.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "BaseSetDelta.h"
23 #include "BaseSetIterationView.h"
24 
25 namespace catapult { namespace deltaset {
26 
29  template<typename TSetTraits>
30  class BaseSetDeltaIterationView {
31  private:
32  using SetType = typename TSetTraits::MemorySetType;
33  using KeyType = typename TSetTraits::KeyType;
34 
35  public:
37  BaseSetDeltaIterationView(const SetType& originalElements, const DeltaElements<SetType>& deltas, size_t size)
38  : m_originalElements(originalElements)
39  , m_deltas(deltas)
40  , m_size(size)
41  {}
42 
43  public:
45  class iterator {
46  public:
47  using difference_type = std::ptrdiff_t;
48  using value_type = const typename TSetTraits::StorageType;
49  using pointer = value_type*;
51  using iterator_category = std::forward_iterator_tag;
52 
53  public:
55  iterator(const SetType& elements, const DeltaElements<SetType>& deltas, size_t position, size_t size)
56  : m_elements(elements)
57  , m_deltas(deltas)
58  , m_position(position)
59  , m_size(size)
60  , m_stage(IterationStage::Copied)
61  , m_iter(m_deltas.Copied.cbegin()) {
62  if (m_position == m_size)
63  m_iter = m_deltas.Added.cend();
64  else
66  }
67 
68  public:
70  bool operator==(const iterator& rhs) const {
71  // don't need to check m_deltas because m_elements is a private member of BaseSetDelta and only one BaseSetDelta
72  // is allowed outstanding at once, so it will never be different when m_elements is the same
73  return &m_elements == &rhs.m_elements && m_position == rhs.m_position;
74  }
75 
77  bool operator!=(const iterator& rhs) const {
78  return !(*this == rhs);
79  }
80 
81  public:
84  if (m_position == m_size)
85  CATAPULT_THROW_OUT_OF_RANGE("cannot advance iterator beyond end");
86 
87  ++m_position;
88  ++m_iter;
90  return *this;
91  }
92 
95  auto copy = *this;
96  ++*this;
97  return copy;
98  }
99 
100  private:
103  if (handleCopiedStage())
104  return;
105 
106  // all copied elements have been iterated, so advance to the next stage
108  m_iter = m_elements.cbegin();
109  }
110 
112  if (handleOriginalStage())
113  return;
114 
115  // all original elements have been iterated, so advance to the next stage
117  m_iter = m_deltas.Added.cbegin();
118  }
119 
120  // last stage
122  }
123 
125  return m_deltas.Copied.cend() != m_iter;
126  }
127 
129  // advance to the next original element that has neither been removed nor copied
130  for (; m_elements.cend() != m_iter; ++m_iter) {
131  const auto& key = TSetTraits::ToKey(*m_iter);
132  if (!Contains(m_deltas.Removed, key) && !Contains(m_deltas.Copied, key))
133  return true;
134  }
135 
136  return false;
137  }
138 
140  // advance to the next added element that has not been removed
141  for (; m_deltas.Added.cend() != m_iter; ++m_iter) {
142  const auto& key = TSetTraits::ToKey(*m_iter);
143  if (!Contains(m_deltas.Removed, key))
144  return true;
145  }
146 
147  return false;
148  }
149 
150  private:
151  static constexpr bool Contains(const SetType& set, const KeyType& key) {
152  return set.cend() != set.find(key);
153  }
154 
155  public:
158  if (m_position == m_size)
159  CATAPULT_THROW_OUT_OF_RANGE("cannot dereference at end");
160 
161  return &*m_iter;
162  }
163 
166  return *(this->operator->());
167  }
168 
169  private:
171 
172  private:
175  size_t m_position;
176  size_t m_size;
178  typename SetType::const_iterator m_iter;
179  };
180 
182  auto begin() const {
184  }
185 
187  auto end() const {
189  }
190 
191  private:
193  DeltaElements<SetType> m_deltas; // by value because deltas holds all sets by reference
194  size_t m_size;
195  };
196 
199  template<typename TElementTraits, typename TSetTraits>
202  }
203 }}
catapult::deltaset::BaseSetDeltaIterationView::iterator::difference_type
std::ptrdiff_t difference_type
Definition: BaseSetDeltaIterationView.h:47
catapult::deltaset::BaseSetDeltaIterationView::iterator::moveToValidElement
void moveToValidElement()
Definition: BaseSetDeltaIterationView.h:101
catapult::deltaset::BaseSetDeltaIterationView::m_size
size_t m_size
Definition: BaseSetDeltaIterationView.h:194
catapult::deltaset::BaseSetDeltaIterationView::iterator::operator*
value_type & operator*() const
Returns a reference to the current element.
Definition: BaseSetDeltaIterationView.h:165
catapult::deltaset::BaseSetDeltaIterationView::iterator::operator++
iterator operator++(int)
Advances the iterator to the next position.
Definition: BaseSetDeltaIterationView.h:94
catapult::deltaset::BaseSetDeltaIterationView::iterator::m_position
size_t m_position
Definition: BaseSetDeltaIterationView.h:175
catapult::deltaset::BaseSetDelta
Definition: BaseSet.h:30
catapult::deltaset::BaseSetDeltaIterationView::iterator::m_iter
SetType::const_iterator m_iter
Definition: BaseSetDeltaIterationView.h:178
catapult::deltaset::BaseSetDelta::m_originalElements
const SetType & m_originalElements
Definition: BaseSetDelta.h:359
catapult::deltaset::BaseSetDeltaIterationView::iterator::m_size
size_t m_size
Definition: BaseSetDeltaIterationView.h:176
catapult::deltaset::DeltaElements::Removed
const TSet & Removed
Removed elements.
Definition: DeltaElements.h:47
catapult::deltaset::DeltaElements::Added
const TSet & Added
Added elements.
Definition: DeltaElements.h:44
catapult::deltaset::BaseSetDeltaIterationView::BaseSetDeltaIterationView
BaseSetDeltaIterationView(const SetType &originalElements, const DeltaElements< SetType > &deltas, size_t size)
Creates a view around originalElements, deltas and size.
Definition: BaseSetDeltaIterationView.h:37
catapult::deltaset::BaseSetDeltaIterationView::iterator
Iterator used for iterating over the view.
Definition: BaseSetDeltaIterationView.h:45
catapult::deltaset::BaseSetDeltaIterationView::iterator::IterationStage::Copied
catapult::deltaset::BaseSetDeltaIterationView::iterator::pointer
value_type * pointer
Definition: BaseSetDeltaIterationView.h:49
catapult::deltaset::BaseSetDeltaIterationView::iterator::operator==
bool operator==(const iterator &rhs) const
Returns true if this iterator and rhs are equal.
Definition: BaseSetDeltaIterationView.h:70
catapult::deltaset::SelectIterableSet
const TSet & SelectIterableSet(const TSet &set)
Selects the iterable set from set.
Definition: BaseSetIterationView.h:67
BaseSetIterationView.h
catapult::deltaset::DeltaElements< SetType >
catapult::deltaset::BaseSetDeltaIterationView::iterator::IterationStage::Added
catapult::deltaset::BaseSetDeltaIterationView::iterator::handleAddedStage
bool handleAddedStage()
Definition: BaseSetDeltaIterationView.h:139
catapult::deltaset::BaseSetDeltaIterationView::iterator::IterationStage::Original
catapult::deltaset::BaseSetDeltaIterationView::iterator::operator++
iterator & operator++()
Advances the iterator to the next position.
Definition: BaseSetDeltaIterationView.h:83
CATAPULT_THROW_OUT_OF_RANGE
#define CATAPULT_THROW_OUT_OF_RANGE(MESSAGE)
Macro used to throw a catapult out of range.
Definition: exceptions.h:191
catapult::deltaset::BaseSetDeltaIterationView::iterator::operator->
value_type * operator->() const
Returns a pointer to the current element.
Definition: BaseSetDeltaIterationView.h:157
catapult::deltaset::BaseSetDeltaIterationView
Definition: BaseSetDelta.h:67
size
uint64_t size
Definition: MemoryCounters.cpp:65
catapult::deltaset::BaseSetDeltaIterationView::iterator::value_type
const typename TSetTraits::StorageType value_type
Definition: BaseSetDeltaIterationView.h:48
catapult::deltaset::BaseSetDeltaIterationView::iterator::iterator
iterator(const SetType &elements, const DeltaElements< SetType > &deltas, size_t position, size_t size)
Creates an iterator around the original elements and deltas at position given a total of size element...
Definition: BaseSetDeltaIterationView.h:55
catapult::deltaset::BaseSetDeltaIterationView::m_deltas
DeltaElements< SetType > m_deltas
Definition: BaseSetDeltaIterationView.h:193
catapult::deltaset::BaseSetDelta::size
size_t size() const
Gets the size of this set.
Definition: BaseSetDelta.h:108
catapult::deltaset::BaseSetDelta::deltas
DeltaElements< MemorySetType > deltas() const
Gets const references to the pending modifications.
Definition: BaseSetDelta.h:304
catapult::deltaset::BaseSetDeltaIterationView::iterator::operator!=
bool operator!=(const iterator &rhs) const
Returns true if this iterator and rhs are not equal.
Definition: BaseSetDeltaIterationView.h:77
catapult::deltaset::BaseSetDeltaIterationView::iterator::m_stage
IterationStage m_stage
Definition: BaseSetDeltaIterationView.h:177
catapult::deltaset::BaseSetDeltaIterationView::end
auto end() const
Returns a const iterator to the element following the last element of the underlying set.
Definition: BaseSetDeltaIterationView.h:187
catapult::deltaset::BaseSetDeltaIterationView::iterator::m_elements
const SetType & m_elements
Definition: BaseSetDeltaIterationView.h:173
catapult::deltaset::BaseSetDeltaIterationView::KeyType
typename TSetTraits::KeyType KeyType
Definition: BaseSetDeltaIterationView.h:33
catapult::deltaset::BaseSetDeltaIterationView::iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: BaseSetDeltaIterationView.h:51
catapult::deltaset::DeltaElements::Copied
const TSet & Copied
Copied elements.
Definition: DeltaElements.h:50
catapult::deltaset::BaseSetDeltaIterationView::begin
auto begin() const
Returns a const iterator to the first element of the underlying set.
Definition: BaseSetDeltaIterationView.h:182
catapult::deltaset::BaseSetDeltaIterationView::SetType
typename TSetTraits::MemorySetType SetType
Definition: BaseSetDeltaIterationView.h:32
catapult::deltaset::BaseSetDeltaIterationView::iterator::reference
value_type & reference
Definition: BaseSetDeltaIterationView.h:50
catapult::deltaset::BaseSetDeltaIterationView::iterator::IterationStage
IterationStage
Definition: BaseSetDeltaIterationView.h:170
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::deltaset::MakeIterableView
BaseSetDeltaIterationView< TSetTraits > MakeIterableView(const BaseSetDelta< TElementTraits, TSetTraits > &delta)
Definition: BaseSetDeltaIterationView.h:200
catapult::deltaset::BaseSetDeltaIterationView::iterator::handleOriginalStage
bool handleOriginalStage()
Definition: BaseSetDeltaIterationView.h:128
BaseSetDelta.h
catapult::deltaset::BaseSetDeltaIterationView::iterator::Contains
static constexpr bool Contains(const SetType &set, const KeyType &key)
Definition: BaseSetDeltaIterationView.h:151
catapult::deltaset::BaseSetDeltaIterationView::iterator::m_deltas
DeltaElements< SetType > m_deltas
Definition: BaseSetDeltaIterationView.h:174
catapult::deltaset::BaseSetDeltaIterationView::iterator::handleCopiedStage
bool handleCopiedStage()
Definition: BaseSetDeltaIterationView.h:124
catapult::deltaset::BaseSetDeltaIterationView::m_originalElements
const SetType & m_originalElements
Definition: BaseSetDeltaIterationView.h:192