#line 1 "/user/cvsspst/ees1cg/RAVL/RAVL-0.7/Core/Container/SArray/SDArray1d.hh" // This file is part of RAVL, Recognition And Vision Library // Copyright (C) 2001, University of Surrey // This code may be redistributed under the terms of the GNU Lesser // General Public License (LGPL). See the lgpl.licence file for details or // see http://www.gnu.org/copyleft/lesser.html // file-header-ends-here #ifndef RAVL_SDARRAY1D_HEADER #define RAVL_SDARRAY1D_HEADER 1 ///////////////////////////////////////////////////////////// //! file="Ravl/Core/Container/SArray/SDArray1d.hh" //! lib=RavlCore //! userlevel=Advanced //! author="Charles Galambos" //! date="4/9/1996" //! docentry="Ravl.Core.Arrays.1D" //! rcsid="$Id: SDArray1d.hh,v 1.3 2002/05/21 14:46:11 craftit Exp $" #include "Ravl/Types.hh" #include "Ravl/Index.hh" #include "Ravl/Assert.hh" #if RAVL_HAVE_ANSICPPHEADERS #include #else #include #endif namespace RavlN { //! userlevel=Advanced //:1D Simple Dynamic Array. // This does not intalise data before it's needed.

// SMALL OBJECT template class SDArray1dC { public: SDArray1dC(UIntT initSize = 0); //: Constructor. SDArray1dC(const SDArray1dC &Oth); //: Copy constructor. SDArray1dC(const SDArray1dC &Oth,IntT Delta); //: Copy constructor, expanding array by delta. ~SDArray1dC(); //: Destructor. void Resize(UIntT size); //: Resize SDArray1d. void Empty(void); //: Remove all contents. UIntT Add(const T &Data); //: Append data to end. // Returns the index of the appended item. // This function will automaticly expand the array // if necessary. inline UIntT Chop(void); //: Chop last number from array. //NB. This does NOT shrink the array in the current // implementation, this may change in future. inline T &operator[](UIntT i); //: Access element. inline const T &operator[](UIntT i) const; //: Access element. inline T &operator[](IndexC i); //: Access element. inline const T &operator[](IndexC i) const; //: Access element. const SDArray1dC &operator=(const SDArray1dC &Oth) ; //: Assign to another SDArray1d. inline const T &Last() const { RavlAssert(Used > 0); return ((T *)Data)[Used-1]; } //: Get last element in array. bool Compare(const SDArray1dC &Oth) const; //: Compare with another SDArray1d. inline bool operator==(const SDArray1dC &Oth) const; //: Comparison. inline UIntT Size(void) const { return Used; } //: Number of elements used. inline UIntT MaxN(void) const { return size; } //: Number of elements used. inline bool IsSpace() const { return Used < size; } //: Is space for more data ? inline bool IsEmpty() const { return Used == 0; } //: Is empty ? private: UIntT Used; // Number used in the SDArray1d. UIntT size; // size of allocated array. char *Data; // Ptr to data array. }; template ostream &operator<<(ostream &out,const SDArray1dC &arr) { out << "0 " << arr.Size() << "\n"; for(UIntT i = 0;i < arr.Size();i++) out << arr[i] << "\n"; return out; } template istream &operator>>(istream &in,SDArray1dC &arr) { UIntT asize,isize; in >> isize >> asize; RavlAssert(isize == 0); arr = SDArray1dC(asize); for(UIntT i = 0;i < asize;i++) in >> arr[i]; return in; } ///////////////////////// // Constructors. template SDArray1dC::SDArray1dC(UIntT Initsize) { Used = 0; size = Initsize; if(size > 0) Data = new char [sizeof(T) * size]; else Data = 0; } template SDArray1dC::SDArray1dC(const SDArray1dC &Oth) : Used(Oth.Used), size(Oth.size) { Data = new char [sizeof(T) * size]; T *Place,*OPlace,*End = &((T *)Data)[Used]; for(Place = (T *) Data,OPlace = (T *) Oth.Data;Place != End;Place++,OPlace++) new((void *)Place) T(*OPlace); } template SDArray1dC::SDArray1dC(const SDArray1dC &Oth,IntT Delta) : Used(Oth.Used), size(Oth.size + Delta) { RavlAssert(Delta >= 0); Data = new char [sizeof(T) * size]; T *Place,*OPlace,*End = &((T *)Data)[Used]; for(Place = (T *) Data,OPlace = (T *) Oth.Data;Place != End;Place++,OPlace++) new((void *)Place) T(*OPlace); } template SDArray1dC::~SDArray1dC() { Empty(); delete [] Data; } template void SDArray1dC::Resize(UIntT nsize) { T *Place,*NPlace,*End = &((T *)Data)[Used]; if(nsize < Used) { RavlAssert(0); // This shouldn't really happen ! for(Place = &((T *)Data)[nsize];Place != End;Place++) Place->~T(); // Destroy unused data. End = &((T *)Data)[nsize]; Used = nsize; } char *NewD = new char [sizeof(T) * nsize]; for(NPlace = (T *)NewD,Place = (T *)Data;Place != End;Place++,NPlace++) { new((void *)NPlace) T(*Place); // Dupicate in new array. Place->~T(); // Destroy old data. } delete [] Data; Data = NewD; size = nsize; }; template inline T &SDArray1dC::operator[](UIntT i) { RavlAssert(i < Used); return ((T *)Data)[i]; } template inline const T &SDArray1dC::operator[](UIntT i) const { RavlAssert(i < Used); return ((T *)Data)[i]; } template T &SDArray1dC::operator[](IndexC i) { RavlAssert(((UIntT) i.V()) < Used); return ((T *)Data)[i.V()]; } template const T &SDArray1dC::operator[](IndexC i) const { RavlAssert(((UIntT) i.V()) < Used); return ((T *)Data)[i.V()]; } template const SDArray1dC &SDArray1dC::operator= (const SDArray1dC &Oth) { if(&Oth == this) return *this; Empty(); if(size < Oth.Used) { delete [] Data; size = Oth.size + 4; Data = new char [sizeof(T) * size]; } Used = Oth.Used; T *Place,*OPlace,*End = &((T *)Data)[Used]; for(Place = (T *) Data,OPlace = (T *) Oth.Data;Place != End;Place++,OPlace++) { new((void *)Place) T(*OPlace); } return *this; } template void SDArray1dC::Empty(void) { T *Place,*End = &(((T *)Data)[Used]); for(Place = (T *) Data;Place != End;Place++) Place->~T(); Used = 0; } template UIntT SDArray1dC::Add(const T &Val) { if(Used >= size) { if(size == 0) Resize(4); else Resize(size << 1); } new((void *) & ((T *)Data)[Used]) T(Val); return Used++; } template inline UIntT SDArray1dC::Chop(void) { RavlAssert(Used > 0); ((T *) Data)[(--Used)].~T(); return Used; } template bool SDArray1dC::Compare(const SDArray1dC &Oth) const { if(&Oth == this) return True; // Short cut. if(Oth.Used != Used) return False; T *Place,*OPlace,*End = &((T *)Data)[Used]; for(Place = (T *)Data,OPlace = (T *)Oth.Data;Place != End;Place++,OPlace++) if(!(*Place == *OPlace)) return False; return True; } template inline bool SDArray1dC::operator==(const SDArray1dC &Oth) const { return Compare(Oth); } } #endif