CCollection.h
//-----------------------------------------------------------------------------
// $Header: /tmp_mnt/asis/offline/ceres/cool/project/RCS/CCollection.h,v 2.1 1996/10/04 08:43:25 voigt Exp $
//
// COOL Program Library
// Copyright (C) CERES collaboration, 1996
//
// Declaration of CCollection<T> class.
//
// CCollection: template class to handle one- and two-dimensional access to
// CERES raw data (RICH-pads or SiDC-cells)
//
// ATTENTION: to use RWTPtrOrderedVector<T> below, the class T MUST have :
// ========== - a well defined equality semantics (T::operator==(const T&))
// - functions getX() and getY() to get coordinates for the array
//
// pads, anodes and time bins count from 0 ... (maxItem-1)
// (as usual in C/C++) as needed for LookupItems
//
//-----------------------------------------------------------------------------
#ifndef CCOLLECTION_H
#define CCOLLECTION_H
#include "cool.h"
#include "rw/tpordvec.h"
template<class T> class CCollection {
public:
CCollection();
virtual ~CCollection();
CCollection(const CCollection<T>&); // prevent use of copy constructor
CCollection<T> & operator = (const CCollection<T>&); // prevent use of assignment operator
public:
// member functions for the list (basically a subset of the RWTPtrOrderedVector) :
virtual CBoolean insert(T* a); // insert a T to the list and array
virtual CBoolean append(T* a); // append a T to the end of the list and the array
virtual CBoolean remove(const T* a); // remove a T from the list and array and destroys the T
virtual T* first () const; // returns the first item in the list
virtual T* last () const; // returns the last item in the list
virtual size_t length () const; // returns number of items currently in the list
virtual size_t index (const T* a) const; // returns of the first object equal to a in the list
virtual void resize (size_t); // resizes the list, to resize the array, see below
virtual void clear (); // removes all items
virtual void clearAndDestroy(); // removes all items AND calls their destructors
virtual RWBoolean contains (const T* a) const; // returns TRUE if the collection contains an item == a
virtual RWBoolean isEmpty () const; // returns TRUE if there are no items in the collection
// operators for the list (basically a subset of the RWTPtrOrderedVector) :
inline virtual T*& operator () (size_t i); // ... no bound checks, i = 0...(nitems-1)
inline virtual T*& operator [] (size_t i); // ... with bound checks, i = 0...(nitems-1)
inline virtual T* operator () (size_t i) const; // not a lvalue
inline virtual T* operator [] (size_t i) const; // not a lvalue
// operators for the array access :
inline virtual T* operator () (int i, size_t j) const; // with bound checks
virtual void resize (size_t, size_t); // resize array
protected:
long minX; // for more easy change of array borders
long minY;
long maxX; // to be able to calculate 'where'
long maxY;
inline virtual long where(int, int) const; // calculates index from x/y according to maxX/Y
inline virtual CBoolean boundCheck(int, int) const; // these two need to be inline for speed ...
T* *parray; // array of pointers to the <T>-list
RWTPtrOrderedVector<T> plist; // list of pointers to the <T>-list
};
// ------------------------------ inline functions ------------------------------
//
// Calculates index from x/y according to maxX/Y
//
template <class T> long CCollection<T>::where(int x, int y) const
{
return (y-minY)*((maxX+1)-minX) + (x-minX); // remember that maxX is xSizeOfArray-1.
}
template<class T>
CBoolean CCollection<T>::boundCheck(int x, int y) const
{
return ( x >= minX && x <= maxX &&
y >= minY && y <= maxY );
}
//
// Operators for the list
// (basically a subset of the RWTPtrOrderedVector class)
//
template<class T> T*& CCollection<T>::operator () (size_t i)
{
return (T*&) plist(i);
}
template<class T> T*& CCollection<T>::operator [] (size_t i)
{
return (T*&) plist[i];
}
template<class T> T* CCollection<T>::operator () (size_t i) const
{
return (T*) plist(i);
}
template<class T> T* CCollection<T>::operator [] (size_t i) const
{
return (T*) plist[i];
}
//
// Operators for the array access
//
template<class T> T* CCollection<T>::operator () (int i, size_t j) const
{
T* ret = 0;
if (parray && boundCheck(i,j)) ret = (T*) parray[where(i,j)];
return ret;
}
#ifdef RW_COMPILE_INSTANTIATE
#include "CCollection.C"
#endif
#endif /* CCOLLECTION_H */