BLOCK(3+) BLOCK(3+) NAME block - adjustable arrays SYNOPSIS #include <Block.h> Blockdeclare(T) Blockimplement(T) struct Block(T) { Block(T)(unsigned = 0); Block(T)(const Block(T&); ~Block(T); Block(T)& operator= (const Block(T)&); T& operator[] (int); operator T* (); unsigned size(); unsigned size(unsigned); T* end(); void swap(const Block(T)&); } DESCRIPTION A Block(T) is an array of zero or more elements of type T, where T is a type name. T must have assignment (operator=) and initialization (T(T&)) operations. The macro call Blockdeclare(T) declares the class Block(T). It must appear once in every source file that uses type Block(T). The macro call Blockimplement(T) defines the functions that implement the block class. It must appear exactly once in the entire program. New elements are initialized to the value of an otherwise uninitialized static object of type T. Constructors Block(T) An empty block. Block(T)(n) A block of n elements. Block(T)(b) A new block whose elements are copies of the ele- ments of b. Operations Assignment copies elements and size. b[k] A reference to element `k' of block b; undefined BLOCK(3+) BLOCK(3+) if k is out of bounds. (T*)b A pointer to the first element of block b. Other functions b.size() Return the number of elements in b. b.size(n) Set the size of b to `n'. If the new size is greater than the old. Otherwise, n old elements are kept. Return the new size. b.reserve(n) The size of b is increased, if necessary, to some value greater than n. If b already has room, b is not changed. Return zero if memory could not be allocated and non-zero otherwise. b.end() Returns a pointer to just past the last element in `b'. Equivalent to (T*)b+b.size(). a.swap(b) The memory associated with blocks a and b is exchanged. Performance Most operations are implemented by the obvious uses of the `new' and `delete' operators. Reserve checks the size inline. If it isn't big enough, the size is increased by multiplying by 3/2 (and adding one) enough times to increase it beyond n . EXAMPLES Blockdeclare(long) unsigned n = 0; Block(long) b; long x; while (cin >> x) { b.reserve(n); b[n++] = x; } SEE ALSO malloc(3), map(3) DIAGNOSTICS The only error detected is running out of memory; this is indicated in all cases by setting the size of the block for which allocation failed to zero. BUGS Elements are copied during reallocation by using `T::operator=' instead of `T(T&)'. Because the `type parameter' T is implemented by the C BLOCK(3+) BLOCK(3+) preprocessor, white space is forbidden inside the parenthe- ses of Block(T).