ITK  4.4.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  if(this != &other)
92  {
93  this->set_size(other.m_ElementCount);
94  for ( unsigned int i = 0; i < other.m_ElementCount; ++i )
95  {
96  this->operator[](i) = other[i];
97  }
99  }
100  return *this;
101  }
103 
105  bool operator==(const Self & other) const
106  {
107  return ( m_Data == other.m_Data );
108  }
109 
111  bool operator!=(const Self & other) const
112  {
113  return ( m_Data != other.m_Data );
114  }
115 
118  { return m_Data; }
120  { return m_Data; }
122  { return ( m_Data + m_ElementCount ); }
124  { return ( m_Data + m_ElementCount ); }
125  unsigned int size() const
126  { return m_ElementCount; }
128 
130  const TPixel & operator[](unsigned int i) const
131  { return m_Data[i]; }
132  TPixel & operator[](unsigned int i)
133  { return m_Data[i]; }
135 
137  void set_size(unsigned int n)
138  {
139  if ( m_Data ) { Deallocate(); }
140  this->Allocate(n);
141  }
143 
144 protected:
145  unsigned int m_ElementCount;
146  TPixel * m_Data;
147 };
148 
149 template< class TPixel >
150 inline std::ostream & operator<<(
151  std::ostream & o, const NeighborhoodAllocator< TPixel >
152  & a)
153 {
154  o << "NeighborhoodAllocator { this = " << &a << ", begin = "
155  << static_cast< const void * >( a.begin() )
156  << ", size=" << a.size()
157  << " }";
158  return o;
159 }
160 } // end namespace itk
161 #endif
162