KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Public Member Functions | List of all members
kjb::Vector Class Reference

This class implements vectors, in the linear-algebra sense, with real-valued elements. More...

#include <m_vector.h>

Public Types

typedef double Value_type
 data type of the elements More...
 
typedef kjb_c::Vector Impl_type
 the underlying implementation More...
 
typedef Matrix Mat_type
 the associated matrix type More...
 
typedef Generic_matrix_view
< Matrix
Mat_view_type
 view type of the associated matrix type More...
 
typedef Vector Vec_type
 the associated vector type More...
 
typedef Value_type(* Mapper )(Value_type)
 element transformer fun More...
 
typedef double value_type
 
typedef double * pointer
 
typedef const double * const_pointer
 
typedef double & reference
 
typedef double const_reference
 
typedef value_typeiterator
 
typedef const value_typeconst_iterator
 
typedef std::reverse_iterator
< iterator
reverse_iterator
 
typedef std::reverse_iterator
< const_iterator
const_reverse_iterator
 const Iterator type More...
 
typedef int size_type
 
typedef int difference_type
 

Public Member Functions

 Vector (int length=0)
 Allocate vector of given length; contents are uninitialized. More...
 
 Vector (unsigned length)
 Allocate vector of given length; contents are uninitialized. More...
 
 Vector (unsigned long length)
 Allocate vector of given length; contents are uninitialized. More...
 
 Vector (int length, Value_type num)
 Allocate vector of given length; initialize all elts to 'num'. More...
 
 Vector (int length, const Value_type *data)
 Initialize from an array of doubles, of given length. More...
 
 Vector (const Int_vector &)
 Ctor builds a vector from one with integer values. More...
 
 Vector (int length, const float *data)
 Initialize from an array of floats, of given length. More...
 
 Vector (Impl_type *vec_ptr)
 Conversion ctor: claim ownership of an existing vector pointer (i.e., make a shallow copy). More...
 
 Vector (const std::vector< double > &src)
 Conversion ctor: Create from stl-style vector. More...
 
 Vector (Value_type val)
 Create a single-element vector. More...
 
 Vector (const Vector_view &view)
 Create a vector from a view of another vector. More...
 
 Vector (const Const_vector_view &view)
 Create a vector from a view of another vector. More...
 
 Vector (Value_type val1, Value_type val2)
 Create a two-vector. More...
 
 Vector (Value_type val1, Value_type val2, Value_type val3)
 Create a three-vector. More...
 
 Vector (Value_type val1, Value_type val2, Value_type val3, Value_type val4)
 Create a four-vector. More...
 
 Vector (const Impl_type &vec_ref)
 Ctor copies contents (i.e., deep copy) of an existing vector. More...
 
 Vector (const Mat_type &src)
 Ctor builds from a one-row or one-column Matrix (making a deep copy). More...
 
 Vector (const Mat_view_type &src)
 Ctor builds from a one-row or one-column view of a Matrix (making a deep copy). More...
 
 Vector (const std::string &file_name)
 Construct a vector by reading contents from a named file. More...
 
 Vector (const Vector &vec_ref)
 Copy ctor – calls the kjb_c function to copy a vector. More...
 
template<typename InputIterator_ >
 Vector (InputIterator_ begin_, InputIterator_ end_)
 "Range constructor" More...
 
 ~Vector ()
 Destructor – which just calls the KJB destructor. More...
 
int get_length () const
 Return the length of the vector. More...
 
size_type size () const
 Alias to get_length(). Required to comply with stl Container concept. More...
 
size_type max_size () const
 Maximum size vector can ever have. Currently defined as INT_MAX. More...
 
bool empty () const
 Returns true iff size is zero. Required to comply with stl Container concept. More...
 
iterator begin ()
 
const_iterator begin () const
 
iterator end ()
 
const_iterator end () const
 
reverse_iterator rbegin ()
 
const_reverse_iterator rbegin () const
 
reverse_iterator rend ()
 
const_reverse_iterator rend () const
 
void reserve (int capacity)
 
Vectorrandomize (int length)
 Clobber current vector; resize and fill with random values. More...
 
Vectorrandomize ()
 Clobber current vector contents with random values. More...
 
Vectorzero_out (int length)
 Clone of randomize(int) More...
 
Vectorzero_out ()
 Clobber current vector contents with zeroes. More...
 
Vectorset (Value_type val)
 Clone of zero_out(int) More...
 
Vectorset (Value_type val1, Value_type val2)
 Convert to a 2-vector and set its values to the arguments. More...
 
Vectorset (Value_type val1, Value_type val2, Value_type val3)
 Convert to a 3-vector and set its values to the arguments. More...
 
Vectorset (Value_type val1, Value_type val2, Value_type val3, Value_type val4)
 Convert to a 4-vector and set its values to the arguments. More...
 
Vectoroperator= (const Mat_view_type &vec_ref)
 Assignment operator: assign from a single-row matrix-view. More...
 
Vectoroperator= (const Impl_type &vec_ref)
 Assignment operator: assign from a kjb_c::Vector, a C struct. More...
 
Vectoroperator= (const Vector &src)
 Assignment operator: assign from a kjb::Vector, a C++ object. More...
 
Vectorreplace (const Vector &iv, int offset, int begin, int length)
 Copy elements from input vector iv into this vector, starting at some offset, leaving the rest of this vector unchanged. More...
 
const Impl_typeget_c_vector () const
 Get const pointer to the underlying kjb_c::Vector C struct. More...
 
Impl_type *& get_underlying_representation_with_guilt ()
 Get pointer to the underlying kjb_c::Vector C struct. More...
 
Impl_typeget_underlying_representation_unsafe ()
 Get pointer to the underlying kjb_c::Vector C struct. More...
 
void swap (Vector &other)
 Swap the representations of two vectors. More...
 
Vectorresize (int new_length, Value_type pad=Value_type(0))
 Resize vector, retaining previous values. More...
 
Value_typeoperator[] (int i)
 Subscript vector like a C array, e.g., A[10], returning an lvalue. More...
 
const Value_typeoperator[] (int i) const
 Subscript vector like a C array, e.g., A[10], returning an rvalue. More...
 
Value_typeoperator() (int i)
 Fortran-style vector subscript, e.g., A(10), to get an lvalue. More...
 
const Value_typeoperator() (int i) const
 Fortran-style vector subscript, e.g., A(10), to get an rvalue. More...
 
Vector_view operator[] (const Index_range &idx)
 
Const_vector_view operator[] (const Index_range &idx) const
 
void check_bounds (int i) const
 Test whether a subscript is valid, throw an exception if not. More...
 
Value_typeat (int i)
 Safely subscript vector, e.g., A.at(10), to get an lvalue. More...
 
const Value_typeat (int i) const
 Safely subscript vector, e.g., A.at(10), to get an rvalue. More...
 
iterator insert (iterator position, value_type t)
 A copy of t is inserted before position. More...
 
void insert (iterator position, size_type N, value_type t)
 Insert N copies of t before position. More...
 
template<typename InputIterator >
void insert (iterator position, InputIterator begin_, InputIterator end_)
 Insert range [begin, end) before position. More...
 
void assign (size_type N, value_type t)
 Resizes to N, filling all values with t. More...
 
template<typename InputIterator >
void assign (InputIterator begin_, InputIterator end_)
 Assign this vector to the values in sequence (begin_, and_) More...
 
iterator erase (iterator position)
 erase element at position and shift all elements after p up by 1. More...
 
void erase (iterator begin_, iterator end_)
 erase all elements beginning with begin_ up to but not including end_ More...
 
void clear ()
 delete all elements from vector More...
 
reference front ()
 Returns the first element of the vector. More...
 
const_reference front () const
 Returns the first element of the vector. More...
 
reference back ()
 Returns the last element of the vector. More...
 
const_reference back () const
 Returns the last element of the vector. More...
 
void push_back (Value_type x)
 inserts an element at the back of the vector in amortized constant time. More...
 
void pop_back ()
 Returns the last element of the vector. More...
 
void write_row (const char *filename=0) const
 Write vector as a row to a file, or to standard output. More...
 
void write_col (const char *filename=0) const
 Write vector as a column to a file, or to standard output. More...
 
void read (const char *filename=0)
 Read vector from a file, or from standard input. More...
 
void write (const char *filename=0) const
 Write vector to a file, or to standard output. More...
 
Vectoroperator*= (Value_type op2)
 Scalar multiply self, in-place, e.g., v *= 6. More...
 
Vectormultiply (Value_type op2)
 Scalar multiply self, in-place, just like v *= 6. More...
 
Vectorew_multiply (const Vector &op2)
 Element wise multiplication of two vectors. More...
 
Vectoroperator/= (Value_type op2)
 Scalar division of self, in-place. More...
 
Vectordivide (Value_type op2)
 Scalar division of self, in-place, just like v /= 2. More...
 
Vectorew_divide (const Vector &op2)
 Element wise division of two vectors; result is stored in first. More...
 
Vectoroperator+= (const Vector &op2)
 Add vector to self, in-place, e.g., v += delta_v. More...
 
Vectoroperator+= (double x)
 Add scalar to self, in-place, e.g., v -= delta_v. More...
 
Vectoradd (const Vector &op2)
 Add vector to self, in-place, just like v += delta_v. More...
 
Vectoroperator-= (const Vector &op2)
 Subtract vector from self, in-place, e.g., v -= delta_v. More...
 
Vectoroperator-= (double x)
 Subtract scalar from self, in-place, e.g., v -= delta_v. More...
 
Vectorsubtract (const Vector &op2)
 Subtract vector from self, in-place, just like v -= delta_v. More...
 
Vectornegate ()
 Negate self, in-place, just like v *= (-1). More...
 
Vec_typemapcar (Mapper)
 Transform the elements of a vector. More...
 
Value_type min (int *min_index) const
 Find minimum element in the vector, both its value and index. More...
 
Value_type max (int *max_index) const
 Find maximum element in the vector, both its value and index. More...
 
double get_max_abs_difference (const Vector &op2) const
 returns the sum of the element-wise differences between this vector and the input vector More...
 
kjb::Matrix hat () const
 Construct an "equivalent" skew-symmetric matrix from 3-vector. More...
 
Vectorcross_with (const Vector &op2)
 Compute cross product of op1 and op2. More...
 
Value_type magnitude () const
 Return this vector's magnitude. More...
 
Value_type magnitude_squared () const
 Return this vector's squared magnitude. More...
 
Value_type sum_vector_elements () const
 Return this vector's elements sum. More...
 
Vectornormalize (kjb_c::Norm_method method=kjb_c::NORMALIZE_BY_MAGNITUDE)
 Normalize vector in place by choice of method (default by magnitude). More...
 
Vector normalized (kjb_c::Norm_method method=kjb_c::NORMALIZE_BY_MAGNITUDE) const
 non=mutating version of "normalize()" More...
 

Detailed Description

This class implements vectors, in the linear-algebra sense, with real-valued elements.

For better maintainability, refer to the element type using this class's Value_type typedef, instead of referring to 'double' directly.

Most methods of this class are implemented in the C language portion of the KJB library, with this class forming a thin (usually inlined) layer.

Member Typedef Documentation

typedef const double* kjb::Vector::const_pointer
typedef std::reverse_iterator<const_iterator> kjb::Vector::const_reverse_iterator

const Iterator type

typedef kjb_c::Vector kjb::Vector::Impl_type

the underlying implementation

typedef Value_type(* kjb::Vector::Mapper)(Value_type)

element transformer fun

the associated matrix type

view type of the associated matrix type

typedef double* kjb::Vector::pointer
typedef double& kjb::Vector::reference
typedef std::reverse_iterator<iterator> kjb::Vector::reverse_iterator
typedef double kjb::Vector::Value_type

data type of the elements

typedef double kjb::Vector::value_type

the associated vector type

Constructor & Destructor Documentation

kjb::Vector::Vector ( int  length = 0)
inlineexplicit

Allocate vector of given length; contents are uninitialized.

This also serves as a default ctor

kjb::Vector::Vector ( unsigned  length)
inlineexplicit

Allocate vector of given length; contents are uninitialized.

Warning
Native type for length is int, not unsigned.

This ctor is almost mandatory since we want to construct from a size_t, which on most platforms is unsigned int or unsigned long int.

kjb::Vector::Vector ( unsigned long  length)
inlineexplicit

Allocate vector of given length; contents are uninitialized.

Warning
Native type for length is int, not unsigned long.

This ctor is almost mandatory since we want to construct from a size_t, which on most platforms is unsigned int or unsigned long int.

kjb::Vector::Vector ( int  length,
Value_type  num 
)
inline

Allocate vector of given length; initialize all elts to 'num'.

kjb::Vector::Vector ( int  length,
const Value_type data 
)
inline

Initialize from an array of doubles, of given length.

kjb::Vector::Vector ( const Int_vector ivec)

Ctor builds a vector from one with integer values.

kjb::Vector::Vector ( int  length,
const float *  data 
)
inline

Initialize from an array of floats, of given length.

kjb::Vector::Vector ( Impl_type vec_ptr)
inlineexplicit

Conversion ctor: claim ownership of an existing vector pointer (i.e., make a shallow copy).

This method is the proper way to say, ''Here is a kjb_c::Vector struct that I am responsible for deleting, and I must make sure that it gets destroyed when it goes out of scope.'' This is a good way to wrap a vector "dynamically," after it has already been created.

Anyplace you find yourself using free_vector() in your C++ code, you should consider using instead this Vector class with this ctor.

If the input pointer equals NULL then a zero length vector is constructed.

Warning
Don't create two Vector objects from the same source this way or you'll get a double deletion bug.
kjb::Vector::Vector ( const std::vector< double > &  src)

Conversion ctor: Create from stl-style vector.

kjb::Vector::Vector ( Value_type  val)
inlineexplicit

Create a single-element vector.

kjb::Vector::Vector ( const Vector_view view)

Create a vector from a view of another vector.

kjb::Vector::Vector ( const Const_vector_view view)

Create a vector from a view of another vector.

kjb::Vector::Vector ( Value_type  val1,
Value_type  val2 
)
inline

Create a two-vector.

kjb::Vector::Vector ( Value_type  val1,
Value_type  val2,
Value_type  val3 
)
inline

Create a three-vector.

kjb::Vector::Vector ( Value_type  val1,
Value_type  val2,
Value_type  val3,
Value_type  val4 
)
inline

Create a four-vector.

kjb::Vector::Vector ( const Impl_type vec_ref)
inlineexplicit

Ctor copies contents (i.e., deep copy) of an existing vector.

Warning
This method should be seldom used: kjb_c::Vector objects should rarely be left in an unwrapped state.

This kind of conversion is relatively expensive, thus we restrict its use only to explicit invocation.

kjb::Vector::Vector ( const Mat_type src)
explicit

Ctor builds from a one-row or one-column Matrix (making a deep copy).

kjb::Vector::Vector ( const Mat_view_type src)
explicit

Ctor builds from a one-row or one-column view of a Matrix (making a deep copy).

This allows syntax like: Matrix m(100, 100); Vector v1 = m["1,3,5,7,9", 0]; Vector v2 = m[":", 0];

kjb::Vector::Vector ( const std::string &  file_name)

Construct a vector by reading contents from a named file.

kjb::Vector::Vector ( const Vector vec_ref)
inline

Copy ctor – calls the kjb_c function to copy a vector.

Although this method is essential, I hope it is seldom actually used. Most of the time it should be unnecessary or optimized away somehow.

template<typename InputIterator_ >
kjb::Vector::Vector ( InputIterator_  begin_,
InputIterator_  end_ 
)
inline

"Range constructor"

Note
Template tricks are used to do fast allocation if possible. That is, if N = end_ - begin_ is possible, a vector of size N is allocated at once. If not, push_back() is called N times, requiring log N allocations.
kjb::Vector::~Vector ( )
inline

Destructor – which just calls the KJB destructor.

Member Function Documentation

Vector& kjb::Vector::add ( const Vector op2)
inline

Add vector to self, in-place, just like v += delta_v.

void kjb::Vector::assign ( size_type  N,
value_type  t 
)

Resizes to N, filling all values with t.

Value_type& kjb::Vector::at ( int  i)
inline

Safely subscript vector, e.g., A.at(10), to get an lvalue.

"Safely" here means we do bounds checking and throw if out of bounds.

const Value_type& kjb::Vector::at ( int  i) const
inline

Safely subscript vector, e.g., A.at(10), to get an rvalue.

"Safely" here means we do bounds checking and throw if out of bounds.

reference kjb::Vector::back ( )
inline

Returns the last element of the vector.

Precondition
!this->empty()
const_reference kjb::Vector::back ( ) const
inline

Returns the last element of the vector.

Precondition
!this->empty()
iterator kjb::Vector::begin ( )
inline

Returns a read/write iterator that points to the first element in the Vector. Iteration is done in ordinary element order.

const_iterator kjb::Vector::begin ( ) const
inline

Returns a read-only (constant) iterator that points to the first element in the Vector. Iteration is done in ordinary element order.

void kjb::Vector::check_bounds ( int  i) const
inline

Test whether a subscript is valid, throw an exception if not.

Exceptions
Index_out_of_boundswith a message, if the index is bad.
void kjb::Vector::clear ( )
inline

delete all elements from vector

Warning
Calling this will never free allocated memory.
Vector & kjb::Vector::cross_with ( const Vector op2)

Compute cross product of op1 and op2.

Store result in this vector. Only defined for dimension = 3.

Vector& kjb::Vector::divide ( Value_type  op2)
inline

Scalar division of self, in-place, just like v /= 2.

bool kjb::Vector::empty ( ) const
inline

Returns true iff size is zero. Required to comply with stl Container concept.

iterator kjb::Vector::end ( )
inline

Returns a read/write iterator that points one past the last element in the Vector. Iteration is done in ordinary element order.

const_iterator kjb::Vector::end ( ) const
inline

Returns a read-only (constant) iterator that points one past the last element in the Vector. Iteration is done in ordinary element order.

Vector::iterator kjb::Vector::erase ( iterator  position)

erase element at position and shift all elements after p up by 1.

Precondition
!this->empty()
Warning
Calling this will never free allocated memory.
void kjb::Vector::erase ( iterator  begin_,
iterator  end_ 
)

erase all elements beginning with begin_ up to but not including end_

Precondition
[begin_, end_) is a valid range in *this
Warning
Calling this will never free allocated memory.
Vector& kjb::Vector::ew_divide ( const Vector op2)
inline

Element wise division of two vectors; result is stored in first.

Vector& kjb::Vector::ew_multiply ( const Vector op2)
inline

Element wise multiplication of two vectors.

reference kjb::Vector::front ( )
inline

Returns the first element of the vector.

Precondition
!this->empty()
const_reference kjb::Vector::front ( ) const
inline

Returns the first element of the vector.

Precondition
!this->empty()
const Impl_type* kjb::Vector::get_c_vector ( ) const
inline

Get const pointer to the underlying kjb_c::Vector C struct.

int kjb::Vector::get_length ( ) const
inline

Return the length of the vector.

double kjb::Vector::get_max_abs_difference ( const Vector op2) const

returns the sum of the element-wise differences between this vector and the input vector

Parameters
op2the input vector
Impl_type* kjb::Vector::get_underlying_representation_unsafe ( )
inline

Get pointer to the underlying kjb_c::Vector C struct.

Warning
This should NOT be used except inside wrapper functions. This function breaks encapsulation, and has the potential to completely screw up the C++ matrix object, so be sure you know what you're doing before calling this!! This method's name is intentionally long and scary to discourage its use outside of wrapper code.
Deprecated:
: don't use in new wrapper code. For new wrapper code, use get_underlying_representation_with_guilt() instead.
Impl_type*& kjb::Vector::get_underlying_representation_with_guilt ( )
inline

Get pointer to the underlying kjb_c::Vector C struct.

Warning
: This should only be used if you know what you're doing. generally, it should only be used to write wrapper functions for c functions.
Matrix kjb::Vector::hat ( ) const

Construct an "equivalent" skew-symmetric matrix from 3-vector.

The result is useful for computing a cross product. Only implemented for dimension = 3. The formula is as follows:

                     (  0 -c  b )
 Hat of (a b c)' :=  (  c  0 -a )
                     ( -b  a  0 ).
Vector::iterator kjb::Vector::insert ( iterator  position,
value_type  t 
)

A copy of t is inserted before position.

void kjb::Vector::insert ( iterator  position,
size_type  N,
value_type  t 
)

Insert N copies of t before position.

Value_type kjb::Vector::magnitude ( ) const
inline

Return this vector's magnitude.

Value_type kjb::Vector::magnitude_squared ( ) const
inline

Return this vector's squared magnitude.

Vector & kjb::Vector::mapcar ( Mapper  fun)

Transform the elements of a vector.

Apply a transformation fun to each element of the vector, in place, and return an lvalue of the modified vector.

Value_type kjb::Vector::max ( int *  max_index) const
inline

Find maximum element in the vector, both its value and index.

This will search for the maximum value in the vector, and return that value. Also this method can work like an argmax function, and emit the index of a location containing that value.

Parameters
[out]max_indexPointer to a location to store the index of the maximum value. If it equals null, then this method behaves the same as max().
Exceptions
Result_errorif the vector has size of zero
Returns
The value of the maximum

The vector might store the maximum value in more than one location. In that case, the value in *max_index will store one of the locations, but it is undefined which one.

size_type kjb::Vector::max_size ( ) const
inline

Maximum size vector can ever have. Currently defined as INT_MAX.

Value_type kjb::Vector::min ( int *  min_index) const
inline

Find minimum element in the vector, both its value and index.

This will search for the minimum value in the vector, and return that value. Also this method can work like an argmin function, and emit the index of a location containing that value.

Parameters
[out]min_indexPointer to a location to store the index of the minimum value. If it equals null, then this method behaves the same as min().
Exceptions
Result_errorif the vector has size of zero
Returns
The value of the minimum, or ERROR if size is zero.

The vector might store the minimum value in more than one location. In that case, the value in *min_index will store one of the locations, but it is undefined which one.

Vector& kjb::Vector::multiply ( Value_type  op2)
inline

Scalar multiply self, in-place, just like v *= 6.

Vector& kjb::Vector::negate ( )
inline

Negate self, in-place, just like v *= (-1).

Vector& kjb::Vector::normalize ( kjb_c::Norm_method  method = kjb_c::NORMALIZE_BY_MAGNITUDE)
inline

Normalize vector in place by choice of method (default by magnitude).

Vector kjb::Vector::normalized ( kjb_c::Norm_method  method = kjb_c::NORMALIZE_BY_MAGNITUDE) const
inline

non=mutating version of "normalize()"

Value_type& kjb::Vector::operator() ( int  i)
inline

Fortran-style vector subscript, e.g., A(10), to get an lvalue.

Warning
No bounds checking!
See Also
at() method, which provides bounds checking.
const Value_type& kjb::Vector::operator() ( int  i) const
inline

Fortran-style vector subscript, e.g., A(10), to get an rvalue.

Warning
No bounds checking!
See Also
at() method, which provides bounds checking.
Vector& kjb::Vector::operator*= ( Value_type  op2)
inline

Scalar multiply self, in-place, e.g., v *= 6.

Vector& kjb::Vector::operator+= ( const Vector op2)
inline

Add vector to self, in-place, e.g., v += delta_v.

Vector & kjb::Vector::operator+= ( double  x)

Add scalar to self, in-place, e.g., v -= delta_v.

Vector& kjb::Vector::operator-= ( const Vector op2)
inline

Subtract vector from self, in-place, e.g., v -= delta_v.

Vector & kjb::Vector::operator-= ( double  x)

Subtract scalar from self, in-place, e.g., v -= delta_v.

Vector& kjb::Vector::operator/= ( Value_type  op2)
inline

Scalar division of self, in-place.

Vector & kjb::Vector::operator= ( const Mat_view_type vec_ref)

Assignment operator: assign from a single-row matrix-view.

Vector& kjb::Vector::operator= ( const Impl_type vec_ref)
inline

Assignment operator: assign from a kjb_c::Vector, a C struct.

Vector& kjb::Vector::operator= ( const Vector src)
inline

Assignment operator: assign from a kjb::Vector, a C++ object.

Value_type& kjb::Vector::operator[] ( int  i)
inline

Subscript vector like a C array, e.g., A[10], returning an lvalue.

Warning
No bounds checking!
See Also
at() method, which provides bounds checking.
const Value_type& kjb::Vector::operator[] ( int  i) const
inline

Subscript vector like a C array, e.g., A[10], returning an rvalue.

Warning
No bounds checking!
See Also
at() method, which provides bounds checking.
Vector_view kjb::Vector::operator[] ( const Index_range idx)

Access a subvector using Matlab-like syntax. Returned object has Vector semantics, but operates on the elements of this vector indexed by the idx parameter.

Note
Accepted input types are int, Index_range.all, string, Int_vector, or vector<int>
Warning
Vector_view is fairly lightweight, but constructing Vector_view and Index_range adds overhead compared tointeger-based indexing. In addition, bounds checking occurs at EVERY index, so this shouldn't be used where performance is a concern.
Const_vector_view kjb::Vector::operator[] ( const Index_range idx) const
void kjb::Vector::pop_back ( )
inline

Returns the last element of the vector.

Precondition
!this->empty()
Warning
This does not release any allocated memory.
void kjb::Vector::push_back ( Value_type  x)
inline

inserts an element at the back of the vector in amortized constant time.

Vector& kjb::Vector::randomize ( int  length)
inline

Clobber current vector; resize and fill with random values.

Returns
an lvalue to this object
See Also
kjb_c::kjb_rand()

Random values are uniformly distributed between 0 and 1.

Vector& kjb::Vector::randomize ( )
inline

Clobber current vector contents with random values.

Returns
an lvalue to this object
See Also
kjb_c::kjb_rand()

Random values are uniformly distributed between 0 and 1.

reverse_iterator kjb::Vector::rbegin ( )
inline

Returns a read/write reverse iterator that points to the last element in the Vector. Iteration is done in reverse element order.

const_reverse_iterator kjb::Vector::rbegin ( ) const
inline

Returns a read-only (constant) reverse iterator that points to the last element in the Vector. Iteration is done in reverse element order.

void kjb::Vector::read ( const char *  filename = 0)
inline

Read vector from a file, or from standard input.

If filename equals NULL or filename[0] is the null character then the output is directed to standard output.

reverse_iterator kjb::Vector::rend ( )
inline

Returns a read/write reverse iterator that points to one before the first element in the Vector. Iteration is done in reverse element order.

const_reverse_iterator kjb::Vector::rend ( ) const
inline

Returns a read-only (constant) reverse iterator that points to one before the first element in the Vector. Iteration is done in reverse element order.

Vector & kjb::Vector::replace ( const Vector iv,
int  offset,
int  begin,
int  length 
)

Copy elements from input vector iv into this vector, starting at some offset, leaving the rest of this vector unchanged.

Parameters
ivThe input vector
offsetThe index of the first target location of this vector.
beginThe index of the first element to copy from iv.
lengthThe number of elements to be copied from iv
Returns
the lvalue for this vector
Exceptions
Illegal_argumentif the source or destination is too small to perform the specified copy, or if an argument is negative.

Just to be absolutely explicit, it works like so, provided all these locations below are valid.

| (*this)[ offset ] = iv[ begin ]; | : | : | (*this)[ offset + length - 1 ] = iv[ begin + length - 1 ];

void kjb::Vector::reserve ( int  capacity)
inline

After calling, the vector will be able to contain capacity elements without needing to resize itself.

Vector & kjb::Vector::resize ( int  new_length,
Value_type  pad = Value_type(0) 
)

Resize vector, retaining previous values.

Vector& kjb::Vector::set ( Value_type  val)
inline

Clone of zero_out(int)

Deprecated:
The name is misleading – do not use in new code.

Convert to a 1-vector and set its value to the argument.

Vector& kjb::Vector::set ( Value_type  val1,
Value_type  val2 
)
inline

Convert to a 2-vector and set its values to the arguments.

Vector& kjb::Vector::set ( Value_type  val1,
Value_type  val2,
Value_type  val3 
)
inline

Convert to a 3-vector and set its values to the arguments.

Vector& kjb::Vector::set ( Value_type  val1,
Value_type  val2,
Value_type  val3,
Value_type  val4 
)
inline

Convert to a 4-vector and set its values to the arguments.

size_type kjb::Vector::size ( ) const
inline

Alias to get_length(). Required to comply with stl Container concept.

Vector& kjb::Vector::subtract ( const Vector op2)
inline

Subtract vector from self, in-place, just like v -= delta_v.

Value_type kjb::Vector::sum_vector_elements ( ) const
inline

Return this vector's elements sum.

void kjb::Vector::swap ( Vector other)
inline

Swap the representations of two vectors.

void kjb::Vector::write ( const char *  filename = 0) const
inline

Write vector to a file, or to standard output.

If filename equals NULL or filename[0] is the null character then the output is directed to standard output.

Note
The write() method is required for the KjbReadableWritable concept.
See Also
KjbReadableWritable_concept
void kjb::Vector::write_col ( const char *  filename = 0) const
inline

Write vector as a column to a file, or to standard output.

See Also
kjb_c::write_col_vector_with_header()

If filename equals NULL or filename[0] is the null character then the output is directed to standard output.

void kjb::Vector::write_row ( const char *  filename = 0) const
inline

Write vector as a row to a file, or to standard output.

See Also
kjb_c::write_row_vector_full_precision()
Warning
This function doesn't handle "empty" vectors correctly. For this reason, write_col is currently preferred.

If filename equals NULL or filename[0] is a null character then the output is directed to standard output.

Vector& kjb::Vector::zero_out ( int  length)
inline

Clone of randomize(int)

Deprecated:
The name is misleading – do not use in new code.

Clobber current vector; resize and fill with zeroes.

Returns
an lvalue to this object
Vector& kjb::Vector::zero_out ( )
inline

Clobber current vector contents with zeroes.

Returns
an lvalue to this object

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