BioDynaMo  v1.05.120-25dc9790
agent_uid.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_AGENT_AGENT_UID_H_
16 #define CORE_AGENT_AGENT_UID_H_
17 
18 #include <limits>
19 #include "core/util/root.h"
20 
21 namespace bdm {
22 
25 class AgentUid {
26  public:
27  using Index_t = uint32_t;
28  using Reused_t = uint32_t;
29  friend std::hash<AgentUid>;
30 
31  static constexpr Reused_t kReusedMax = std::numeric_limits<Reused_t>::max();
32 
33  constexpr AgentUid() noexcept
34  : index_(std::numeric_limits<Index_t>::max()),
35  reused_(std::numeric_limits<Reused_t>::max()) {}
36 
37  explicit AgentUid(Index_t index) : index_(index), reused_(0) {}
38 
39  AgentUid(Index_t idx, Reused_t reused) : index_(idx), reused_(reused) {}
40 
41  Reused_t GetReused() const { return reused_; }
42  Index_t GetIndex() const { return index_; }
43 
44  // Operators needed for nanoflann kd-tree implementation
45  void operator++() { ++index_; }
46  void operator--() { --index_; }
47 
48  bool operator==(const AgentUid& other) const {
49  return index_ == other.index_ && reused_ == other.reused_;
50  }
51 
52  bool operator!=(const AgentUid& other) const { return !(*this == other); }
53 
54  bool operator<(const AgentUid& other) const {
55  if (reused_ == other.reused_) {
56  return index_ < other.index_;
57  } else {
58  return reused_ < other.reused_;
59  }
60  }
61 
62  bool operator<(size_t other) const { return index_ < other; }
63 
64  AgentUid operator+(int i) const {
65  AgentUid uid(*this);
66  uid.index_ += i;
67  return uid;
68  }
69 
70  AgentUid operator+(uint64_t i) const {
71  AgentUid uid(*this);
72  uid.index_ += i;
73  return uid;
74  }
75 
76  AgentUid operator-(int i) const {
77  AgentUid uid(*this);
78  uid.index_ -= i;
79  return uid;
80  }
81 
82  AgentUid operator-(uint64_t i) const {
83  AgentUid uid(*this);
84  uid.index_ -= i;
85  return uid;
86  }
87 
88  AgentUid& operator+=(const AgentUid& uid) {
89  index_ += uid.index_;
90  return *this;
91  }
92 
93  operator uint64_t() const {
94  return (static_cast<uint64_t>(reused_) << 32) |
95  static_cast<uint64_t>(index_);
96  }
97 
98  friend std::ostream& operator<<(std::ostream& stream,
99  const AgentUid& handle) {
100  stream << handle.index_ << "-" << handle.reused_;
101  return stream;
102  }
103 
104  private:
108 
111 
113 };
114 
115 } // namespace bdm
116 
117 namespace std {
118 
119 template <>
120 struct hash<bdm::AgentUid> {
121  std::size_t operator()(const bdm::AgentUid& uid) const noexcept {
122  // at any given time in a simulation there exists only one
123  // agent with a specific index_ value.
124  // Therefore it is sufficient as a hash.
125  // This might change for the distributed runtime.
126  return uid.index_;
127  }
128 };
129 
130 } // namespace std
131 
132 #endif // CORE_AGENT_AGENT_UID_H_
std::hash< bdm::AgentUid >::operator()
std::size_t operator()(const bdm::AgentUid &uid) const noexcept
Definition: agent_uid.h:121
bdm::AgentUid::operator++
void operator++()
Definition: agent_uid.h:45
bdm::AgentUid::operator<
bool operator<(size_t other) const
Definition: agent_uid.h:62
bdm
Definition: agent.cc:39
bdm::AgentUid::Index_t
uint32_t Index_t
Definition: agent_uid.h:27
bdm::AgentUid::operator<<
friend std::ostream & operator<<(std::ostream &stream, const AgentUid &handle)
Definition: agent_uid.h:98
bdm::AgentUid::index_
Index_t index_
Definition: agent_uid.h:107
bdm::AgentUid::GetReused
Reused_t GetReused() const
Definition: agent_uid.h:41
bdm::AgentUid::operator==
bool operator==(const AgentUid &other) const
Definition: agent_uid.h:48
bdm::AgentUid::operator-
AgentUid operator-(int i) const
Definition: agent_uid.h:76
bdm::AgentUid::AgentUid
AgentUid(Index_t index)
Definition: agent_uid.h:37
bdm::AgentUid::operator+=
AgentUid & operator+=(const AgentUid &uid)
Definition: agent_uid.h:88
bdm::AgentUid::AgentUid
AgentUid(Index_t idx, Reused_t reused)
Definition: agent_uid.h:39
bdm::AgentUid::BDM_CLASS_DEF_NV
BDM_CLASS_DEF_NV(AgentUid, 1)
bdm::AgentUid::GetIndex
Index_t GetIndex() const
Definition: agent_uid.h:42
root.h
bdm::AgentUid::operator<
bool operator<(const AgentUid &other) const
Definition: agent_uid.h:54
bdm::AgentUid::operator!=
bool operator!=(const AgentUid &other) const
Definition: agent_uid.h:52
bdm::AgentUid::operator+
AgentUid operator+(uint64_t i) const
Definition: agent_uid.h:70
bdm::AgentUid::AgentUid
constexpr AgentUid() noexcept
Definition: agent_uid.h:33
bdm::AgentUid
Definition: agent_uid.h:25
std
Definition: agent_uid.h:117
bdm::AgentUid::reused_
Reused_t reused_
Determines how often index_ has been resused.
Definition: agent_uid.h:110
bdm::AgentUid::operator-
AgentUid operator-(uint64_t i) const
Definition: agent_uid.h:82
bdm::AgentUid::kReusedMax
static constexpr Reused_t kReusedMax
Definition: agent_uid.h:31
bdm::AgentUid::operator--
void operator--()
Definition: agent_uid.h:46
bdm::AgentUid::Reused_t
uint32_t Reused_t
Definition: agent_uid.h:28
bdm::AgentUid::operator+
AgentUid operator+(int i) const
Definition: agent_uid.h:64