35VectorWithOffset<T>::init()
40 begin_allocated_memory =
nullptr;
41 end_allocated_memory =
nullptr;
42 allocated_memory_sptr =
nullptr;
49 this->pointer_access =
false;
50 this->
resize(min_index, max_index);
51 std::copy(data_ptr, data_ptr + this->length, this->
begin());
64 this->pointer_access =
false;
65 this->length = max_index >= min_index ?
static_cast<unsigned>(max_index - min_index) + 1 : 0U;
66 this->start = min_index;
67 this->begin_allocated_memory = data_ptr;
68 this->end_allocated_memory = data_ptr + this->length;
69 this->
num = this->begin_allocated_memory - this->start;
78 return this->allocated_memory_sptr ? true :
false;
91 assert(((length > 0) || (length == 0 && start == 0 &&
num == begin_allocated_memory)));
94 assert(begin_allocated_memory <=
num + start);
95 assert(end_allocated_memory >= begin_allocated_memory);
96 assert(
static_cast<unsigned>(end_allocated_memory - begin_allocated_memory) >= length);
97 assert(!allocated_memory_sptr || (allocated_memory_sptr.get() == begin_allocated_memory));
102VectorWithOffset<T>::_destruct_and_deallocate()
105 assert(pointer_access ==
false);
110 this->allocated_memory_sptr =
nullptr;
118 this->_destruct_and_deallocate();
133 return start + length - 1;
165 throw std::out_of_range(
"index out of range");
175 throw std::out_of_range(
"index out of range");
189typename VectorWithOffset<T>::iterator
197typename VectorWithOffset<T>::const_iterator
201 return typename VectorWithOffset<T>::const_iterator(
num + this->
get_min_index());
205typename VectorWithOffset<T>::iterator
209 return typename VectorWithOffset<T>::iterator(
num + (this->
get_max_index() + 1));
213typename VectorWithOffset<T>::const_iterator
217 return typename VectorWithOffset<T>::const_iterator(
num + (this->
get_max_index() + 1));
221typename VectorWithOffset<T>::reverse_iterator
222VectorWithOffset<T>::rbegin()
225 return std::make_reverse_iterator(
end());
229typename VectorWithOffset<T>::const_reverse_iterator
230VectorWithOffset<T>::rbegin()
const
233 return std::make_reverse_iterator(
end());
237typename VectorWithOffset<T>::reverse_iterator
238VectorWithOffset<T>::rend()
241 return std::make_reverse_iterator(
begin());
245typename VectorWithOffset<T>::const_reverse_iterator
246VectorWithOffset<T>::rend()
const
249 return std::make_reverse_iterator(
begin());
254 : pointer_access(false)
266 : length(static_cast<unsigned>(max_index - min_index) + 1),
268 pointer_access(false)
270 if (max_index >= min_index)
272 allocated_memory_sptr = shared_ptr<T[]>(
new T[length]);
273 begin_allocated_memory = allocated_memory_sptr.get();
274 end_allocated_memory = begin_allocated_memory + length;
275 num = begin_allocated_memory - min_index;
282#if STIR_VERSION < 070000
285 : length(static_cast<unsigned>(max_index - min_index) + 1),
287 allocated_memory_sptr(nullptr),
288 pointer_access(false)
290 this->begin_allocated_memory = data_ptr;
291 this->end_allocated_memory = end_of_data_ptr;
292 this->
num = this->begin_allocated_memory - this->start;
318 this->allocated_memory_sptr = data_sptr;
319 this->
init(min_index, max_index, data_sptr.get(),
false);
333 assert(pointer_access ==
false);
334 _destruct_and_deallocate();
345 num += start - min_index;
360 return size_t(end_allocated_memory - begin_allocated_memory);
368 assert(length > 0 ||
num == begin_allocated_memory);
369 return static_cast<int>(begin_allocated_memory -
num);
377 assert(length > 0 ||
num == begin_allocated_memory);
378 return static_cast<int>(end_allocated_memory -
num - 1);
388 const int actual_capacity_min_index
389 = length == 0 ? new_capacity_min_index : std::min(this->
get_capacity_min_index(), new_capacity_min_index);
390 const int actual_capacity_max_index
391 = length == 0 ? new_capacity_max_index : std::max(this->
get_capacity_max_index(), new_capacity_max_index);
392 if (actual_capacity_min_index > actual_capacity_max_index)
395 const unsigned int new_capacity =
static_cast<unsigned>(actual_capacity_max_index - actual_capacity_min_index) + 1;
396 if (new_capacity <= this->
capacity())
400 assert(pointer_access ==
false);
402 shared_ptr<T[]> new_allocated_memory_sptr(
new T[new_capacity]);
403 const unsigned extra_at_the_left = length == 0 ? 0U : std::max(0, this->
get_min_index() - actual_capacity_min_index);
404 std::copy(this->
begin(), this->
end(), new_allocated_memory_sptr.get() + extra_at_the_left);
405 this->_destruct_and_deallocate();
406 allocated_memory_sptr = std::move(new_allocated_memory_sptr);
407 begin_allocated_memory = allocated_memory_sptr.get();
408 end_allocated_memory = begin_allocated_memory + new_capacity;
409 num = begin_allocated_memory + extra_at_the_left - (length > 0 ? start : 0);
420 reserve(0,
static_cast<int>(new_size - 1));
429 if (min_index > max_index)
433 num = begin_allocated_memory;
436 const unsigned old_length = length;
442 const int overlap_min_index = std::max(this->
get_min_index(), min_index);
443 const int overlap_max_index = std::min(this->
get_max_index(), max_index);
445 length = overlap_max_index - overlap_min_index < 0 ? 0 : static_cast<unsigned>(overlap_max_index - overlap_min_index) + 1;
449 num = begin_allocated_memory;
454 start = overlap_min_index;
457 const unsigned overlapping_length = length;
458 this->
reserve(min_index, max_index);
461 length =
static_cast<unsigned>(max_index - min_index) + 1;
463 if (overlapping_length > 0)
470 num = begin_allocated_memory - min_index;
483 num = begin_allocated_memory;
486 this->resize(0,
static_cast<int>(new_size - 1));
493 this->
resize(min_index, max_index);
500 this->grow(0,
static_cast<int>(new_size - 1));
516 num = begin_allocated_memory;
530 : pointer_access(false)
541 return static_cast<int>(length);
549 return size_t(length);
557 if (length != iv.length || start != iv.start)
559 return std::equal(this->begin(), this->end(), iv.
begin());
564VectorWithOffset<T>::operator!=(
const VectorWithOffset& iv)
const
566 return !(*
this == iv);
574 std::fill(this->
begin(), this->
end(), n);
614 assert(!pointer_access);
616 pointer_access =
true;
617 return (
num + start);
636 assert(!pointer_access);
638 pointer_access =
true;
639 return (
num + start);
657 assert(pointer_access);
659 pointer_access =
false;
674 assert(pointer_access);
676 pointer_access =
false;
687 error(
"VectorWithOffset::+= with non-matching range");
709 error(
"VectorWithOffset::-= with non-matching range");
732 error(
"VectorWithOffset::*= with non-matching range");
756 error(
"VectorWithOffset::/= with non-matching range");
781 typename iterator iter = this->begin();
782 const typename iterator end_iter = this->end();
783 while (iter != end_iter)
788inline VectorWithOffset<T>&
791 typename iterator iter = this->begin();
792 const typename iterator end_iter = this->end();
793 while (iter != end_iter)
800 typename iterator iter = this->begin();
801 const typename iterator end_iter = this->end();
802 while (iter != end_iter)
809 typename iterator iter = this->begin();
810 const typename iterator end_iter = this->end();
811 while (iter != end_iter)
This file defines the stir::IndexRange class.
A templated class for vectors, but with indices starting not from 0.
Definition VectorWithOffset.h:65
void release_const_data_ptr() const
signal end of access to const T*
Definition VectorWithOffset.inl:672
T & at(int i)
allow array-style access, read/write, but with range checking (throws std::out_of_range)
Definition VectorWithOffset.inl:162
const T * get_const_data_ptr() const
member function for access to the data via a const T*
Definition VectorWithOffset.inl:634
iterator begin()
use to initialise an iterator to the first element of the vector
Definition VectorWithOffset.inl:190
VectorWithOffset & operator=(const VectorWithOffset &il)
assignment operator with another vector
Definition VectorWithOffset.inl:505
void check_state() const
Called internally to see if all variables are consistent.
Definition VectorWithOffset.inl:87
virtual ~VectorWithOffset()
Destructor.
Definition VectorWithOffset.inl:330
int get_max_index() const
get value of last valid index
Definition VectorWithOffset.inl:131
void init(const int min_index, const int max_index, T *const data_ptr, bool copy_data)
initialise vector to the given index range and either copy from or point to data_ptr
Definition VectorWithOffset.inl:56
VectorWithOffset & operator*=(const VectorWithOffset &v)
multiplying elements of the current vector with elements of v
Definition VectorWithOffset.inl:727
size_t capacity() const
get allocated size
Definition VectorWithOffset.inl:358
VectorWithOffset & operator+=(const VectorWithOffset &v)
adding elements of v to the current vector
Definition VectorWithOffset.inl:682
VectorWithOffset operator-(const VectorWithOffset &v) const
subtracting vectors, element by element
Definition VectorWithOffset.inl:832
int get_min_index() const
get value of first valid index
Definition VectorWithOffset.inl:124
virtual void grow(const int min_index, const int max_index)
grow the range of the vector, new elements are set to T()
Definition VectorWithOffset.inl:491
bool owns_memory_for_data() const
check if this object owns the memory for the data
Definition VectorWithOffset.inl:76
void release_data_ptr()
signal end of access to T*
Definition VectorWithOffset.inl:655
void reserve(const int min_index, const int max_index)
make the allocated range at least from min_index to max_index
Definition VectorWithOffset.inl:385
VectorWithOffset & operator-=(const VectorWithOffset &v)
subtracting elements of v from the current vector
Definition VectorWithOffset.inl:704
VectorWithOffset operator*(const VectorWithOffset &v) const
multiplying vectors, element by element
Definition VectorWithOffset.inl:842
VectorWithOffset operator+(const VectorWithOffset &v) const
adding vectors, element by element
Definition VectorWithOffset.inl:822
int get_length() const
return number of elements in this vector
Definition VectorWithOffset.inl:538
void set_min_index(const int min_index)
identical to set_offset()
Definition VectorWithOffset.inl:351
virtual void resize(const int min_index, const int max_index)
change the range of the vector, new elements are set to T()
Definition VectorWithOffset.inl:426
void set_offset(const int min_index)
change value of starting index
Definition VectorWithOffset.inl:339
VectorWithOffset & operator/=(const VectorWithOffset &v)
dividing all elements of the current vector by elements of v
Definition VectorWithOffset.inl:751
iterator end()
iterator 'past' the last element of the vector
Definition VectorWithOffset.inl:206
VectorWithOffset operator/(const VectorWithOffset &v) const
dividing vectors, element by element
Definition VectorWithOffset.inl:852
void apply_lower_threshold(const T &lower)
Sets elements below value to the value.
Definition VectorWithOffset.inl:580
int get_capacity_max_index() const
get max_index within allocated range
Definition VectorWithOffset.inl:374
VectorWithOffset()
Default constructor: creates a vector of length 0.
Definition VectorWithOffset.inl:253
void recycle()
Free all memory and make object as if default-constructed.
Definition VectorWithOffset.inl:115
void fill(const T &n)
fill elements with value n
Definition VectorWithOffset.inl:571
T * get_data_ptr()
member function for access to the data via a T*
Definition VectorWithOffset.inl:612
T & operator[](int i)
allow array-style access, read/write
Definition VectorWithOffset.inl:139
int get_capacity_min_index() const
get min_index within allocated range
Definition VectorWithOffset.inl:365
bool empty() const
checks if the vector is empty
Definition VectorWithOffset.inl:183
void init_with_copy(const int min_index, const int max_index, T const *const data_ptr)
change vector to the new index range and copy data from data_ptr
Definition VectorWithOffset.inl:47
void apply_upper_threshold(const T &upper)
Sets elements above value to the value.
Definition VectorWithOffset.inl:589
friend void swap(VectorWithOffset &first, VectorWithOffset &second)
Swap content/members of 2 objects.
Definition VectorWithOffset.h:141
T * num
pointer to (*this)[0] (taking get_min_index() into account that is).
Definition VectorWithOffset.h:370
size_t size() const
return number of elements in this vector
Definition VectorWithOffset.inl:546
Declaration of stir::error()
void error(const char *const s,...)
Print error with format string a la printf and throw exception.
Definition error.cxx:42
void threshold_upper(forw_iterT begin, forw_iterT end, const elemT new_max)
Threshold a sequence from above.
Definition thresholding.h:61
void threshold_lower(forw_iterT begin, forw_iterT end, const elemT new_min)
Threshold a sequence from below.
Definition thresholding.h:75
Declaration of functions that threshold sequences (specified by iterators).