BioDynaMo  v1.05.119-a4ff3934
type_index.cc
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 #include "core/type_index.h"
16 
17 #include <TClass.h>
18 
19 namespace bdm {
20 
21 // -----------------------------------------------------------------------------
22 void TypeIndex::Add(Agent* agent) {
23  auto& type_vector = data_[agent->IsA()];
24  auto uid = agent->GetUid();
25  if (index_.size() <= uid.GetIndex()) {
26  Reserve(uid.GetIndex() + 1);
27  }
28  index_.Insert(uid, type_vector.size());
29  type_vector.push_back(agent);
30 }
31 
32 // -----------------------------------------------------------------------------
33 void TypeIndex::Update(Agent* new_agent) {
34  auto idx = index_[new_agent->GetUid()];
35  auto& type_vector = data_[new_agent->IsA()];
36  type_vector[idx] = new_agent;
37 }
38 
39 // -----------------------------------------------------------------------------
40 void TypeIndex::Remove(Agent* agent) {
41  auto idx = index_[agent->GetUid()];
42  auto& type_vector = data_[agent->IsA()];
43  if (idx == type_vector.size() - 1) {
44  type_vector.pop_back();
45  } else {
46  // swap
47  auto* reordered = type_vector.back();
48  type_vector[idx] = reordered;
49  type_vector.pop_back();
50  index_.Insert(reordered->GetUid(), idx);
51  }
52 }
53 
54 // -----------------------------------------------------------------------------
56  index_.clear();
57  for (auto& pair : data_) {
58  pair.second.clear();
59  }
60 }
61 
62 // -----------------------------------------------------------------------------
63 void TypeIndex::Reserve(uint64_t capacity) {
64  if (index_.size() < capacity) {
65  index_.resize(capacity * 1.5);
66  }
67 }
68 
69 // -----------------------------------------------------------------------------
70 const std::vector<Agent*>& TypeIndex::GetType(TClass* tclass) const {
71  return data_[tclass];
72 }
73 
74 } // namespace bdm
bdm::TypeIndex::Add
void Add(Agent *agent)
Definition: type_index.cc:22
bdm
Definition: agent.cc:39
bdm::TypeIndex::data_
UnorderedFlatmap< TClass *, std::vector< Agent * > > data_
Definition: type_index.h:42
bdm::Agent::GetUid
const AgentUid & GetUid() const
Definition: agent.cc:123
bdm::TypeIndex::GetType
const std::vector< Agent * > & GetType(TClass *tclass) const
Definition: type_index.cc:70
bdm::AgentUidMap::size
uint64_t size() const
Definition: agent_uid_map.h:66
bdm::Agent
Contains code required by all agents.
Definition: agent.h:79
bdm::AgentUidMap::resize
void resize(uint64_t new_size)
Definition: agent_uid_map.h:48
bdm::TypeIndex::Remove
void Remove(Agent *agent)
Definition: type_index.cc:40
bdm::TypeIndex::Clear
void Clear()
Definition: type_index.cc:55
type_index.h
bdm::AgentUidMap::clear
void clear()
Definition: agent_uid_map.h:53
bdm::TypeIndex::Update
void Update(Agent *new_agent)
Definition: type_index.cc:33
bdm::TypeIndex::index_
AgentUidMap< uint64_t > index_
Definition: type_index.h:43
bdm::AgentUidMap::Insert
void Insert(const AgentUid &uid, const TValue &value)
Definition: agent_uid_map.h:85
bdm::TypeIndex::Reserve
void Reserve(uint64_t capacity)
Definition: type_index.cc:63