STIR  6.2.0
VectorWithOffset.h
Go to the documentation of this file.
1 //
2 //
3 /*
4  Copyright (C) 2000 PARAPET partners
5  Copyright (C) 2000 - 2007-10-08, Hammersmith Imanet Ltd
6  Copyright (C) 2012-06-01 - 2012, Kris Thielemans
7  Copyright (C) 2023 - 2024, University College London
8  This file is part of STIR.
9 
10  SPDX-License-Identifier: Apache-2.0 AND License-ref-PARAPET-license
11  See STIR/LICENSE.txt for details
12 */
13 #ifndef __stir_VectorWithOffset_H__
14 #define __stir_VectorWithOffset_H__
15 
26 #include "stir/shared_ptr.h"
27 #include "stir/deprecated.h"
28 #include <iterator>
29 
30 START_NAMESPACE_STIR
31 
63 template <class T>
64 class VectorWithOffset
65 {
66 public:
68 
69  typedef T value_type;
70  typedef value_type& reference;
71  typedef const value_type& const_reference;
72  typedef ptrdiff_t difference_type;
73  typedef T* iterator;
74  typedef T const* const_iterator;
75 
76  typedef std::reverse_iterator<iterator> reverse_iterator;
77  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
79  typedef size_t size_type;
80 
81 public:
83  inline VectorWithOffset();
84 
86  inline explicit VectorWithOffset(const int hsz);
87 
89  inline VectorWithOffset(const int min_index, const int max_index);
90 
91 #if STIR_VERSION < 070000
92 
99  STIR_DEPRECATED VectorWithOffset(const int hsz, T* const data_ptr, T* const end_of_data_ptr);
100 
102 
108  STIR_DEPRECATED inline VectorWithOffset(const int min_index, const int max_index, T* const data_ptr, T* const end_of_data_ptr);
109 #endif
110 
112  VectorWithOffset(const int hsz, const T* const data_ptr);
113 
115  inline VectorWithOffset(const int min_index, const int max_index, const T* const data_ptr);
116 
118 
122  inline VectorWithOffset(const int min_index, const int max_index, shared_ptr<T[]> data_sptr);
123 
125 
129  inline VectorWithOffset(const int sz, shared_ptr<T[]> data_sptr)
130  : VectorWithOffset(0, sz - 1, data_sptr)
131  {}
132 
134  inline VectorWithOffset(const VectorWithOffset& il);
135 
137  inline virtual ~VectorWithOffset();
138 
140  // implementation in .h because of templates/friends/whatever, see https://stackoverflow.com/a/61020224
141  friend inline void swap(VectorWithOffset& first, VectorWithOffset& second) // nothrow
142  {
143  using std::swap;
144  // swap the members of two objects
145  swap(first.num, second.num);
146  swap(first.length, second.length);
147  swap(first.start, second.start);
148  swap(first.begin_allocated_memory, second.begin_allocated_memory);
149  swap(first.end_allocated_memory, second.end_allocated_memory);
150  swap(first.pointer_access, second.pointer_access);
151  swap(first.allocated_memory_sptr, second.allocated_memory_sptr);
152  }
153 
155 
156  VectorWithOffset(VectorWithOffset&& other) noexcept;
157 
159 
162  inline void recycle();
163 
165 
166  inline VectorWithOffset& operator=(const VectorWithOffset& il);
167 
169 
170 
172  inline int get_length() const;
173 
175  inline size_t size() const;
176 
178  inline int get_min_index() const;
179 
181  inline int get_max_index() const;
182 
184  inline void set_offset(const int min_index);
185 
187  inline void set_min_index(const int min_index);
188 
190 
197  inline virtual void grow(const int min_index, const int max_index);
198 
200  inline void grow(const unsigned int new_size);
201 
203 
210  inline virtual void resize(const int min_index, const int max_index);
211 
213  inline void resize(const unsigned int new_size);
214 
216  inline void reserve(const int min_index, const int max_index);
217 
219  inline void reserve(const unsigned int new_size);
220 
222  inline size_t capacity() const;
223 
225 
227  inline bool owns_memory_for_data() const;
228 
230 
235  inline int get_capacity_min_index() const;
236 
238 
243  inline int get_capacity_max_index() const;
245 
247  inline T& operator[](int i);
248 
250  inline const T& operator[](int i) const;
251 
253  inline T& at(int i);
254 
256  inline const T& at(int i) const;
257 
259  inline bool empty() const;
260 
262 
263  inline bool operator==(const VectorWithOffset& iv) const;
264  inline bool operator!=(const VectorWithOffset& iv) const;
266 
268  inline void fill(const T& n);
269 
271  inline void apply_lower_threshold(const T& lower);
272 
274  inline void apply_upper_threshold(const T& upper);
275 
277 
278  inline T* get_data_ptr();
280 
282  inline const T* get_const_data_ptr() const;
283 
285  inline void release_data_ptr();
286 
288  inline void release_const_data_ptr() const;
290 
292 
293  inline iterator begin();
296  inline const_iterator begin() const;
298  inline iterator end();
300  inline const_iterator end() const;
301 
302  inline reverse_iterator rbegin();
303  inline reverse_iterator rend();
304  inline const_reverse_iterator rbegin() const;
305  inline const_reverse_iterator rend() const;
307 
312  inline VectorWithOffset& operator+=(const VectorWithOffset& v);
314 
316  inline VectorWithOffset& operator-=(const VectorWithOffset& v);
317 
319  inline VectorWithOffset& operator*=(const VectorWithOffset& v);
320 
322  inline VectorWithOffset& operator/=(const VectorWithOffset& v);
324 #if 0
325  // next operators are disabled for now
326  // if enabling then, you should also put them in NumericVectorWithOffset probably
334  inline VectorWithOffset & operator+= (const T &t);
336 
338  inline VectorWithOffset & operator-= (const T &t);
339 
341  inline VectorWithOffset & operator*= (const T &t);
342 
344  inline VectorWithOffset & operator/= (const T &t);
346 #endif
347 
355  inline VectorWithOffset operator+(const VectorWithOffset& v) const;
357 
359  inline VectorWithOffset operator-(const VectorWithOffset& v) const;
360 
362  inline VectorWithOffset operator*(const VectorWithOffset& v) const;
363 
365  inline VectorWithOffset operator/(const VectorWithOffset& v) const;
367 
368 protected:
370  T* num; // TODO make private
371 
373  inline void check_state() const;
375 
378  inline void init(const int min_index, const int max_index, T* const data_ptr, bool copy_data);
379 
380 private:
382  unsigned int length;
384  int start;
385 
386  T* begin_allocated_memory;
387  T* end_allocated_memory;
388 
390  shared_ptr<T[]> allocated_memory_sptr;
391 
393  inline void init();
394 
396  inline void _destruct_and_deallocate();
397 
399  // This variable is declared mutable such that get_const_data_ptr() can change it.
400  mutable bool pointer_access;
401 };
402 
403 END_NAMESPACE_STIR
404 
405 #include "stir/VectorWithOffset.inl"
406 
407 #endif // __VectorWithOffset_H__
#define STIR_DEPRECATED
Deprecation macro.
Definition: deprecated.h:19
A templated class for vectors, but with indices starting not from 0.
Definition: ArrayFilter1DUsingConvolution.h:31
This file declares a deprecation macro.
Import of std::shared_ptr, std::dynamic_pointer_cast and std::static_pointer_cast (or corresponding b...
T * num
pointer to (*this)[0] (taking get_min_index() into account that is).
Definition: VectorWithOffset.h:370
friend void swap(VectorWithOffset &first, VectorWithOffset &second)
Swap content/members of 2 objects.
Definition: VectorWithOffset.h:141
inline implementations of stir::VectorWithOffset
VectorWithOffset(const int sz, shared_ptr< T[]> data_sptr)
Construct a VectorWithOffset sharing existing data.
Definition: VectorWithOffset.h:129