CatapultServer  v0.5.0.1 (Elephant)
CompactMosaicMap.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "catapult/utils/Hashers.h"
24 #include "catapult/exceptions.h"
25 #include "catapult/types.h"
26 #include <map>
27 
28 namespace catapult { namespace state {
29 
32  // This is acceptable for mosaics stored in AccountBalances but not for a general purpose map.
34  private:
35  static constexpr auto Array_Size = 5;
36 
37  // in order for this map to behave like std::unordered_map, the element type needs to be a pair, not model::Mosaic
38  using Mosaic = std::pair<const MosaicId, Amount>;
39  using MutableMosaic = std::pair<MosaicId, Amount>;
40 
41  // gcc strict aliasing workaround because pair<X, Y> and pair<const X, Y> are two different types
42  struct MosaicUnion {
43  public:
45  {}
46 
47  MosaicUnion(MosaicUnion&& rhs) : Mosaic(std::move(rhs.Mosaic))
48  {}
49 
50  public:
52  Mosaic = std::move(rhs.Mosaic);
53  return *this;
54  }
55 
56  public:
57  union {
60  };
61  };
62 
63  using MosaicArray = std::array<MosaicUnion, Array_Size>;
64  using MosaicMap = std::map<MosaicId, Amount>;
65 
66  private:
69  uint8_t ArraySize;
70  std::unique_ptr<MosaicMap> pMapStorage;
71  };
72 
74  public:
76  std::unique_ptr<SecondLevelStorage> pNextStorage;
77 
78  public:
79  bool hasValue() const;
80 
81  bool hasArray() const;
82 
83  bool hasMap() const;
84 
85  public:
86  uint8_t& arraySize() const;
87 
88  MosaicArray& array() const;
89 
90  MosaicMap& map() const;
91  };
92 
93  private:
94  enum class MosaicSource { Value, Array, Map };
95 
96  struct MosaicLocation {
97  public:
99  : Source(static_cast<MosaicSource>(-1))
100  , ArrayIndex(0)
101  , MapIterator()
102  {}
103 
104  public:
106  size_t ArrayIndex;
107  MosaicMap::iterator MapIterator;
108  };
109 
110  private:
113  public:
114  using difference_type = std::ptrdiff_t;
115  using iterator_category = std::forward_iterator_tag;
116 
117  public:
119  enum class Stage { Start, Value, Array, Map, End };
120 
121  public:
123  explicit basic_iterator(FirstLevelStorage& storage, Stage stage);
124 
126  explicit basic_iterator(FirstLevelStorage& storage, const MosaicLocation& location);
127 
128  public:
130  bool operator==(const basic_iterator& rhs) const;
131 
133  bool operator!=(const basic_iterator& rhs) const;
134 
135  public:
138 
141 
142  protected:
144  Mosaic& current() const;
145 
146  private:
147  void advance();
148 
149  void setValueMosaic();
150 
151  void setArrayMosaic();
152 
153  void setMapMosaic();
154 
155  void setEnd();
156 
157  bool isEnd() const;
158 
159  private:
163 
164  // indexing into sub containers
165  size_t m_arrayIndex;
166  MosaicMap::iterator m_mapIterator;
167  };
168 
170  template<typename T>
172  public:
173  using value_type = T;
174  using pointer = value_type*;
176 
177  public:
179 
180  public:
183  return current();
184  }
185 
187  pointer operator->() const {
188  return &current();
189  }
190  };
191 
192  public:
195 
198 
199  public:
201  const_iterator begin() const;
202 
204  const_iterator end() const;
205 
207  iterator begin();
208 
210  iterator end();
211 
212  public:
214  bool empty() const;
215 
217  size_t size() const;
218 
220  const_iterator find(MosaicId id) const;
221 
223  iterator find(MosaicId id);
224 
226  void insert(const Mosaic& pair);
227 
229  void erase(MosaicId id);
230 
232  void optimize(MosaicId id);
233 
234  private:
235  bool find(MosaicId id, MosaicLocation& location) const;
236 
237  void insertIntoArray(size_t index, const Mosaic& pair);
238 
239  void insertIntoMap(const Mosaic& pair);
240 
241  void eraseFromArray(size_t index);
242 
243  void compact();
244 
245  private:
248  };
249 }}
catapult::state::CompactMosaicMap::FirstLevelStorage::pNextStorage
std::unique_ptr< SecondLevelStorage > pNextStorage
Definition: CompactMosaicMap.h:76
catapult::state::CompactMosaicMap::basic_iterator::difference_type
std::ptrdiff_t difference_type
Definition: CompactMosaicMap.h:114
catapult::state::CompactMosaicMap::basic_iterator::Stage
Stage
Iterator stage.
Definition: CompactMosaicMap.h:119
catapult::state::CompactMosaicMap::FirstLevelStorage::map
MosaicMap & map() const
Definition: CompactMosaicMap.cpp:48
exceptions.h
catapult::state::CompactMosaicMap::MosaicUnion::ConstMosaic
CompactMosaicMap::Mosaic ConstMosaic
Definition: CompactMosaicMap.h:59
catapult::state::CompactMosaicMap::FirstLevelStorage::hasValue
bool hasValue() const
Definition: CompactMosaicMap.cpp:28
catapult::state::CompactMosaicMap::eraseFromArray
void eraseFromArray(size_t index)
Definition: CompactMosaicMap.cpp:355
catapult::utils::HexFormat
constexpr void HexFormat(const ByteArray< N, TTag > &)
catapult::state::CompactMosaicMap::basic_iterator::basic_iterator
basic_iterator(FirstLevelStorage &storage, Stage stage)
Creates an iterator around storage with initial stage.
Definition: CompactMosaicMap.cpp:56
catapult::state::CompactMosaicMap::Array_Size
static constexpr auto Array_Size
Definition: CompactMosaicMap.h:35
catapult::state::CompactMosaicMap::MosaicLocation::MapIterator
MosaicMap::iterator MapIterator
Definition: CompactMosaicMap.h:107
catapult::state::CompactMosaicMap::basic_iterator::Stage::Start
catapult::state::CompactMosaicMap::MosaicSource::Map
catapult::state::CompactMosaicMap::basic_iterator::advance
void advance()
Definition: CompactMosaicMap.cpp:112
catapult::state::CompactMosaicMap::MosaicLocation
Definition: CompactMosaicMap.h:96
catapult::state::CompactMosaicMap::m_optimizedMosaicId
MosaicId m_optimizedMosaicId
Definition: CompactMosaicMap.h:247
catapult::state::CompactMosaicMap::erase
void erase(MosaicId id)
Erases the mosaic with id.
Definition: CompactMosaicMap.cpp:259
CATAPULT_THROW_INVALID_ARGUMENT_2
#define CATAPULT_THROW_INVALID_ARGUMENT_2(MESSAGE, PARAM1, PARAM2)
Macro used to throw a catapult invalid argument with two parameters.
Definition: exceptions.h:187
catapult::state::CompactMosaicMap::basic_iterator::setValueMosaic
void setValueMosaic()
Definition: CompactMosaicMap.cpp:139
catapult::utils::to_underlying_type
constexpr std::underlying_type_t< TEnum > to_underlying_type(TEnum value)
Converts a strongly typed enumeration value to its underlying integral value.
Definition: Casting.h:37
catapult::state::CompactMosaicMap::Mosaic
std::pair< const MosaicId, Amount > Mosaic
Definition: CompactMosaicMap.h:38
catapult::state::CompactMosaicMap::basic_iterator::m_stage
Stage m_stage
Definition: CompactMosaicMap.h:161
catapult::state::CompactMosaicMap::basic_iterator_t::reference
value_type & reference
Definition: CompactMosaicMap.h:175
catapult::state::CompactMosaicMap::basic_iterator::current
Mosaic & current() const
Gets a reference to the current value.
Definition: CompactMosaicMap.cpp:105
catapult::utils::as_const
constexpr const T & as_const(T &ref)
Coerces a reference (ref) to a const reference.
Definition: Casting.h:31
catapult::state::CompactMosaicMap::basic_iterator_t::value_type
T value_type
Definition: CompactMosaicMap.h:173
catapult::state::CompactMosaicMap::find
const_iterator find(MosaicId id) const
Finds the mosaic with id.
Definition: CompactMosaicMap.cpp:199
Value
uint32_t Value
Definition: BlockScorer.cpp:32
catapult::state::CompactMosaicMap::basic_iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: CompactMosaicMap.h:115
catapult::state::CompactMosaicMap::basic_iterator_t
A basic typed iterator that adds support for dereferencing.
Definition: CompactMosaicMap.h:171
catapult::utils::BaseValue< uint64_t, MosaicId_tag >
catapult::state::CompactMosaicMap::SecondLevelStorage::pMapStorage
std::unique_ptr< MosaicMap > pMapStorage
Definition: CompactMosaicMap.h:70
catapult::state::CompactMosaicMap::basic_iterator::Stage::End
catapult::state::CompactMosaicMap::empty
bool empty() const
Returns true if the map is empty.
Definition: CompactMosaicMap.cpp:181
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::state::CompactMosaicMap::basic_iterator_t::pointer
value_type * pointer
Definition: CompactMosaicMap.h:174
catapult::state::CompactMosaicMap::basic_iterator::setEnd
void setEnd()
Definition: CompactMosaicMap.cpp:154
catapult::state::CompactMosaicMap::insert
void insert(const Mosaic &pair)
Inserts a mosaic pair.
Definition: CompactMosaicMap.cpp:222
catapult::state::CompactMosaicMap::basic_iterator::operator++
basic_iterator & operator++()
Advances the iterator to the next position.
Definition: CompactMosaicMap.cpp:91
catapult::state::CompactMosaicMap::insertIntoArray
void insertIntoArray(size_t index, const Mosaic &pair)
Definition: CompactMosaicMap.cpp:335
catapult::state::CompactMosaicMap::basic_iterator_t::operator->
pointer operator->() const
Returns a pointer to the current value.
Definition: CompactMosaicMap.h:187
catapult::state::CompactMosaicMap::FirstLevelStorage::hasArray
bool hasArray() const
Definition: CompactMosaicMap.cpp:32
catapult::state::CompactMosaicMap::basic_iterator::m_arrayIndex
size_t m_arrayIndex
Definition: CompactMosaicMap.h:165
catapult::state::CompactMosaicMap::basic_iterator::Stage::Array
catapult::state::CompactMosaicMap::FirstLevelStorage::array
MosaicArray & array() const
Definition: CompactMosaicMap.cpp:44
catapult::state::CompactMosaicMap::basic_iterator::Stage::Value
catapult::state::CompactMosaicMap::MosaicUnion::MosaicUnion
MosaicUnion(MosaicUnion &&rhs)
Definition: CompactMosaicMap.h:47
catapult::state::CompactMosaicMap::MosaicUnion::Mosaic
MutableMosaic Mosaic
Definition: CompactMosaicMap.h:58
catapult::state::CompactMosaicMap::SecondLevelStorage::ArrayStorage
MosaicArray ArrayStorage
Definition: CompactMosaicMap.h:68
catapult::state::CompactMosaicMap::basic_iterator::operator!=
bool operator!=(const basic_iterator &rhs) const
Returns true if this iterator and rhs are not equal.
Definition: CompactMosaicMap.cpp:87
catapult::state::CompactMosaicMap::MosaicSource
MosaicSource
Definition: CompactMosaicMap.h:94
catapult::state::CompactMosaicMap::FirstLevelStorage
Definition: CompactMosaicMap.h:73
catapult::state::CompactMosaicMap::basic_iterator
Base of mosaic iterators.
Definition: CompactMosaicMap.h:112
CATAPULT_THROW_INVALID_ARGUMENT_1
#define CATAPULT_THROW_INVALID_ARGUMENT_1(MESSAGE, PARAM1)
Macro used to throw a catapult invalid argument with a single parameter.
Definition: exceptions.h:183
catapult::state::CompactMosaicMap::basic_iterator::m_pCurrent
Mosaic * m_pCurrent
Definition: CompactMosaicMap.h:162
catapult::state::CompactMosaicMap::basic_iterator::m_mapIterator
MosaicMap::iterator m_mapIterator
Definition: CompactMosaicMap.h:166
catapult::state::CompactMosaicMap::basic_iterator::operator==
bool operator==(const basic_iterator &rhs) const
Returns true if this iterator and rhs are equal.
Definition: CompactMosaicMap.cpp:83
catapult::state::CompactMosaicMap::FirstLevelStorage::hasMap
bool hasMap() const
Definition: CompactMosaicMap.cpp:36
catapult::state::CompactMosaicMap::MutableMosaic
std::pair< MosaicId, Amount > MutableMosaic
Definition: CompactMosaicMap.h:39
catapult::utils::MoveOnly
A class that can be moved but not copied.
Definition: NonCopyable.h:43
catapult::state::CompactMosaicMap::MosaicUnion
Definition: CompactMosaicMap.h:42
catapult::state::CompactMosaicMap::const_iterator
basic_iterator_t< const Mosaic > const_iterator
Mosaic const iterator.
Definition: CompactMosaicMap.h:194
catapult::state::CompactMosaicMap::size
size_t size() const
Gets the number of mosaics in the map.
Definition: CompactMosaicMap.cpp:185
catapult::state::CompactMosaicMap::MosaicMap
std::map< MosaicId, Amount > MosaicMap
Definition: CompactMosaicMap.h:64
catapult::state::CompactMosaicMap::iterator
basic_iterator_t< Mosaic > iterator
Mosaic non-const iterator.
Definition: CompactMosaicMap.h:197
catapult::state::CompactMosaicMap::optimize
void optimize(MosaicId id)
Optimizes access of the mosaic with id.
Definition: CompactMosaicMap.cpp:286
catapult::state::CompactMosaicMap::SecondLevelStorage::ArraySize
uint8_t ArraySize
Definition: CompactMosaicMap.h:69
catapult::state::CompactMosaicMap::basic_iterator::isEnd
bool isEnd() const
Definition: CompactMosaicMap.cpp:159
catapult::state::CompactMosaicMap::MosaicArray
std::array< MosaicUnion, Array_Size > MosaicArray
Definition: CompactMosaicMap.h:63
catapult::state::CompactMosaicMap
Definition: CompactMosaicMap.h:33
catapult::state::CompactMosaicMap::basic_iterator::setMapMosaic
void setMapMosaic()
Definition: CompactMosaicMap.cpp:149
catapult::state::CompactMosaicMap::FirstLevelStorage::Value
MosaicUnion Value
Definition: CompactMosaicMap.h:75
types.h
catapult::state::CompactMosaicMap::MosaicLocation::ArrayIndex
size_t ArrayIndex
Definition: CompactMosaicMap.h:106
Casting.h
catapult::state::CompactMosaicMap::end
const_iterator end() const
Returns a const iterator to the element following the last element of the underlying container.
Definition: CompactMosaicMap.cpp:169
catapult::state::CompactMosaicMap::MosaicUnion::MosaicUnion
MosaicUnion()
Definition: CompactMosaicMap.h:44
catapult
Definition: AddressExtractionExtension.cpp:28
Hashers.h
catapult::state::CompactMosaicMap::MosaicLocation::Source
MosaicSource Source
Definition: CompactMosaicMap.h:105
catapult::state::CompactMosaicMap::basic_iterator::setArrayMosaic
void setArrayMosaic()
Definition: CompactMosaicMap.cpp:144
catapult::state::CompactMosaicMap::basic_iterator::Stage::Map
CompactMosaicMap.h
catapult::state::CompactMosaicMap::SecondLevelStorage
Definition: CompactMosaicMap.h:67
catapult::state::CompactMosaicMap::MosaicLocation::MosaicLocation
MosaicLocation()
Definition: CompactMosaicMap.h:98
catapult::state::CompactMosaicMap::MosaicSource::Value
catapult::state::CompactMosaicMap::insertIntoMap
void insertIntoMap(const Mosaic &pair)
Definition: CompactMosaicMap.cpp:348
catapult::state::CompactMosaicMap::m_storage
FirstLevelStorage m_storage
Definition: CompactMosaicMap.h:246
catapult::state::CompactMosaicMap::MosaicSource::Array
catapult::state::CompactMosaicMap::basic_iterator::m_storage
FirstLevelStorage & m_storage
Definition: CompactMosaicMap.h:160
catapult::state::CompactMosaicMap::compact
void compact()
Definition: CompactMosaicMap.cpp:362
catapult::state::CompactMosaicMap::FirstLevelStorage::arraySize
uint8_t & arraySize() const
Definition: CompactMosaicMap.cpp:40
catapult::state::CompactMosaicMap::begin
const_iterator begin() const
Returns a const iterator to the first element of the underlying container.
Definition: CompactMosaicMap.cpp:165
catapult::state::CompactMosaicMap::MosaicUnion::operator=
MosaicUnion & operator=(MosaicUnion &&rhs)
Definition: CompactMosaicMap.h:51
NonCopyable.h
catapult::state::CompactMosaicMap::basic_iterator_t::operator*
reference operator*() const
Returns a reference to the current value.
Definition: CompactMosaicMap.h:182