Diffpack Documentation


Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

VecSimple(Type) Class Reference

simple vector, base=1, no arithmetic operations. More...

#include <VecSimple_Type.h>

Inheritance diagram for VecSimple(Type)::

VecSimplest_Type ArrayGenSimple_Type VecSimpleH_Type VecSort_Type ArrayGen_Type Vec_Type ArrayGenSel_Type ArrayGen_Type ArrayGenSel_Type List of all members.

Public Methods

 VecSimple(Type) ()
 VecSimple(Type) (int length)
 VecSimple(Type) (Type *a, int n, int base=0)
 VecSimple(Type) (const VecSimple(Type) &X)
 ~VecSimple(Type) ()
void fill (const Type &value)
VecSimple(Type)& operator= (const Type &value)
VecSimple(Type)& operator= (const VecSimple(Type) &X)
VecSimple(Type)& append (const VecSimple(Type) &X)
bool grow (int length)
void print (Os os, const char *header=NULL, int nentries_per_line=3) const
void scan (Is is)
void printAsIndex (Os os) const
void printAscii (Os os, const char *header=NULL, int nentries_per_line=3) const
void scanFromFile (const String &filename)

Friends

Osoperator<< (Os &os, const VecSimple(Type) &x)
Isoperator>> (Is &is, VecSimple(Type) &x)

Detailed Description

simple vector, base=1, no arithmetic operations.

NAME: VecSimple(Type) - simple vector, base=1, no arithmetic operations

DESCRIPTION:

The class implements a standard vector type derived from "VecSimplest(Type)". The public interface consists of various operations such as subscripting (w/array-range-check if desired), assignment operators, fill functions, print and scan functions, etc. The subscript operator assumes that the base equals 1. If other bases are desired, the "ArrayGenSimplest(Type)" or "ArrayGen(Type)" classes can be used.

No arithmetic operations are used in any of the member functions. Hence this class is suited as a general vector class for various objects. The only requirement is that the object has a constructor with no arguments so that an array of the class'' objects can be allocated by "new". In addition, the class must define "operator=", "operator<<" and "operator>>". Macros for generating default versions of these operators are collected in the file "default_op.h".


Constructor & Destructor Documentation

VecSimple(Type)::VecSimple_Type ( )
 

Several constructors are available. The constructor without parameters allocates no memory. To allocate the proper amount of memory at a later stage, the "redim" function can be used. Another constructor takes the number of entries in the vector as parameter and allocates memory for the corresponding vector representation. Finally, a copy-constructor is available.

To initialize the vector entries one can use the member functions "fill" or "operator()".

VecSimple(Type)::VecSimple_Type ( int length )
 

See documentation of one of the overloaded constructor.

VecSimple(Type)::VecSimple_Type ( Type * a,
int n,
int base = 0 )
 

See documentation of one of the overloaded constructor.

VecSimple(Type)::VecSimple_Type ( const VecSimple(Type) & X )
 

See documentation of one of the overloaded constructor.

VecSimple(Type)::~VecSimple(Type) ( )
 


Member Function Documentation

VecSimple(Type) & VecSimple(Type)::append ( const VecSimple(Type) & X )
 

appends the vector "X" to the current vector.

void VecSimple(Type)::fill ( const Type & a )
 

Reimplemented in ArrayGen(Type), ArrayGenSel(Type), ArrayGenSimple(Type), and Vec(Type).

bool VecSimple(Type)::grow ( int length )
 

as "VecSimplest redim", but the contents of the existing vector are preserved (like the "realloc" function in C). The supplied length can also be smaller than the original length, providing a ""shrink"" functionality.

VecSimple(Type) & VecSimple(Type)::operator= ( const VecSimple(Type) & X )
 

See documentation of one of the overloaded functions.

VecSimple(Type) & VecSimple(Type)::operator= ( const Type & a ) [inline]
 

the version that takes another "VecSimple(Type)" as argument makes the object a copy of the vector argument, that is, the object is redimensioned according to the dimensions of "X" and thereafter each entry is set equal to the corresponding entry in "X". The version that takes a "Type" value as argument sets all the entries in the vector to have this value.

Reimplemented in ArrayGen(Type), ArrayGenSel(Type), Vec(Type), and VecSort(Type).

void VecSimple(Type)::print ( Os os,
const char * header = NULL,
int nentries_per_line = 3 ) const
 

prints the contents of the vector. See documentation of the function "scan".

Reimplemented in ArrayGen(Type), ArrayGenSel(Type), ArrayGenSimple(Type), and Vec(Type).

void VecSimple(Type)::printAsIndex ( Os os ) const
 

prints the vector as an index, that is on the form

      (x1,x2,x3,x4,x5,...)

void VecSimple(Type)::printAscii ( Os os,
const char * header = NULL,
int nentries_per_line = 3 ) const
 

sets the format to ASCII and calls "print".

Reimplemented in ArrayGen(Type), and ArrayGenSimple(Type).

void VecSimple(Type)::scan ( Is is )
 

reads the vector from an "Is" object. The entries of an array can be written and read in two main formats, the binary format or the ascii format. When "print" is invoked, the format is determined by the state of the "Os" object (the result of the "getFormat" function). Each of the ascii/binary formats can have a header or not. The header is present if "print" is called with a non-"NULL" "header" string. In "scan" one detects the header if the first non-space character is "[". If the header is present, its syntax is like this:

      [size]X text $
where "size" is the size of the array (for a vector it is the length, for a matrix it is a string on the form "rxc", where "r" is the number of rows and "c" is the number of columns), "X" is a character that equals "b" in case of a binary format or space in case of an ascii format. "text" is the "header" string given to "print". In "scan" this string is ignored, but it is available in the internal "Is" buffer so the programmer can in principle extract the header after "scan" is called.

After the header a character "@" follows if the format is binary, if not, a space appears. Hence one can detect whether the format is binary or ascii even when the header does not appear. After the "@", the binary format has two binary numbers: The number of array entries and the size of each entry.

If the header is present and the format is ascii, there will be "nentires_per_line" array entries on each output line. There array index will also be written. In all other cases, the array entries are written with no index, but in the ascii format there will be an extra space.

With a header the "scan" functions can read the dimensions of the array and call "redim" before reading the entries. Without a header the dimensions of the array must be correct BEFORE "scan" is called. In other words, a file containing the array entries only can only be read when the number of entries is known. The function "scanFromFile" can, however, read an unknown number of entries from file into a vector with wrong dimensions (see documentation below). If the format is binary, the "scan" function detects the array length and redimensions the array, but a warning is issued. The reason is that, without a header, the main rule is that the programmer must have redimensioned the array on beforehand.

After the header the data appears. If they are written in binary mode, the first non-white character (after the header) is "@". Then, in binary format, the number of items must appear and after that the size of each item. Then the data appear in binary format. If the "@" character is not present, the data appear in ascii format.

Let us present some examples on valid data files. First the most comprehensive form:

      [3]  Example of a data file with much additional information such as
      indices for each vector entry. This file is in ascii format.
  
      (1)=1.2
      (2)=3.4
      (3)=1.8

Here is an ascii format file with as little information as possible (a NULL header was given when calling "print":

      1.2 3.4 1.8
An equivalent form may be

      1.2 3.4
      1.8

Finally we show a binary file without header. To indicate a binary number (f.ex. 8.1) we use the notation [B8.1].

      @[B3][B4][B1.2][B3.4][B1.8]
Observe that the item is a float and that the size of the item is "sizeof(float)" which equals 4. There is a comprehensive demonstration and test program for the various ascii and binary file formats of arrays in the directory "$DPR/src/app/class-verify/category2/arrayprint".

Reimplemented in ArrayGen(Type), ArrayGenSel(Type), ArrayGenSimple(Type), and Vec(Type).

void VecSimple(Type)::scanFromFile ( const String & filename )
 

enables an unknown numbers of "Type" objects to be read from file into a possibly empty vector. The objects must appear on the file without any text. Hence, this function cannot read a file written by "print" with a comment. First, the number of objects on the file is found, then the vector is correctly redimensioned and then an ordinary "scan" function is called. Only ascii files can be read by the present implementation (that is also the most useful application of a function like this).


Friends And Related Function Documentation

Os & operator<< ( Os & os,
const VecSimple(Type) & x ) [friend]
 

is defined in terms of the "print" function.

Is & operator>> ( Is & is,
VecSimple(Type) & x ) [friend]
 

is defined in terms of the "scan" function.


The documentation for this class was generated from the following file:
Copyright © 2003 inuTech GmbH. All rights reserved.