00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00139
00140 o << " } }";
00141 return o;
00142 }
00143
00144
00145
00146 }
00147 #endif