Dune Core Modules (2.5.0)

indexstack.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_ALBERTAGRID_INDEXSTACK_HH
4#define DUNE_ALBERTAGRID_INDEXSTACK_HH
5
6#include <assert.h>
7#include <stack>
8
11
18namespace Dune {
19
22 template <class T, int length>
24 {
25 class MyFiniteStack : public ReservedVector<T,length>
26 {
27 typedef ReservedVector<T,length> BaseType ;
28 public:
30 bool full () const { return this->size() >= length; }
31
33 void push( const T& t ) { BaseType :: push_back( t ); }
34
36 T topAndPop ()
37 {
38 assert( !this->empty() );
39 assert( this->size() <= length );
40 // This code is not slower than using the array structure directly.
41 // The compiler removes the temporary completely. I measured this.
42 // See the commit message for revision 7837 for more details.
43 T tmp = this->back();
44 this->pop_back();
45 return tmp;
46 }
47 };
48
49 typedef MyFiniteStack StackType;
50 typedef typename std::stack < StackType * > StackListType;
51
52 StackListType fullStackList_;
53 StackListType emptyStackList_;
54
55 //typedef typename StackListType::Iterator DListIteratorType;
56 StackType * stack_;
57
58 // current maxIndex
59 int maxIndex_;
60 public:
62 inline IndexStack();
63
65 inline ~IndexStack ();
66
68 inline void checkAndSetMax(T index) { if(index > maxIndex_) maxIndex_ = index;}
69
71 inline void setMaxIndex(T index) { maxIndex_ = index; }
72
74 inline int getMaxIndex() const { return maxIndex_; }
75
77 inline int size() const { return getMaxIndex(); }
78
80 inline T getIndex ();
81
83 inline void freeIndex(T index);
84
86 inline void test ();
87
88 // backup set to out stream
89 inline void backupIndexSet ( std::ostream & os );
90
91 // restore from in stream
92 inline void restoreIndexSet ( std::istream & is );
93 private:
94 // no copy constructor allowed
95 IndexStack( const IndexStack<T,length> & s) : maxIndex_ (0) , stack_(0) {}
96
97 // no assignment operator allowed
98 IndexStack<T,length> & operator = ( const IndexStack<T,length> & s)
99 {
100 DUNE_THROW(Exception, "IndexStack::operator = () not allowed!");
101 return *this;
102 }
103
104 // clear fullStacks
105 void clearStack ();
106
107 }; // end class IndexStack
108
109 //****************************************************************
110 // Inline implementation
111 // ***************************************************************
112 template <class T, int length>
114 : stack_ ( new StackType () ) , maxIndex_ (0) {}
115
116 template <class T, int length>
118 {
119 if(stack_) delete stack_;
120 stack_ = 0;
121
122 while( !fullStackList_.empty() )
123 {
124 StackType * st = fullStackList_.top();
125 if(st) delete st;
126 fullStackList_.pop();
127 }
128 while( !emptyStackList_.empty() )
129 {
130 StackType * st = emptyStackList_.top();
131 if(st) delete st;
132 emptyStackList_.pop();
133 }
134 }
135
136 template <class T, int length>
138 {
139 if((*stack_).empty())
140 {
141 if( fullStackList_.size() <= 0)
142 {
143 return maxIndex_++;
144 }
145 else
146 {
147 emptyStackList_.push( stack_ );
148 stack_ = fullStackList_.top();
149 fullStackList_.pop();
150 }
151 }
152 return (*stack_).topAndPop();
153 }
154
155 template <class T, int length>
156 inline void IndexStack<T,length>::freeIndex ( T index )
157 {
158 if((*stack_).full())
159 {
160 fullStackList_.push( stack_ );
161 if(emptyStackList_.size() <= 0)
162 {
163 stack_ = new StackType ();
164 }
165 else
166 {
167 stack_ = emptyStackList_.top();
168 emptyStackList_.pop();
169 }
170 }
171 (*stack_).push(index);
172 }
173
174 template <class T, int length>
176 {
177 T vec[2*length];
178
179 for(int i=0; i<2*length; i++)
180 vec[i] = getIndex();
181
182 for(int i=0; i<2*length; i++)
183 freeIndex(vec[i]);
184
185 for(int i=0; i<2*length; i++)
186 vec[i] = getIndex();
187
188 for(int i=0; i<2*length; i++)
189 printf(" index [%d] = %d \n",i,vec[i]);
190 }
191
192 template <class T, int length>
193 inline void IndexStack<T,length>::backupIndexSet ( std::ostream & os )
194 {
195 // holes are not stored at the moment
196 os.write( ((const char *) &maxIndex_ ), sizeof(int) ) ;
197 return ;
198 }
199
200 template <class T, int length>
201 inline void IndexStack<T,length>::restoreIndexSet ( std::istream & is )
202 {
203 is.read ( ((char *) &maxIndex_), sizeof(int) );
204 clearStack ();
205
206 return ;
207 }
208
209 template <class T, int length>
210 inline void IndexStack<T,length>::clearStack ()
211 {
212 if(stack_)
213 {
214 delete stack_;
215 stack_ = new StackType();
216 assert(stack_);
217 }
218
219 while( !fullStackList_.empty() )
220 {
221 StackType * st = fullStackList_.top();
222 if(st) delete st;
223 fullStackList_.pop();
224 }
225 return;
226 }
227
228} // end namespace Dune
229#endif
Definition: indexstack.hh:24
void checkAndSetMax(T index)
set index as maxIndex if index is bigger than maxIndex
Definition: indexstack.hh:68
~IndexStack()
Destructor, deleting all stacks.
Definition: indexstack.hh:117
T getIndex()
restore index from stack or create new index
Definition: indexstack.hh:137
IndexStack()
Constructor, create new IndexStack.
Definition: indexstack.hh:113
void setMaxIndex(T index)
set index as maxIndex
Definition: indexstack.hh:71
int size() const
return maxIndex which is also the
Definition: indexstack.hh:77
void test()
test stack functionality
Definition: indexstack.hh:175
void freeIndex(T index)
store index on stack
Definition: indexstack.hh:156
int getMaxIndex() const
return maxIndex which is also the
Definition: indexstack.hh:74
A Vector class with statically reserved memory.
Definition: reservedvector.hh:40
void push_back(const T &t)
Appends an element to the end of a vector, up to the maximum size n, O(1) time.
Definition: reservedvector.hh:94
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Dune namespace.
Definition: alignment.hh:11
An stl-compliant random-access container which stores everything on the stack.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)