indexstack.hh
Go to the documentation of this file.00001 #ifndef DUNE_INDEXSTACK_HH
00002 #define DUNE_INDEXSTACK_HH
00003
00004 #include <assert.h>
00005 #include <stack>
00006
00007 #include <dune/common/stack.hh>
00008
00015 namespace Dune {
00016
00019 template <class T, int length>
00020 class IndexStack
00021 {
00022 typedef typename Dune::FiniteStack<T,length> StackType;
00023 typedef typename std::stack < StackType * > StackListType;
00024
00025 StackListType fullStackList_;
00026 StackListType emptyStackList_;
00027
00028
00029 StackType * stack_;
00030
00031
00032 int maxIndex_;
00033 public:
00035 inline IndexStack();
00036
00038 inline ~IndexStack ();
00039
00041 inline void checkAndSetMax(T index) { if(index > maxIndex_) maxIndex_ = index; }
00042
00044 inline void setMaxIndex(T index) { maxIndex_ = index; }
00045
00047 inline int getMaxIndex() const { return maxIndex_; }
00048
00050 inline int size() const { return getMaxIndex(); }
00051
00053 inline T getIndex ();
00054
00056 inline void freeIndex(T index);
00057
00059 inline void test ();
00060
00061
00062 inline void backupIndexSet ( std::ostream & os );
00063
00064
00065 inline void restoreIndexSet ( std::istream & is );
00066 private:
00067
00068 IndexStack( const IndexStack<T,length> & s) : maxIndex_ (0) , stack_(0) {}
00069
00070
00071 IndexStack<T,length> & operator = ( const IndexStack<T,length> & s)
00072 {
00073 DUNE_THROW(Exception, "IndexStack::operator = () not allowed!");
00074 return *this;
00075 }
00076
00077
00078 void clearStack ();
00079
00080 };
00081
00082
00083
00084
00085 template <class T, int length>
00086 inline IndexStack<T,length>::IndexStack()
00087 : stack_ ( new StackType () ) , maxIndex_ (0) {}
00088
00089 template <class T, int length>
00090 inline IndexStack<T,length>::~IndexStack ()
00091 {
00092 if(stack_) delete stack_;
00093 stack_ = 0;
00094
00095 while( !fullStackList_.empty() )
00096 {
00097 StackType * st = fullStackList_.top();
00098 if(st) delete st;
00099 fullStackList_.pop();
00100 }
00101 while( !emptyStackList_.empty() )
00102 {
00103 StackType * st = emptyStackList_.top();
00104 if(st) delete st;
00105 emptyStackList_.pop();
00106 }
00107 }
00108
00109 template <class T, int length>
00110 inline T IndexStack<T,length>::getIndex ()
00111 {
00112 if((*stack_).empty())
00113 {
00114 if( fullStackList_.size() <= 0)
00115 {
00116 return maxIndex_++;
00117 }
00118 else
00119 {
00120 emptyStackList_.push( stack_ );
00121 stack_ = fullStackList_.top();
00122 fullStackList_.pop();
00123 }
00124 }
00125 return (*stack_).pop();
00126 }
00127
00128 template <class T, int length>
00129 inline void IndexStack<T,length>::freeIndex ( T index )
00130 {
00131 if((*stack_).full())
00132 {
00133 fullStackList_.push( stack_ );
00134 if(emptyStackList_.size() <= 0)
00135 {
00136 stack_ = new StackType ();
00137 }
00138 else
00139 {
00140 stack_ = emptyStackList_.top();
00141 emptyStackList_.pop();
00142 }
00143 }
00144 (*stack_).push(index);
00145 }
00146
00147 template <class T, int length>
00148 inline void IndexStack<T,length>::test ()
00149 {
00150 T vec[2*length];
00151
00152 for(int i=0; i<2*length; i++)
00153 vec[i] = getIndex();
00154
00155 for(int i=0; i<2*length; i++)
00156 freeIndex(vec[i]);
00157
00158 for(int i=0; i<2*length; i++)
00159 vec[i] = getIndex();
00160
00161 for(int i=0; i<2*length; i++)
00162 printf(" index [%d] = %d \n",i,vec[i]);
00163 }
00164
00165 template <class T, int length>
00166 inline void IndexStack<T,length>::backupIndexSet ( std::ostream & os )
00167 {
00168
00169 os.write( ((const char *) &maxIndex_ ), sizeof(int) ) ;
00170 return ;
00171 }
00172
00173 template <class T, int length>
00174 inline void IndexStack<T,length>::restoreIndexSet ( std::istream & is )
00175 {
00176 is.read ( ((char *) &maxIndex_), sizeof(int) );
00177 clearStack ();
00178
00179 return ;
00180 }
00181
00182 template <class T, int length>
00183 inline void IndexStack<T,length>::clearStack ()
00184 {
00185 if(stack_)
00186 {
00187 delete stack_;
00188 stack_ = new StackType();
00189 assert(stack_);
00190 }
00191
00192 while( !fullStackList_.empty() )
00193 {
00194 StackType * st = fullStackList_.top();
00195 if(st) delete st;
00196 fullStackList_.pop();
00197 }
00198 return;
00199 }
00200
00201 }
00202 #endif