STIR
6.2.0
|
A templated class for vectors, but with indices starting not from 0. More...
#include "stir/VectorWithOffset.h"
Public Member Functions | |
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. More... | |
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. More... | |
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. More... | |
VectorWithOffset (const int sz, shared_ptr< T[]> data_sptr) | |
Construct a VectorWithOffset sharing existing data. More... | |
VectorWithOffset (const VectorWithOffset &il) | |
copy constructor | |
virtual | ~VectorWithOffset () |
Destructor. | |
VectorWithOffset (VectorWithOffset &&other) noexcept | |
move constructor More... | |
void | recycle () |
Free all memory and make object as if default-constructed. More... | |
VectorWithOffset & | operator= (const VectorWithOffset &il) |
assignment operator with another vector More... | |
T & | operator[] (int i) |
allow array-style access, read/write More... | |
const T & | operator[] (int i) const |
array access, read-only More... | |
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 | |
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. | |
index range operations | |
int | get_length () const |
return number of elements in this vector More... | |
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() More... | |
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() More... | |
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 More... | |
int | get_capacity_min_index () const |
get min_index within allocated range More... | |
int | get_capacity_max_index () const |
get max_index within allocated range More... | |
comparison operators | |
bool | operator== (const VectorWithOffset &iv) const |
bool | operator!= (const VectorWithOffset &iv) const |
access to the data via a pointer | |
T * | get_data_ptr () |
member function for access to the data via a T* More... | |
const T * | get_const_data_ptr () const |
member function for access to the data via a const T* More... | |
void | release_data_ptr () |
signal end of access to T* More... | |
void | release_const_data_ptr () const |
signal end of access to const T* More... | |
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 | |
arithmetic operators with objects of the same type | |
| |
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 | |
Protected Member Functions | |
void | check_state () const |
Called internally to see if all variables are consistent. More... | |
void | init (const int min_index, const int max_index, T *const data_ptr, bool copy_data) |
change vector with new index range and point to data_ptr More... | |
Protected Attributes | |
T * | num |
pointer to (*this)[0] (taking get_min_index() into account that is). | |
Friends | |
void | swap (VectorWithOffset &first, VectorWithOffset &second) |
Swap content/members of 2 objects. | |
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
|
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()).
Referenced by stir::SeparableGaussianArrayFilter< num_dimensions, elemT >::SeparableGaussianArrayFilter().
|
inline |
assignment operator with another vector
implementation avoids reallocating if sufficient memory already exists.
|
inline |
return number of elements in this vector
Referenced by stir::LogcoshPrior< elemT >::accumulate_Hessian_times_input(), stir::QuadraticPrior< float >::accumulate_Hessian_times_input(), stir::RelativeDifferencePrior< elemT >::accumulate_Hessian_times_input(), stir::QuadraticPrior< float >::add_multiplication_with_approximate_Hessian(), stir::ArrayFilter1DUsingConvolutionSymmetricKernel< elemT >::ArrayFilter1DUsingConvolutionSymmetricKernel(), stir::QuadraticPrior< float >::compute_gradient(), stir::LogcoshPrior< elemT >::compute_gradient(), stir::RelativeDifferencePrior< elemT >::compute_gradient(), stir::QuadraticPrior< float >::compute_value(), stir::LogcoshPrior< elemT >::compute_value(), stir::RelativeDifferencePrior< elemT >::compute_value(), stir::SegmentByView< elemT >::get_num_axial_poss(), stir::SegmentByView< elemT >::get_num_tangential_poss(), stir::SegmentBySinogram< elemT >::get_num_tangential_poss(), stir::Sinogram< elemT >::get_num_tangential_poss(), stir::Viewgram< float >::get_num_tangential_poss(), stir::SegmentBySinogram< elemT >::get_num_views(), stir::VoxelsOnCartesianGrid< elemT >::get_x_size(), stir::ArrayFilter1DUsingConvolutionSymmetricKernel< elemT >::is_trivial(), stir::ArrayFilter1DUsingConvolution< elemT >::is_trivial(), stir::make_det_pair_data(), stir::QuadraticPrior< float >::parabolic_surrogate_curvature(), stir::LogcoshPrior< elemT >::parabolic_surrogate_curvature(), stir::ProjDataInfoCylindrical::ProjDataInfoCylindrical(), stir::SeparableMetzArrayFilter< num_dimensions, elemT >::SeparableMetzArrayFilter(), and stir::truncate_end_planes().
|
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.
Reimplemented in stir::Array< 1, elemT >.
Referenced by stir::ArrayFilter1DUsingConvolutionSymmetricKernel< elemT >::is_trivial(), stir::make_det_pair_data(), stir::SeparableGaussianArrayFilter< num_dimensions, elemT >::SeparableGaussianArrayFilter(), and stir::SeparableMetzArrayFilter< num_dimensions, elemT >::SeparableMetzArrayFilter().
|
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::Array< 1, elemT >.
Referenced by stir::IOTests_ParametricDiscretisedDensity::create_image(), stir::DataSymmetriesForBins_PET_CartesianGrid::DataSymmetriesForBins_PET_CartesianGrid(), stir::get_scale_factors_per_sinogram(), stir::ScatterSimulation::initialise_cache_for_scattpoint_det_integrals_over_activity(), stir::ScatterSimulation::initialise_cache_for_scattpoint_det_integrals_over_attenuation(), stir::ProjDataInfoCylindrical::ProjDataInfoCylindrical(), 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.
|
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.
|
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.
|
inline |
allow array-style access, read/write
Out of range errors are detected using assert()
|
inline |
array access, read-only
Out of range errors are detected using assert()
|
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.)
|
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.
|
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.
|
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
|
inlineprotected |
change vector with new index range and point to data_ptr