ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkNeighborhoodAllocator.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkNeighborhoodAllocator_h
00019 #define __itkNeighborhoodAllocator_h
00020 #include <iostream>
00021 
00022 namespace itk
00023 {
00039 template< class TPixel >
00040 class NeighborhoodAllocator
00041 {
00042 public:
00044   typedef NeighborhoodAllocator Self;
00045 
00050   typedef TPixel *      iterator;
00051   typedef const TPixel *const_iterator;
00052 
00054   NeighborhoodAllocator():m_ElementCount(0), m_Data(0)  {}
00055 
00057   ~NeighborhoodAllocator()
00058   { this->Deallocate(); }
00059 
00061   void Allocate(unsigned int n)
00062   {
00063     m_Data = new TPixel[n];
00064     m_ElementCount = n;
00065   }
00067 
00069   void Deallocate()
00070   {
00071     if ( m_Data ) { delete[] m_Data; }
00072     m_ElementCount = 0;
00073   }
00075 
00077   NeighborhoodAllocator(const Self & other):m_ElementCount(0), m_Data(0)
00078   {
00079     this->set_size(other.m_ElementCount);
00080     for ( unsigned int i = 0; i < other.m_ElementCount; ++i )
00081       {
00082       this->operator[](i) = other[i];
00083       }
00084     m_ElementCount = other.m_ElementCount;
00085   }
00087 
00089   const Self & operator=(const Self & other)
00090   {
00091     this->set_size(other.m_ElementCount);
00092     for ( unsigned int i = 0; i < other.m_ElementCount; ++i )
00093       {
00094       this->operator[](i) = other[i];
00095       }
00096     m_ElementCount = other.m_ElementCount;
00097     return *this;
00098   }
00100 
00102   bool operator==(const Self & other) const
00103   {
00104     return ( m_Data == other.m_Data );
00105   }
00106 
00108   bool operator!=(const Self & other) const
00109   {
00110     return ( m_Data != other.m_Data );
00111   }
00112 
00114   iterator begin()
00115   { return m_Data; }
00116   const_iterator begin() const
00117   { return m_Data; }
00118   iterator end()
00119   { return ( m_Data + m_ElementCount ); }
00120   const_iterator end() const
00121   { return ( m_Data + m_ElementCount ); }
00122   unsigned int size() const
00123   { return m_ElementCount; }
00125 
00127   const TPixel & operator[](unsigned int i) const
00128   { return m_Data[i]; }
00129   TPixel & operator[](unsigned int i)
00130   { return m_Data[i]; }
00132 
00134   void set_size(unsigned int n)
00135   {
00136     if ( m_Data ) { Deallocate(); }
00137     this->Allocate(n);
00138   }
00140 
00141 protected:
00142   unsigned int m_ElementCount;
00143   TPixel *     m_Data;
00144 };
00145 
00146 template< class TPixel >
00147 inline std::ostream & operator<<(
00148   std::ostream & o, const NeighborhoodAllocator< TPixel >
00149   & a)
00150 {
00151   o << "NeighborhoodAllocator { this = " << &a << ", begin = "
00152   << static_cast< const void * >( a.begin() )
00153   << ", size=" << a.size()
00154   << " }";
00155   return o;
00156 }
00157 } // end namespace itk
00158 #endif
00159