openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
SF_vector.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // openCARP is an open cardiac electrophysiology simulator.
3 //
4 // Copyright (C) 2020 openCARP project
5 //
6 // This program is licensed under the openCARP Academic Public License (APL)
7 // v1.0: You can use and redistribute it and/or modify it in non-commercial
8 // academic environments under the terms of APL as published by the openCARP
9 // project v1.0, or (at your option) any later version. Commercial use requires
10 // a commercial license (info@opencarp.org).
11 //
12 // This program is distributed without any warranty; see the openCARP APL for
13 // more details.
14 //
15 // You should have received a copy of the openCARP APL along with this program
16 // and can find it online: http://www.opencarp.org/license
17 // ----------------------------------------------------------------------------
18 
27 #ifndef _SF_VECTOR_H
28 #define _SF_VECTOR_H
29 
30 #include <iterator>
31 #include <cstring>
32 
33 namespace SF {
34 
41 template <class T>
42 class vector
43 {
44 public:
46  vector(): _data(NULL), _capacity(0), _size(0)
47  {}
48 
50  vector(size_t n): _data(NULL), _capacity(0), _size(0)
51  {
52  this->resize(n);
53  }
54 
56  vector(size_t n, const T val): _data(NULL), _capacity(0), _size(0)
57  {
58  this->assign(n, val);
59  }
60 
62  vector(const vector<T> &vec): _data(NULL), _capacity(0), _size(0)
63  {
64  this->assign(vec.begin(), vec.end());
65  }
66 
67  virtual ~vector()
68  {
69  delete [] _data;
70  }
71 
73  inline const T& operator[](size_t i) const
74  {
75  return _data[i];
76  }
77 
79  inline T& operator[](size_t i)
80  {
81  return _data[i];
82  }
83 
85  inline void operator= (const vector<T> & vec)
86  {
87  this->assign(vec.begin(), vec.end());
88  }
89 
91  inline T* data()
92  {
93  return _data;
94  }
95 
97  inline const T* data() const
98  {
99  return _data;
100  }
101 
102 
104  inline size_t size() const
105  {
106  return _size;
107  }
108 
110  inline size_t capacity() const
111  {
112  return _capacity;
113  }
114 
116  inline const T* begin() const
117  {
118  return _data;
119  }
120 
122  inline T* begin()
123  {
124  return _data;
125  }
126 
128  inline const T* end() const
129  {
130  return _data + _size;
131  }
132 
134  inline T* end()
135  {
136  return _data + _size;
137  }
138 
139  T& front()
140  {
141  return _data[0];
142  }
143 
144  T& back()
145  {
146  return _data[_size - 1];
147  }
148 
149  const T& front() const
150  {
151  return _data[0];
152  }
153 
154  const T& back() const
155  {
156  return _data[_size - 1];
157  }
158 
160  template<class InputIterator>
161  inline void assign(InputIterator s, InputIterator e)
162  {
163  long int n = std::distance(s, e);
164  assert(n >= 0);
165 
166  // realloc if necessary
167  if ( ((long int)_capacity) < n) {
168  delete [] _data;
169  _data = new T[n];
170  _capacity = n;
171  }
172 
173  // copy data
174  size_t idx = 0;
175  while(s != e) {
176  _data[idx++] = *s;
177  ++s;
178  }
179 
180  _size = n;
181  }
182 
184  inline void assign(size_t n, T val = T()) {
185  if (_capacity < n) {
186  if(_size > 0) delete [] _data;
187  _data = new T[n];
188  _capacity = n;
189  }
190 
191  for (size_t i = 0; i < n; i++) _data[i] = val;
192  _size = n;
193  }
194 
201  inline void assign(size_t n, T* array, bool del = true) {
202  if (del) delete [] _data;
203  _data = array;
204  _size = n;
205  _capacity = n;
206  }
207 
209  inline void resize(size_t n)
210  {
211  if(_capacity < n)
212  {
213  if(_size > 0) {
214  T* buf = _data;
215  _data = new T[n];
216  for (size_t i = 0; i < _size; i++) _data[i] = buf[i];
217  delete [] buf;
218  }
219  else {
220  _data = new T[n];
221  }
222  _capacity = n;
223  }
224  _size = n;
225  }
226 
228  inline void resize(size_t n, const T val)
229  {
230  size_t oldsize = this->size();
231  this->resize(n);
232 
233  if(n > oldsize) {
234  T* out = _data + oldsize;
235  size_t num = n - oldsize;
236 
237  for (size_t i = 0; i < num; i++) out[i] = val;
238  }
239  }
240 
241  void reserve(size_t n)
242  {
243  size_t osize = _size;
244  this->resize(n);
245  this->resize(osize);
246  }
247 
248  void zero()
249  {
250  if(_size != 0) memset(_data, 0, _size * sizeof(T));
251  }
252 
257  inline void reallocate()
258  {
259  _capacity = _size;
260  T* buf = _data;
261  _data = new T[_size];
262  for (size_t i = 0; i < _size; i++) _data[i] = buf[i];
263  delete [] buf;
264  }
265 
267  template<class InputIterator>
268  inline void append(InputIterator s, InputIterator e)
269  {
270  size_t addsize = std::distance(s, e);
271  size_t oldsize = _size;
272 
273  this->resize(oldsize + addsize);
274 
275  // copy data
276  size_t idx = 0;
277  while(s != e) {
278  _data[oldsize + idx++] = *s;
279  ++s;
280  }
281  }
282 
283  T & push_back(T val)
284  {
285  if(_capacity > _size)
286  {
287  _data[_size] = val;
288  _size++;
289  }
290  else {
291  // if current capacity is not larger than size + 1 we reallocate, but keep size to
292  // old value plus one.
293  size_t newcap = _capacity > 0 ? _capacity * 2 : 1;
294  size_t cursize = _size;
295  this->resize(newcap);
296  _data[cursize] = val;
297  _size = cursize + 1;
298  }
299  return _data[_size-1];
300  }
301 private:
302  T* _data;
303  size_t _capacity;
304  size_t _size;
305 
306 };
307 
309 template<class T>
310 inline void dsp_from_cnt(const vector<T> & cnt, vector<T> & dsp)
311 {
312  dsp.resize(cnt.size()+1);
313  dsp[0] = 0;
314  for(size_t i=0; i<cnt.size(); i++) dsp[i+1] = dsp[i] + cnt[i];
315 }
316 
318 template<class T>
319 inline void cnt_from_dsp(const vector<T> & dsp, vector<T> & cnt)
320 {
321  cnt.resize(dsp.size() - 1);
322  for(size_t i=0; i<dsp.size()-1; i++) cnt[i] = dsp[i+1] - dsp[i];
323 }
324 
331 template<class T, class S>
332 inline void count(const vector<T> & data, vector<S> & cnt)
333 {
334  cnt.zero();
335  for(size_t i=0; i<data.size(); i++) cnt[data[i]]++;
336 }
337 
339 template<class T>
340 inline T sum(const vector<T> & vec)
341 {
342  T sum = 0;
343  for(size_t i=0; i<vec.size(); i++) sum += vec[i];
344  return sum;
345 }
346 
347 
349 template<class T>
350 inline void interval(vector<T> & vec, size_t start, size_t end)
351 {
352  vec.resize(end - start);
353  for(size_t i=0; i<vec.size(); i++) vec[i] = start + i;
354 }
355 
357 template<class T>
358 inline void divide(const size_t gsize, const size_t num_parts, vector<T> & loc_sizes)
359 {
360  // initialize the distribution to gsize / size
361  loc_sizes.assign(size_t(num_parts), T(gsize/num_parts));
362 
363  // then we evenly distribute the remainder of the division
364  for(size_t i=0; i<size_t(gsize % num_parts); i++) loc_sizes[i]++;
365 }
366 
367 
370 template<class S, class V>
371 inline void vec_assign(S* lhs, const V* rhs, size_t size)
372 {
373  for(size_t i=0; i<size; i++) lhs[i] = (S)rhs[i];
374 }
375 
377 template<class T>
378 inline bool isEmpty(vector<T> & v)
379 {
380  bool ret = true;
381  for(size_t i=0; i<v.size(); i++)
382  if(v[i] != T(0)) {ret = false; break;}
383 
384  return ret;
385 }
386 
387 
388 }
389 
390 #endif
391 
vector(size_t n)
Initialize a vector of size n.
Definition: SF_vector.h:50
Definition: dense_mat.hpp:34
void resize(size_t n, const T val)
Resize a vector setting the newly allocated elements to val.
Definition: SF_vector.h:228
void zero()
Definition: SF_vector.h:248
size_t capacity() const
The maximum amount of entries the vector can hold without reallocating.
Definition: SF_vector.h:110
T & front()
Definition: SF_vector.h:139
void assign(size_t n, T val=T())
Assign n many elements of value val.
Definition: SF_vector.h:184
void assign(size_t n, T *array, bool del=true)
Make a vector point to an existing array.
Definition: SF_vector.h:201
void interval(vector< T > &vec, size_t start, size_t end)
Create an integer interval between start and end.
Definition: SF_vector.h:350
void divide(const size_t gsize, const size_t num_parts, vector< T > &loc_sizes)
divide gsize into num_parts local parts with even distribution of the remainder
Definition: SF_vector.h:358
void count(const vector< T > &data, vector< S > &cnt)
Count number of occurrences of indices.
Definition: SF_vector.h:332
T * data()
Pointer to the vector&#39;s start.
Definition: SF_vector.h:91
T & push_back(T val)
Definition: SF_vector.h:283
vector(const vector< T > &vec)
Initialize a vector from another vector.
Definition: SF_vector.h:62
T * end()
Pointer to the vector&#39;s end.
Definition: SF_vector.h:134
const T * end() const
Pointer to the vector&#39;s end.
Definition: SF_vector.h:128
T & operator[](size_t i)
Vector access.
Definition: SF_vector.h:79
T & back()
Definition: SF_vector.h:144
double distance(const Point &a, const Point &b)
Definition: SF_container.h:149
void cnt_from_dsp(const vector< T > &dsp, vector< T > &cnt)
Compute counts from displacements.
Definition: SF_vector.h:319
bool isEmpty(vector< T > &v)
Return whether an vector is empty (all values are 0).
Definition: SF_vector.h:378
const T * data() const
Pointer to the vector&#39;s start.
Definition: SF_vector.h:97
virtual ~vector()
Definition: SF_vector.h:67
const T & operator[](size_t i) const
Vector access.
Definition: SF_vector.h:73
const T & front() const
Definition: SF_vector.h:149
T sum(const vector< T > &vec)
Compute sum of a vector&#39;s entries.
Definition: SF_vector.h:340
void reallocate()
Definition: SF_vector.h:257
size_t size() const
The current size of the vector.
Definition: SF_vector.h:104
const T & back() const
Definition: SF_vector.h:154
A vector storing arbitrary data.
Definition: SF_vector.h:42
vector()
Initialize an empty vector.
Definition: SF_vector.h:46
const T * begin() const
Pointer to the vector&#39;s start.
Definition: SF_vector.h:116
T * begin()
Pointer to the vector&#39;s start.
Definition: SF_vector.h:122
void reserve(size_t n)
Definition: SF_vector.h:241
void assign(InputIterator s, InputIterator e)
Assign a memory range.
Definition: SF_vector.h:161
void vec_assign(S *lhs, const V *rhs, size_t size)
Assign the values in rhs to lhs. The data-type of rhs is cast to the type of lhs. ...
Definition: SF_vector.h:371
vector(size_t n, const T val)
Initialize a vector of size n and of constant value val.
Definition: SF_vector.h:56
void operator=(const vector< T > &vec)
Deep copy of a vector.
Definition: SF_vector.h:85
void resize(size_t n)
Resize a vector.
Definition: SF_vector.h:209
void append(InputIterator s, InputIterator e)
Append data to the current data chunk.
Definition: SF_vector.h:268
void dsp_from_cnt(const vector< T > &cnt, vector< T > &dsp)
Compute displacements from counts.
Definition: SF_vector.h:310