ITK  4.8.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 <iostream>
21 #include "itkMacro.h"
22 
23 namespace itk
24 {
40 template< typename TPixel >
42 {
43 public:
46 
51  typedef TPixel * iterator;
52  typedef const TPixel * const_iterator;
53 
56 
59  { this->Deallocate(); }
60 
62  void Allocate(unsigned int n)
63  {
64  m_Data = new TPixel[n];
65  m_ElementCount = n;
66  }
68 
70  void Deallocate()
71  {
72  delete[] m_Data;
73  m_ElementCount = 0;
74  }
76 
79  {
80  this->set_size(other.m_ElementCount);
81  for ( unsigned int i = 0; i < other.m_ElementCount; ++i )
82  {
83  this->operator[](i) = other[i];
84  }
86  }
88 
90  const Self & operator=(const Self & other)
91  {
92  if(this != &other)
93  {
94  this->set_size(other.m_ElementCount);
95  for ( unsigned int i = 0; i < other.m_ElementCount; ++i )
96  {
97  this->operator[](i) = other[i];
98  }
100  }
101  return *this;
102  }
104 
106  bool operator==(const Self & other) const
107  {
108  return ( m_Data == other.m_Data );
109  }
110 
112  bool operator!=(const Self & other) const
113  {
114  return ( m_Data != other.m_Data );
115  }
116 
119  { return m_Data; }
121  { return m_Data; }
123  { return ( m_Data + m_ElementCount ); }
125  { return ( m_Data + m_ElementCount ); }
126  unsigned int size() const
127  { return m_ElementCount; }
129 
131  const TPixel & operator[](unsigned int i) const
132  { return m_Data[i]; }
133  TPixel & operator[](unsigned int i)
134  { return m_Data[i]; }
136 
138  void set_size(unsigned int n)
139  {
140  if ( m_Data ) { Deallocate(); }
141  this->Allocate(n);
142  }
144 
145 protected:
146  unsigned int m_ElementCount;
147  TPixel * m_Data;
148 };
149 
150 template< typename TPixel >
151 inline std::ostream & operator<<(
152  std::ostream & o, const NeighborhoodAllocator< TPixel >
153  & a)
154 {
155  o << "NeighborhoodAllocator { this = " << &a << ", begin = "
156  << static_cast< const void * >( a.begin() )
157  << ", size=" << a.size()
158  << " }";
159  return o;
160 }
161 } // end namespace itk
162 #endif
const TPixel & operator[](unsigned int i) const
bool operator==(const Self &other) const
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:184
A memory allocator for use as the default allocator type in Neighborhood.
const Self & operator=(const Self &other)
NeighborhoodAllocator(const Self &other)
TPixel & operator[](unsigned int i)
bool operator!=(const Self &other) const