ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkListSample.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkListSample_h
00019 #define __itkListSample_h
00020 
00021 #include "itkObjectFactory.h"
00022 #include "itkFixedArray.h"
00023 #include "itkSample.h"
00024 
00025 #include <vector>
00026 
00027 namespace itk
00028 {
00029 namespace Statistics
00030 {
00050 template< class TMeasurementVector >
00051 class ITK_EXPORT ListSample:public Sample< TMeasurementVector >
00052 {
00053 public:
00055   typedef ListSample                   Self;
00056   typedef Sample< TMeasurementVector > Superclass;
00057   typedef SmartPointer< Self >         Pointer;
00058   typedef SmartPointer< const Self >   ConstPointer;
00059 
00061   itkTypeMacro(ListSample, Sample);
00062 
00064   itkNewMacro(Self);
00065 
00067   typedef typename Superclass::MeasurementVectorType      MeasurementVectorType;
00068   typedef typename Superclass::MeasurementVectorSizeType  MeasurementVectorSizeType;
00069   typedef typename Superclass::MeasurementType            MeasurementType;
00070   typedef typename Superclass::AbsoluteFrequencyType      AbsoluteFrequencyType;
00071   typedef typename Superclass::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType;
00072   typedef typename Superclass::InstanceIdentifier         InstanceIdentifier;
00073 
00076   typedef MeasurementVectorType ValueType;
00077 
00079   typedef std::vector< MeasurementVectorType > InternalDataContainerType;
00080 
00088   void Resize(InstanceIdentifier newsize);
00089 
00091   void Clear();
00092 
00094   void PushBack(const MeasurementVectorType & mv);
00095 
00097   InstanceIdentifier Size() const;
00098 
00101   const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const;
00102 
00104   void SetMeasurement(InstanceIdentifier id,
00105                       unsigned int dim,
00106                       const MeasurementType & value);
00107 
00109   void SetMeasurementVector(InstanceIdentifier id,
00110                             const MeasurementVectorType & mv);
00111 
00114   AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const;
00115 
00118   TotalAbsoluteFrequencyType GetTotalFrequency() const;
00119 
00121   virtual void Graft(const DataObject *thatObject);
00122 
00127   class ConstIterator
00128   {
00129     friend class ListSample;
00130 public:
00131 
00132     ConstIterator(const ListSample *sample)
00133     {
00134       *this = sample->Begin();
00135     }
00136 
00137     ConstIterator(const ConstIterator & iter)
00138     {
00139       m_Iter = iter.m_Iter;
00140       m_InstanceIdentifier = iter.m_InstanceIdentifier;
00141     }
00142 
00143     ConstIterator & operator=(const ConstIterator & iter)
00144     {
00145       m_Iter = iter.m_Iter;
00146       m_InstanceIdentifier = iter.m_InstanceIdentifier;
00147       return *this;
00148     }
00149 
00150     AbsoluteFrequencyType GetFrequency() const
00151     {
00152       return 1;
00153     }
00154 
00155     const MeasurementVectorType & GetMeasurementVector() const
00156     {
00157       return static_cast< const MeasurementVectorType & >( *m_Iter );
00158     }
00159 
00160     InstanceIdentifier GetInstanceIdentifier() const
00161     {
00162       return m_InstanceIdentifier;
00163     }
00164 
00165     ConstIterator & operator++()
00166     {
00167       ++m_Iter;
00168       ++m_InstanceIdentifier;
00169       return *this;
00170     }
00171 
00172     bool operator!=(const ConstIterator & it)
00173     {
00174       return ( m_Iter != it.m_Iter );
00175     }
00176 
00177     bool operator==(const ConstIterator & it)
00178     {
00179       return ( m_Iter == it.m_Iter );
00180     }
00181 
00182 protected:
00183     // This method should only be available to the ListSample class
00184     ConstIterator(
00185       typename InternalDataContainerType::const_iterator iter,
00186       InstanceIdentifier iid)
00187     {
00188       m_Iter = iter;
00189       m_InstanceIdentifier = iid;
00190     }
00191 
00192     // This method is purposely not implemented
00193     ConstIterator();
00194 private:
00195     typedef typename InternalDataContainerType::const_iterator InternalIterator;
00196     InternalIterator   m_Iter;
00197     InstanceIdentifier m_InstanceIdentifier;
00198   };
00199 
00204   class Iterator:public ConstIterator
00205   {
00206     friend class ListSample;
00207 public:
00208 
00209     Iterator(Self *sample):ConstIterator(sample)
00210     {}
00211 
00212     Iterator(const Iterator & iter):ConstIterator(iter)
00213     {}
00214 
00215     Iterator & operator=(const Iterator & iter)
00216     {
00217       this->ConstIterator::operator=(iter);
00218       return *this;
00219     }
00220 
00221 protected:
00222     // To ensure const-correctness these method must not be in the public API.
00223     // The are purposly not implemented, since they should never be called.
00224     Iterator();
00225     Iterator(const Self *sample);
00226     Iterator(typename InternalDataContainerType::const_iterator iter, InstanceIdentifier iid);
00227     Iterator(const ConstIterator & it);
00228     ConstIterator & operator=(const ConstIterator & it);
00229 
00230     Iterator(
00231       typename InternalDataContainerType::iterator iter,
00232       InstanceIdentifier iid):ConstIterator(iter, iid)
00233     {}
00234 
00235 private:
00236   };
00237 
00239   Iterator Begin()
00240   {
00241     Iterator iter(m_InternalContainer.begin(), 0);
00242 
00243     return iter;
00244   }
00245 
00247   Iterator End()
00248   {
00249     Iterator iter( m_InternalContainer.end(), m_InternalContainer.size() );
00250 
00251     return iter;
00252   }
00253 
00255   ConstIterator Begin() const
00256   {
00257     ConstIterator iter(m_InternalContainer.begin(), 0);
00258 
00259     return iter;
00260   }
00261 
00263   ConstIterator End() const
00264   {
00265     ConstIterator iter( m_InternalContainer.end(), m_InternalContainer.size() );
00266 
00267     return iter;
00268   }
00269 
00270 protected:
00271 
00272   ListSample();
00273   virtual ~ListSample() {}
00274   void PrintSelf(std::ostream & os, Indent indent) const;
00275 
00276 private:
00277   ListSample(const Self &);     //purposely not implemented
00278   void operator=(const Self &); //purposely not implemented
00279 
00280   InternalDataContainerType m_InternalContainer;
00281 };
00282 } // end of namespace Statistics
00283 } // end of namespace itk
00284 
00285 #ifndef ITK_MANUAL_INSTANTIATION
00286 #include "itkListSample.hxx"
00287 #endif
00288 
00289 #endif
00290