Next: Stack
Up: Utility objects
Previous: NameList
  Contents
Subsections
ResizeArray
Files: |
ResizeArray.h, ResizeArray.c |
Derived from: |
none |
Global instance (if any): |
none |
Used in optional component: |
Part of main VMD code |
A template class which implements a dynamically-growing, automatically
resizing array of data of a given type. Elements in the array may be
accessed via the [] operator. When new data is added to the end of an
array, the size of the array is automatically increased if necessary.
A ResizeArray has two sizes that are important:
- The internal size, which is currently allocated size of the
array. This is almost always larger than the total number of items
stored in the array. A larger-than-necessary buffer is kept so that
data may be easily put at the end of the array without having to always
reallocate and copy the existing array storage.
- The external size, which is the largest index in the array
which has been accessed by the user (either by reading from that index,
or writing a value at that index. This is what the user would consider
the `size' of the array.
- ResizeArray::ResizeArray(int = 10, float = 2.0)
The first argument is the initial internal size of the array, i.e. the
initial number of elements for which to allocate memory (although the
initial external size of the array will be zero). The second argument
is the resizing factor: when an element of the array beyond the
size the internal array is accessed, the array will be made larger by
multiplying the current internal size by the rescaling factor. This
must be larger than 1.
- T *data - the current array of data, which will be made
larger as the array grows.
- int sz - the internal size of the array.
- float resizeFactor - factor by which the number of elements
in data is increased when the array grows too large. sz will be
made larger by sz *= resizeFactor.
- int currSize - the external size of the array, that is, the
largest index into data which has been accessed. This is always
less than or equal to sz.
- int num(void) - return the external size of the array.
- T& item(int N) - return (as an lvalue) the Nth item in the
array. If N is larger than the internal size of the array, the size of
the array will be automatically increased by the current resize factor.
- T& operator[](int N) - same as item, just a nicer
form (this really makes it look like a regular array. In fact, you can
have 2D and 3D resizing arrays by using this operator, and declaring
a template of a template).
- int append(const T&) - append the given data at the end of
the array, resizing it if necessary. Returns the index of the item.
- int insert_after(int, const T&) - inserts the given data in
the location following the given index. If the given index is less than
0, this puts the item at the beginning of the array. If the given index
is larger than the external size, this puts the item at the end of the
array. Returns the index of the item.
- int insert_before(int, const T&) - inserts the given data in
the location just before the given index. If the given index is less than
0, this puts the item at the beginning of the array. If the given index
is larger than the external size, this puts the item at the end of the
array. Returns the index of the item.
- void remove(int M= -1, int N= -1) - removes the items with
index M ... N, with all items in array positions below this region moved
up to fill the resulting hole. If both arguments are negative, this
removes ALL the array items. If the second argument is negative, only
item M is removed.
- int find(const T&) - scans the array for an element which
matches the given argument, and returns the index of the first matching
item found. If no match is found, this returns -1.
A ResizeArray is created by specifying the type of data to store, i.e.
ResizeArray<char *> stringArray;
Once created, it looks just like an array, and you can use the [] operator
to get and set data. For example:
stringArray[3] = "Some text.";
or
cout « stringArray[3] « endl;
Next: Stack
Up: Utility objects
Previous: NameList
  Contents
vmd@ks.uiuc.edu