ITK  6.0.0
Insight Toolkit
itkNeighborhoodAllocator.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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  * https://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 itkNeighborhoodAllocator_h
19 #define itkNeighborhoodAllocator_h
21 #include <algorithm>
22 #include <iostream>
23 #include <memory>
24 #include "itkMacro.h"
25 
26 namespace itk
27 {
43 template <typename TPixel>
45 {
46 public:
49 
54  using iterator = TPixel *;
55  using const_iterator = const TPixel *;
56 
58  NeighborhoodAllocator() = default;
59 
61  ~NeighborhoodAllocator() = default;
62 
64  void
65  Allocate(unsigned int n)
66  {
67  m_Data = make_unique_for_overwrite<TPixel[]>(n);
68  m_ElementCount = n;
69  }
73  void
75  {
76  m_Data.reset();
77  m_ElementCount = 0;
78  }
82  NeighborhoodAllocator(const Self & other)
85  {
86  std::copy_n(other.m_Data.get(), m_ElementCount, m_Data.get());
87  }
88 
89 
91  NeighborhoodAllocator(Self && other) noexcept
92  : m_ElementCount{ other.m_ElementCount }
93  , m_Data{ std::move(other.m_Data) }
94  {
95  other.m_ElementCount = 0;
96  }
101  Self &
102  operator=(const Self & other)
103  {
104  if (this != &other)
105  {
106  this->set_size(other.m_ElementCount);
107  std::copy_n(other.m_Data.get(), m_ElementCount, m_Data.get());
108  }
109  return *this;
110  }
115  Self &
116  operator=(Self && other) noexcept
117  {
118  if (this != &other)
119  {
120  m_ElementCount = other.m_ElementCount;
121  m_Data = std::move(other.m_Data);
122  other.m_ElementCount = 0;
123  }
124  return *this;
125  }
130  iterator
132  {
133  return m_Data.get();
134  }
136  begin() const
137  {
138  return m_Data.get();
139  }
140  iterator
141  end()
142  {
143  return (m_Data.get() + m_ElementCount);
144  }
146  end() const
147  {
148  return (m_Data.get() + m_ElementCount);
149  }
150  unsigned int
151  size() const
152  {
153  return m_ElementCount;
154  }
158  const TPixel &
159  operator[](unsigned int i) const
160  {
161  return m_Data[i];
162  }
163  TPixel &
164  operator[](unsigned int i)
165  {
166  return m_Data[i];
167  }
171  void
172  set_size(unsigned int n)
173  {
174  if (n != m_ElementCount)
175  {
176  *this = NeighborhoodAllocator();
177  m_Data = make_unique_for_overwrite<TPixel[]>(n);
178  m_ElementCount = n;
179  }
180  }
183  TPixel *
184  data() noexcept
185  {
186  return m_Data.get();
187  }
188 
189  const TPixel *
190  data() const noexcept
191  {
192  return m_Data.get();
193  }
194 
195 private:
196  unsigned int m_ElementCount{ 0 };
197  std::unique_ptr<TPixel[]> m_Data;
198 };
199 
200 template <typename TPixel>
201 inline std::ostream &
202 operator<<(std::ostream & o, const NeighborhoodAllocator<TPixel> & a)
203 {
204  o << "NeighborhoodAllocator { this = " << &a << ", begin = " << static_cast<const void *>(a.begin())
205  << ", size=" << a.size() << " }";
206  return o;
207 }
208 
209 
210 // Equality operator.
211 template <typename TPixel>
212 inline bool
214 {
215  const unsigned int size = lhs.size();
216  return (size == rhs.size()) && ((size == 0) || std::equal(lhs.begin(), lhs.end(), rhs.begin()));
217 }
218 
219 // Inequality operator.
220 template <typename TPixel>
221 inline bool
223 {
224  return !(lhs == rhs);
225 }
226 } // end namespace itk
227 #endif
itk::NeighborhoodAllocator::operator[]
TPixel & operator[](unsigned int i)
Definition: itkNeighborhoodAllocator.h:164
itk::NeighborhoodAllocator::Allocate
void Allocate(unsigned int n)
Definition: itkNeighborhoodAllocator.h:65
itk::NeighborhoodAllocator::m_ElementCount
unsigned int m_ElementCount
Definition: itkNeighborhoodAllocator.h:196
itk::NeighborhoodAllocator::NeighborhoodAllocator
NeighborhoodAllocator(Self &&other) noexcept
Definition: itkNeighborhoodAllocator.h:91
itk::NeighborhoodAllocator::set_size
void set_size(unsigned int n)
Definition: itkNeighborhoodAllocator.h:172
itk::NeighborhoodAllocator::data
const TPixel * data() const noexcept
Definition: itkNeighborhoodAllocator.h:190
itk::make_unique_for_overwrite
auto make_unique_for_overwrite(const vcl_size_t numberOfElements)
Definition: itkMakeUniqueForOverwrite.h:61
itk::NeighborhoodAllocator::m_Data
std::unique_ptr< TPixel[]> m_Data
Definition: itkNeighborhoodAllocator.h:197
itk::operator<<
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
itk::NeighborhoodAllocator::begin
const_iterator begin() const
Definition: itkNeighborhoodAllocator.h:136
itk::NeighborhoodAllocator::size
unsigned int size() const
Definition: itkNeighborhoodAllocator.h:151
itk::NeighborhoodAllocator::data
TPixel * data() noexcept
Definition: itkNeighborhoodAllocator.h:184
itk::NeighborhoodAllocator::Deallocate
void Deallocate()
Definition: itkNeighborhoodAllocator.h:74
itk::NeighborhoodAllocator::operator=
Self & operator=(const Self &other)
Definition: itkNeighborhoodAllocator.h:102
itkMacro.h
itk::NeighborhoodAllocator::operator=
Self & operator=(Self &&other) noexcept
Definition: itkNeighborhoodAllocator.h:116
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:552
itk::NeighborhoodAllocator< ImageType ::InternalPixelType * >::const_iterator
const ImageType ::InternalPixelType * * const_iterator
Definition: itkNeighborhoodAllocator.h:55
itk::NeighborhoodAllocator::begin
iterator begin()
Definition: itkNeighborhoodAllocator.h:131
itk::operator!=
bool operator!=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:559
itk::NeighborhoodAllocator::NeighborhoodAllocator
NeighborhoodAllocator()=default
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itkMakeUniqueForOverwrite.h
itk::NeighborhoodAllocator::operator[]
const TPixel & operator[](unsigned int i) const
Definition: itkNeighborhoodAllocator.h:159
itk::NeighborhoodAllocator::end
iterator end()
Definition: itkNeighborhoodAllocator.h:141
itk::NeighborhoodAllocator::end
const_iterator end() const
Definition: itkNeighborhoodAllocator.h:146
AddImageFilter
Definition: itkAddImageFilter.h:81
itk::NeighborhoodAllocator::NeighborhoodAllocator
NeighborhoodAllocator(const Self &other)
Definition: itkNeighborhoodAllocator.h:82
itk::NeighborhoodAllocator::~NeighborhoodAllocator
~NeighborhoodAllocator()=default
itk::NeighborhoodAllocator< ImageType ::InternalPixelType * >::iterator
ImageType ::InternalPixelType * * iterator
Definition: itkNeighborhoodAllocator.h:54
itk::NeighborhoodAllocator
A memory allocator for use as the default allocator type in Neighborhood.
Definition: itkNeighborhoodAllocator.h:44