ITK  5.0.0
Insight Segmentation and Registration 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 "itkMacro.h"
23 
24 namespace itk
25 {
41 template< typename TPixel >
43 {
44 public:
47 
52  using iterator = TPixel *;
53  using const_iterator = const TPixel *;
54 
57 
60  { this->Deallocate(); }
61 
63  void Allocate(unsigned int n)
64  {
65  m_Data = new TPixel[n];
66  m_ElementCount = n;
67  }
69 
71  void Deallocate()
72  {
73  delete[] m_Data;
74  m_ElementCount = 0;
75  }
77 
79  NeighborhoodAllocator(const Self & other):
81  m_Data(new TPixel[other.m_ElementCount])
82  {
83  std::copy(other.m_Data, other.m_Data + m_ElementCount, m_Data);
84  }
85 
86 
88  NeighborhoodAllocator(Self&& other) ITK_NOEXCEPT
89  :
90  m_ElementCount{ other.m_ElementCount },
91  m_Data{ other.m_Data }
92  {
93  other.m_ElementCount = 0;
94  other.m_Data = nullptr;
95  }
96 
97 
99  Self & operator=(const Self & other)
100  {
101  if(this != &other)
102  {
103  this->set_size(other.m_ElementCount);
104  std::copy(other.m_Data, other.m_Data + m_ElementCount, m_Data);
105  }
106  return *this;
107  }
109 
110 
112  Self& operator=(Self&& other) ITK_NOEXCEPT
113  {
114  if (this != &other)
115  {
116  this->Deallocate();
117  m_ElementCount = other.m_ElementCount;
118  m_Data = other.m_Data;
119  other.m_ElementCount = 0;
120  other.m_Data = nullptr;
121  }
122  return *this;
123  }
125 
126 
129  { return m_Data; }
131  { return m_Data; }
133  { return ( m_Data + m_ElementCount ); }
135  { return ( m_Data + m_ElementCount ); }
136  unsigned int size() const
137  { return m_ElementCount; }
139 
141  const TPixel & operator[](unsigned int i) const
142  { return m_Data[i]; }
143  TPixel & operator[](unsigned int i)
144  { return m_Data[i]; }
146 
148  void set_size(unsigned int n)
149  {
150  if (n != m_ElementCount)
151  {
152  if ( m_Data )
153  {
154  this->Deallocate();
155  }
156  this->Allocate(n);
157  }
158  }
160 
161 protected:
162  unsigned int m_ElementCount{0};
163  TPixel * m_Data;
164 };
165 
166 template< typename TPixel >
167 inline std::ostream & operator<<(
168  std::ostream & o, const NeighborhoodAllocator< TPixel >
169  & a)
170 {
171  o << "NeighborhoodAllocator { this = " << &a << ", begin = "
172  << static_cast< const void * >( a.begin() )
173  << ", size=" << a.size()
174  << " }";
175  return o;
176 }
177 
178 
179 // Equality operator.
180 template< typename TPixel >
181 inline bool operator==(
184 {
185  const unsigned int size = lhs.size();
186  return (size == rhs.size()) &&
187  ((size == 0) || std::equal(lhs.begin(), lhs.end(), rhs.begin()));
188 }
189 
190 // Inequality operator.
191 template< typename TPixel >
192 inline bool operator!=(
195 {
196  return ! (lhs == rhs);
197 }
198 } // end namespace itk
199 #endif
NeighborhoodAllocator(Self &&other) noexcept
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:485
const TPixel & operator[](unsigned int i) const
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:188
A memory allocator for use as the default allocator type in Neighborhood.
Self & operator=(const Self &other)
NeighborhoodAllocator(const Self &other)
bool operator!=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:492
Self & operator=(Self &&other) noexcept
TPixel & operator[](unsigned int i)