1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
\chapter{{\tt IV}: Integer Vector Object}
\par
The {\tt IV} object is a wrapper around an {\tt int} vector,
thus the acronym {\bf I}nteger {\bf V}ector.
The driving force for its creation is more convenience than
performance.
There are several reasons that led to its development.
\begin{itemize}
\item
Often a method will create a vector (allocate storage for and fill
the entries) whose size is not known before the method call.
Instead of having a pointer to {\tt int} and a pointer to {\tt int*}
in the calling sequence, we can return a pointer to an {\tt IV}
object that contains the newly created vector and its size.
\item
In many cases we need a persistent {\tt int} vector object,
and file IO is simplified if we have an object to deal with.
The filename is of the form {\tt *.ivf} for a formatted file
or {\tt *.ivb} for a binary file.
Another case is where an object contains one or more {\tt int}
vectors.
When they are held as {\tt IV} objects, (e.g., the {\tt ETree}
object contains a {\tt Tree} object and three {\tt IV} objects),
the method to read and write the object is much cleaner.
\item
Prototyping can go much faster with this object as opposed to
working with an {\tt int} array.
Consider the case when one wants to accumulate a list of integers,
but one doesn't know how large the list will be.
The method {\tt IV\_setSize()} can be used to set
the size of the vector to zero.
Another method {\tt IV\_push()} appends an element to the vector,
growing the storage if necessary.
\item
Having the size of a vector and a pointer to the base location
wrapped together makes it easier to check for valid input inside
a method.
\item
Sometimes an object needs to change its size, i.e., vectors need to
grow or shrink.
It is easier and more robust to tell an {\tt IV} object to resize
itself (see the {\tt IV\_setSize()} and {\tt IV\_setMaxsize()}
methods) than it is to duplicate code to work on an {\tt int} vector.
\end{itemize}
One must choose where to use this object.
There is a substantial performance penalty for doing the simplest
operations, and so when we need to manipulate an {\tt int} vector
inside a loop, we extract out the size and pointer to the base
array from the {\tt IV} object.
On the other hand, the convenience makes it a widely used object.
Originally its use was restricted to reading and writing {\tt
*.iv\{f,b\}} files, but now {\tt IV} objects appear much more
frequently in new development.
\par
|