// // Copyright 1997 by Craig Stuart Sapp, All Rights Reserved. // Programmer: Craig Stuart Sapp // Creation Date: Wed Feb 5 19:42:53 PST 1997 // Last Modified: Wed Apr 23 22:08:34 GMT-0800 1997 // Last Modified: Fri Sep 14 15:50:52 PDT 2001 (added last() function) // Filename: ...sig/maint/code/base/Collection/Collection.cpp // Web Address: http://sig.sapp.org/src/sigBase/Collection.cpp // Syntax: C++ // // Description: A dynamic array which can grow as necessary. // This class can hold any type of item, but the // derived Array class is specifically for collections // of numbers. // #ifndef _COLLECTION_CPP_INCLUDED #define _COLLECTION_CPP_INCLUDED #include "Collection.h" #include #include ////////////////////////////// // // Collection::Collection // template Collection::Collection(void) { allocSize = 0; size = 0; array = NULL; allowGrowthQ = 0; growthAmount = 8; maxSize = 0; } template Collection::Collection(int arraySize) { array = new type[arraySize]; size = arraySize; allocSize = arraySize; allowGrowthQ = 0; growthAmount = arraySize; maxSize = 0; } template Collection::Collection(int arraySize, type *aCollection) { size = arraySize; allocSize = arraySize; array = new type[size]; for (int i=0; i Collection::Collection(Collection& aCollection) { size = aCollection.size; allocSize = size; array = new type[size]; for (int i=0; i Collection::~Collection() { if (getAllocSize() != 0) { delete [] array; } } ////////////////////////////// // // Collection::allowGrowth // default value: status = 1 // template void Collection::allowGrowth(int status) { if (status == 0) { allowGrowthQ = 0; } else { allowGrowthQ = 1; } } ////////////////////////////// // // Collection::append // template void Collection::append(type& element) { if (size == getAllocSize()) { grow(); } array[size] = element; size++; } template void Collection::appendcopy(type element) { if (size == getAllocSize()) { grow(); } array[size] = element; size++; } template void Collection::append(type *element) { if (size == getAllocSize()) { grow(); } array[size] = *element; size++; } ////////////////////////////// // // Collection::grow // default parameter: growamt = -1 // template void Collection::grow(long growamt) { allocSize += growamt > 0 ? growamt : growthAmount; if (maxSize != 0 && getAllocSize() > maxSize) { std::cerr << "Error: Maximum size allowed for array exceeded." << std::endl; exit(1); } type *temp = new type[getAllocSize()]; for (int i=0; i type* Collection::pointer(void) { return array; } ////////////////////////////// // // Collection::getBase // template type* Collection::getBase(void) { return array; } ////////////////////////////// // // Collection::getAllocSize // template long Collection::getAllocSize(void) const { return allocSize; } ////////////////////////////// // // Collection::getSize -- // template long Collection::getSize(void) const { return size; } ////////////////////////////// // // Collection::last -- // template type& Collection::last(void) { return array[getSize()-1]; } ////////////////////////////// // // Collection::setAllocSize // template void Collection::setAllocSize(long aSize) { if (aSize < getSize()) { std::cerr << "Error: cannot set allocated size smaller than actual size." << std::endl; exit(1); } if (aSize <= getAllocSize()) { shrinkTo(aSize); } else { grow(aSize-getAllocSize()); size = aSize; } } ////////////////////////////// // // Collection::setGrowth // default parameter: growth = -1 // template void Collection::setGrowth(long growth) { if (growth > 0) { growthAmount = growth; } } ////////////////////////////// // // Collection::setSize // template void Collection::setSize(long newSize) { if (newSize <= getAllocSize()) { size = newSize; } else { grow(newSize-getAllocSize()); size = newSize; } } //////////////////////////////////////////////////////////////////////////////// // // Collection operators // ////////////////////////////// // // Collection::operator[] // template type& Collection::operator[](int elementIndex) { if (allowGrowthQ && elementIndex == size) { if (size == getAllocSize()) { grow(); } size++; } else if (elementIndex >= size) { std::cerr << "Error: accessing invalid array location " << elementIndex << " Maximum is " << size-1 << std::endl; exit(1); } return array[elementIndex]; } ////////////////////////////// // // Collection::operator[] const // template type Collection::operator[](int elementIndex) const { if (elementIndex >= size) { std::cerr << "Error: accessing invalid array location " << elementIndex << " Maximum is " << size-1 << std::endl; exit(1); } return array[elementIndex]; } ////////////////////////////// // // shrinkTo // template void Collection::shrinkTo(long aSize) { if (aSize < getSize()) { exit(1); } type *temp = new type[aSize]; for (int i=0; i allocSize) { size = allocSize; } } #endif /* _COLLECTION_CPP_INCLUDED */ // md5sum: 9929fee30b1bede4305e1fb46303ddc1 - Collection.cpp =css= 20030102