00001 /*========================================================================= 00002 00003 Program: Insight Segmentation & Registration Toolkit 00004 Module: $RCSfile: itkNeighborhoodAllocator.h,v $ 00005 Language: C++ 00006 Date: $Date: 2009-03-03 15:07:52 $ 00007 Version: $Revision: 1.16 $ 00008 00009 Copyright (c) Insight Software Consortium. All rights reserved. 00010 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. 00011 00012 This software is distributed WITHOUT ANY WARRANTY; without even 00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00014 PURPOSE. See the above copyright notices for more information. 00015 00016 =========================================================================*/ 00017 #ifndef __itkNeighborhoodAllocator_h 00018 #define __itkNeighborhoodAllocator_h 00019 #include <iostream> 00020 00021 namespace itk { 00034 template <class TPixel> 00035 class NeighborhoodAllocator 00036 { 00037 public: 00039 typedef NeighborhoodAllocator Self; 00040 00045 typedef TPixel * iterator; 00046 typedef const TPixel * const_iterator; 00047 00049 NeighborhoodAllocator() : m_ElementCount(0), m_Data(0) {} 00050 00052 ~NeighborhoodAllocator() 00053 { this->Deallocate(); } 00054 00056 void Allocate(unsigned int n) 00057 { 00058 m_Data = new TPixel[n]; 00059 m_ElementCount = n; 00060 } 00062 00064 void Deallocate() 00065 { 00066 if (m_Data) delete[] m_Data; 00067 m_ElementCount = 0; 00068 } 00070 00072 NeighborhoodAllocator(const Self& other) : m_ElementCount(0), m_Data(0) 00073 { 00074 this->set_size(other.m_ElementCount); 00075 for (unsigned int i = 0; i < other.m_ElementCount; ++i) 00076 { 00077 this->operator[](i) = other[i]; 00078 } 00079 m_ElementCount = other.m_ElementCount; 00080 } 00082 00084 const Self& operator=(const Self& other) 00085 { 00086 this->set_size(other.m_ElementCount); 00087 for (unsigned int i = 0; i < other.m_ElementCount; ++i) 00088 { 00089 this->operator[](i) = other[i]; 00090 } 00091 m_ElementCount = other.m_ElementCount; 00092 return *this; 00093 } 00095 00097 bool operator==(const Self& other) const 00098 { 00099 return (m_Data == other.m_Data); 00100 } 00101 00103 bool operator!=(const Self& other) const 00104 { 00105 return (m_Data != other.m_Data); 00106 } 00107 00109 iterator begin() 00110 { return m_Data; } 00111 const_iterator begin() const 00112 { return m_Data; } 00113 iterator end() 00114 { return (m_Data + m_ElementCount); } 00115 const_iterator end() const 00116 { return (m_Data + m_ElementCount); } 00117 unsigned int size() const 00118 { return m_ElementCount; } 00120 00122 const TPixel & operator[](unsigned int i) const 00123 { return m_Data[i]; } 00124 TPixel &operator[](unsigned int i) 00125 { return m_Data[i]; } 00127 00129 void set_size(unsigned int n) 00130 { 00131 if (m_Data) { Deallocate(); } 00132 this->Allocate(n); 00133 } 00135 00136 protected: 00137 unsigned int m_ElementCount; 00138 TPixel *m_Data; 00139 }; 00140 00141 template<class TPixel> 00142 inline std::ostream& operator<<( 00143 std::ostream &o, const NeighborhoodAllocator<TPixel> 00144 & a) 00145 { 00146 o << "NeighborhoodAllocator { this = " << &a << ", begin = " 00147 << static_cast<const void *>(a.begin()) 00148 << ", size=" << a.size() 00149 << " }"; 00150 return o; 00151 } 00152 00153 } // end namespace itk 00154 #endif 00155