#line 1 "/user/cvsspst/ees1cg/RAVL/RAVL-0.7/PatternRec/DataSet/Sample.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_SAMPLE_HEADER #define RAVL_SAMPLE_HEADER 1 //! rcsid="$Id: Sample.hh,v 1.16 2002/06/11 18:23:42 craftit Exp $" //! author="Kieron Messer" //! docentry="Ravl.Pattern Recognition.Data Set" //! lib=RavlPatternRec //! file="Ravl/PatternRec/DataSet/Sample.hh" #include "Ravl/DArray1d.hh" #include "Ravl/DArray1dIter.hh" #include "Ravl/Collection.hh" namespace RavlN { template class DataSet1IterC; template class DataSet2IterC; template class DataSet3IterC; template class SampleIterC; //! userlevel=Normal //: Sample of DataT's template class SampleC : protected DArray1dC { public: typedef DataT ElementT; //: Type of data in sample. SampleC(SizeT maxSize=10) : DArray1dC(maxSize,true) {} //: Create a sample of data with a maximum size SampleC(const SArray1dC & dat) : DArray1dC(dat) {} //: Create a sample of data from an array SampleC SubSample(const CollectionC &x); //: Take a subsample of the given indexes in x. DataT ExtractEntry(int ind); //: Extract an entry from sample. UIntT Append(const DataT & dat) { return DArray1dC::Append(dat).V(); } //: Insert a single sample into sample UIntT operator+=(const DataT & dat) { return Append(dat); } //: Indentical to Append(). SizeT Size() const { return DArray1dC::Size(); } //: Return the number of valid samples in the collection DataT PickElement(UIntT i); //: Pick a item i from the collection // Note: The order of the collection is NOT preserved. // This minimizes the distruption to the underlying // representation by removing an element from the end // of the array and placing it in the hole left by // removing 'i'. DataT &Nth(UIntT i) { return DArray1dC::Nth(i); } //: Access nth element in sample. const DataT &Nth(UIntT i) const { return DArray1dC::Nth(i); } //: Access nth element in sample. DataT Pick() { return PickElement(RandomInt() % Size()); } //: Pick a random item from the collection // Note: The order of the collection is NOT preserved. DataT &operator[](IndexC ind) { return DArray1dC::operator[](ind); } //: Access a sample. const DataT &operator[](IndexC ind) const { return DArray1dC::operator[](ind); } //: Access a sample. friend class DataSet1IterC; DArray1dC &DArray() { return *this; } //: Access DArray. // For internal use only. const DArray1dC &DArray() const { return *this; } //: Access DArray. // For internal use only. DataT &First() { return DArray1dC::First(); } //: Access first element in the array. const DataT &First() const { return DArray1dC::First(); } //: Access first element in the array. DataT &Last() { return DArray1dC::Last(); } //: Access last element in the array. const DataT &Last() const { return DArray1dC::Last(); } //: Access last element in the array. bool IsEmpty() const { return DArray1dC::IsEmpty(); } //: Is this empty ? }; // end of class SampleC template ostream &operator<<(ostream &s,const SampleC &dat ) { s << ((int) 0) << " " << ((int) dat.Size()-1) << "\n"; for(DArray1dIterC it(dat.DArray());it;it++) s << *it << "\n"; return s; } //: Output to stream. template istream &operator>>(istream &s,SampleC &dat ) { int min,max; s >> min >> max; dat = SampleC((min - max) + 1); for(DArray1dIterC it(dat);it;it++) { DataT v; s >> v; dat += v; } return s; } //: Read from stream. template SampleC SampleC::SubSample(const CollectionC &x) { SampleC ret(x.Size()); for(CollectionIterC it(x);it;it++) ret.Insert((*this)[*it]); return ret; } template DataT SampleC::ExtractEntry(int ind) { RavlAssert(Contains(ind)); DataT ret = (*this)[ind]; Remove(ind); return ret; } template DataT SampleC::PickElement(UIntT i) { DataT &val = Nth(i); DataT ret = val; val = Last(); RemoveLast(); return ret; } } #endif