KWStyle - itkNeighborhoodAllocator.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkNeighborhoodAllocator.h.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:42 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12      This software is distributed WITHOUT ANY WARRANTY; without even 
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14      PURPOSE.  See the above copyright notices for more information.
15
16 =========================================================================*/
17 #ifndef __itkNeighborhoodAllocator_h
18 #define __itkNeighborhoodAllocator_h
19 #include <iostream>
20
21 namespace itk {
22 /** \class NeighborhoodAllocator
23  * 
24  * This is a memory allocator for use as the default allocator type in
25  * Neighborhood.  The API is designed to mimic that of vnl_vector so that
26  * vnl_vector can also be used as an allocator for Neighborhood.
27  *
28  * The decision to create this allocator with the vnl_vector api (versus
29  * using an STL allocator and wrapping the vnl_vector API) was made because
30  * the STL allocator API is not guaranteed stable at this time.
31  *
32  * \ingroup Operators
33  */
34 template <class TPixel>
35 class NeighborhoodAllocator
36 {
37 public:
38   /** Standard class typedefs. */
39   typedef NeighborhoodAllocator Self;
40   
41   /** Iterator support. Note that the naming of the typedefs is on purpose.
42 IND *** itk::Neighborhood makes reference to the allocator, which because it may
43 IND *** be vnl or other type, uses the lower case/underscore forms iterator and
44 IND *** const_iterator. */
45 TDR   typedef TPixel * iterator;
46 TDA,TDR   typedef const TPixel * const_iterator;
47   
48   /** Default constructor */
49   NeighborhoodAllocator() : m_ElementCount(0), m_Data(0)  {}
50
51   /** Default destructor */
52   ~NeighborhoodAllocator()
53     { this->Deallocate(); }
54
55   /** Allocates memory using new() */
56   void Allocate(unsigned int n)
57 IND **{
58     m_Data = new TPixel[n];
59     m_ElementCount = n;    
60 IND **}
61
62   /** Deallocates memory using delete[](). */
63   void Deallocate()
64 IND **{
65     if (m_Data) delete[] m_Data;
66     m_ElementCount = 0;
67 IND **}
68
69   /** Copy constructor. */
70   NeighborhoodAllocator(const Self& other) : m_ElementCount(0), m_Data(0)
71 IND **{
72     this->set_size(other.m_ElementCount);
73     for (unsigned int i = 0; i < other.m_ElementCount; ++i)
74       this->operator[](i) = other[i];
75     m_ElementCount = other.m_ElementCount;
76 IND **}
77
78   /** Assignment operator. */
79   const Self& operator=(const Self& other)
80 IND **{
81     this->set_size(other.m_ElementCount);
82     for (unsigned int i = 0; i < other.m_ElementCount; ++i)
83       this->operator[](i) = other[i];
84     m_ElementCount = other.m_ElementCount;
85     return *this;
86 IND **}
87
88   /** Comparison operator. */
89   bool operator==(const Self& other) const
90 IND **{
91     return (m_Data == other.m_Data);
92 IND **}
93
94   /** Not Equal operator. */
95   bool operator!=(const Self& other) const
96 IND **{
97     return (m_Data != other.m_Data);
98 IND **}
99
100   /** STL-style iterator support for the memory buffer. */
101   iterator begin()
102     { return m_Data; }
103   const_iterator begin() const
104     { return m_Data; }
105   iterator end()
106     { return (m_Data + m_ElementCount); }
107   const_iterator end() const
108     { return (m_Data + m_ElementCount); }
109   unsigned int size() const
110     { return m_ElementCount; }
111     
112   /** Data access methods */
113   const TPixel & operator[](unsigned int i) const
114     { return m_Data[i]; }
115   TPixel &operator[](unsigned int i)
116     { return m_Data[i]; }
117   
118   /** Allocates or Reallocates a buffer of size n */
119   void set_size(unsigned int n)
120     {
121     if (m_Data) { Deallocate(); }
122     this->Allocate(n);
123     }
124
125 protected:
126   unsigned int m_ElementCount;
127   TPixel *m_Data;
128 };
129
130 template<class TPixel>
131 LEN inline std::ostream& operator<<(std::ostream &o, const NeighborhoodAllocator<TPixel>
132                                 & a)
133 {
134 IND ****o << "NeighborhoodAllocator { this = " << &a << ", begin = "
135       << static_cast<const void *>(a.begin())
136 IND ******<< ", size=" << a.size()
137 IND ******<< " }";
138 IND ******//      << ", contents:{ ";
139 IND ******//    for (int i = 0; i < a.size(); ++i) o << a[i] << " ";
140 IND ****o << " } }";
141 IND **return o;
142 }
143   
144
145
146 // end namespace itk
147 #endif
148

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:27PM
© Kitware Inc.