BioDynaMo  v1.05.159-3be850bb
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_
AgentUidMap(uint64_t initial_size)
Definition: agent_uid_map.h:43
AgentUid::Reused_t GetReused(uint64_t index) const
Definition: agent_uid_map.h:95
const TValue & operator[](const AgentUid &key) const
Definition: agent_uid_map.h:91
void Insert(const AgentUid &uid, const TValue &value)
Definition: agent_uid_map.h:85
AgentUidMap(const AgentUidMap &other)
Definition: agent_uid_map.h:40
void Remove(const AgentUid &key)
Definition: agent_uid_map.h:70
std::vector< typename AgentUid::Reused_t > agent_uid_reused_
AgentUidMap()=default
void resize(uint64_t new_size)
Definition: agent_uid_map.h:48
uint64_t size() const
Definition: agent_uid_map.h:66
bool Contains(const AgentUid &uid) const
Definition: agent_uid_map.h:77
std::vector< TValue > data_
Index_t GetIndex() const
Definition: agent_uid.h:42
Reused_t GetReused() const
Definition: agent_uid.h:41
static constexpr Reused_t kReusedMax
Definition: agent_uid.h:31
uint32_t Reused_t
Definition: agent_uid.h:28
Definition: agent.cc:39