STIR  6.2.0
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
stir::VectorWithOffset< T > Class Template Reference

A templated class for vectors, but with indices starting not from 0. More...

#include "stir/VectorWithOffset.h"

Inheritance diagram for stir::VectorWithOffset< T >:
Inheritance graph
[legend]

Public Types

typedef size_t size_type
 
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
 

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...
 
VectorWithOffsetoperator= (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
Warning
Arguments must have matching index ranges. Otherwise error() is called.
VectorWithOffsetoperator+= (const VectorWithOffset &v)
 adding elements of v to the current vector
 
VectorWithOffsetoperator-= (const VectorWithOffset &v)
 subtracting elements of v from the current vector
 
VectorWithOffsetoperator*= (const VectorWithOffset &v)
 multiplying elements of the current vector with elements of v
 
VectorWithOffsetoperator/= (const VectorWithOffset &v)
 dividing all elements of the current vector by elements of v
 
arithmetic operators with objects of the same type
Warning
Arguments must have matching index ranges. Otherwise error() is called.
current implementation involves a temporary copy of the data, unless you have a really smart compiler.
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.
 

Detailed Description

template<class T>
class stir::VectorWithOffset< T >

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().

Warning
This class does not satisfy full Container requirements.
Current implementation relies on shifting a 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.
Todo:
add allocator template as in std::vector. This is non-trivial as we would have to use uninitialized_copy etc. in some places.

Constructor & Destructor Documentation

◆ VectorWithOffset() [1/5]

template<class T>
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.

Warning
This refers to the original memory range, so any modifications to this object will modify the original data as well.
Deprecated:

◆ VectorWithOffset() [2/5]

template<class T>
stir::VectorWithOffset< T >::VectorWithOffset ( const int  min_index,
const int  max_index,
T *const  data_ptr,
T *const  end_of_data_ptr 
)
inline

Construct a VectorWithOffset with offset min_index pointing to existing data.

Warning
This refers to the original memory range, so any modifications to this object will modify the original data as well.
Deprecated:

◆ VectorWithOffset() [3/5]

template<class T>
stir::VectorWithOffset< T >::VectorWithOffset ( const int  min_index,
const int  max_index,
shared_ptr< T[]>  data_sptr 
)
inline

Construct a VectorWithOffset sharing existing data.

Warning
This refers to the original memory range, so any modifications to this object will modify the original data as well.

◆ VectorWithOffset() [4/5]

template<class T>
stir::VectorWithOffset< T >::VectorWithOffset ( const int  sz,
shared_ptr< T[]>  data_sptr 
)
inline

Construct a VectorWithOffset sharing existing data.

Warning
This refers to the original memory range, so any modifications to this object will modify the original data as well.

◆ VectorWithOffset() [5/5]

template<class T>
stir::VectorWithOffset< T >::VectorWithOffset ( VectorWithOffset< T > &&  other)
noexcept

move constructor

implementation uses the copy-and-swap idiom, see e.g. https://stackoverflow.com/a/3279550

Member Function Documentation

◆ recycle()

template<class T >
void stir::VectorWithOffset< T >::recycle ( )
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().

◆ operator=()

template<class T >
VectorWithOffset< T > & stir::VectorWithOffset< T >::operator= ( const VectorWithOffset< T > &  il)
inline

assignment operator with another vector

implementation avoids reallocating if sufficient memory already exists.

◆ get_length()

template<class T >
int stir::VectorWithOffset< T >::get_length ( ) const
inline

return number of elements in this vector

Deprecated:
Use size() instead.

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().

◆ grow()

template<class T >
void stir::VectorWithOffset< T >::grow ( const int  min_index,
const int  max_index 
)
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().

◆ resize()

template<class T >
void stir::VectorWithOffset< T >::resize ( const int  min_index,
const int  max_index 
)
inlinevirtual

◆ owns_memory_for_data()

template<class T >
bool stir::VectorWithOffset< T >::owns_memory_for_data ( ) const
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.

◆ get_capacity_min_index()

template<class T >
int stir::VectorWithOffset< T >::get_capacity_min_index ( ) const
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.

◆ get_capacity_max_index()

template<class T >
int stir::VectorWithOffset< T >::get_capacity_max_index ( ) const
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.

◆ operator[]() [1/2]

template<class T >
T & stir::VectorWithOffset< T >::operator[] ( int  i)
inline

allow array-style access, read/write

Out of range errors are detected using assert()

◆ operator[]() [2/2]

template<class T >
const T & stir::VectorWithOffset< T >::operator[] ( int  i) const
inline

array access, read-only

Out of range errors are detected using assert()

◆ get_data_ptr()

template<class T >
T * stir::VectorWithOffset< T >::get_data_ptr ( )
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.)

◆ get_const_data_ptr()

template<class T >
const T * stir::VectorWithOffset< T >::get_const_data_ptr ( ) const
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.

See also
get_data_ptr()

◆ release_data_ptr()

template<class T >
void stir::VectorWithOffset< T >::release_data_ptr ( )
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.

See also
get_data_ptr()

◆ release_const_data_ptr()

template<class T >
void stir::VectorWithOffset< T >::release_const_data_ptr ( ) const
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.

See also
get_const_data_ptr()

◆ check_state()

template<class T >
void stir::VectorWithOffset< T >::check_state ( ) const
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

◆ init()

template<class T>
void stir::VectorWithOffset< T >::init ( const int  min_index,
const int  max_index,
T *const  data_ptr,
bool  copy_data 
)
inlineprotected

change vector with new index range and point to data_ptr

  • data_ptr should start to a contiguous block of correct size

The documentation for this class was generated from the following files: