ITK  4.4.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 
131 public:
132 
133  ConstIterator(const ListSample *sample)
134  {
135  *this = sample->Begin();
136  }
137 
139  {
140  m_Iter = iter.m_Iter;
141  m_InstanceIdentifier = iter.m_InstanceIdentifier;
142  }
143 
144  ConstIterator & operator=(const ConstIterator & iter)
145  {
146  m_Iter = iter.m_Iter;
147  m_InstanceIdentifier = iter.m_InstanceIdentifier;
148  return *this;
149  }
150 
151  AbsoluteFrequencyType GetFrequency() const
152  {
153  return 1;
154  }
155 
156  const MeasurementVectorType & GetMeasurementVector() const
157  {
158  return static_cast< const MeasurementVectorType & >( *m_Iter );
159  }
160 
161  InstanceIdentifier GetInstanceIdentifier() const
162  {
163  return m_InstanceIdentifier;
164  }
165 
166  ConstIterator & operator++()
167  {
168  ++m_Iter;
169  ++m_InstanceIdentifier;
170  return *this;
171  }
172 
173  bool operator!=(const ConstIterator & it)
174  {
175  return ( m_Iter != it.m_Iter );
176  }
177 
178  bool operator==(const ConstIterator & it)
179  {
180  return ( m_Iter == it.m_Iter );
181  }
182 
183 protected:
184  // This method should only be available to the ListSample class
186  typename InternalDataContainerType::const_iterator iter,
187  InstanceIdentifier iid)
188  {
189  m_Iter = iter;
190  m_InstanceIdentifier = iid;
191  }
192 
193  // This method is purposely not implemented
194  ConstIterator();
195 
196 private:
197  typedef typename InternalDataContainerType::const_iterator InternalIterator;
200  };
201 
206  class Iterator:public ConstIterator
207  {
208  friend class ListSample;
209 
210 public:
211 
212  Iterator(Self *sample):ConstIterator(sample)
213  {}
214 
215  Iterator(const Iterator & iter):ConstIterator(iter)
216  {}
217 
218  Iterator & operator=(const Iterator & iter)
219  {
220  this->ConstIterator::operator=(iter);
221  return *this;
222  }
223 
224 protected:
225  // To ensure const-correctness these method must not be in the public API.
226  // The are purposly not implemented, since they should never be called.
227  Iterator();
228  Iterator(const Self *sample);
229  Iterator(typename InternalDataContainerType::const_iterator iter, InstanceIdentifier iid);
230  Iterator(const ConstIterator & it);
231  ConstIterator & operator=(const ConstIterator & it);
232 
234  typename InternalDataContainerType::iterator iter,
235  InstanceIdentifier iid):ConstIterator(iter, iid)
236  {}
237 
238 private:
239  };
240 
242  Iterator Begin()
243  {
244  Iterator iter(m_InternalContainer.begin(), 0);
245 
246  return iter;
247  }
248 
250  Iterator End()
251  {
252  Iterator iter( m_InternalContainer.end(), m_InternalContainer.size() );
253 
254  return iter;
255  }
256 
258  ConstIterator Begin() const
259  {
260  ConstIterator iter(m_InternalContainer.begin(), 0);
261 
262  return iter;
263  }
264 
266  ConstIterator End() const
267  {
268  ConstIterator iter( m_InternalContainer.end(), m_InternalContainer.size() );
269 
270  return iter;
271  }
272 
273 protected:
274 
275  ListSample();
276  virtual ~ListSample() {}
277  void PrintSelf(std::ostream & os, Indent indent) const;
278 
279 private:
280  ListSample(const Self &); //purposely not implemented
281  void operator=(const Self &); //purposely not implemented
282 
284 };
285 } // end of namespace Statistics
286 } // end of namespace itk
287 
288 #ifndef ITK_MANUAL_INSTANTIATION
289 #include "itkListSample.hxx"
290 #endif
291 
292 #endif
293