BioDynaMo  v1.05.120-25dc9790
agent_uid_map.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_AGENT_UID_MAP_H_
16 #define CORE_CONTAINER_AGENT_UID_MAP_H_
17 
18 #include <limits>
19 #include <vector>
20 
21 #include "core/agent/agent_uid.h"
22 
23 namespace bdm {
24 
30 template <typename TValue>
31 class AgentUidMap {
32  struct Iterator {
34  uint64_t idx_;
35  };
36 
37  public:
38  AgentUidMap() = default;
39 
40  AgentUidMap(const AgentUidMap& other)
42 
43  explicit AgentUidMap(uint64_t initial_size) {
44  data_.resize(initial_size);
45  agent_uid_reused_.resize(initial_size, AgentUid::kReusedMax);
46  }
47 
48  void resize(uint64_t new_size) { // NOLINT
49  data_.resize(new_size);
50  agent_uid_reused_.resize(new_size, AgentUid::kReusedMax);
51  }
52 
53  void clear() { // NOLINT
54  for (auto& el : agent_uid_reused_) {
56  }
57  }
58 
59  void ParallelClear() {
60 #pragma omp parallel for
61  for (uint64_t i = 0; i < data_.size(); ++i) {
63  }
64  }
65 
66  uint64_t size() const { // NOLINT
67  return data_.size();
68  }
69 
70  void Remove(const AgentUid& key) {
71  if (key.GetIndex() >= data_.size()) {
72  return;
73  }
75  }
76 
77  bool Contains(const AgentUid& uid) const {
78  auto idx = uid.GetIndex();
79  if (idx >= data_.size()) {
80  return false;
81  }
82  return uid.GetReused() == agent_uid_reused_[idx];
83  }
84 
85  void Insert(const AgentUid& uid, const TValue& value) {
86  auto idx = uid.GetIndex();
87  data_[idx] = value;
88  agent_uid_reused_[idx] = uid.GetReused();
89  }
90 
91  const TValue& operator[](const AgentUid& key) const {
92  return data_[key.GetIndex()];
93  }
94 
95  typename AgentUid::Reused_t GetReused(uint64_t index) const {
96  return agent_uid_reused_[index];
97  }
98 
99  private:
100  std::vector<TValue> data_;
101  std::vector<typename AgentUid::Reused_t> agent_uid_reused_;
102 };
103 
104 } // namespace bdm
105 
106 #endif // CORE_CONTAINER_AGENT_UID_MAP_H_
bdm::AgentUidMap::AgentUidMap
AgentUidMap(uint64_t initial_size)
Definition: agent_uid_map.h:43
bdm::AgentUidMap::Iterator
Definition: agent_uid_map.h:32
agent_uid.h
bdm
Definition: agent.cc:39
bdm::AgentUidMap::AgentUidMap
AgentUidMap()=default
bdm::AgentUid::GetReused
Reused_t GetReused() const
Definition: agent_uid.h:41
bdm::AgentUidMap::Iterator::idx_
uint64_t idx_
Definition: agent_uid_map.h:34
bdm::AgentUidMap::size
uint64_t size() const
Definition: agent_uid_map.h:66
bdm::AgentUidMap::ParallelClear
void ParallelClear()
Definition: agent_uid_map.h:59
bdm::AgentUidMap::operator[]
const TValue & operator[](const AgentUid &key) const
Definition: agent_uid_map.h:91
bdm::AgentUidMap::Contains
bool Contains(const AgentUid &uid) const
Definition: agent_uid_map.h:77
bdm::AgentUidMap::resize
void resize(uint64_t new_size)
Definition: agent_uid_map.h:48
bdm::AgentUidMap::agent_uid_reused_
std::vector< typename AgentUid::Reused_t > agent_uid_reused_
Definition: agent_uid_map.h:101
bdm::AgentUidMap::data_
std::vector< TValue > data_
Definition: agent_uid_map.h:100
bdm::AgentUidMap::AgentUidMap
AgentUidMap(const AgentUidMap &other)
Definition: agent_uid_map.h:40
bdm::AgentUid::GetIndex
Index_t GetIndex() const
Definition: agent_uid.h:42
bdm::AgentUidMap::Remove
void Remove(const AgentUid &key)
Definition: agent_uid_map.h:70
bdm::AgentUidMap
Definition: agent_uid_map.h:31
bdm::AgentUidMap::Iterator::map_
AgentUidMap * map_
Definition: agent_uid_map.h:33
bdm::AgentUid
Definition: agent_uid.h:25
bdm::AgentUidMap::clear
void clear()
Definition: agent_uid_map.h:53
bdm::AgentUidMap::GetReused
AgentUid::Reused_t GetReused(uint64_t index) const
Definition: agent_uid_map.h:95
bdm::AgentUid::kReusedMax
static constexpr Reused_t kReusedMax
Definition: agent_uid.h:31
bdm::AgentUidMap::Insert
void Insert(const AgentUid &uid, const TValue &value)
Definition: agent_uid_map.h:85
bdm::AgentUid::Reused_t
uint32_t Reused_t
Definition: agent_uid.h:28