// // Programmer: Craig Stuart Sapp // Creation Date: Wed Feb 5 19:42:53 PST 1997 // Last Modified: Tue Feb 11 23:19:31 PST 1997 // Last Modified: Fri May 28 15:15:47 PDT 1999 (zero() off-by one fixed) // Last Modified: Sun Jun 11 13:13:49 PDT 2000 (added setSize() function) // Filename: ...sig/maint/code/base/Block/Block.cpp // Web Address: http://sig.sapp.org/include/sigBase/Block.cpp // Documentation: http://sig.sapp.org/doc/classes/Block // Syntax: C++ // #ifndef _BLOCK_CPP_INCLUDED #define _BLOCK_CPP_INCLUDED #include "Block.h" #include #ifndef OLDCPP #include using namespace std; #else #include #endif ////////////////////////////// // // Block::Block -- // template Block::Block(void) { size = 0; array = NULL; } template Block::Block(int blockSize) { array = new type[blockSize]; size = blockSize; } template Block::Block(Block& aBlock) { size = aBlock.size; array = new type[size]; memcpy(array, aBlock.array, sizeof(type)*size); } template Block::Block(const type* anArray, int arraySize) { size = arraySize; array = new type[size]; memcpy(array, anArray, sizeof(type)*size); } ////////////////////////////// // // Block::~Block -- // template Block::~Block() { if (size != 0) { delete [] array; } } ////////////////////////////// // // Block::getBase -- // template type* Block::getBase(void) { return array; } ////////////////////////////// // // Block::getSize -- // template int Block::getSize(void) const { return size; } ////////////////////////////// // // Block::zero -- // template void Block::zero(int minIndex, int maxIndex) { if (minIndex == -1) minIndex = 0; if (maxIndex == -1) maxIndex = getSize()-1; if (minIndex < 0 || maxIndex < 0 || minIndex > maxIndex || maxIndex >= getSize()) { cerr << "Error in Zero function: min = " << minIndex << " max = " << maxIndex << " size = " << getSize() << endl; exit(1); } for (int i=minIndex; i<=maxIndex; i++) { array[i] = 0; } } ////////////////////////////// // // Block::operator[] -- // template type& Block::operator[](int elementIndex) { if (elementIndex >= getSize()) { cerr << "Error: accessing invalid array location " << elementIndex << " Maximum is " << getSize()-1 << endl; exit(1); } return array[elementIndex]; } template type Block::operator[](int elementIndex) const { if (elementIndex >= getSize()) { cerr << "Error: accessing invalid array location " << elementIndex << " Maximum is " << getSize()-1 << endl; exit(1); } return array[elementIndex]; } ////////////////////////////// // // Block::operator= -- // template Block& Block::operator=(const Block& aBlock) { if (size != aBlock.size) { if (size != 0) { delete [] array; } size = aBlock.size; array = new type[size]; } memcpy(array, aBlock.array, sizeof(type)*size); return *this; } ////////////////////////////// // // Block::operator+= -- // template Block& Block::operator+=(const Block& aBlock) { if (size != aBlock.size) { cerr << "Error: different size blocks " << size << " and " << aBlock.size << endl; exit(1); } for (int i=0; i Block Block::operator+(const Block& aBlock) const { if (size != aBlock.size) { cerr << "Error: different size blocks " << size << " and " << aBlock.size << endl; exit(1); } Block bBlock(*this); bBlock += aBlock; return bBlock; } template Block Block::operator+(type aNumber) const { Block aBlock(*this); for (int i=0; i Block& Block::operator-=(const Block& aBlock) { if (size != aBlock.size) { cerr << "Error: different size blocks " << size << " and " << aBlock.size << endl; exit(1); } for (int i=0; i Block Block::operator-(const Block& aBlock) const { if (size != aBlock.size) { cerr << "Error: different size blocks " << size << " and " << aBlock.size << endl; exit(1); } Block bBlock(*this); bBlock -= aBlock; return bBlock; } // unary operator template Block Block::operator-(void) const { Block aBlock(*this); for (int i=0; i Block Block::operator-(type aNumber) const { Block aBlock(*this); for (int i=0; i Block& Block::operator*=(const Block& aBlock) { if (size != aBlock.size) { cerr << "Error: different size blocks " << size << " and " << aBlock.size << endl; exit(1); } for (int i=0; i Block Block::operator*(const Block& aBlock) const { if (size != aBlock.size) { cerr << "Error: different size blocks " << size << " and " << aBlock.size << endl; exit(1); } Block bBlock(*this); bBlock *= aBlock; return bBlock; } template Block Block::operator*(type aNumber) const { Block aBlock(*this); for (int i=0; i Block& Block::operator/=(const Block& aBlock) { if (size != aBlock.size) { cerr << "Error: different size blocks " << size << " and " << aBlock.size << endl; exit(1); } for (int i=0; i Block Block::operator/(const Block& aBlock) const { if (size != aBlock.size) { cerr << "Error: different size blocks " << size << " and " << aBlock.size << endl; exit(1); } Block bBlock(*this); bBlock /= aBlock; return bBlock; } ////////////////////////////// // // Block::setSize -- // template void Block::setSize(int aSize) { if (aSize < 0) { cout << "Error: Cannot have a negative block size." << endl; } if (array != NULL) { delete [] array; } size = aSize; if (size == 0) { array = NULL; return; } array = new type[aSize]; } #endif /* _BLOCK_CPP_INCLUDED */ // md5sum: b6f2feb0b36abac0a9591e6db79838ba Block.cpp [20050403]