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 &
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
195private:
196 unsigned int m_ElementCount{ 0 };
197 std::unique_ptr<TPixel[]> m_Data;
198};
199
200template <typename TPixel>
201inline std::ostream &
202operator<<(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.
211template <typename TPixel>
212inline 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.
220template <typename TPixel>
221inline bool
223{
224 return !(lhs == rhs);
225}
226} // end namespace itk
227#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:552
bool operator!=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:559
auto make_unique_for_overwrite(const vcl_size_t numberOfElements)