ITK  5.1.0
Insight Toolkit
itkNeighborhoodAllocator.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 itkNeighborhoodAllocator_h
19 #define itkNeighborhoodAllocator_h
20 #include <algorithm>
21 #include <iostream>
22 #include <memory>
23 #include "itkMacro.h"
24 
25 namespace itk
26 {
42 template <typename TPixel>
44 {
45 public:
48 
53  using iterator = TPixel *;
54  using const_iterator = const TPixel *;
55 
57  NeighborhoodAllocator() = default;
58 
60  ~NeighborhoodAllocator() = default;
61 
63  void
64  Allocate(unsigned int n)
65  {
66  m_Data.reset(new TPixel[n]);
67  m_ElementCount = n;
68  }
70 
72  void
74  {
75  m_Data.reset();
76  m_ElementCount = 0;
77  }
79 
81  NeighborhoodAllocator(const Self & other)
83  , m_Data(new TPixel[other.m_ElementCount])
84  {
85  std::copy_n(other.m_Data.get(), m_ElementCount, m_Data.get());
86  }
87 
88 
90  NeighborhoodAllocator(Self && other) ITK_NOEXCEPT
91  : m_ElementCount{ other.m_ElementCount }
92  , m_Data{ std::move(other.m_Data) }
93  {
94  other.m_ElementCount = 0;
95  }
97 
98 
100  Self &
101  operator=(const Self & other)
102  {
103  if (this != &other)
104  {
105  this->set_size(other.m_ElementCount);
106  std::copy_n(other.m_Data.get(), m_ElementCount, m_Data.get());
107  }
108  return *this;
109  }
111 
112 
114  Self &
115  operator=(Self && other) ITK_NOEXCEPT
116  {
117  if (this != &other)
118  {
119  m_ElementCount = other.m_ElementCount;
120  m_Data = std::move(other.m_Data);
121  other.m_ElementCount = 0;
122  }
123  return *this;
124  }
126 
127 
129  iterator
131  {
132  return m_Data.get();
133  }
135  begin() const
136  {
137  return m_Data.get();
138  }
139  iterator
140  end()
141  {
142  return (m_Data.get() + m_ElementCount);
143  }
145  end() const
146  {
147  return (m_Data.get() + m_ElementCount);
148  }
149  unsigned int
150  size() const
151  {
152  return m_ElementCount;
153  }
155 
157  const TPixel & operator[](unsigned int i) const { return m_Data[i]; }
158  TPixel & operator[](unsigned int i) { return m_Data[i]; }
160 
162  void
163  set_size(unsigned int n)
164  {
165  if (n != m_ElementCount)
166  {
167  *this = NeighborhoodAllocator();
168  m_Data.reset(new TPixel[n]);
169  m_ElementCount = n;
170  }
171  }
173 
174  TPixel *
175  data() ITK_NOEXCEPT
176  {
177  return m_Data.get();
178  }
179 
180  const TPixel *
181  data() const ITK_NOEXCEPT
182  {
183  return m_Data.get();
184  }
185 
186 private:
187  unsigned int m_ElementCount{ 0 };
188  std::unique_ptr<TPixel[]> m_Data;
189 };
190 
191 template <typename TPixel>
192 inline std::ostream &
193 operator<<(std::ostream & o, const NeighborhoodAllocator<TPixel> & a)
194 {
195  o << "NeighborhoodAllocator { this = " << &a << ", begin = " << static_cast<const void *>(a.begin())
196  << ", size=" << a.size() << " }";
197  return o;
198 }
199 
200 
201 // Equality operator.
202 template <typename TPixel>
203 inline bool
205 {
206  const unsigned int size = lhs.size();
207  return (size == rhs.size()) && ((size == 0) || std::equal(lhs.begin(), lhs.end(), rhs.begin()));
208 }
209 
210 // Inequality operator.
211 template <typename TPixel>
212 inline bool
214 {
215  return !(lhs == rhs);
216 }
217 } // end namespace itk
218 #endif
itk::NeighborhoodAllocator::operator[]
TPixel & operator[](unsigned int i)
Definition: itkNeighborhoodAllocator.h:158
itk::NeighborhoodAllocator::Allocate
void Allocate(unsigned int n)
Definition: itkNeighborhoodAllocator.h:64
itk::NeighborhoodAllocator::m_ElementCount
unsigned int m_ElementCount
Definition: itkNeighborhoodAllocator.h:187
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:211
itk::NeighborhoodAllocator::NeighborhoodAllocator
NeighborhoodAllocator(Self &&other) noexcept
Definition: itkNeighborhoodAllocator.h:90
itk::NeighborhoodAllocator::set_size
void set_size(unsigned int n)
Definition: itkNeighborhoodAllocator.h:163
itk::NeighborhoodAllocator::data
const TPixel * data() const noexcept
Definition: itkNeighborhoodAllocator.h:181
itk::NeighborhoodAllocator::m_Data
std::unique_ptr< TPixel[]> m_Data
Definition: itkNeighborhoodAllocator.h:188
itk::NeighborhoodAllocator::Self
NeighborhoodAllocator Self
Definition: itkNeighborhoodAllocator.h:47
itk::NeighborhoodAllocator::begin
const_iterator begin() const
Definition: itkNeighborhoodAllocator.h:135
itk::NeighborhoodAllocator::size
unsigned int size() const
Definition: itkNeighborhoodAllocator.h:150
itk::NeighborhoodAllocator::data
TPixel * data() noexcept
Definition: itkNeighborhoodAllocator.h:175
itk::NeighborhoodAllocator::Deallocate
void Deallocate()
Definition: itkNeighborhoodAllocator.h:73
itk::NeighborhoodAllocator::operator=
Self & operator=(const Self &other)
Definition: itkNeighborhoodAllocator.h:101
itkMacro.h
itk::NeighborhoodAllocator::operator=
Self & operator=(Self &&other) noexcept
Definition: itkNeighborhoodAllocator.h:115
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:519
itk::NeighborhoodAllocator< ImageType ::InternalPixelType * >::const_iterator
const ImageType ::InternalPixelType * * const_iterator
Definition: itkNeighborhoodAllocator.h:54
itk::NeighborhoodAllocator::begin
iterator begin()
Definition: itkNeighborhoodAllocator.h:130
itk::operator!=
bool operator!=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:526
itk::NeighborhoodAllocator::NeighborhoodAllocator
NeighborhoodAllocator()=default
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itk::NeighborhoodAllocator::operator[]
const TPixel & operator[](unsigned int i) const
Definition: itkNeighborhoodAllocator.h:157
itk::NeighborhoodAllocator::end
iterator end()
Definition: itkNeighborhoodAllocator.h:140
itk::NeighborhoodAllocator::end
const_iterator end() const
Definition: itkNeighborhoodAllocator.h:145
itk::NeighborhoodAllocator::NeighborhoodAllocator
NeighborhoodAllocator(const Self &other)
Definition: itkNeighborhoodAllocator.h:81
itk::NeighborhoodAllocator::~NeighborhoodAllocator
~NeighborhoodAllocator()=default
itk::NeighborhoodAllocator< ImageType ::InternalPixelType * >::iterator
ImageType ::InternalPixelType * * iterator
Definition: itkNeighborhoodAllocator.h:53
itk::NeighborhoodAllocator
A memory allocator for use as the default allocator type in Neighborhood.
Definition: itkNeighborhoodAllocator.h:43