1 #ifndef TrackingTools_TrajectoryCleaning_src_OtherHashMaps
2 #define TrackingTools_TrajectoryCleaning_src_OtherHashMaps
10 #include <type_traits>
26 typename Hasher = std::hash<K>,
27 typename Equals = std::equal_to<K>,
28 typename Alloc = std::allocator<V> >
31 static_assert(std::conjunction<std::is_trivially_destructible<K>, std::is_trivially_destructible<V> >::
value);
43 void clear(
size_t newBucketSize = 0) {
44 if (newBucketSize != 0) {
85 template <
typename Item>
106 bool good()
const {
return (
it_ !=
nullptr); }
126 typename std::list<KeyItem *>::iterator
132 typename std::list<ValueItem *>::iterator
149 std::cout <<
"Dumping HASH MULTIMAP" << std::endl;
160 std::cout <<
" [" << row <<
"] " << *
p << std::endl;
169 std::cout <<
" @ " <<
p <<
" [" <<
p->key <<
", @" <<
p->value <<
"], next = " <<
p->next << std::endl;
171 std::cout <<
" ^^^ this was the last valid item." << std::endl;
180 std::cout <<
" Value Items " << std::endl;
186 std::cout <<
" @ " <<
p <<
" [" <<
p->value <<
"], next = " <<
p->next << std::endl;
188 std::cout <<
" ^^^ this was the last valid item." << std::endl;
196 std::cout <<
" End of dump" << std::endl;
200 template <
typename K,
typename V,
typename Hasher,
typename Equals,
typename Alloc>
205 : bucketSize_(buckets),
206 bucketCapacity_(bucketSize_),
207 keyRowSize_(keyRowSize),
208 valueRowSize_(valueRowSize),
215 template <
typename K,
typename V,
typename Hasher,
typename Equals,
typename Alloc>
217 for (
typename std::list<KeyItem *>::iterator it = keyRows_.begin(),
last = keyRows_.end(); it !=
last; ++it) {
218 keyAlloc_.deallocate(*it, keyRowSize_);
220 for (
typename std::list<ValueItem *>::iterator it = valueRows_.begin(),
last = valueRows_.end(); it !=
last; ++it) {
221 valueAlloc_.deallocate(*it, valueRowSize_);
223 ptrAlloc_.deallocate(buckets_, bucketCapacity_);
226 template <
typename K,
typename V,
typename Hasher,
typename Equals,
typename Alloc>
228 if (keyRows_.size() > maxRows_) {
230 typename std::list<KeyItem *>::iterator it = keyRows_.begin(),
last = keyRows_.end();
231 for (std::advance(it, maxRows_); it !=
last; ++it) {
232 keyAlloc_.deallocate(*it, keyRowSize_);
234 keyRows_.resize(maxRows_);
236 if (valueRows_.size() > maxRows_) {
238 typename std::list<ValueItem *>::iterator it = valueRows_.begin(),
last = valueRows_.end();
239 for (std::advance(it, maxRows_); it !=
last; ++it) {
240 valueAlloc_.deallocate(*it, valueRowSize_);
242 valueRows_.resize(maxRows_);
246 template <
typename K,
typename V,
typename Hasher,
typename Equals,
typename Alloc>
249 if (nextKeyItem_ == keyEndMarker_) {
251 if (currentKeyRow_ == keyRows_.end()) {
252 keyRows_.push_back(keyAlloc_.allocate(keyRowSize_));
253 currentKeyRow_ = keyRows_.end();
256 nextKeyItem_ = *currentKeyRow_;
257 keyEndMarker_ = nextKeyItem_ + keyRowSize_;
261 return (nextKeyItem_ - 1);
263 template <
typename K,
typename V,
typename Hasher,
typename Equals,
typename Alloc>
266 if (nextValueItem_ == valueEndMarker_) {
268 if (currentValueRow_ == valueRows_.end()) {
269 valueRows_.push_back(valueAlloc_.allocate(valueRowSize_));
270 currentValueRow_ = valueRows_.end();
273 nextValueItem_ = *currentValueRow_;
274 valueEndMarker_ = nextValueItem_ + valueRowSize_;
278 return (nextValueItem_ - 1);
281 template <
typename K,
typename V,
typename Hasher,
typename Equals,
typename Alloc>
289 if (eq_(curr->
key,
key)) {
295 buck = push_back_(
key, buck);
299 template <
typename K,
typename V,
typename Hasher,
typename Equals,
typename Alloc>
304 k.value = push_back_(
value,
k.value);
307 template <
typename K,
typename V,
typename Hasher,
typename Equals,
typename Alloc>
312 for (
KeyItem *curr = buckets_[
hash % bucketSize_]; curr; curr = curr->
next) {
313 if (eq_(curr->key,
key))
324 template <
typename K,
typename V>
329 typedef typename std::vector<value_type>::iterator
iterator;
339 for (
typename std::vector<value_type>::iterator it =
data_.begin(), ed =
data_.end(); it != ed; ++it) {
344 return data_.back().second;