#include <slice_pointer.h>
Public Types | |
typedef T | value_type |
Type pointed to. | |
typedef PTR | pointer |
Pointer to type. | |
typedef REF | reference |
Reference to type pointed to. | |
typedef std::random_access_iterator_tag | iterator_category |
This is a random access iterator. | |
typedef Subscript | difference_type |
Type for number of elements separating two iterators. | |
Public Member Functions | |
slice_pointer_base () | |
default constructor | |
slice_pointer_base (pointer first, Subscript span, Subscript size) | |
Constructor that initializes the slice_pointer_base. | |
slice_pointer_base (const slice_pointer_base< T, T *, T & > &p) | |
Copy constructor for non-const type, and construction of const type from non-const type. | |
void | set (pointer first, Subscript span, Subscript) |
Set values of slice_pointer_base members. | |
pointer | get_current () const |
Return pointer to current element. | |
Subscript | get_span () const |
Return distance separating consecutive elements. | |
reference | operator * () const |
Return current element pointed to. | |
pointer | operator-> () const |
Dereference. | |
reference | operator[] (Subscript i) const |
zero-offset subscripting | |
slice_pointer_base & | operator++ () |
Increment to point to next element and return modified slice_pointer_base (preincrement). | |
slice_pointer_base | operator++ (int) |
Increment to point to next element and return new slice_pointer_base pointing to original element (postincrement). | |
slice_pointer_base & | operator-- () |
Decrement to point to previous element and return modified slice_pointer_base (predecrement). | |
slice_pointer_base | operator-- (int) |
Decrement to point to previous element and return new slice_pointer_base pointing to original element (postdecrement). | |
slice_pointer_base & | operator+= (Subscript i) |
Increment to point to element that is i elements greater than current element. | |
slice_pointer_base & | operator-= (Subscript i) |
Decrement to point to element that is i elements less than current element. | |
slice_pointer_base | operator+ (Subscript i) const |
Return new slice_pointer_base that points to i elements greater than the current element. | |
slice_pointer_base | operator- (Subscript i) const |
Return new slice_pointer_base that points to i elements less than the current element. | |
Friends | |
Subscript | operator- (const slice_pointer_base< T, PTR, REF > &lhs, const slice_pointer_base< T, PTR, REF > &rhs) |
Return number of elements separating current elements pointed to by lhs and rhs. | |
bool | operator== (const slice_pointer_base< T, PTR, REF > &lhs, const slice_pointer_base< T, PTR, REF > &rhs) |
Returns true if two slice_pointer_base objects point to the same element. | |
bool | operator!= (const slice_pointer_base< T, PTR, REF > &lhs, const slice_pointer_base< T, PTR, REF > &rhs) |
Returns true if two slice_pointer_base objects point to the different elements. | |
bool | operator> (const slice_pointer_base< T, PTR, REF > &lhs, const slice_pointer_base< T, PTR, REF > &rhs) |
Returns true if lhs points to an element greater than the element pointed to by rhs. | |
bool | operator>= (const slice_pointer_base< T, PTR, REF > &lhs, const slice_pointer_base< T, PTR, REF > &rhs) |
Returns true if lhs points to an element greater than or equal to the element pointed to by rhs. | |
bool | operator< (const slice_pointer_base< T, PTR, REF > &lhs, const slice_pointer_base< T, PTR, REF > &rhs) |
Returns true if lhs points to an element less than the element pointed to by rhs. | |
bool | operator<= (const slice_pointer_base< T, PTR, REF > &lhs, const slice_pointer_base< T, PTR, REF > &rhs) |
Returns true if lhs points to an element less than or equal to the element pointed to by rhs. |
This class is a generalization of a normal pointer that allows consecutive elements to be stored non-consecutively in memory (consecutive elements are separated from one another by a constant distance).
T | Type of elements pointed to. | |
PTR | Type of pointer to element (T* or const T*) | |
REF | Type of reference to element (T& or const T&) |
Austern, Matt (1999). Generic programming and the STL. Reading, MA: Addison-Wesley.
A slice_pointer_base<T, T*, T&> with a distance of 1 separating consecutive elements should have the same behavior as type T*, and a slice_pointer_base<T, const T*, const T&> with a distance of 1 separating consecutive elements should have the same behavior as type const T*.
An example of the use of a slice_pointer_base is to allow iterators over column elements in row-major matrices stored in one contiguous block of memory, where the matrix has M rows and two consecutive elements in a column are separated by M-1 elements.
This class does not manage the memory it points to. In other words, slice_pointer_base objects are initialized with pointers that point to allocated areas of memory, and the memory pointed to is not released when slice_pointer_base objects are destroyed.
The techinique used for logical comparison operators that allow constant types to be compared with non-constant types is described in:
Austern, Matt (2001, January). Defining iterators and const iterators. C/C++ Users Journal (www.cuj.com), 74-79.
Definition at line 74 of file slice_pointer.h.
SCPPNT::slice_pointer_base< T, PTR, REF >::slice_pointer_base | ( | pointer | first, | |
Subscript | span, | |||
Subscript | size | |||
) | [inline] |
Constructor that initializes the slice_pointer_base.
first | Pointer to first element. | |
span | Distance between consecutive elements (i-th element is given by first + (i-1) * span. If span == 1 then the object behaves the same as a T*. | |
size | Number of elements pointed to. This argument is only used when bounds checking is enabled. |
Definition at line 102 of file slice_pointer.h.
void SCPPNT::slice_pointer_base< T, PTR, REF >::set | ( | pointer | first, | |
Subscript | span, | |||
Subscript | ||||
) | [inline] |
Set values of slice_pointer_base members.
first | Pointer to first element. | |
span | Distance between consecutive elements (i-th element is given by first + (i-1) * span. If span == 1 then behavior is the same as T*. | |
size | Number of elements pointed to. This argument is only used when bounds checking is enabled. |
Definition at line 134 of file slice_pointer.h.
00137 { 00138 current = first; 00139 this->span = span; 00140 #ifdef SCPPNT_BOUNDS_CHECK 00141 this->first = first; 00142 last = first + (size-1) * span; 00143 #endif 00144 }