CCollection.C
//-----------------------------------------------------------------------------
// $Header: /tmp_mnt/asis/offline/ceres/cool/project/RCS/CCollection.C,v 2.1 1996/10/04 08:43:26 voigt Exp $
//
// COOL Program Library
// Copyright (C) CERES collaboration, 1996
//
// Template definitions for CCollection<T> class.
//
//-----------------------------------------------------------------------------
#include <iostream.h>
#include <stdlib.h>
#ifndef RW_COMPILE_INSTANTIATE
#include "CCollection.h"
#endif
template<class T> CCollection<T>::CCollection(const CCollection<T>& old)
{
plist = old.plist; // shallow copy
minX = old.minX;
minY = old.minY;
maxX = old.maxX;
maxY = old.maxY;
int newx = int(maxX-minX+1);
int newy = int(maxY-minY+1);
parray = new T* [newx*newy];
if (parray == 0) {
cerr << "CCollection<T>::CCollection(const CCollection<T>&) \n";
cerr << "\tFATAL:\n";
cerr << "\tCannot allocate memory for 2-d array. Run out of free store.\n";
cerr << "\tThis is, so far, prohibiting the further flow of the program.\n";
cerr << "\tABORTING. Sorry for that. \n";
abort();
}
memcpy( (void *) parray, (const void *) old.parray, (size_t) (newx*newy)*sizeof(T*) );
}
template<class T> CCollection<T> & CCollection<T>::operator = (const CCollection<T>& old)
{
if (this == &old) // same class object
return *this;
else {
//
// Clear old stuff (do not destroy!)
//
plist.clear();
delete [] parray;
plist = old.plist; // shallow copy
minX = old.minX;
minY = old.minY;
maxX = old.maxX;
maxY = old.maxY;
int newx = int(maxX-minX+1);
int newy = int(maxY-minY+1);
parray = new T* [newx*newy];
if (parray == 0) {
cerr << "CCollection<T>::::operator =()\n";
cerr << "\tFATAL:\n";
cerr << "\tCannot allocate memory for 2-d array. Run out of free store.\n";
cerr << "\tThis is, so far, prohibiting the further flow of the program.\n";
cerr << "\tABORTING. Sorry for that. \n";
abort();
}
memcpy( (void *) parray, (const void *) old.parray, (size_t) (newx*newy)*sizeof(T*) );
return *this;
}
}
template<class T> CCollection<T>::CCollection()
{
RWTPtrOrderedVector<T> plist();
minX = 0; // lower limits in x/y
minY = 0; // change here, if needed.
maxX = 0;
maxY = 0;
parray = 0; // no array requested ...
}
template<class T> CCollection<T>::~CCollection()
{
plist.clearAndDestroy();
delete [] parray;
}
//
// Member functions for the list
// (basically a subset of the RWTPtrOrderedVector class)
//
template<class T> CBoolean CCollection<T>::insert(T* a)
{
if (parray != 0) { // array exists ...
int x = a->getX(); // get x (anode) from T
int y = a->getY(); // get y (time) from T
if ( boundCheck(x,y) ) { // check bounds
parray[where(x,y)] = a; // ... ok, so store pointer to object
plist.insert(a); // insert in list
return True;
} else { // illegal item ...
return False;
}
} else { // no array ... tell user it's ok
plist.insert(a); // insert in list
return True;
}
}
template<class T> CBoolean CCollection<T>::append(T* a)
{
if (parray != 0) { // array exists ...
int x = a->getX(); // get x (anode) from T
int y = a->getY(); // get y (time) from T
if ( boundCheck(x,y) ) { // check bounds
parray[where(x,y)] = a; // ... ok, so store pointer to object
plist.append(a); // append to end of list
return True;
} else { // illegal item ...
return False;
}
} else { // no array ... tell user it's ok
plist.append(a); // append to end of list
return True;
}
}
template<class T> CBoolean CCollection<T>::remove(const T* a)
{
if (parray != 0) { // array exists ...
int x = a->getX(); // get x (anode) from T
int y = a->getY(); // get y (time) from T
if ( boundCheck(x,y) ) { // check bounds
parray[where(x,y)] = 0; // ... ok, so remove pointer to object
delete plist.remove(a); // removes and destroys item
return True;
} else { // illegal item ...
return False;
}
} else { // no array ... tell user it's ok
delete plist.remove(a); // removes and destroys item
return True;
}
}
template<class T> T* CCollection<T>::first () const
{
return (T*) plist.first();
}
template<class T> T* CCollection<T>::last () const
{
return (T*) plist.last();
}
template<class T> size_t CCollection<T>::length () const
{
return plist.length();
}
template<class T> size_t CCollection<T>::index (const T* a) const
{
return plist.index ( (const T*) a);
}
template<class T> void CCollection<T>::resize(size_t newSize)
{
plist.resize(newSize);
}
template<class T> void CCollection<T>::clear ()
{
for (int i=0; i<plist.length(); i++) { // loop all items in the list. no bound check
T* item = (T*) plist(i);
int x = item->getX();
int y = item->getY();
if ( boundCheck(x, y) ) { // check bounds
parray[where(x, y)] = 0 ; // needed, since all items are already in the list
}
}
plist.clear();
}
template<class T> void CCollection<T>::clearAndDestroy()
{
for (int i=0; i<plist.length(); i++) { // loop all items in the list. no bound check
T* item = (T*) plist(i);
int x = item->getX();
int y = item->getY();
if ( boundCheck(x, y) ) { // check bounds
parray[where(x, y)] = 0 ; // needed, since all items are already in the list
}
}
plist.clearAndDestroy();
}
template<class T> RWBoolean CCollection<T>::contains (const T* a) const
{
return plist.contains( (const T*) a);
}
template<class T> RWBoolean CCollection<T>::isEmpty () const
{
return plist.isEmpty();
}
template<class T> void CCollection<T>::resize (size_t newx, size_t newy)
{
if ( newx < 0 || newy < 0 ) { // stupid request, do nothing...
cerr << "CCollection<T>::resize():\n";
cerr << "\tERROR\n";
cerr << "\tIllegal size of (" << newx << ',' << newy << ") requested.\n";
cerr << "\tResize requested ignored.\n";
return;
}
if ( newx < maxX || newy < maxY ) {
cerr << "CCollection<T>::resize():\n";
cerr << "\tWARNING\n";
cerr << "\tResize request for smaller array than existing one.\n";
cerr << "\t(" << maxX << ',' << maxY << ") -> (" << newx << ',' << newy << ")\n";
cerr << "\tYou may not be able to access all (x,y) coordinates.\n";
}
maxX = newx+minX-1; // pads/cells ... count from (0/0) to (max-1/max-1)
maxY = newy+minY-1; // using C-style allocation: array[max] goes from 0 ... max-1
long newsize = (newx) * (newy); // new linear size
delete [] parray; // remove old existing array
parray = new T* [newsize]; // and create a new pointer
if (parray == 0) {
cerr << "CCollection<T>::resize() \n";
cerr << "\tFATAL:\n";
cerr << "\tCannot allocate memory for 2-d array. Run out of free store.\n";
cerr << "\tThis is, so far, prohibiting the further flow of the program.\n";
cerr << "\tABORTING. Sorry for that. \n";
abort();
}
memset( (void *) parray, (int) 0, (size_t) (newsize)*sizeof(T*) ); // fills 0-bytes into array
for (int i = 0; i < plist.length(); i++) { // loop list to fill array
T* item = (T*) plist(i); // item is pointer to T
int x = item->getX();
int y = item->getY();
if ( boundCheck(x, y) ) { // check bounds
parray[where(x, y)] = item; // store item
}
}
}