|
STIR 6.4.0
|
A templated class for vectors, but with indices starting not from 0. More...
#include "stir/VectorWithOffset.h"

Public Member Functions | |
index range operations | |
| int | get_length () const |
| return number of elements in this vector | |
| size_t | size () const |
| return number of elements in this vector | |
| int | get_min_index () const |
| get value of first valid index | |
| int | get_max_index () const |
| get value of last valid index | |
| void | set_offset (const int min_index) |
| change value of starting index | |
| void | set_min_index (const int min_index) |
| identical to set_offset() | |
| virtual void | grow (const int min_index, const int max_index) |
grow the range of the vector, new elements are set to T() | |
| void | grow (const unsigned int new_size) |
grow the range of the vector from 0 to new_size-1, new elements are set to T() | |
| virtual void | resize (const int min_index, const int max_index) |
change the range of the vector, new elements are set to T() | |
| void | resize (const unsigned int new_size) |
change the range of the vector from 0 to new_size-1, new elements are set to T() | |
| void | reserve (const int min_index, const int max_index) |
| make the allocated range at least from min_index to max_index | |
| void | reserve (const unsigned int new_size) |
| make the allocated range at least from 0 to new_size-1 | |
| size_t | capacity () const |
| get allocated size | |
| bool | owns_memory_for_data () const |
| check if this object owns the memory for the data | |
| int | get_capacity_min_index () const |
| get min_index within allocated range | |
| int | get_capacity_max_index () const |
| get max_index within allocated range | |
| T & | operator[] (int i) |
| allow array-style access, read/write | |
| const T & | operator[] (int i) const |
| array access, read-only | |
| T & | at (int i) |
| allow array-style access, read/write, but with range checking (throws std::out_of_range) | |
| const T & | at (int i) const |
| array access, read-only, but with range checking (throws std::out_of_range) | |
| bool | empty () const |
| checks if the vector is empty | |
comparison operators | |
| bool | operator== (const VectorWithOffset &iv) const |
| bool | operator!= (const VectorWithOffset &iv) const |
| void | fill (const T &n) |
| fill elements with value n | |
| void | apply_lower_threshold (const T &lower) |
| Sets elements below value to the value. | |
| void | apply_upper_threshold (const T &upper) |
| Sets elements above value to the value. | |
access to the data via a pointer | |
| T * | get_data_ptr () |
| member function for access to the data via a T* | |
| const T * | get_const_data_ptr () const |
| member function for access to the data via a const T* | |
| void | release_data_ptr () |
| signal end of access to T* | |
| void | release_const_data_ptr () const |
| signal end of access to const T* | |
basic iterator support | |
| iterator | begin () |
| use to initialise an iterator to the first element of the vector | |
| const_iterator | begin () const |
| use to initialise an iterator to the first element of the (const) vector | |
| iterator | end () |
| iterator 'past' the last element of the vector | |
| const_iterator | end () const |
| iterator 'past' the last element of the (const) vector | |
| reverse_iterator | rbegin () |
| reverse_iterator | rend () |
| const_reverse_iterator | rbegin () const |
| const_reverse_iterator | rend () const |
arithmetic assignment operators with objects of the same type | |
| |
| VectorWithOffset & | operator+= (const VectorWithOffset &v) |
adding elements of v to the current vector | |
| VectorWithOffset & | operator-= (const VectorWithOffset &v) |
subtracting elements of v from the current vector | |
| VectorWithOffset & | operator*= (const VectorWithOffset &v) |
multiplying elements of the current vector with elements of v | |
| VectorWithOffset & | operator/= (const VectorWithOffset &v) |
dividing all elements of the current vector by elements of v | |
typedefs for iterator support | |
| typedef T | value_type |
| typedef value_type & | reference |
| typedef const value_type & | const_reference |
| typedef ptrdiff_t | difference_type |
| typedef T * | iterator |
| typedef T const * | const_iterator |
| typedef std::reverse_iterator< iterator > | reverse_iterator |
| typedef std::reverse_iterator< const_iterator > | const_reverse_iterator |
| typedef size_t | size_type |
| VectorWithOffset () | |
| Default constructor: creates a vector of length 0. | |
| VectorWithOffset (const int hsz) | |
Construct a VectorWithOffset of given length (initialised with T()) | |
| VectorWithOffset (const int min_index, const int max_index) | |
Construct a VectorWithOffset with offset min_index (initialised with T()) | |
| STIR_DEPRECATED | VectorWithOffset (const int hsz, T *const data_ptr, T *const end_of_data_ptr) |
| Construct a VectorWithOffset of given length pointing to existing data. | |
| STIR_DEPRECATED | VectorWithOffset (const int min_index, const int max_index, T *const data_ptr, T *const end_of_data_ptr) |
Construct a VectorWithOffset with offset min_index pointing to existing data. | |
| VectorWithOffset (const int hsz, const T *const data_ptr) | |
| Construct a VectorWithOffset of given length from a bare pointer (copying data) | |
| VectorWithOffset (const int min_index, const int max_index, const T *const data_ptr) | |
Construct a VectorWithOffset with offset min_index from a bare pointer (copying data) | |
| VectorWithOffset (const int min_index, const int max_index, shared_ptr< T[]> data_sptr) | |
| Construct a VectorWithOffset sharing existing data. | |
| VectorWithOffset (const int sz, shared_ptr< T[]> data_sptr) | |
| Construct a VectorWithOffset sharing existing data. | |
| VectorWithOffset (const VectorWithOffset &il) | |
| copy constructor | |
| virtual | ~VectorWithOffset () |
| Destructor. | |
| VectorWithOffset (VectorWithOffset &&other) noexcept | |
| move constructor | |
| void | recycle () |
| Free all memory and make object as if default-constructed. | |
| VectorWithOffset & | operator= (const VectorWithOffset &il) |
| assignment operator with another vector | |
| void | swap (VectorWithOffset &first, VectorWithOffset &second) |
| Swap content/members of 2 objects. | |
arithmetic operators with objects of the same type | |
| |
| T * | num |
| pointer to (*this)[0] (taking get_min_index() into account that is). | |
| VectorWithOffset | operator+ (const VectorWithOffset &v) const |
| adding vectors, element by element | |
| VectorWithOffset | operator- (const VectorWithOffset &v) const |
| subtracting vectors, element by element | |
| VectorWithOffset | operator* (const VectorWithOffset &v) const |
| multiplying vectors, element by element | |
| VectorWithOffset | operator/ (const VectorWithOffset &v) const |
| dividing vectors, element by element | |
| void | check_state () const |
| Called internally to see if all variables are consistent. | |
| 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 | |
| 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 | |
A templated class for vectors, but with indices starting not from 0.
Elements are guaranteed to be stored contiguously. (Emergency) methods are provided for accessing the data via a T*.
This class tries to mimic std::vector for the most common methods, but it is much more conservative in its memory allocations. The only memory that is allocated is what you asked for (although the allocated memory hardly ever shrinks, except when calling recycle()). So, std::vector::push_back() etc are not provided, as they would be horribly inefficient for the current class (ok, except if you would have called reserve() first).
It is possible to construct a VectorWithOffset that uses existing memory. It will then never deallocate that memory obviously. Note that when growing the vector (or assigning a bigger vector), the vector will allocate new memory. Any modifications to the vector then will no longer be connected to the original data block. This can always be tested using owns_memory_for_data().
T* outside the range of allocated data. This is not guaranteed to be valid by ANSI C++. It is fine however as long as the min_index is negative and such that abs(min_index) is smaller than max_index. | stir::VectorWithOffset< T >::VectorWithOffset | ( | const int | hsz, |
| T *const | data_ptr, | ||
| T *const | end_of_data_ptr ) |
Construct a VectorWithOffset of given length pointing to existing data.
|
inline |
Construct a VectorWithOffset with offset min_index pointing to existing data.
|
inline |
Construct a VectorWithOffset sharing existing data.
|
inline |
Construct a VectorWithOffset sharing existing data.
|
noexcept |
move constructor
implementation uses the copy-and-swap idiom, see e.g. https://stackoverflow.com/a/3279550
References swap, and VectorWithOffset().
|
inline |
Free all memory and make object as if default-constructed.
This is not the same as resize(0), as the latter does not deallocate the memory (i.e. does not change the capacity()).
References check_state(), init(), and recycle().
Referenced by recycle(), and stir::zoom_image().
|
inline |
assignment operator with another vector
implementation avoids reallocating if sufficient memory already exists.
References begin(), capacity(), check_state(), end(), get_max_index(), get_min_index(), num, reserve(), set_offset(), size(), and VectorWithOffset().
Referenced by stir::NumericVectorWithOffset< T, elemT >::operator=().
|
inline |
return number of elements in this vector
References check_state().
Referenced by stir::LogcoshPrior< elemT >::compute_gradient(), stir::QuadraticPrior< elemT >::compute_gradient(), stir::RelativeDifferencePrior< elemT >::compute_gradient(), stir::LogcoshPrior< elemT >::compute_value(), stir::QuadraticPrior< elemT >::compute_value(), stir::RelativeDifferencePrior< elemT >::compute_value(), stir::ecat::ecat6::DiscretisedDensity_to_ECAT6(), stir::ecat::ecat7::DiscretisedDensity_to_ECAT7(), stir::display(), stir::Viewgram< elemT >::get_num_axial_poss(), stir::VoxelsOnCartesianGrid< elemT >::get_x_size(), stir::IndexRange< 2 >::IndexRange(), stir::ecat::ecat6::make_ECAT6_Main_header(), stir::ecat::ecat7::make_ECAT7_main_header(), stir::ML_estimate_component_based_normalisation(), stir::NumericVectorWithOffset< T, elemT >::operator*=(), operator*=(), stir::NumericVectorWithOffset< T, elemT >::operator+=(), operator+=(), stir::NumericVectorWithOffset< T, elemT >::operator-=(), operator-=(), stir::NumericVectorWithOffset< T, elemT >::operator/=(), operator/=(), stir::operator<<(), stir::ProjDataInfoCylindrical::ProjDataInfoCylindrical(), stir::find_fwhm_in_imageTests::run_tests(), stir::VectorWithOffsetTests::run_tests(), stir::SeparableMetzArrayFilter< num_dimensions, elemT >::SeparableMetzArrayFilter(), stir::truncate_end_planes(), stir::write_basic_interfile_image_header(), and stir::zoom_image().
|
inlinevirtual |
grow the range of the vector, new elements are set to T()
Currently, it is only checked with assert() if old range is a subinterval of the new range.
grow() currently simply calls resize(). However, if you overload resize() in a derived class, it is probably safest to overload grow() as well.
References resize().
Referenced by stir::NumericVectorWithOffset< T, elemT >::operator*=(), operator*=(), operator+=(), stir::NumericVectorWithOffset< T, elemT >::operator-=(), operator-=(), stir::NumericVectorWithOffset< T, elemT >::operator/=(), and operator/=().
|
inlinevirtual |
change the range of the vector, new elements are set to T()
New memory is allocated if the range grows outside the range specified by get_capacity_min_index() till get_capacity_max_index(). Data is then copied and old memory deallocated (unless it is shared).
Reimplemented in stir::IndexRange< 1 >.
References check_state(), get_max_index(), get_min_index(), num, and reserve().
Referenced by stir::IOTests_ParametricDiscretisedDensity::create_image(), stir::get_scale_factors_per_sinogram(), grow(), init_with_copy(), stir::LmToProjData::process_data(), stir::Array< num_dimensions, elemT >::resize(), stir::VectorWithOffsetTests::run_tests(), stir::VoxelsOnCartesianGridTests::run_tests(), and stir::ScatterEstimation::upsample_and_fit_scatter_estimate().
|
inline |
check if this object owns the memory for the data
Will be false if one of the constructors is used that passes in a data block.
References owns_memory_for_data().
Referenced by owns_memory_for_data(), stir::ArrayTests::run_tests(), and stir::VectorWithOffsetTests::run_tests().
|
inline |
get min_index within allocated range
This value depends on get_min_index() and hence will change after calling set_min_index().
For a vector of 0 length, this function returns 0.
References num.
Referenced by reserve().
|
inline |
get max_index within allocated range
This value depends on get_min_index() and hence will change after calling set_min_index().
For a vector of 0 length, this function returns capacity()-1.
References num.
|
inline |
allow array-style access, read/write
Out of range errors are detected using assert()
References check_state(), get_max_index(), get_min_index(), num, and operator[]().
Referenced by stir::Array< num_dimensions, elemT >::operator[](), stir::Array< num_dimensions, elemT >::operator[](), operator[](), and operator[]().
|
inline |
array access, read-only
Out of range errors are detected using assert()
References check_state(), get_max_index(), get_min_index(), and operator[]().
|
inline |
member function for access to the data via a T*
This returns a T* to the first element of a, members are guaranteed to be stored contiguously in memory.
Use only in emergency cases...
To prevent invalidating the safety checks (and making reimplementation more difficult), NO manipulation with the vector is allowed between the pairs get_data_ptr() and release_data_ptr() and get_const_data_ptr() and release_data_ptr(). (This is checked with assert() in DEBUG mode.)
References num.
Referenced by stir::GE::RDF_HDF5::GEHDF5Wrapper::read_singles(), and stir::VectorWithOffsetTests::run_tests().
|
inline |
member function for access to the data via a const T*
This returns a const T* to the first element of a, members are guaranteed to be stored contiguously in memory.
Use get_const_data_ptr() when you are not going to modify the data.
References num.
|
inline |
signal end of access to T*
This has to be used when access to the T* returned by get_data_ptr() is finished. It updates the vector with any changes you made, and allows access to the other member functions again.
Referenced by stir::GE::RDF_HDF5::GEHDF5Wrapper::read_singles(), and stir::VectorWithOffsetTests::run_tests().
|
inline |
signal end of access to const T*
This has to be used when access to the const T* returned by get_const_data_ptr() is finished. It allows access to the other member functions again.
|
inlineprotected |
Called internally to see if all variables are consistent.
This function (only non-empty when debugging) is used before and after any modification of the object
References check_state().
Referenced by stir::Array< num_dimensions, elemT >::apply_lower_threshold(), apply_lower_threshold(), stir::Array< num_dimensions, elemT >::apply_upper_threshold(), apply_upper_threshold(), begin(), begin(), check_state(), end(), stir::Array< num_dimensions, elemT >::fill(), fill(), stir::Array< num_dimensions, elemT >::find_min(), get_length(), init(), stir::NumericVectorWithOffset< T, elemT >::operator*(), operator*(), stir::NumericVectorWithOffset< T, elemT >::operator*=(), operator*=(), stir::NumericVectorWithOffset< T, elemT >::operator+(), operator+(), stir::NumericVectorWithOffset< T, elemT >::operator+=(), operator+=(), stir::NumericVectorWithOffset< T, elemT >::operator-(), stir::NumericVectorWithOffset< Array< num_dimensions - 1, elemT >, elemT >::operator-(), operator-(), stir::NumericVectorWithOffset< T, elemT >::operator-=(), stir::NumericVectorWithOffset< Array< num_dimensions - 1, elemT >, elemT >::operator-=(), operator-=(), stir::NumericVectorWithOffset< Array< num_dimensions - 1, elemT >, elemT >::operator/(), operator/(), stir::NumericVectorWithOffset< T, elemT >::operator/=(), stir::NumericVectorWithOffset< Array< num_dimensions - 1, elemT >, elemT >::operator/=(), operator/=(), operator=(), operator[](), operator[](), recycle(), reserve(), resize(), set_offset(), size(), stir::Array< num_dimensions, elemT >::sum(), stir::Array< num_dimensions, elemT >::sum_positive(), stir::Array< num_dimensions, elemT >::xapyb(), stir::Array< num_dimensions, elemT >::xapyb(), and stir::NumericVectorWithOffset< T, elemT >::xapyb().
|
inlineprotected |
change vector to the new index range and copy data from data_ptr
calls resize()
References begin(), init_with_copy(), and resize().
Referenced by init(), init_with_copy(), and VectorWithOffset().
|
inlineprotected |
initialise vector to the given index range and either copy from or point to data_ptr
true, fall-back to init_with_copy(min_index, max_index, data_ptr). Otherwise, make this object point to the same memory as data_ptr.References check_state(), init(), init_with_copy(), and num.
Referenced by init(), recycle(), VectorWithOffset(), and VectorWithOffset().