![]() |
Prev | Next |
# include <cppad/utility/vector.hpp>
cppad/vector.hpp
defines the
vector template class CppAD::vector
.
This is a SimpleVector
template class and in addition
it has the features listed below:
cppad/vector.hpp
is included by cppad/cppad.hpp
but it can also be included separately with out the rest of the
CppAD include files.
x
is a
CppAD::vector<Scalar>
,
and
cap
is a size_t
object,
cap = x.capacity()
set
cap
to the number of
Scalar
objects that
could fit in the memory currently allocated for
x
.
Note that
x.size() <= x.capacity()
x
and
y
are
CppAD::vector<Scalar>
objects,
y = x
has all the properties listed for a
simple vector assignment
plus the following:
CppAD::vector
template class will check that
the size of
x
is either zero or the size of
y
before doing the assignment.
If this is not the case, CppAD::vector
will use
ErrorHandler
to generate an appropriate error report.
Allowing for assignment to a vector with size zero makes the following
code work:
CppAD::vector<Scalar> y;
y = x;
y
is returned.
An example use of this reference is in multiple assignments of the form
z = y = x
&&
syntax, then it will be used during the vector assignment statement.
This means that return values and other temporaries are not be copied,
but rather pointers are transferred.
x
is a
CppAD::vector<Scalar>
object
and
i
has type size_t
,
x[i]
has all the properties listed for a
simple vector element access
plus the following:
The object
x[i]
has type
Scalar
(is not possibly a different type that can be converted to
Scalar
).
If
i
is not less than the size of the
x
,
CppAD::vector
will use
ErrorHandler
to generate an appropriate error report.
x
is a
CppAD::vector<Scalar>
object
with size equal to
n
and
s
has type
Scalar
,
x.push_back(s)
extends the vector
x
so that its new size is
n
plus one
and
x[n]
is equal to
s
(equal in the sense of the
Scalar
assignment operator).
x
is a
CppAD::vector<Scalar>
object
with size equal to
n
and
v
is a simple vector
with elements of type
Scalar
and size
m
,
x.push_vector(v)
extends the vector
x
so that its new size is
n+m
and
x[n + i]
is equal to
v[i]
for
i = 1 , ... , m-1
(equal in the sense of the
Scalar
assignment operator).
x
is a
CppAD::vector<Scalar>
object
and
os
is an std::ostream
,
and the operation
os << x
will output the vector
x
to the standard
output stream
os
.
The elements of
x
are enclosed at the beginning by a
{
character,
they are separated by ,
characters,
and they are enclosed at the end by }
character.
It is assumed by this operation that if
e
is an object with type
Scalar
,
os << e
will output the value
e
to the standard
output stream
os
.
x.resize(n)
set the size of
x
equal to
n
.
If
n <= x.capacity()
,
no memory is freed or allocated, the capacity of
x
does not change,
and the data in
x
is preserved.
If
n > x.capacity()
,
new memory is allocated and the data in
x
is lost
(not copied to the new memory location).
x
is a
CppAD::vector<Scalar>
object
x.data()
returns a pointer to a
Scalar
object such that for
0 <= i < x.size()
,
x[i]
and
x.data()[i]
are the same
Scalar
object.
If
x
is const
, the pointer is const
.
If
x.capacity()
is zero, the value of the pointer is not defined.
The pointer may no longer be valid after the following operations on
x
:
its destructor,
clear
,
resize
,
push_back
,
push_vector
,
assignment to another vector when original size of
x
is zero.
<cppad/vector.hpp>
also defines the class
CppAD::vectorBool
.
This has the same specifications as CppAD::vector<bool>
with the following exceptions:
vectorBool
conserves on memory
(on the other hand, CppAD::vector<bool>
is expected to be faster
than vectorBool
).
s = vectorBool::bit_per_unit()
returns the size_t
value
s
which is equal to the number of boolean values (bits) that are
packed into one operational unit.
For example, a logical or
acts on this many boolean values with one operation.
vectorBool
.
CppAD::vectorBool
output operator
prints each boolean value as
a 0
for false,
a 1
for true,
and does not print any other output; i.e.,
the vector is written a long sequence of zeros and ones with no
surrounding {
, }
and with no separating commas or spaces.
x
has type vectorBool
and
i
has type size_t
,
the element access value
x[i]
has an unspecified type,
referred to here as
elementType
, that supports the following
operations:
elementType
can be converted to bool
; e.g.
the following syntax is supported:
static_cast<bool>( x[i] )
elementType
supports the assignment operator =
where the
right hand side is a bool
or an
elementType
object; e.g.,
if
y
has type bool
, the following syntax is supported:
x[i] = y
elementType
also has type
elementType
.
Thus, if
z
has type bool
, the following syntax is supported:
z = x[i] = y
thread_alloc
)
to the current thread.
CppAD::vector<double> x(3);
size_t i;
for(i = 0; i < 3; i++)
x[i] = 4. - i;
std::cout << "x = " << x << std::endl;