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

This class implements vectors, in the linear-algebra sense, restricted to integer-valued elements. More...

#include <l_int_vector.h>

Public Types

typedef int Value_type
 data type of the elements More...
 
typedef kjb_c::Int_vector Impl_type
 the underlying implementation More...
 
typedef Int_matrix Mat_type
 the associated matrix type More...
 
typedef Int_vector Vec_type
 the associated vector type More...
 
typedef Value_type(* Mapper )(Value_type)
 element transformer fun More...
 
typedef int value_type
 
typedef int * pointer
 
typedef const int * const_pointer
 
typedef int & reference
 
typedef int 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

 Int_vector (int length=0)
 Allocate vector of given length; contents are uninitialized. More...
 
 Int_vector (unsigned length)
 Allocate vector of given length; contents are uninitialized. More...
 
 Int_vector (unsigned long length)
 Allocate vector of given length; contents are uninitialized. More...
 
 Int_vector (int length, Value_type num)
 Allocate vector of given length; initialize all elts to 'num'. More...
 
 Int_vector (int length, const Value_type *data)
 Initialize from an array of int, of given length. More...
 
 Int_vector (Impl_type *vec_ptr)
 Conversion ctor: claim ownership of an existing int vector pointer (i.e., make a shallow copy). More...
 
 Int_vector (const std::vector< int > &src)
 Conversion ctor: Create from stl-style vector. More...
 
 Int_vector (const Impl_type &vec_ref)
 Ctor copies contents (i.e., deep copy) of an existing vector. More...
 
 Int_vector (const Mat_type &src)
 Ctor builds from a one-row or one-column Int_matrix (making a deep copy). More...
 
 Int_vector (const std::string &file_name)
 Construct a vector by reading contents from a named file. More...
 
 Int_vector (const Int_vector &vec_ref)
 Copy ctor – calls the kjb_c function to copy an int vector. More...
 
template<typename InputIterator_ >
 Int_vector (InputIterator_ begin_, InputIterator_ end_)
 "Range constructor" More...
 
 ~Int_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)
 
Int_vectorrandomize (int length)
 Clobber current vector; resize and fill with random values. More...
 
Int_vectorrandomize ()
 Clobber current vector contents with random values. More...
 
Int_vectoroperator= (const Impl_type &vec_ref)
 Assignment operator: assign from a kjb_c::Int_vector, a C struct. More...
 
Int_vectoroperator= (const Int_vector &src)
 Assignment operator: assign from a kjb::Int_vector, a C++ object. More...
 
const Impl_typeget_c_vector () const
 Get const pointer to the underlying kjb_c::Int_vector C struct. More...
 
Impl_typeget_underlying_representation_with_guilt ()
 Get pointer to the underlying kjb_c::Vector C struct. More...
 
void swap (Int_vector &other)
 Swap the representations of two vectors. More...
 
Int_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...
 
Value_type operator[] (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...
 
Value_type operator() (int i) const
 Fortran-style vector subscript, e.g., A(10), to get an rvalue. More...
 
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...
 
Value_type at (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...
 
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 write (const char *filename=0) const
 Write vector to a file, or to standard output. More...
 
Int_vectoroperator*= (Value_type op2)
 Scalar multiply self, in-place, e.g., v *= 6. More...
 
Int_vectormultiply (Value_type op2)
 Scalar multiply self, in-place, just like v *= 6. More...
 
Int_vectoroperator/= (Value_type op2)
 Scalar integer division of self, in-place. More...
 
Int_vectordivide (Value_type op2)
 Scalar integer division of self, in-place, just like v /= 2. More...
 
Int_vectoroperator+= (const Int_vector &op2)
 Add vector to self, in-place, e.g., v += delta_v. More...
 
Int_vectoradd (const Int_vector &op2)
 Add vector to self, in-place, just like v += delta_v. More...
 
Int_vectoroperator-= (const Int_vector &op2)
 Subtract vector from self, in-place, e.g., v -= delta_v. More...
 
Int_vectorsubtract (const Int_vector &op2)
 Subtract vector from self, in-place, just like v -= delta_v. More...
 
Int_vectornegate ()
 Negate self, in-place, just like v *= (-1). More...
 
Vec_typemapcar (Mapper)
 Transform the elements of a vector. More...
 
Value_type min () const
 Return the value of the minimum element in the vector. More...
 
Value_type min (int *min_index) const
 Find minimum element in the vector, both its value and index. More...
 
Value_type max () const
 Return the value of the maximum element in the vector. More...
 
Value_type max (int *max_index) const
 Find maximum element in the vector, both its value and index. More...
 
Int_vectorcross_with (const Int_vector &op2)
 Compute (in place) cross product of this vector and op2. More...
 
kjb::Int_matrix hat () const
 Construct an "equivalent" skew-symmetric matrix from 3-vector. More...
 
double magnitude () const
 Return this vector's magnitude. More...
 
long int magnitude_squared () const
 Return this vector's squared magnitude. More...
 

Detailed Description

This class implements vectors, in the linear-algebra sense, restricted to integer-valued elements.

For better maintainability, refer to the element type using this class's Value_type typedef, instead of referring to 'int' 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 int* kjb::Int_vector::const_pointer

const Iterator type

typedef kjb_c::Int_vector kjb::Int_vector::Impl_type

the underlying implementation

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

element transformer fun

the associated matrix type

typedef std::reverse_iterator<iterator> kjb::Int_vector::reverse_iterator

data type of the elements

the associated vector type

Constructor & Destructor Documentation

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

Allocate vector of given length; contents are uninitialized.

This also serves as a default ctor

kjb::Int_vector::Int_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::Int_vector::Int_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::Int_vector::Int_vector ( int  length,
Value_type  num 
)
inline

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

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

Initialize from an array of int, of given length.

kjb::Int_vector::Int_vector ( Impl_type vec_ptr)
inline

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

This method is the proper way to say, ''Here is a kjb_c::Int_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_int_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 Int_vector objects from the same source this way or you'll get a double deletion bug.
kjb::Int_vector::Int_vector ( const std::vector< int > &  src)
inline

Conversion ctor: Create from stl-style vector.

kjb::Int_vector::Int_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::Int_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::Int_vector::Int_vector ( const Int_vector vec_ref)
inline

Copy ctor – calls the kjb_c function to copy an int 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::Int_vector::Int_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::Int_vector::~Int_vector ( )
inline

Destructor – which just calls the KJB destructor.

Member Function Documentation

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

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

Value_type& kjb::Int_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.

Value_type kjb::Int_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::Int_vector::back ( )
inline

Returns the last element of the vector.

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

Returns the last element of the vector.

Precondition
!this->empty()
iterator kjb::Int_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::Int_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::Int_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::Int_vector::clear ( )
inline

delete all elements from vector

Warning
Calling this will never free allocated memory.
Int_vector& kjb::Int_vector::divide ( Value_type  op2)
inline

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

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

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

iterator kjb::Int_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::Int_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.

iterator kjb::Int_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::Int_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.
reference kjb::Int_vector::front ( )
inline

Returns the first element of the vector.

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

Returns the first element of the vector.

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

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

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

Return the length of the vector.

Impl_type* kjb::Int_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.
iterator kjb::Int_vector::insert ( iterator  position,
value_type  t 
)

A copy of t is inserted before position.

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

Insert N copies of t before position.

double kjb::Int_vector::magnitude ( ) const
inline

Return this vector's magnitude.

long int kjb::Int_vector::magnitude_squared ( ) const
inline

Return this vector's squared magnitude.

Value_type kjb::Int_vector::max ( ) const
inline

Return the value of the maximum element in the vector.

Value_type kjb::Int_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::Int_vector::max_size ( ) const
inline

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

Value_type kjb::Int_vector::min ( ) const
inline

Return the value of the minimum element in the vector.

Value_type kjb::Int_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.

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

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

Int_vector& kjb::Int_vector::negate ( )
inline

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

Value_type& kjb::Int_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.
Value_type kjb::Int_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.
Int_vector& kjb::Int_vector::operator*= ( Value_type  op2)
inline

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

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

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

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

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

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

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

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

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

Value_type& kjb::Int_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.
Value_type kjb::Int_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.
void kjb::Int_vector::pop_back ( )
inline

Returns the last element of the vector.

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

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

Int_vector& kjb::Int_vector::randomize ( )
inline

Clobber current vector contents with random values.

Returns
an lvalue to this object
See Also
rand(3)

Random values are uniformly distributed between 0 and RAND_MAX.

reverse_iterator kjb::Int_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::Int_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.

reverse_iterator kjb::Int_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::Int_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.

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

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

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

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

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

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

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

Swap the representations of two vectors.

void kjb::Int_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::Int_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_int_vector_with_header()

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

void kjb::Int_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_int_vector_full_precision()

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


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