The Vector class family implement vectors in the usual algebraic notion.
It contains three persistent
vector types with different data storage organization:
template <typename ElementType> class SparseVector;
is an associative container with element indices (coordinates) as keys; elements equal to the default value
(lementTypeE(), which is 0 for most numerical types) are not stored, but implicitly encoded by the gaps in the
key set. It is based on a balanced binary search (AVL) tree.
template <typename ElementType, int size> class FixedVector;
is a built-in array of a fixed dimension decorated with the common vector interface.
The following brief analysis might help you when choosing the most efficient representation for a concrete case.
n is the number of (non-zero in sparse case) elements in the vector.
if no preceding random access operations were performed on the sparse vector
if there already were random access operations on the sparse vector
Top, the concrete vector object behind the generic reference
generic_type
GenericVector<Top> itself
persistent_type
Vector<ElementType> for dense vectors,
SparseVector<ElementType> for sparse
vectors.
Methods and friend functions applicable to GenericVector directly are scattered over the entire page.
They are discernible on the prefix GenericVector:: or parameters of type GenericVector&.
To use other methods via a generic reference (or pointer), you must first move to the concrete object using the
top() method.
Any persistent class: Vector, SparseVector, or FixedVector.
In the latter case the dimension argument is ignored, as the vector dimension is hard encoded into the object type.
Vector or FixedVector.
In the latter case the dimension argument is ignored, as the vector dimension is hard encoded into the object type.
Create a vector of dimension n, initialize all elements with value.
Use of corresponding constructor for SparseVector would defeat all the advantages of the
sparse structure, therefore it is not defined.
Create a vector of dimension n, initialize the elements from a data sequence.
For SparseVector, Iterator can be either
indexed,
or supply index-value pairs, e.g. std::pair<int, ElementType>
or a plain sequence of data items. In the latter case zero elements are filtered out.
Persistent vector objects have after the assignment the same dimension as the right hand side operand.
Alias objects, such as vector slice or concatenated vector, cannot be resized, thus
they must have the same dimension as on the right hand side.
Tell the current vector dimension. For dense vectors it is always the same as size().
For sparse vectors, however, the latter tells the number of non-zero elements.
Sequential access
All vector classes implement the STL
Forward Container interface
or one of its refinements. This means, every vector object can be traversed using an
iterator or const_iterator created by begin() method or
entire() function,
its size can be retrieved with size() method and so on.
For a vector object accessed via GenericVector reference or
pointer, you need first get to the concrete class with top() method
before you can use the standard container interface.
SparseVector inherits the
data access methods from the binary tree.
Note that dereferencing the SparseVector::iterator
yields a reference to the element value, not a key-value pair as in the
std::map class or other associative containers.
The SparseVector::iterator::index() method tells the current element index (coordinate.)
Random access
Random-access vector object: Vector, concatenation of random-access vectors,
or a slice of a random-access vector with a random-access index set
(sequence or series).
The real return type is a temporary proxy object, forwarding the read/write access operations
to the sparse vector. Prior to the destruction, which happens after the completion of the expression
envolving the random access, it checks whether the element became zero; if so, it is removed from
the vector.
The real return type is a temporary object (expression template) implementing the lazy evaluation
of the operation result. You should have it in mind when passing the result of a vector expression
as a GenericVector argument to a template function where it has a chance to be evaluated multiple
times. See also prevent_lazy class.
The vector and matrix operators defined in the Polymake Template Library do take care of this,
so this remark applies more likely to your own functions.
The concrete vector type hidden behind the GenericVector.
Do exactly what the mnemonics suggest. Every type combination is allowed, including
mixing of sparse and dense vectors. ElementType of vector operands and Scalar type
can be different, provided the arithmetic operator with corresponding arguments exists.
The dimension check can be activated.
The real result type is a temporary alias object of type pm::VectorSlice.
It inherits the const-ness from the original vector.
The real result type is a temporary alias object of type pm::VectorChain.
It is mutable only if both operands are mutable too.
The real result type is a masquerade reference pm::SingleElementVector&.
The real result type is a temporary object of type pm::SameElementVector.
It contains only a single copy of an element, or just a reference to an element,
but makes it to appear as a sequence of identical elements.
The real result type is a temporary object of type pm::SameElementSparseVector.
It contains only a single copy of an element, or just a reference to it,
but makes it to appear as a sequence of elements.
GenericVector GenericVector::slice (int start, int size=0);
Select a vector slice consisting of elements with given indices. The last variant
selects a contiguous range of indices beginning with start. size==0 means
up to the end of the vector. The const variants of these methods create immutable slice objects.
The index range check can be activated.
Concatenate two vectors, yielding a longer vector. A scalar argument is treated
as a vector of dimension 1. ElementType of vector operands must be identical, otherwise you will
get a rather cryptic message from the compiler.
Create a dense vector of dimension n, with all elements equal to x, 1, or 0, respectively.
Note the obligatory explicit specification of the template parameter ElementType, where
it cannot be deduced from the function arguments.
Create a sparse vector of dimension n, with elements equal to x
at given coordinates and 0 as the rest (vice versa by Complement.)
If omitted, x is taken as 1; note the obligatory explicit template parameter specification in this case.
Create a sparse vector of dimension n, whose only non-zero element
has the coordinate i and value x.
If omitted, x is taken as 1; note the obligatory explicit template parameter specification in this case.
If std::ostream::width() is not 0 before the operator is called, each element of a vector
is printed in a width-wide field; lacking elements of a sparse vector are printed as dots.
If width is 0, a dense vector as well as a sparse vector with fill grade over 0.5 is printed
as a space-separated list of elements. A "really" sparse vector appears as a list of (index value) pairs.
Read the space-separated data items from the input stream and assign them to the
vector elements. The current dimension of the vector is not changed.
When reading a sparse vector, single dots and items equal to zero are not stored.
Read a property from the polymake server. Set the vector dimension to the number of elements read.
Parse errors are reported by raising the std::iostream::failure exception in the constructor,
or via std::iostream::fail() flag in the input operator.