00001 /*========================================================================= 00002 00003 Program: Insight Segmentation & Registration Toolkit 00004 Module: $RCSfile: itkNeighborhoodAllocator.h,v $ 00005 Language: C++ 00006 Date: $Date: 2004/04/07 13:34:55 $ 00007 Version: $Revision: 1.13 $ 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 } 00061 00063 void Deallocate() 00064 { 00065 if (m_Data) delete[] m_Data; 00066 m_ElementCount = 0; 00067 } 00068 00070 NeighborhoodAllocator(const Self& other) : m_ElementCount(0), m_Data(0) 00071 { 00072 this->set_size(other.m_ElementCount); 00073 for (unsigned int i = 0; i < other.m_ElementCount; ++i) 00074 this->operator[](i) = other[i]; 00075 m_ElementCount = other.m_ElementCount; 00076 } 00077 00079 const Self& operator=(const Self& other) 00080 { 00081 this->set_size(other.m_ElementCount); 00082 for (unsigned int i = 0; i < other.m_ElementCount; ++i) 00083 this->operator[](i) = other[i]; 00084 m_ElementCount = other.m_ElementCount; 00085 return *this; 00086 } 00087 00089 bool operator==(const Self& other) const 00090 { 00091 return (m_Data == other.m_Data); 00092 } 00093 00095 bool operator!=(const Self& other) const 00096 { 00097 return (m_Data != other.m_Data); 00098 } 00099 00101 iterator begin() 00102 { return m_Data; } 00103 const_iterator begin() const 00104 { return m_Data; } 00105 iterator end() 00106 { return (m_Data + m_ElementCount); } 00107 const_iterator end() const 00108 { return (m_Data + m_ElementCount); } 00109 unsigned int size() const 00110 { return m_ElementCount; } 00111 00113 const TPixel & operator[](unsigned int i) const 00114 { return m_Data[i]; } 00115 TPixel &operator[](unsigned int i) 00116 { return m_Data[i]; } 00117 00119 void set_size(unsigned int n) 00120 { 00121 if (m_Data) { Deallocate(); } 00122 this->Allocate(n); 00123 } 00124 00125 protected: 00126 unsigned int m_ElementCount; 00127 TPixel *m_Data; 00128 }; 00129 00130 template<class TPixel> 00131 inline std::ostream& operator<<(std::ostream &o, const NeighborhoodAllocator<TPixel> 00132 & a) 00133 { 00134 o << "NeighborhoodAllocator { this = " << &a << ", begin = " 00135 << static_cast<const void *>(a.begin()) 00136 << ", size=" << a.size() 00137 << " }"; 00138 // << ", contents:{ "; 00139 // for (int i = 0; i < a.size(); ++i) o << a[i] << " "; 00140 o << " } }"; 00141 return o; 00142 } 00143 00144 00145 00146 } // end namespace itk 00147 #endif