ITK  4.2.0
Insight Segmentation and Registration Toolkit
itkListSample.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkListSample_h
19 #define __itkListSample_h
20 
21 #include "itkObjectFactory.h"
22 #include "itkFixedArray.h"
23 #include "itkSample.h"
24 
25 #include <vector>
26 
27 namespace itk
28 {
29 namespace Statistics
30 {
50 template< class TMeasurementVector >
51 class ITK_EXPORT ListSample:public Sample< TMeasurementVector >
52 {
53 public:
55  typedef ListSample Self;
59 
61  itkTypeMacro(ListSample, Sample);
62 
64  itkNewMacro(Self);
65 
67  typedef typename Superclass::MeasurementVectorType MeasurementVectorType;
68  typedef typename Superclass::MeasurementVectorSizeType MeasurementVectorSizeType;
69  typedef typename Superclass::MeasurementType MeasurementType;
70  typedef typename Superclass::AbsoluteFrequencyType AbsoluteFrequencyType;
71  typedef typename Superclass::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType;
72  typedef typename Superclass::InstanceIdentifier InstanceIdentifier;
73 
77 
79  typedef std::vector< MeasurementVectorType > InternalDataContainerType;
80 
88  void Resize(InstanceIdentifier newsize);
89 
91  void Clear();
92 
94  void PushBack(const MeasurementVectorType & mv);
95 
97  InstanceIdentifier Size() const;
98 
101  const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const;
102 
104  void SetMeasurement(InstanceIdentifier id,
105  unsigned int dim,
106  const MeasurementType & value);
107 
109  void SetMeasurementVector(InstanceIdentifier id,
110  const MeasurementVectorType & mv);
111 
114  AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const;
115 
118  TotalAbsoluteFrequencyType GetTotalFrequency() const;
119 
121  virtual void Graft(const DataObject *thatObject);
122 
128  {
129  friend class ListSample;
130 public:
131 
132  ConstIterator(const ListSample *sample)
133  {
134  *this = sample->Begin();
135  }
136 
138  {
139  m_Iter = iter.m_Iter;
140  m_InstanceIdentifier = iter.m_InstanceIdentifier;
141  }
142 
143  ConstIterator & operator=(const ConstIterator & iter)
144  {
145  m_Iter = iter.m_Iter;
146  m_InstanceIdentifier = iter.m_InstanceIdentifier;
147  return *this;
148  }
149 
150  AbsoluteFrequencyType GetFrequency() const
151  {
152  return 1;
153  }
154 
155  const MeasurementVectorType & GetMeasurementVector() const
156  {
157  return static_cast< const MeasurementVectorType & >( *m_Iter );
158  }
159 
160  InstanceIdentifier GetInstanceIdentifier() const
161  {
162  return m_InstanceIdentifier;
163  }
164 
165  ConstIterator & operator++()
166  {
167  ++m_Iter;
168  ++m_InstanceIdentifier;
169  return *this;
170  }
171 
172  bool operator!=(const ConstIterator & it)
173  {
174  return ( m_Iter != it.m_Iter );
175  }
176 
177  bool operator==(const ConstIterator & it)
178  {
179  return ( m_Iter == it.m_Iter );
180  }
181 
182 protected:
183  // This method should only be available to the ListSample class
185  typename InternalDataContainerType::const_iterator iter,
186  InstanceIdentifier iid)
187  {
188  m_Iter = iter;
189  m_InstanceIdentifier = iid;
190  }
191 
192  // This method is purposely not implemented
193  ConstIterator();
194 private:
195  typedef typename InternalDataContainerType::const_iterator InternalIterator;
198  };
199 
204  class Iterator:public ConstIterator
205  {
206  friend class ListSample;
207 public:
208 
209  Iterator(Self *sample):ConstIterator(sample)
210  {}
211 
212  Iterator(const Iterator & iter):ConstIterator(iter)
213  {}
214 
215  Iterator & operator=(const Iterator & iter)
216  {
217  this->ConstIterator::operator=(iter);
218  return *this;
219  }
220 
221 protected:
222  // To ensure const-correctness these method must not be in the public API.
223  // The are purposly not implemented, since they should never be called.
224  Iterator();
225  Iterator(const Self *sample);
226  Iterator(typename InternalDataContainerType::const_iterator iter, InstanceIdentifier iid);
227  Iterator(const ConstIterator & it);
228  ConstIterator & operator=(const ConstIterator & it);
229 
231  typename InternalDataContainerType::iterator iter,
232  InstanceIdentifier iid):ConstIterator(iter, iid)
233  {}
234 
235 private:
236  };
237 
239  Iterator Begin()
240  {
241  Iterator iter(m_InternalContainer.begin(), 0);
242 
243  return iter;
244  }
245 
247  Iterator End()
248  {
249  Iterator iter( m_InternalContainer.end(), m_InternalContainer.size() );
250 
251  return iter;
252  }
253 
255  ConstIterator Begin() const
256  {
257  ConstIterator iter(m_InternalContainer.begin(), 0);
258 
259  return iter;
260  }
261 
263  ConstIterator End() const
264  {
265  ConstIterator iter( m_InternalContainer.end(), m_InternalContainer.size() );
266 
267  return iter;
268  }
269 
270 protected:
271 
272  ListSample();
273  virtual ~ListSample() {}
274  void PrintSelf(std::ostream & os, Indent indent) const;
275 
276 private:
277  ListSample(const Self &); //purposely not implemented
278  void operator=(const Self &); //purposely not implemented
279 
281 };
282 } // end of namespace Statistics
283 } // end of namespace itk
284 
285 #ifndef ITK_MANUAL_INSTANTIATION
286 #include "itkListSample.hxx"
287 #endif
288 
289 #endif
290