ITK  5.0.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< typename TMeasurementVector >
51 class ITK_TEMPLATE_EXPORT ListSample:public Sample< TMeasurementVector >
52 {
53 public:
54  ITK_DISALLOW_COPY_AND_ASSIGN(ListSample);
55 
57  using Self = ListSample;
61 
63  itkTypeMacro(ListSample, Sample);
64 
66  itkNewMacro(Self);
67 
69  using MeasurementVectorType = typename Superclass::MeasurementVectorType;
70  using MeasurementVectorSizeType = typename Superclass::MeasurementVectorSizeType;
71  using MeasurementType = typename Superclass::MeasurementType;
72  using AbsoluteFrequencyType = typename Superclass::AbsoluteFrequencyType;
73  using TotalAbsoluteFrequencyType = typename Superclass::TotalAbsoluteFrequencyType;
74  using InstanceIdentifier = typename Superclass::InstanceIdentifier;
75 
79 
81  using InternalDataContainerType = std::vector< MeasurementVectorType >;
82 
90  void Resize(InstanceIdentifier newsize);
91 
93  void Clear();
94 
96  void PushBack(const MeasurementVectorType & mv);
97 
99  InstanceIdentifier Size() const override;
100 
103  const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const override;
104 
106  void SetMeasurement(InstanceIdentifier id,
107  unsigned int dim,
108  const MeasurementType & value);
109 
111  void SetMeasurementVector(InstanceIdentifier id,
112  const MeasurementVectorType & mv);
113 
116  AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const override;
117 
120  TotalAbsoluteFrequencyType GetTotalFrequency() const override;
121 
123  void Graft(const DataObject *thatObject) override;
124 
130  {
131  friend class ListSample;
132 
133 public:
134 
135  ConstIterator(const ListSample *sample)
136  {
137  *this = sample->Begin();
138  }
139 
141  {
142  m_Iter = iter.m_Iter;
143  m_InstanceIdentifier = iter.m_InstanceIdentifier;
144  }
145 
147  {
148  m_Iter = iter.m_Iter;
149  m_InstanceIdentifier = iter.m_InstanceIdentifier;
150  return *this;
151  }
152 
154  {
155  return 1;
156  }
157 
159  {
160  return static_cast< const MeasurementVectorType & >( *m_Iter );
161  }
162 
164  {
165  return m_InstanceIdentifier;
166  }
167 
169  {
170  ++m_Iter;
171  ++m_InstanceIdentifier;
172  return *this;
173  }
174 
175  bool operator!=(const ConstIterator & it)
176  {
177  return ( m_Iter != it.m_Iter );
178  }
179 
180  bool operator==(const ConstIterator & it)
181  {
182  return ( m_Iter == it.m_Iter );
183  }
184 
185 protected:
186  // This method should only be available to the ListSample class
188  typename InternalDataContainerType::const_iterator iter,
189  InstanceIdentifier iid)
190  {
191  m_Iter = iter;
192  m_InstanceIdentifier = iid;
193  }
194 
195 private:
196  ConstIterator() = delete;
197  using InternalIterator = typename InternalDataContainerType::const_iterator;
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 
227  typename InternalDataContainerType::iterator iter,
228  InstanceIdentifier iid):ConstIterator(iter, iid)
229  {}
230 
231 private:
232  // To ensure const-correctness these method must not be in the public API.
233  // The are purposly not implemented, since they should never be called.
234  Iterator() = delete;
235  Iterator(const Self *sample) = delete;
236  Iterator(typename InternalDataContainerType::const_iterator iter, InstanceIdentifier iid) = delete;
237  Iterator(const ConstIterator & it) = delete;
238  ConstIterator & operator=(const ConstIterator & it) = delete;
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(), static_cast<InstanceIdentifier>( 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(), static_cast<InstanceIdentifier>( m_InternalContainer.size() ) );
269 
270  return iter;
271  }
272 
273 protected:
274 
275  ListSample() = default;
276  ~ListSample() override = default;
277  void PrintSelf(std::ostream & os, Indent indent) const override;
278 
279 private:
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
ConstIterator(const ConstIterator &iter)
const MeasurementVectorType & GetMeasurementVector() const
AbsoluteFrequencyType GetFrequency() const
Iterator(typename InternalDataContainerType::iterator iter, InstanceIdentifier iid)
InstanceIdentifier GetInstanceIdentifier() const
ConstIterator & operator=(const ConstIterator &iter)
bool operator==(const ConstIterator &it)
ConstIterator(const ListSample *sample)
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:68
Iterator & operator=(const Iterator &iter)
ConstIterator End() const
ConstIterator(typename InternalDataContainerType::const_iterator iter, InstanceIdentifier iid)
This class is the native implementation of the a Sample with an STL container.
Definition: itkListSample.h:51
A collection of measurements for statistical analysis.
Definition: itkSample.h:61
Control indentation during Print() invocation.
Definition: itkIndent.h:49
bool operator!=(const ConstIterator &it)
Base class for most ITK classes.
Definition: itkObject.h:60
ConstIterator Begin() const
Base class for all data objects in ITK.
InternalDataContainerType m_InternalContainer
typename InternalDataContainerType::const_iterator InternalIterator