template <class T, int NBLK = 16>
class Segtor : public MoveableAndDeepCopyOption< Segtor<T, NBLK> >
T |
Type of elements stored in Segtor. T must have either deep copy constructor, pick constructor or default constructor. |
Segtor is special flavor of random access container that can be sometimes used to improve memory consumption. It never invalidates iterators nor references to elements, but it also lacks operations to insert and remove elements at any position. Segtor is designed to be used in situations where size of each element is small, but it is either non-moveable or references to elements must be never invalidated.
Typical implementation stores elements in segments of fixed size. This fixed size is determined by template parameter NBLK.
Iterators to Segtor satisfy all C++ standard library requirements for random access iterator plus they allow assignment (and copy constructor) and testing for 0 (that is NULL) pointer.
Like any other NTL container, Segtor is moveable type with pick and optional deep copy transfer semantics. Calling methods of picked Segtor is logic error with exception of
void operator=(pick_ Array& v)
void operator<<=(const Array& v) (defined using DeepCopyOptionTemplate)
void Clear()
bool IsPicked() const
Optional deep copy is implemented through DeepCopyOptionTemplate macro.
Segtor()
Default constructor. Constructs an empty Segtor.
Segtor(pick_ Segtor& s)
Pick constructor. Transfers source Segtor in low constant time, but destroys it by picking.
Segtor(const Segtor& s, int)
Optional deep copy constructor.
Requires T to have deep copy constructor or optional deep copy constructor.
~Segtor()
Destructor. Invokes the destructor of all elements in the Segtor.
T& Add()
Adds a new default constructed element to Segtor.
Requires T to have default constructor.
|
Return value |
Reference to the newly added default constructed element. |
void Add(const T& x)
Adds a new element with the specified value to Segtor.
Requires T to have deep copy constructor.
|
x |
The value that is copied to the newly created element. |
void AddPick(pick_ T& x)
Adds a new element to Segtor and picks value of parameter x.
Requires T to have pick constructor.
|
x |
Source instance of T that is to be picked. |
T& operator[](int i)
Returns a reference to the element at the specified position.
|
i |
Position of the element. |
|
Return value |
Reference to the element. |
const T& operator[](int i) const
Returns a reference to the element at the specified position.
|
i |
Position of the element. |
|
Return value |
Constant reference to the element. |
int GetCount() const
Return the number of elements in Segtor.
|
Return value |
Actual number of elements. |
bool IsEmpty() const
Tests whether Segtor is empty. Same as GetCount() == 0.
|
Return value |
true if Segtor is empty, false otherwise. |
void SetCount(int n)
Changes count of elements in Segtor to specified value. If required number of elements is greater than actual number, newly added elements are default constructed.
Requires T to have default constructor.
|
n |
Required number of elements. |
void SetCount(int n, const T& init)
Changes count of elements in Segtor to specified value. If required number of elements is greater than actual number, newly added elements are initialized to the specified value using copy constructor.
Requires T to have deep copy constructor.
|
n |
Required number of elements. |
|
init |
Initialization value of newly added elements. |
void Clear()
Removes all elements from Segtor. Capacity is also cleared to zero.
T& At(int i)
If specified position is lower than number of elements in Segtor (i < GetCount()), returns reference to element at specified position. Otherwise increases number of elements in Segtor to i + 1. Newly added elements are default constructed.
Requires T to have default constructor.
|
i |
Position of required element. |
|
Return value |
Reference to required element. |
T& At(int i, const T& x)
If the specified position is lower than number of elements in Segtor (i < GetCount()), returns reference to element at specified position. Otherwise increases number of elements in Segtor to i + 1. Newly added elements are deep-copy constructed from x.
Requires T to have deep copy constructor.
|
i |
Position of the required element. |
|
x |
Initialization value of the newly added elements. |
|
Return value |
Reference to the required element. |
void Shrink()
Minimizes memory consumption of Segtor.
void Reserve(int xtra)
Reserves capacity. If the required capacity is greater than the current capacity, capacity is increased to the required value.
int GetAlloc() const
Returns current capacity of Segtor.
|
Return value |
Capacity of Segtor. |
void Set(int i, const T& x, int count = 1)
Sets requested number of elements starting at the position i to the specified value. If required number of elements exceeds existing elements of Segtor then elements are added to Segtor.
Requires T to have deep copy constructor.
|
count |
Number of elements. |
void Drop(int n = 1)
Drops specified number of last elements in the Segtor.
T& Top()
Returns reference to the last element in the Segtor.
|
Return value |
Reference of last element in the Segtor. |
const T& Top() const
Returns a constant reference to the last element in the Segtor.
|
Return value |
Reference of last element in the Segtor. |
T Pop()
Drops the last element of the Segtor and returns its value.
Requires T to have deep copy constructor.
|
Return value |
Value of the dropped element. |
Segtor& operator<<(const T& x)
Operator variant of void Add(const T&x). Returning a reference to the Segtor enables adding more elements in a single expression, thus e.g. allowing to construct a temporary Segtor as part of an expression like Foo((Segtor<int>() << 1 << 2 << 4)).
Requires T to have deep copy constructor.
|
x |
The value that is copied to the newly created element. |
|
Return value |
Reference to Segtor (*this). |
Segtor& operator|(pick_ T& x)
Operator replacement of void AddPick(pick_ T&x). By returning reference to to Segtor allows adding more elements in single expression, thus e.g. allowing to construct temporary Segtor as part of expression.
Requires T to have pick constructor.
|
x |
Source instance of T that is to be picked. |
|
Return value |
Reference to Segtor (*this). |
void Serialize(Stream& s)
Serializes the content of the Segtor to/from the Stream. Works only if NTL is used as part of UPP.
Requires T to have serialization operator defined.
bool IsPicked() const
Returns true if Segtor is in picked state.
|
Return value |
true if Segtor is in picked state, false otherwise. |
typedef T ValueType
Typedef of T for use in templated algorithms.
typedef IIterator<Segtor> Iterator
Iterator type.
typedef ConstIIterator<Segtor> ConstIterator
Constant iterator type.
ConstIterator Begin() const
Returns a constant iterator to the first element in Segtor.
ConstIterator End() const
Returns a constant iterator to the position just beyond the last element in Segtor.
ConstIterator GetIter(int pos) const
Returns a constant iterator to the element at the specified position. Same as Begin() + pos. The benefit of this method is that in debug mode pos is range checked.
Iterator Begin()
Returns a non-constant iterator to the first element in Segtor.
Iterator End()
Returns a non-constant iterator to the position just beyond the last element in Segtor.
Iterator GetIter(int pos)
Returns a non-constant iterator to the element at specified position. Same as Begin() + pos. The benefit of this methods is that in debug mode pos is range checked.
friend void Swap(Segtor& a, Segtor& b)
Specialization of generic Swap for Segtor. Swaps array in simple constant time operation.
|