SimpleVector

Code Last Updated: 05/04/98

SimpleVector is a very simple C++ template class for implementing one-dimensional vectors of small objects. It is similar in functionality to the STL vector class, which is no longer supplied with CodeWarrior. It even shares some of the same method names. But unlike the <vector.h> shipped with CW11, this one does not include ten gazillion ANSI files, and does not require extreme gyrations on the part of the programmer. You just use it.

Important Update: a potentially dangerous (memory-leaking) bug in deleteIdx() was fixed on 5/04/98. Update your projects immediately!


Here's a sample code snippet that illustrates some uses. The methods are defined below.

	SimpleVector vec;		// create an empty vector
	vec.mBlockItems = 5;		// allocate in blocks of 5 items
	vec.push_back('a');		// add some items
	vec.push_back('b');
	mychar = vec[0];		// get some items
	mychar = pop_back();		// get last item and remove it
	// iterate over the list
	for (short i=0; i<vec.size(); i++) {
		DoSomethingWith( vec[i] );
	}
	mychar.deleteAll();		// empty the list


Construction

	SimpleVector myVector;
	SimpleVector myPresizedVector(20);
The first format above creates a vector, whose elements are of type "myType", without actually allocating any memory. The second form preallocates enough room for 20 elements.

As elements are added, a buffer is created in blocks of mBlockItems, a member of each vector which currently defaults to 16. You can change how big the block size is by, e.g., myVector.mBlockItems = 20 (allocate room for 20 items at a time).

Inspection

Use the size() method to return the number of elements in the vector. If you're curious about the buffer itself (i.e., how much room is currently allocated), use bufitems() and bufbytes() to get the buffer size in elements or bytes, respectively.

Use as an Array

You can use the square-bracket notation, just like an array. If you refer to an array element beyond its size(), then the size will be increased to just include your element (though the buffer will increase in even increments of mBlockSize).

Note that you should never keep the address of an array element, since it might get moved around when you add or delete other items. (For the same reason, don't build a SimpleVector of certain reference- counted objects which rely on staying put.)

Use as a Stack

To add an item at the end of the array, use the push_back(item) method. pop_back returns the last item from the stack, and decrements the array size. Note that the latter method currently does not reallocate the buffer; if you do a lot of pop_backs, you may want to manually resize(). Or, switch to one of the methods below.

Other ways to Delete Elements

vec.deleteIdx(num) deletes the element that would be returned by vec[num]. Elements beyond num are shifted down towards 0. When the empty space in the buffer is greater than twice the block size, the buffer shrinks to fit.

vec.deleteAll() deletes all elements in vec, and frees the buffer completely.

Manual Buffer Management

The resize(n) method reallocates a buffer big enough to hold n elements; previous data is copied into the new buffer. Note that this does not check to make sure that size() is less than n -- misusing this function could be dangerous.


Please send questions or comments to joe@strout.net.
http://www.strout.net/info/coding/macdev/simplevector/index.html
Last updated: 5/04/98 . . . . . Joe Strout