BioDynaMo  v1.05.119-a4ff3934
fixed_size_vector.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_FIXED_SIZE_VECTOR_H_
16 #define CORE_CONTAINER_FIXED_SIZE_VECTOR_H_
17 
18 #include <algorithm>
19 #include <cassert>
20 #include <cstddef>
21 #include <initializer_list>
22 
23 namespace bdm {
24 
29 template <typename T, std::size_t N>
31  public:
32  FixedSizeVector() = default;
33 
37  constexpr FixedSizeVector(const std::initializer_list<T>& l) {
38  assert(l.size() <= N);
39  auto it = l.begin();
40  for (size_t i = 0; i < N; i++) {
41  data_[i] = *(it++);
42  }
43  size_ = l.size();
44  }
45 
46  size_t size() const { return size_; } // NOLINT
47 
48  const T& operator[](size_t idx) const { return data_[idx]; }
49 
50  T& operator[](size_t idx) { return data_[idx]; }
51 
52  bool operator==(const FixedSizeVector& other) const {
53  if (size_ != other.size_) {
54  return false;
55  }
56  for (size_t i = 0; i < std::min(size_, other.size_); i++) {
57  if (data_[i] != other.data_[i]) {
58  return false;
59  }
60  }
61  return true;
62  }
63 
65 #pragma omp simd
66  for (size_t i = 0; i < N; i++) {
67  ++data_[i];
68  }
69  return *this;
70  }
71 
72  void clear() { size_ = 0; } // NOLINT
73 
74  void push_back(const T& value) { // NOLINT
75  assert(size_ < N);
76  data_[size_++] = value;
77  }
78 
79  const T* data() const { return data_; }
80 
81  const T* begin() const { return &(data_[0]); } // NOLINT
82  const T* end() const { return &(data_[size_]); } // NOLINT
83  T* begin() { return &(data_[0]); } // NOLINT
84  T* end() { return &(data_[size_]); } // NOLINT
85 
86  private:
87  T data_[N];
88  std::size_t size_ = 0;
89 };
90 
91 } // namespace bdm
92 
93 #endif // CORE_CONTAINER_FIXED_SIZE_VECTOR_H_
bdm::FixedSizeVector::begin
const T * begin() const
Definition: fixed_size_vector.h:81
bdm::FixedSizeVector::FixedSizeVector
constexpr FixedSizeVector(const std::initializer_list< T > &l)
Definition: fixed_size_vector.h:37
bdm::FixedSizeVector
Definition: fixed_size_vector.h:30
bdm::FixedSizeVector::size_
std::size_t size_
Definition: fixed_size_vector.h:88
bdm
Definition: agent.cc:39
bdm::FixedSizeVector::end
const T * end() const
Definition: fixed_size_vector.h:82
bdm::FixedSizeVector::begin
T * begin()
Definition: fixed_size_vector.h:83
bdm::FixedSizeVector::operator[]
const T & operator[](size_t idx) const
Definition: fixed_size_vector.h:48
bdm::FixedSizeVector::push_back
void push_back(const T &value)
Definition: fixed_size_vector.h:74
bdm::FixedSizeVector::end
T * end()
Definition: fixed_size_vector.h:84
bdm::FixedSizeVector::operator[]
T & operator[](size_t idx)
Definition: fixed_size_vector.h:50
bdm::FixedSizeVector::FixedSizeVector
FixedSizeVector()=default
bdm::FixedSizeVector::data_
T data_[N]
Definition: fixed_size_vector.h:87
bdm::FixedSizeVector::size
size_t size() const
Definition: fixed_size_vector.h:46
bdm::FixedSizeVector::clear
void clear()
Definition: fixed_size_vector.h:72
bdm::FixedSizeVector::data
const T * data() const
Definition: fixed_size_vector.h:79
bdm::FixedSizeVector::operator==
bool operator==(const FixedSizeVector &other) const
Definition: fixed_size_vector.h:52
bdm::FixedSizeVector::operator++
FixedSizeVector & operator++()
Definition: fixed_size_vector.h:64