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
26namespace itk
27{
43template <typename TPixel>
45{
46public:
49
54 using iterator = TPixel *;
55 using const_iterator = const TPixel *;
56
59
62
64 void
65 Allocate(unsigned int n)
66 {
67 m_Data = make_unique_for_overwrite<TPixel[]>(n);
69 }
73 void
75 {
76 m_Data.reset();
78 }
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 }
132 {
133 return m_Data.get();
134 }
136 begin() const
137 {
138 return m_Data.get();
139 }
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
187private:
188 unsigned int m_ElementCount{ 0 };
189 std::unique_ptr<TPixel[]> m_Data;
190};
191
192template <typename TPixel>
193inline std::ostream &
194operator<<(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.
203template <typename TPixel>
204inline 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.
212template <typename TPixel>
213inline bool
215{
216 return !(lhs == rhs);
217}
218} // end namespace itk
219#endif
Pixel-wise addition of two images.
A memory allocator for use as the default allocator type in Neighborhood.
const TPixel * data() const noexcept
Self & operator=(const Self &other)
NeighborhoodAllocator(Self &&other) noexcept
TPixel & operator[](unsigned int i)
const TPixel & operator[](unsigned int i) const
std::unique_ptr< TPixel[]> m_Data
Self & operator=(Self &&other) noexcept
NeighborhoodAllocator(const Self &other)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:543
bool operator!=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:550
auto make_unique_for_overwrite(const vcl_size_t numberOfElements)