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 } 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 this->operator[](i) = other[i]; 00077 m_ElementCount = other.m_ElementCount; 00078 } 00080 00082 const Self& operator=(const Self& other) 00083 { 00084 this->set_size(other.m_ElementCount); 00085 for (unsigned int i = 0; i < other.m_ElementCount; ++i) 00086 this->operator[](i) = other[i]; 00087 m_ElementCount = other.m_ElementCount; 00088 return *this; 00089 } 00091 00093 bool operator==(const Self& other) const 00094 { 00095 return (m_Data == other.m_Data); 00096 } 00097 00099 bool operator!=(const Self& other) const 00100 { 00101 return (m_Data != other.m_Data); 00102 } 00103 00105 iterator begin() 00106 { return m_Data; } 00107 const_iterator begin() const 00108 { return m_Data; } 00109 iterator end() 00110 { return (m_Data + m_ElementCount); } 00111 const_iterator end() const 00112 { return (m_Data + m_ElementCount); } 00113 unsigned int size() const 00114 { return m_ElementCount; } 00116 00118 const TPixel & operator[](unsigned int i) const 00119 { return m_Data[i]; } 00120 TPixel &operator[](unsigned int i) 00121 { return m_Data[i]; } 00123 00125 void set_size(unsigned int n) 00126 { 00127 if (m_Data) { Deallocate(); } 00128 this->Allocate(n); 00129 } 00131 00132 protected: 00133 unsigned int m_ElementCount; 00134 TPixel *m_Data; 00135 }; 00136 00137 template<class TPixel> 00138 inline std::ostream& operator<<(std::ostream &o, const NeighborhoodAllocator<TPixel> 00139 & a) 00140 { 00141 o << "NeighborhoodAllocator { this = " << &a << ", begin = " 00142 << static_cast<const void *>(a.begin()) 00143 << ", size=" << a.size() 00144 << " }"; 00145 // << ", contents:{ "; 00146 // for (int i = 0; i < a.size(); ++i) o << a[i] << " "; 00147 o << " } }"; 00148 return o; 00149 } 00150 00151 00152 00153 } // end namespace itk 00154 #endif 00155