5#ifndef DUNE_ALBERTAGRID_INDEXSTACK_HH
6#define DUNE_ALBERTAGRID_INDEXSTACK_HH
11#include <dune/common/exceptions.hh>
12#include <dune/common/reservedvector.hh>
24 template <
class T,
int length>
27 class MyFiniteStack :
public ReservedVector<T,length>
29 typedef ReservedVector<T,length> BaseType ;
32 bool full ()
const {
return this->
size() >= length; }
35 void push(
const T& t ) { BaseType :: push_back( t ); }
40 assert( !this->empty() );
41 assert( this->
size() <= length );
51 typedef MyFiniteStack StackType;
52 typedef typename std::stack < StackType * > StackListType;
54 StackListType fullStackList_;
55 StackListType emptyStackList_;
70 inline void checkAndSetMax(T index) {
if(index > maxIndex_) maxIndex_ = index;}
100 IndexStack<T,length> & operator = (
const IndexStack<T,length> & s)
102 DUNE_THROW(Exception,
"IndexStack::operator = () not allowed!");
114 template <
class T,
int length>
116 : stack_ ( new StackType () ) , maxIndex_ (0) {}
118 template <
class T,
int length>
121 if(stack_)
delete stack_;
124 while( !fullStackList_.empty() )
126 StackType * st = fullStackList_.top();
128 fullStackList_.pop();
130 while( !emptyStackList_.empty() )
132 StackType * st = emptyStackList_.top();
134 emptyStackList_.pop();
138 template <
class T,
int length>
141 if((*stack_).empty())
143 if( fullStackList_.size() <= 0)
149 emptyStackList_.push( stack_ );
150 stack_ = fullStackList_.top();
151 fullStackList_.pop();
154 return (*stack_).topAndPop();
157 template <
class T,
int length>
162 fullStackList_.push( stack_ );
163 if(emptyStackList_.size() <= 0)
165 stack_ =
new StackType ();
169 stack_ = emptyStackList_.top();
170 emptyStackList_.pop();
173 (*stack_).push(index);
176 template <
class T,
int length>
181 for(
int i=0; i<2*length; i++)
184 for(
int i=0; i<2*length; i++)
187 for(
int i=0; i<2*length; i++)
190 for(
int i=0; i<2*length; i++)
191 printf(
" index [%d] = %d \n",i,vec[i]);
194 template <
class T,
int length>
198 os.write( ((
const char *) &maxIndex_ ),
sizeof(
int) ) ;
202 template <
class T,
int length>
205 is.read ( ((
char *) &maxIndex_),
sizeof(
int) );
211 template <
class T,
int length>
217 stack_ =
new StackType();
221 while( !fullStackList_.empty() )
223 StackType * st = fullStackList_.top();
225 fullStackList_.pop();
Include standard header files.
Definition: agrid.hh:60
Definition: indexstack.hh:26
void restoreIndexSet(std::istream &is)
Definition: indexstack.hh:203
void checkAndSetMax(T index)
set index as maxIndex if index is bigger than maxIndex
Definition: indexstack.hh:70
void backupIndexSet(std::ostream &os)
Definition: indexstack.hh:195
~IndexStack()
Destructor, deleting all stacks.
Definition: indexstack.hh:119
T getIndex()
restore index from stack or create new index
Definition: indexstack.hh:139
IndexStack()
Constructor, create new IndexStack.
Definition: indexstack.hh:115
void setMaxIndex(T index)
set index as maxIndex
Definition: indexstack.hh:73
int size() const
return maxIndex which is also the
Definition: indexstack.hh:79
void test()
test stack functionality
Definition: indexstack.hh:177
void freeIndex(T index)
store index on stack
Definition: indexstack.hh:158
int getMaxIndex() const
return maxIndex which is also the
Definition: indexstack.hh:76