#line 1 "/user/cvsspst/ees1cg/RAVL/RAVL-0.7/Core/Container/Buffer/SBfAcc2d.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_SBFACC2D_HEADER #define RAVL_SBFACC2D_HEADER 1 /////////////////////////////////////////////////////////// //! rcsid="$Id: SBfAcc2d.hh,v 1.11 2002/08/09 19:59:14 craftit Exp $" //! file="Ravl/Core/Container/Buffer/SBfAcc2d.hh" //! lib=RavlCore //! userlevel=Develop //! author="Charles Galambos" //! date="24/01/2001" //! docentry="Ravl.Core.Arrays.Buffer" #include "Ravl/SBfAcc.hh" #include "Ravl/BfAcc2Iter.hh" #include "Ravl/Index2d.hh" #include "Ravl/Types.hh" namespace RavlN { class BinOStreamC; class BinIStreamC; //: Basic access to buffer with limited size // The class SizeBufferAccessC enables to random indexed access to // a sequentially organised continous part of memory called buffer. // The access functions check if an accessed element is valid only in // debug mode. template class SizeBufferAccess2dC : public SizeBufferAccessC > { public: SizeBufferAccess2dC(SizeT nsize2) : size2(nsize2) {} //: Constructor. SizeBufferAccess2dC(const SizeBufferAccessC > &ab,SizeT nsize2) : SizeBufferAccessC >(ab), size2(nsize2) {} //: Constructor. SizeBufferAccess2dC(const BufferAccessC > &ab,SizeT nsize1,SizeT nsize2) : SizeBufferAccessC >(ab,nsize1), size2(nsize2) {} //: Constructor. SizeBufferAccess2dC(const SizeBufferAccess2dC &ab,SizeT nsize1,SizeT nsize2) : SizeBufferAccessC >(ab,nsize1), size2(nsize2) { RavlAssert(nsize1 <= ab.Size1()); RavlAssert(nsize2 <= ab.Size2()); } //: Constructor. SizeBufferAccess2dC() : size2(0) {} //: Default constructor. inline bool Contains(const Index2dC &i) const { return (((UIntT) i.Col().V()) < size2) && (((UIntT) i.Row().V()) < Size1()); } //: Does this buffer contain the index i ? // Returns true if yes. inline DataT & operator[](const Index2dC & i) { #if RAVL_CHECK if (((UIntT) i.Col().V()) >= size2) IssueError(__FILE__,__LINE__,"Index %u out of index range 0-%u ",i.Col().V(),size2-1); #endif return SizeBufferAccessC >::operator[](i.Row())[i.Col()]; } //: access to the item array[(i)] inline const DataT & operator[](const Index2dC & i) const { #if RAVL_CHECK if (((UIntT) i.Col().V()) >= size2) IssueError(__FILE__,__LINE__,"Index %u out of index range 0-%u ",i.Col().V(),size2-1); #endif return SizeBufferAccessC >::operator[](i.Row())[i.Col()]; } //: return the item array[(i)] inline SizeBufferAccessC operator[](IndexC i) { return SizeBufferAccessC(SizeBufferAccessC >::operator[](i),size2); } //: access to the item array[(i)] inline const SizeBufferAccessC operator[](IndexC i) const { return SizeBufferAccessC(SizeBufferAccessC >::operator[](i),size2); } //: return the item array[(i)] inline SizeT Size1() const { return SizeBufferAccessC >::Size(); } //: Size. inline SizeT Size2() const { return size2; } //: Size. UIntT Size() const { return Size1() * Size2(); } //: Get the total number of elements in the array. void Fill(const DataT &d); //: Fill array with value. IntT Stride() const { if(Size1() <= 1) return size2; return (IntT) (SizeBufferAccessC >::operator[](1).ReferenceElm() - SizeBufferAccessC >::operator[](0).ReferenceElm()); } //: Get the stide of the 2d array. bool IsContinuous() const { return Stride() == (IntT) size2; } //: Test if the array is allocated in a continous area of memory. // Note: this only checks the first two rows follow each other in // memory, this may miss other discontunities. protected: SizeT size2; }; template void SizeBufferAccess2dC::Fill(const DataT &d) { for(BufferAccess2dIterC it(*this,size2);it;it++) *it = d; } template ostream & operator<<(ostream & s, const SizeBufferAccess2dC & arr) { for(BufferAccess2dIterC it(arr,arr.Size2());it;) { s << *it; for(;it.Next();) s << ' ' << *it; s << '\n'; } return s; } template istream & operator>>(istream & s, SizeBufferAccess2dC & arr) { for(BufferAccess2dIterC it(arr,arr.Size2());it;it++) s >> *it; return s; } template BinOStreamC &operator<<(BinOStreamC & s, const SizeBufferAccess2dC & arr) { for(BufferAccess2dIterC it(arr,arr.Size2());it;it++) s << *it; return s; } template BinIStreamC &operator>>(BinIStreamC & s, SizeBufferAccess2dC & arr) { for(BufferAccess2dIterC it(arr,arr.Size2());it;it++) s >> *it; return s; } } #endif