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 & operator[](unsigned int i) const { return m_Data[i]; }
159  TPixel & operator[](unsigned int i) { return m_Data[i]; }
163  void
164  set_size(unsigned int n)
165  {
166  if (n != m_ElementCount)
167  {
168  *this = NeighborhoodAllocator();
169  m_Data = make_unique_for_overwrite<TPixel[]>(n);
170  m_ElementCount = n;
171  }
172  }
175  TPixel *
176  data() noexcept
177  {
178  return m_Data.get();
179  }
180 
181  const TPixel *
182  data() const noexcept
183  {
184  return m_Data.get();
185  }
186 
187 private:
188  unsigned int m_ElementCount{ 0 };
189  std::unique_ptr<TPixel[]> m_Data;
190 };
191 
192 template <typename TPixel>
193 inline std::ostream &
194 operator<<(std::ostream & o, const NeighborhoodAllocator<TPixel> & a)
195 {
196  o << "NeighborhoodAllocator { this = " << &a << ", begin = " << static_cast<const void *>(a.begin())
197  << ", size=" << a.size() << " }";
198  return o;
199 }
200 
201 
202 // Equality operator.
203 template <typename TPixel>
204 inline bool
206 {
207  const unsigned int size = lhs.size();
208  return (size == rhs.size()) && ((size == 0) || std::equal(lhs.begin(), lhs.end(), rhs.begin()));
209 }
210 
211 // Inequality operator.
212 template <typename TPixel>
213 inline bool
215 {
216  return !(lhs == rhs);
217 }
218 } // end namespace itk
219 #endif
itk::NeighborhoodAllocator::operator[]
TPixel & operator[](unsigned int i)
Definition: itkNeighborhoodAllocator.h:159
itk::NeighborhoodAllocator::Allocate
void Allocate(unsigned int n)
Definition: itkNeighborhoodAllocator.h:65
itk::NeighborhoodAllocator::m_ElementCount
unsigned int m_ElementCount
Definition: itkNeighborhoodAllocator.h:188
itk::NeighborhoodAllocator::NeighborhoodAllocator
NeighborhoodAllocator(Self &&other) noexcept
Definition: itkNeighborhoodAllocator.h:91
itk::NeighborhoodAllocator::set_size
void set_size(unsigned int n)
Definition: itkNeighborhoodAllocator.h:164
itk::NeighborhoodAllocator::data
const TPixel * data() const noexcept
Definition: itkNeighborhoodAllocator.h:182
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:189
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:176
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:543
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:550
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:158
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