Jive reference manual
|
Implements a generic, resizable vector. More...
#include <jem/util/Flex.h>
Public Types | |
typedef T * | Iterator |
A random access iterator type. More... | |
typedef const T * | ConstIterator |
A read-only random access iterator type. More... | |
Public Member Functions | |
Flex () | |
Constructs an empty flex. More... | |
Flex (int n) | |
Constructs a flex of a given size. More... | |
Flex (const Flex &rhs) | |
Constructs a copy of another flex. More... | |
template<class InputIterator > | |
Flex (InputIterator first, InputIterator last) | |
Constructs a flex given two input iterators. More... | |
~Flex () | |
Deallocates all memory allocated by this flex. More... | |
Iterator | begin () |
Returns an iterator pointing to the begin of this flex. More... | |
ConstIterator | begin () const |
Returns a const iterator pointing to the begin of this flex. More... | |
Iterator | end () |
Returns an iterator pointing to the end of this flex. More... | |
ConstIterator | end () const |
Returns a const iterator pointing to the end of this flex. More... | |
T & | front () |
Returns a reference to the first element in this flex. More... | |
const T & | front () const |
Returns a const reference to the first element in this flex. More... | |
T & | back () |
Returns a reference to the last element in this flex. More... | |
const T & | back () const |
Returns a const reference to the last element in this flex. More... | |
Flex & | operator= (const Flex &rhs) |
Copies another flex. More... | |
Flex & | operator= (T rhs) |
Sets all elements in this flex to a given value. More... | |
void | swap (Flex &rhs) |
Swaps the contents of this flex with another flex. More... | |
T & | operator[] (int i) |
Returns the element at a given index. More... | |
const T & | operator[] (int i) const |
Returns the element at a given index. More... | |
void | pushBack (T item) |
Appends an element to this flex. More... | |
void | pushBack (T item, int n) |
Appends multiple copies of an element to this flex. More... | |
template<class InputIterator > | |
void | pushBack (InputIterator first, InputIterator last) |
Appends the elements between two input iterators to this flex. More... | |
void | popBack () |
Deletes the last element in this flex. More... | |
void | popBack (int n) |
Deletes multiple elements at the end of this flex. More... | |
void | insert (Iterator pos, T item) |
Inserts an element into this flex. More... | |
void | insert (Iterator pos, T item, int n) |
Inserts multiple copies of an element into this flex. More... | |
template<class InputIterator > | |
void | insert (Iterator pos, InputIterator first, InputIterator last) |
Inserts the elements between two input iterators into this flex. More... | |
void | erase (Iterator pos) |
Deletes an element from this flex. More... | |
void | erase (Iterator first, Iterator last) |
Deletes multiple elements from this flex. More... | |
void | clear () |
Deletes all elements from this flex. More... | |
void | resize (int n) |
Adjusts the size of this flex. More... | |
void | reserve (int cap) |
Sets the capacity of this flex to a given value. More... | |
void | setExpansionFactor (float x) |
Sets the expansion factor if this flex to a given value. More... | |
float | getExpansionFactor () const |
Returns the expansion factor of this flex. More... | |
void | trimToSize () |
Sets the capacity to the size of this flex. More... | |
int | capacity () const |
Returns the capacity of this flex. More... | |
int | size () const |
Returns the size of this flex. More... | |
T * | addr (int i=0) |
Returns a pointer to an element in this flex. More... | |
const T * | addr (int i=0) const |
Returns a const pointer to an element in this flex. More... | |
Static Public Attributes | |
static const float | EXPANSION_FACTOR |
The default expansion factor. More... | |
Related Functions | |
(Note that these are not member functions.) | |
template<class T > | |
io::DataInput & | operator>> (io::DataInput &in, Flex< T > &f) |
Flex de-serialization operator. More... | |
template<class T > | |
io::DataOutput & | operator<< (io::DataOutput &out, const Flex< T > &f) |
Flex serialization operator. More... | |
template<class T > | |
io::TextOutput & | operator<< (io::TextOutput &out, const Flex< T > &f) |
Flex print operator. More... | |
template<class T > | |
void | swap (Flex< T > &lhs, Flex< T > &rhs) |
Interchanges two flex instances. More... | |
The template class Flex
represents a vector (one-dimensional array) containing elements of type T. The elements are numbered in the usual way from zero to the total number of elements minus one. They can be accessed in constant time by calling the overloaded subscript operator or by dereferencing an iterator returned by a Flex
instance.
In contrast to the Array
class, the Flex
class provides functions for adding and removing elements. Although elements can be inserted at arbitrary positions, the Flex
class is optimized for appending elements to the end of a Flex
. Likewise, removing elements from the end of a Flex
is much more efficient than removing elements from the middle or the beginning of a Flex
.
A Flex
has a capacity that specifies how many elements it can store without having to allocate additional memory. The capacity is automatically increased whenever there is no more space to store additional elements. The capacity can also be set manually when you know beforehand how many elements you are going to add to a Flex
.
The automatic capacity growth of a Flex
can be controlled by means of its expansion factor. Each time a Flex
needs to allocate memory for additional elements, it multiplies its current capacity with its expansion factor to determine its new capacity. A larger expansion factor therefore leads to a more `aggressive' capacity growth.
Several members of the Flex
class return iterators, pointers and references that refer to an element in a Flex
instance. Unless stated otherwise, these iterators, pointers and references are valid as long as the Flex
instance is not structurally modified, either by adding/removing elements, or by changing the capacity of the flex.
A Flex
stores its elements in a contiguous array that can be accessed directly. This means that one can pass the elements in a Flex
to a function that expects a standard array.
Array
, ArrayBuffer
. typedef T* jem::util::Flex< T >::Iterator |
A random access iterator type pointing to elements of type T. This iterator fulfills the requirements of the random access iterator category of the standard C++ library.
typedef const T* jem::util::Flex< T >::ConstIterator |
A random access iterator type pointing to elements of type const T. This iterator fulfills the requirements of the random access iterator category of the standard C++ library.
jem::util::Flex< T >::Flex | ( | ) |
Constructs an empty flex.
this->size() == 0 &&
this->capacity() == 0 &&
this->getExpansionFactor() == EXPANSION_FACTOR
|
explicit |
Constructs a flex of size n.
n | - the size of this Flex . |
n >= 0
this->size() == n &&
this->capacity() == n &&
this->getExpansionFactor() == EXPANSION_FACTOR
jem::util::Flex< T >::Flex | ( | const Flex< T > & | rhs | ) |
Constructs a flex containing copies of the elements of the rhs flex. The elements of the newly created flex are initialized by calling the copy constructor of type T.
rhs | - the Flex to be copied. |
this->size() == rhs.size() &&
this->capacity() == this->size() &&
this->getExpansionFactor() == rhs.getExpansionFactor()
jem::util::Flex< T >::Flex | ( | InputIterator | first, |
InputIterator | last | ||
) |
Constructs a flex containing copies of the elements between the input iterators first and last. The elements of the newly created flex are initialized by calling the copy constructor of type T.
first | - an input iterator pointing to the first element. |
last | - an input iterator pointing one position past the last element. |
*first
can be converted to type T, this->size() == std::distance( first, last ) &&
this->capacity() == this->size() &&
this->getExpansionFactor() == EXPANSION_FACTOR
jem::util::Flex< T >::~Flex | ( | ) |
Deallocates all memory that has been allocated by this flex. The destructor invalidates all iterators, pointers and references that point to an element in this flex.
Iterator jem::util::Flex< T >::begin | ( | ) |
Returns an iterator pointing to the first element in this flex, or end()
if this flex is empty. The iterator is valid as long as this flex is not structurally modified, either by adding/removing elements, or by changing the capacity of this flex.
ConstIterator jem::util::Flex< T >::begin | ( | ) | const |
Returns a const iterator pointing to the first element in this flex, or end()
if this flex is empty. The iterator is valid as long as this flex is not structurally modified.
Iterator jem::util::Flex< T >::end | ( | ) |
Returns an iterator pointing to one position past the last element in this flex. The iterator is valid as long as this flex is not structurally modified.
ConstIterator jem::util::Flex< T >::end | ( | ) | const |
Returns a const iterator pointing to one position past the last element in this flex. The iterator is valid as long as this flex is not structurally modified.
T& jem::util::Flex< T >::front | ( | ) |
Returns a reference to the first element in this flex. The reference is valid as long as this flex is not structurally modified.
this->size() > 0
(*this)[0]
const T& jem::util::Flex< T >::front | ( | ) | const |
Returns a const reference to the first element in this flex. The reference is valid as long as this flex is not structurally modified.
this->size() > 0
(*this)[0]
T& jem::util::Flex< T >::back | ( | ) |
const T& jem::util::Flex< T >::back | ( | ) | const |
Flex& jem::util::Flex< T >::operator= | ( | const Flex< T > & | rhs | ) |
The assignment operator first resizes this flex so that it has the same size as the rhs flex. Next, the values of the elements in the rhs flex are assigned to the elements in this flex by calling the assignment operator of type T.
rhs | - the Flex to be copied. |
this->size() == rhs.size() &&
this->getExpansionFactor() == rhs.getExpansionFactor()
*this
Flex& jem::util::Flex< T >::operator= | ( | T | rhs | ) |
Sets all elements in this flex to the value rhs by calling the assignment operator of type T.
rhs | - an instance of type T. |
*this
void jem::util::Flex< T >::swap | ( | Flex< T > & | rhs | ) |
Swaps the internal state of this flex with that of the rhs flex. Note that this function just swaps a few pointers; it does not swap the individual elements.
rhs | - a Flex instance. |
T& jem::util::Flex< T >::operator[] | ( | int | i | ) |
Returns a reference to the i-th element in this flex. The reference is valid as long as this flex is not structurally modified.
i | - a valid index. |
i >= 0 && i < this->size()
const T& jem::util::Flex< T >::operator[] | ( | int | i | ) | const |
Returns a const reference to the i-th element in this flex. The reference is valid as long as this flex is not structurally modified.
i | - a valid index. |
i >= 0 && i < this->size()
void jem::util::Flex< T >::pushBack | ( | T | item | ) |
Appends a copy of the object item to the end of this flex. The copy is created by calling the copy constructor of type T. The capacity of this flex will be increased if necessary.
The time complexity of this function is O(1), provided that the capacity of this flex is large enough.
item | - the object to be appended. |
this->back() == item
void jem::util::Flex< T >::pushBack | ( | T | item, |
int | n | ||
) |
Appends n copies of the object item to the end of this flex. The copies are initialized by calling the copy constructor of type T. The capacity of this flex will be increased if necessary.
The time complexity of this function is O(n), provided that the capacity of this flex is large enough.
item | - the object to be copied and appended. |
n | - the number of copies to be appended. |
n >= 0
void jem::util::Flex< T >::pushBack | ( | InputIterator | first, |
InputIterator | last | ||
) |
Appends copies of the elements between the input iterators first and last to the end of this flex. The copies are initialized by calling the copy constructor of type T. The capacity of this will be increased if necessary.
Provided that the capacity of this flex is large enough, the time complexity of this function is O(n) with n the number elements between the two input iterators.
first | - an input iterator pointing to the first element to be appended. |
last | - an input iterator pointing one position past the lest element to be appended. |
*first
can be converted to type T, void jem::util::Flex< T >::popBack | ( | ) |
Deletes the last element in this flex by calling the destructor of type T. The capacity of this flex is not modified.
The time complexity of this function is O(1).
this->size() > 0
void jem::util::Flex< T >::popBack | ( | int | n | ) |
Deletes the last n elements in this flex by calling the destructor of type T. The capacity of this flex is not modified.
This function is equivalent with:
The time complexity of this function is O(n).
n | - the number of elements to be deleted. |
this->size() >= n
void jem::util::Flex< T >::insert | ( | Iterator | pos, |
T | item | ||
) |
Inserts a copy of the object item into this flex before the iterator pos. The copy is initialized by calling the assignent operator of type T. The element pointed to by pos, and all succeeding elements, are shifted one position to the right. The capacity of this flex will be increased is necessary.
If the capacity of this flex is large enough, the time complexity of this function is O(k) with k equal to (end() - pos)
.
pos | - an iterator pointing to an element in this flex, or end() . |
item | - the object to be inserted. |
void jem::util::Flex< T >::insert | ( | Iterator | pos, |
T | item, | ||
int | n | ||
) |
Inserts n copies of the object item into this flex before the iterator pos. The copies are initialized by calling the assignent operator of type T. The element pointed to by pos, and all succeeding elements, are shifted n positions to the right. The capacity of this flex will be increased is necessary.
If the capacity of this flex is sufficient, the time complexity of this function if O(n + k) with k equal to (end() - pos)
.
pos | - an iterator pointing to an element in this flex, or end() . |
item | - the object to be inserted. |
n | - the number of copies to be inserted. |
void jem::util::Flex< T >::insert | ( | Iterator | pos, |
InputIterator | first, | ||
InputIterator | last | ||
) |
Inserts copies of the elements between the input iterators first and last into this flex before the iterator pos. The copies are initialized by calling the assignent operator of type T. The element pointed to by pos, and all succeeding elements, are shifted to the right. The capacity of this flex will be increased is necessary.
If the capacity of this flex is large enough, the time complexity of this function if O(n + k) with n equal to std::distance( first, last )
and k equal to (end() - pos)
.
pos | - an iterator pointing to an element in this flex, or end() . |
first | - an input iterator pointing to the first element to be inserted. |
last | - an input iterator pointing to one position past the last element to be inserted. |
*first
can be converted to type T, void jem::util::Flex< T >::erase | ( | Iterator | pos | ) |
Deletes the element pointed to by the iterator pos from this flex by calling the destructor of type T. All succeeding elements are shifted one position to the left. The capacity of this flex is not modified.
The time complexity of this function is O(k) with k equal to (end() - pos)
.
pos | - an iterator pointing to an element in this flex, or end() . |
void jem::util::Flex< T >::erase | ( | Iterator | first, |
Iterator | last | ||
) |
Deletes all elements between the iterators first and last from this flex by calling the destructor of type T. All elements succeeding the last deleted element are shifted to the left. The capacity of this flex is not modified.
The time complexity of this function is O(n + k) with n equal to (last - first)
and k equal to (end() - pos)
.
first | - an iterator pointing to the first element to be deleted, or end() . |
last | - an iterator pointing one position past the last element to be deleted, or end() . |
void jem::util::Flex< T >::clear | ( | ) |
void jem::util::Flex< T >::resize | ( | int | n | ) |
void jem::util::Flex< T >::reserve | ( | int | cap | ) |
If cap is larger than the current capacity, then the capacity of this flex is increased to cap. Otherwise, this function does nothing.
cap | - the new capacity of this flex. |
this->capacity() >= cap
void jem::util::Flex< T >::setExpansionFactor | ( | float | x | ) |
Sets the expansion factor of this flex to the value x.
x | - the new expansion factor of this flex. |
x >= 1.0
this->getExpansionFactor() == x
float jem::util::Flex< T >::getExpansionFactor | ( | ) | const |
Returns the expansion factor of this flex.
void jem::util::Flex< T >::trimToSize | ( | ) |
Adjusts the capacity so that it becomes equal to the size of this flex.
this->capacity() == this->size()
int jem::util::Flex< T >::capacity | ( | ) | const |
Returns the capacity of this flex.
int jem::util::Flex< T >::size | ( | ) | const |
Returns the number of elements stored in this flex.
T* jem::util::Flex< T >::addr | ( | int | i = 0 | ) |
const T* jem::util::Flex< T >::addr | ( | int | i = 0 | ) | const |
|
related |
Reads the Flex
f from the data input stream in. The current contents of the flex f are discarded by calling f.clear()
.
in | - a data input stream. |
f | - the flex to be read. |
io::IOException | - if an IO error occurs. |
io::SerializationException | - if the data input stream is corrupt. |
|
related |
Writes the Flex
f to the data output stream out. The de-serialization operator can be used to restore the Flex
object.
out | - a data output stream. |
f | - the flex to be written. |
io::IOException | - if an I/O error occurs. |
|
related |
Prints the contents of the Flex
f in a human readable format to the text output stream out.
out | - a text output stream. |
f | - the flex to be printed. |
io::IOException | - if an I/O error occurs. |
Interchanges the state of two Flex
objects. This function is equivalent with:
lhs.swap ( rhs )
lhs | - a flex. |
rhs | - another flex. |
|
static |
This floating point constant specifies the default expansion factor of a Flex
. Its current value is 1.5, but a different value may be used in a future version of Jem.