ITK  4.3.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 
22 namespace itk
23 {
39 template< class TPixel >
41 {
42 public:
45 
50  typedef TPixel * iterator;
51  typedef const TPixel *const_iterator;
52 
55 
58  { this->Deallocate(); }
59 
61  void Allocate(unsigned int n)
62  {
63  m_Data = new TPixel[n];
64  m_ElementCount = n;
65  }
67 
69  void Deallocate()
70  {
71  if ( m_Data ) { delete[] m_Data; }
72  m_ElementCount = 0;
73  }
75 
78  {
79  this->set_size(other.m_ElementCount);
80  for ( unsigned int i = 0; i < other.m_ElementCount; ++i )
81  {
82  this->operator[](i) = other[i];
83  }
85  }
87 
89  const Self & operator=(const Self & other)
90  {
91  this->set_size(other.m_ElementCount);
92  for ( unsigned int i = 0; i < other.m_ElementCount; ++i )
93  {
94  this->operator[](i) = other[i];
95  }
97  return *this;
98  }
100 
102  bool operator==(const Self & other) const
103  {
104  return ( m_Data == other.m_Data );
105  }
106 
108  bool operator!=(const Self & other) const
109  {
110  return ( m_Data != other.m_Data );
111  }
112 
115  { return m_Data; }
117  { return m_Data; }
119  { return ( m_Data + m_ElementCount ); }
121  { return ( m_Data + m_ElementCount ); }
122  unsigned int size() const
123  { return m_ElementCount; }
125 
127  const TPixel & operator[](unsigned int i) const
128  { return m_Data[i]; }
129  TPixel & operator[](unsigned int i)
130  { return m_Data[i]; }
132 
134  void set_size(unsigned int n)
135  {
136  if ( m_Data ) { Deallocate(); }
137  this->Allocate(n);
138  }
140 
141 protected:
142  unsigned int m_ElementCount;
143  TPixel * m_Data;
144 };
145 
146 template< class TPixel >
147 inline std::ostream & operator<<(
148  std::ostream & o, const NeighborhoodAllocator< TPixel >
149  & a)
150 {
151  o << "NeighborhoodAllocator { this = " << &a << ", begin = "
152  << static_cast< const void * >( a.begin() )
153  << ", size=" << a.size()
154  << " }";
155  return o;
156 }
157 } // end namespace itk
158 #endif
159