#line 1 "/user/cvsspst/ees1cg/RAVL/RAVL-0.7/PatternRec/DataSet/DataSet1.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_DATASET1_HEADER #define RAVL_DATASET1_HEADER 1 //! rcsid="$Id: DataSet1.hh,v 1.16 2002/07/02 08:57:49 craftit Exp $" //! author="Kieron Messer" //! docentry="Ravl.Pattern Recognition.Data Set" //! lib=RavlPatternRec //! file="Ravl/PatternRec/DataSet/DataSet1.hh" #include "Ravl/PatternRec/DataSetBase.hh" #include "Ravl/Vector.hh" #include "Ravl/DArray1dIter.hh" namespace RavlN { template class DataSet1C; //! userlevel=Develop //: Data set template class DataSet1BodyC : public DataSetBaseBodyC { public: typedef typename SampleT::ElementT Element1T; DataSet1BodyC(UIntT sizeEstimate) : DataSetBaseBodyC(sizeEstimate), samp1(sizeEstimate) {} //: Create an empty dataset. DataSet1BodyC(const SampleT & samp); //: Create a dataset from a sample SampleT &Sample1() { return samp1; } //: Access sample. const SampleT &Sample1() const { return samp1; } //: Access sample. UIntT Append(const Element1T &data); //: Append a data entry. // returns its index. DataSet1C ExtractSample(RealT proportion); //: Extract a sample. // The elements are removed from this set. NB. The order // of this dataset is NOT preserved. virtual void Shuffle(); //: Shuffle the order of the dataset. UIntT Size() const { return samp1.Size(); } //: Get the size of the dataset. protected: SampleT samp1; //: the actual data }; //! userlevel=Normal //: Data set template class DataSet1C : public DataSetBaseC { public: typedef typename SampleT::ElementT Element1T; DataSet1C() {} //: Default constructor. DataSet1C(UIntT sizeEstimate) : DataSetBaseC(*new DataSet1BodyC(sizeEstimate)) {} //: Default constructor. DataSet1C(const SampleT & dat) : DataSetBaseC(*new DataSet1BodyC(dat)) {} //: Create a dataset from a sample protected: DataSet1C(DataSet1BodyC &bod) : DataSetBaseC(bod) {} //: Body constructor. DataSet1BodyC &Body() { return static_cast &>(DataSetBaseC::Body()); } //: Access body. const DataSet1BodyC &Body() const { return static_cast &>(DataSetBaseC::Body()); } //: Access body. public: SampleT &Sample1() { return Body().Sample1(); } //: Access complete sample. const SampleT &Sample1() const { return Body().Sample1(); } //: Access complete sample. UIntT Append(const Element1T &data) { return Body().Append(data); } //: Append a data entry. // returns its index. DataSet1C ExtractSample(RealT proportion) { return Body().ExtractSample(proportion); } //: Extract a sample. // The elements are removed from this set. NB. The order // of this dataset is NOT preserved. UIntT Size() const { return Body().Size(); } //: Get the size of the dataset. friend class DataSet1BodyC; }; template DataSet1BodyC::DataSet1BodyC(const SampleT & sp) : DataSetBaseBodyC(sp.Size()), samp1(sp) {} template UIntT DataSet1BodyC::Append(const Element1T &data) { return samp1.Append(data); } template DataSet1C DataSet1BodyC::ExtractSample(RealT proportion) { RavlAssert(proportion >= 0 && proportion <= 1); UIntT size = Size(); UIntT entries = (UIntT) (proportion * (RealT) size); DataSet1C ret(size); for(;entries > 0;entries--) { UIntT entry = RandomInt() % size; ret.Append(samp1.PickElement(entry)); size--; } return ret; } template void DataSet1BodyC::Shuffle() { UIntT size = Size(); for(DArray1dIterC it(samp1.DArray());it;it++) Swap(*it,samp1.Nth(RandomInt() % size)); } } #endif