BioDynaMo  v1.05.120-25dc9790
flatmap.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #ifndef CORE_CONTAINER_FLATMAP_H_
16 #define CORE_CONTAINER_FLATMAP_H_
17 
18 #include <cassert>
19 #include <cstddef>
20 #include <cstdint>
21 #include <utility>
22 #include <vector>
23 #include "core/util/string.h"
24 
25 namespace bdm {
26 
27 template <typename TKey, typename TValue>
29  public:
30  using value_type = TValue;
31  using Pair = std::pair<TKey, TValue>;
32  using Iterator = Pair*;
33  using ConstIterator = const Pair*;
34 
35  UnorderedFlatmap() = default;
36 
37  void reserve(uint64_t new_capacity) {
38  if (new_capacity > size_) {
39  data_.resize(new_capacity);
40  }
41  }
42 
43  size_t size() const { return size_; } // NOLINT
44 
45  size_t Capacity() const { return data_.capacity(); }
46 
47  void clear() { size_ = 0; }
48 
49  const TValue& at(const TKey& key) const {
50  auto idx = FindIndexConst(key);
51  assert(idx < size_ &&
52  Concat("Key (", key, ") not found in UnorderedFlatmap").c_str());
53  return data_.at(idx).second;
54  }
55 
56  void insert(Pair&& pair) {
57  if (size_ <= data_.size()) {
58  data_.resize((size_ + 1) * 2);
59  }
60  data_[size_++] = pair;
61  }
62 
63  TValue& operator[](const TKey& key) { return data_[FindIndex(key)].second; }
64 
65  const TValue& operator[](const TKey& key) const {
66  return data_[FindIndexConst(key)].second;
67  }
68 
69  Iterator find(const TKey& key) { return &(data_[FindIndexConst(key)]); }
70 
71  ConstIterator find(const TKey& key) const {
72  return &(data_[FindIndexConst(key)]);
73  }
74 
75  Iterator begin() { return &(data_[0]); }
76  ConstIterator begin() const { return &(data_[0]); }
77  Iterator end() { return &(data_[size_]); }
78  ConstIterator end() const { return &(data_[size_]); }
79 
80  private:
81  std::vector<Pair> data_;
82  std::size_t size_ = 0;
83 
84  uint64_t FindIndex(const TKey& key) {
85  for (uint64_t i = 0; i < size_; i++) {
86  if (data_[i].first == key) {
87  return i;
88  }
89  }
90  // insert new element
91  if (size_ <= data_.size()) {
92  data_.resize((size_ + 1) * 2);
93  }
94  data_[size_].first = key;
95  return size_++;
96  }
97 
98  uint64_t FindIndexConst(const TKey& key) const {
99  for (uint64_t i = 0; i < size_; i++) {
100  if (data_[i].first == key) {
101  return i;
102  }
103  }
104  return size_;
105  }
106 };
107 
108 } // namespace bdm
109 
110 #endif // CORE_CONTAINER_FLATMAP_H_
bdm::UnorderedFlatmap< std::size_t, bdm::memory_manager_detail::PoolAllocator * >::Iterator
Pair * Iterator
Definition: flatmap.h:32
bdm::UnorderedFlatmap::FindIndexConst
uint64_t FindIndexConst(const TKey &key) const
Definition: flatmap.h:98
bdm::UnorderedFlatmap::clear
void clear()
Definition: flatmap.h:47
bdm
Definition: agent.cc:39
string.h
bdm::UnorderedFlatmap::UnorderedFlatmap
UnorderedFlatmap()=default
bdm::UnorderedFlatmap::reserve
void reserve(uint64_t new_capacity)
Definition: flatmap.h:37
bdm::UnorderedFlatmap::end
Iterator end()
Definition: flatmap.h:77
bdm::UnorderedFlatmap< std::size_t, bdm::memory_manager_detail::PoolAllocator * >::ConstIterator
const Pair * ConstIterator
Definition: flatmap.h:33
bdm::UnorderedFlatmap::begin
Iterator begin()
Definition: flatmap.h:75
bdm::UnorderedFlatmap::end
ConstIterator end() const
Definition: flatmap.h:78
bdm::UnorderedFlatmap::FindIndex
uint64_t FindIndex(const TKey &key)
Definition: flatmap.h:84
bdm::UnorderedFlatmap::size
size_t size() const
Definition: flatmap.h:43
bdm::UnorderedFlatmap::operator[]
const TValue & operator[](const TKey &key) const
Definition: flatmap.h:65
bdm::UnorderedFlatmap::find
ConstIterator find(const TKey &key) const
Definition: flatmap.h:71
bdm::UnorderedFlatmap::Capacity
size_t Capacity() const
Definition: flatmap.h:45
bdm::Concat
std::string Concat(const Args &... parts)
Concatenates all arguments into a string. Equivalent to streaming all arguments into a stringstream a...
Definition: string.h:70
bdm::UnorderedFlatmap::insert
void insert(Pair &&pair)
Definition: flatmap.h:56
bdm::UnorderedFlatmap::find
Iterator find(const TKey &key)
Definition: flatmap.h:69
bdm::UnorderedFlatmap::operator[]
TValue & operator[](const TKey &key)
Definition: flatmap.h:63
bdm::UnorderedFlatmap::data_
std::vector< Pair > data_
Definition: flatmap.h:81
bdm::UnorderedFlatmap< std::size_t, bdm::memory_manager_detail::PoolAllocator * >::Pair
std::pair< std::size_t, bdm::memory_manager_detail::PoolAllocator * > Pair
Definition: flatmap.h:31
bdm::UnorderedFlatmap::size_
std::size_t size_
Definition: flatmap.h:82
bdm::UnorderedFlatmap::at
const TValue & at(const TKey &key) const
Definition: flatmap.h:49
bdm::UnorderedFlatmap::begin
ConstIterator begin() const
Definition: flatmap.h:76
bdm::memory_manager_detail::PoolAllocator
Definition: memory_manager.h:129
bdm::UnorderedFlatmap
Definition: flatmap.h:28