BioDynaMo  v1.05.120-25dc9790
parallel_resize_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_PARALLEL_RESIZE_VECTOR_H_
16 #define CORE_CONTAINER_PARALLEL_RESIZE_VECTOR_H_
17 
18 #include <cstdlib>
19 #include <vector>
20 
21 #include "core/util/root.h"
22 
23 namespace bdm {
24 
26 template <typename T>
28  public:
29  using iterator = T*;
30  using const_iterator = const T*;
31  using value_type = T;
32 
33  explicit ParallelResizeVector(TRootIOCtor* io_ctor) {
34  } // Constructor for ROOT I/O
35  ParallelResizeVector() = default;
36  ParallelResizeVector(std::initializer_list<T> init) {
37  reserve(init.size());
38  for (auto& el : init) {
39  push_back(el);
40  }
41  }
42 
43  ParallelResizeVector(std::size_t new_size, const T& t = T()) {
44  resize(new_size, t);
45  }
46 
48  if (other.data_ != nullptr && other.capacity_ != 0) {
49  reserve(other.capacity_);
50 // initialize using copy ctor
51 #pragma omp parallel for
52  for (std::size_t i = 0; i < other.size_; i++) {
53  new (&(data_[i])) T(other.data_[i]);
54  }
55  }
56  size_ = other.size_;
57  capacity_ = other.capacity_;
58  }
59 
61  if (data_ != nullptr) {
62 #pragma omp parallel for
63  for (std::size_t i = 0; i < size_; i++) {
64  data_[i].~T();
65  }
66  capacity_ = 0;
67  free(data_);
68  data_ = nullptr;
69  }
70  }
71 
72  std::size_t size() const { return size_; } // NOLINT
73 
74  T* data() noexcept { return data_; } // NOLINT
75  const T* data() const noexcept { return data_; } // NOLINT
76 
77  void swap(ParallelResizeVector& other) { // NOLINT
78  // size_
79  size_ ^= other.size_;
80  other.size_ ^= size_;
81  size_ ^= other.size_;
82  // capacity_
83  capacity_ ^= other.capacity_;
84  other.capacity_ ^= capacity_;
85  capacity_ ^= other.capacity_;
86  // data_
87  auto* tmp = data_;
88  data_ = other.data_;
89  other.data_ = tmp;
90  }
91 
92  std::size_t capacity() const { return capacity_; } // NOLINT
93 
94  void push_back(const T& element) { // NOLINT
95  if (capacity_ == size_) {
97  }
98  new (&(data_[size_++])) T(element);
99  }
100 
101  void reserve(std::size_t new_capacity) { // NOLINT
102  if (new_capacity > capacity_) {
103  T* new_data = static_cast<T*>(malloc(new_capacity * sizeof(T)));
104  if (data_ != nullptr) {
105 // initialize using copy ctor
106 #pragma omp parallel for
107  for (std::size_t i = 0; i < size_; i++) {
108  new (&(new_data[i])) T(data_[i]);
109  }
110 // destruct old elements
111 #pragma omp parallel for
112  for (std::size_t i = 0; i < size_; i++) {
113  data_[i].~T();
114  }
115  free(data_);
116  }
117  data_ = new_data;
118  capacity_ = new_capacity;
119  }
120  }
121 
122  void resize(std::size_t new_size, const T& t = T()) { // NOLINT
123  if (capacity_ < new_size) {
124  reserve(new_size);
125  }
126 
127 // grow
128 #pragma omp parallel for
129  for (std::size_t i = size_; i < new_size; i++) {
130  new (&(data_[i])) T(t);
131  }
132 // shrink
133 #pragma omp parallel for
134  for (std::size_t i = new_size; i < size_; i++) {
135  data_[i].~T();
136  }
137  size_ = new_size;
138  }
139 
140  void clear() { // NOLINT
141  for (std::size_t i = 0; i < size_; i++) {
142  data_[i].~T();
143  }
144  size_ = 0;
145  }
146 
148  if (&other == this) {
149  return *this;
150  }
151 
152  clear();
153  // the following function call ensures that at least other.capacity_
154  // elements can be stored. If capacity_ > other.capacity_
155  // the container is NOT shrank.
156  reserve(other.capacity_);
157  size_ = other.size_;
158 
159 #pragma omp parallel for
160  for (std::size_t i = 0; i < size_; i++) {
161  new (&(data_[i])) T(other.data_[i]);
162  }
163  return *this;
164  }
165 
166  T& operator[](std::size_t index) { return data_[index]; }
167 
168  const T& operator[](std::size_t index) const { return data_[index]; }
169 
170  iterator begin() { return &(data_[0]); } // NOLINT
171 
172  iterator end() { return &(data_[size_]); } // NOLINT
173 
174  const_iterator cbegin() { return &(data_[0]); } // NOLINT
175 
176  const_iterator cend() { return &(data_[size_]); } // NOLINT
177 
178  private:
179  static constexpr float kGrowFactor = 1.5;
180  std::size_t size_ = 0;
181  std::size_t capacity_ = 0;
182  T* data_ = nullptr; //[capacity_] // NOLINT
183  BDM_CLASS_DEF(ParallelResizeVector, 1); // NOLINT
184 };
185 
186 } // namespace bdm
187 
188 #endif // CORE_CONTAINER_PARALLEL_RESIZE_VECTOR_H_
bdm::ParallelResizeVector::end
iterator end()
Definition: parallel_resize_vector.h:172
bdm::ParallelResizeVector::kGrowFactor
static constexpr float kGrowFactor
Definition: parallel_resize_vector.h:179
bdm
Definition: agent.cc:39
bdm::ParallelResizeVector::ParallelResizeVector
ParallelResizeVector(std::initializer_list< T > init)
Definition: parallel_resize_vector.h:36
bdm::ParallelResizeVector::size
std::size_t size() const
Definition: parallel_resize_vector.h:72
bdm::ParallelResizeVector::begin
iterator begin()
Definition: parallel_resize_vector.h:170
bdm::ParallelResizeVector::operator=
ParallelResizeVector & operator=(const ParallelResizeVector &other)
Definition: parallel_resize_vector.h:147
bdm::ParallelResizeVector::push_back
void push_back(const T &element)
Definition: parallel_resize_vector.h:94
bdm::ParallelResizeVector
std::vector with parallel resize
Definition: parallel_resize_vector.h:27
bdm::ParallelResizeVector::ParallelResizeVector
ParallelResizeVector(std::size_t new_size, const T &t=T())
Definition: parallel_resize_vector.h:43
bdm::ParallelResizeVector::data_
T * data_
Definition: parallel_resize_vector.h:182
bdm::UniformGridEnvironment::Box
A single unit cube of the grid.
Definition: uniform_grid_environment.h:69
bdm::ParallelResizeVector::operator[]
const T & operator[](std::size_t index) const
Definition: parallel_resize_vector.h:168
bdm::ParallelResizeVector::ParallelResizeVector
ParallelResizeVector(const ParallelResizeVector &other)
Definition: parallel_resize_vector.h:47
bdm::ParallelResizeVector::operator[]
T & operator[](std::size_t index)
Definition: parallel_resize_vector.h:166
bdm::ParallelResizeVector::cend
const_iterator cend()
Definition: parallel_resize_vector.h:176
bdm::ParallelResizeVector::reserve
void reserve(std::size_t new_capacity)
Definition: parallel_resize_vector.h:101
bdm::ParallelResizeVector::BDM_CLASS_DEF
BDM_CLASS_DEF(ParallelResizeVector, 1)
bdm::ParallelResizeVector::size_
std::size_t size_
Definition: parallel_resize_vector.h:180
root.h
bdm::ParallelResizeVector::capacity_
std::size_t capacity_
Definition: parallel_resize_vector.h:181
bdm::ParallelResizeVector::~ParallelResizeVector
virtual ~ParallelResizeVector()
Definition: parallel_resize_vector.h:60
bdm::ParallelResizeVector::data
T * data() noexcept
Definition: parallel_resize_vector.h:74
bdm::ParallelResizeVector::ParallelResizeVector
ParallelResizeVector(TRootIOCtor *io_ctor)
Definition: parallel_resize_vector.h:33
bdm::ParallelResizeVector::resize
void resize(std::size_t new_size, const T &t=T())
Definition: parallel_resize_vector.h:122
bdm::ParallelResizeVector::data
const T * data() const noexcept
Definition: parallel_resize_vector.h:75
bdm::ParallelResizeVector::cbegin
const_iterator cbegin()
Definition: parallel_resize_vector.h:174
bdm::ParallelResizeVector::ParallelResizeVector
ParallelResizeVector()=default
bdm::ParallelResizeVector::clear
void clear()
Definition: parallel_resize_vector.h:140
bdm::ParallelResizeVector::capacity
std::size_t capacity() const
Definition: parallel_resize_vector.h:92
bdm::ParallelResizeVector::swap
void swap(ParallelResizeVector &other)
Definition: parallel_resize_vector.h:77