15 #include <string_view> 19 template <
typename KEY,
typename VALUE>
54 typename KEY,
typename VALUE,
unsigned int NUM_BYTES_HASH_TABLE = 1,
55 unsigned int NUM_HAS_TABLE_COLLISIONS_ALLOWED = 5,
56 typename VECTOR_T = std::array<
57 std::array<ts_map_entry<KEY, VALUE>, NUM_HAS_TABLE_COLLISIONS_ALLOWED>,
58 1u << (8 * NUM_BYTES_HASH_TABLE)>>
65 KEY, VALUE, NUM_BYTES_HASH_TABLE, NUM_HAS_TABLE_COLLISIONS_ALLOWED,
69 using vec_t = VECTOR_T;
75 const_iterator() :
m_vec(nullptr), m_parent(nullptr) {}
77 const VECTOR_T& vec,
const self_t& parent,
int idx_outer,
79 :
m_vec(const_cast<VECTOR_T*>(&vec)),
80 m_parent(const_cast<self_t*>(&parent)),
81 m_idx_outer(idx_outer),
82 m_idx_inner(idx_inner)
85 const_iterator& operator=(
const const_iterator& o)
88 m_idx_outer = o.m_idx_outer;
89 m_idx_inner = o.m_idx_inner;
94 return m_vec == o.m_vec && m_idx_outer == o.m_idx_outer &&
95 m_idx_inner == o.m_idx_inner;
97 bool operator!=(
const const_iterator& o)
const {
return !(*
this == o); }
100 return (*
m_vec)[m_idx_outer][m_idx_inner];
102 const value_type* operator->()
const 104 return &(*m_vec)[m_idx_outer][m_idx_inner];
108 const_iterator aux = *
this;
121 int m_idx_outer{0}, m_idx_inner{0};
129 static_cast<int>(NUM_HAS_TABLE_COLLISIONS_ALLOWED))
134 }
while (m_idx_outer < static_cast<int>(m_parent->m_vec.size()) &&
135 !(*
m_vec)[m_idx_outer][m_idx_inner].used);
139 struct iterator :
public const_iterator
142 iterator() : const_iterator() {}
143 iterator(VECTOR_T& vec, self_t& parent,
int idx_outer,
int idx_inner)
144 : const_iterator(vec, parent, idx_outer, idx_inner)
150 [const_iterator::m_idx_inner];
152 value_type* operator->()
154 return &(*const_iterator::m_vec)[const_iterator::m_idx_outer]
155 [const_iterator::m_idx_inner];
159 iterator aux = *
this;
165 const_iterator::incr();
185 for (
size_t oi = 0; oi <
m_vec.size(); oi++)
186 for (
size_t ii = 0; ii < NUM_HAS_TABLE_COLLISIONS_ALLOWED; ii++)
187 m_vec[oi][ii] = value_type();
198 std::array<ts_map_entry<KEY, VALUE>, NUM_HAS_TABLE_COLLISIONS_ALLOWED>&
199 match_arr =
m_vec[hash];
200 for (
unsigned int i = 0; i < NUM_HAS_TABLE_COLLISIONS_ALLOWED; i++)
202 if (!match_arr[i].used)
205 match_arr[i].used =
true;
206 match_arr[i].first = key;
207 return match_arr[i].second;
209 if (match_arr[i].
first == key)
return match_arr[i].second;
211 throw std::runtime_error(
"ts_hash_map: too many hash collisions!");
213 const_iterator
find(
const KEY& key)
const 220 match_arr =
m_vec[hash];
221 for (
unsigned int i = 0; i < NUM_HAS_TABLE_COLLISIONS_ALLOWED; i++)
223 if (match_arr[i].used && match_arr[i].
first == key)
224 return const_iterator(
m_vec, *
this, hash, i);
231 const_iterator it(
m_vec, *
this, 0, -1);
235 const_iterator
end()
const 237 return const_iterator(
m_vec, *
this,
m_vec.size(), 0);
241 iterator it(
m_vec, *
this, 0, -1);
245 iterator
end() {
return iterator(
m_vec, *
this,
m_vec.size(), 0); }
unsigned __int16 uint16_t
const_iterator find(const KEY &key) const
VALUE & operator[](const KEY &key)
Write/read via [i] operator, that creates an element if it didn't exist already.
Usage: uint_select_by_bytecount<N>type var; allows defining var as a unsigned integer with...
void reduced_hash(const std::string_view &value, uint8_t &hash)
hash function used by ts_hash_map.
ts_hash_map()=default
< Default constructor */
bool operator==(const mrpt::img::TCamera &a, const mrpt::img::TCamera &b)
unsigned __int64 uint64_t
const_iterator end() const
const_iterator begin() const
vec_t m_vec
The actual container.
GLsizei const GLfloat * value
iterator operator++(int)
A thread-safe (ts) container which minimally emulates a std::map<>'s [] and find() methods but which ...
unsigned __int32 uint32_t
bool operator!=(const mrpt::img::TCamera &a, const mrpt::img::TCamera &b)
void clear()
Clear the contents of this container.
size_t m_size
Number of elements accessed with write access so far.
std::vector< T1 > operator*(const std::vector< T1 > &a, const std::vector< T2 > &b)
a*b (element-wise multiplication)