// // Programmer: Craig Stuart Sapp // Creation Date: Thu Oct 9 10:53:57 GMT-0800 1997 // Last Modified: Thu Oct 9 10:54:01 GMT-0800 1997 // Filename: ...sig/code/base/History/History.cpp // Syntax: C++ // $Smake: cc -Wall -g -c %b.cpp -I../../../include && rm -f %b.o // #ifndef _HISTORY_CC_INCLUDED #define _HISTORY_CC_INCLUDED #include "History.h" #include #ifndef OLDCPP #include using namespace std; #else #include #endif ////////////////////////////// // // History::History // template History::History(int aSize) { if (aSize <= 0) { cerr << "Error: cannot have a non-positive history size: " << aSize << endl; exit(1); } storageSize = aSize; storage = new type[storageSize]; writeIndex = 0; } ////////////////////////////// // // History::~History // template History::~History() { if (storage != NULL) { delete [] storage; } } ////////////////////////////// // // History::fillArray // default value: youngestIndex = 0; // template void History::fillArray(type* anArray, long numElements, long youngestIndex) const { if (youngestIndex > 0) youngestIndex = -youngestIndex; for (int i=0; i long History::getSize(void) const { return storageSize; } ////////////////////////////// // // History::initialize // template void History::initialize(type aValue) { for (int i=0; i void History::insert(type anElement) { writeIndex++; if (writeIndex >= getSize()) { // should never be greater than getSize() writeIndex = 0; } storage[writeIndex] = anElement; } ////////////////////////////// // // History::operator[] // template type& History::operator[](long anIndex) { static long index; if (anIndex > 0) anIndex = -anIndex; // anIndex always negative index = writeIndex + anIndex; if (index < 0) index += getSize(); // index in range [0..getSize()-1] return storage[index]; } ////////////////////////////// // // History::setSize // template void History::setSize(long aSize) { if (aSize <= 0) { cerr << "Error: cannot have a non-positive history size: " << aSize << endl; exit(1); } delete [] storage; storage = new type[aSize]; storageSize = aSize; writeIndex = 0; } ////////////////////////////// // // History::zero // template void History::zero(void) { initialize(0); } /////////////////////////////////////////////////////////////////////////// // // Private functions // ////////////////////////////// // // History::history -- like operator[] // template type History::history(long anIndex) const { static long index; if (anIndex > 0) anIndex = -anIndex; // anIndex always negative index = writeIndex + anIndex; if (index < 0) index += getSize(); // index in range [0..getSize()-1] return storage[index]; } #endif /* _HISTORY_CC_INCLUDED */ // md5sum: 497eb64f705557677c76d3c3c8501e0d History.cpp [20050403]