Dune Core Modules (2.9.0)

indexstack.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4// vi: set et ts=4 sw=2 sts=2:
5#ifndef DUNE_ALBERTAGRID_INDEXSTACK_HH
6#define DUNE_ALBERTAGRID_INDEXSTACK_HH
7
8#include <assert.h>
9#include <stack>
10
13
20namespace Dune {
21
24 template <class T, int length>
26 {
27 class MyFiniteStack : public ReservedVector<T,length>
28 {
29 typedef ReservedVector<T,length> BaseType ;
30 public:
32 bool full () const { return this->size() >= length; }
33
35 void push( const T& t ) { BaseType :: push_back( t ); }
36
38 T topAndPop ()
39 {
40 assert( !this->empty() );
41 assert( this->size() <= length );
42 // This code is not slower than using the array structure directly.
43 // The compiler removes the temporary completely. I measured this.
44 // See the commit message for revision 7837 for more details.
45 T tmp = this->back();
46 this->pop_back();
47 return tmp;
48 }
49 };
50
51 typedef MyFiniteStack StackType;
52 typedef typename std::stack < StackType * > StackListType;
53
54 StackListType fullStackList_;
55 StackListType emptyStackList_;
56
57 //typedef typename StackListType::Iterator DListIteratorType;
58 StackType * stack_;
59
60 // current maxIndex
61 int maxIndex_;
62 public:
64 inline IndexStack();
65
67 inline ~IndexStack ();
68
70 inline void checkAndSetMax(T index) { if(index > maxIndex_) maxIndex_ = index;}
71
73 inline void setMaxIndex(T index) { maxIndex_ = index; }
74
76 inline int getMaxIndex() const { return maxIndex_; }
77
79 inline int size() const { return getMaxIndex(); }
80
82 inline T getIndex ();
83
85 inline void freeIndex(T index);
86
88 inline void test ();
89
90 // backup set to out stream
91 inline void backupIndexSet ( std::ostream & os );
92
93 // restore from in stream
94 inline void restoreIndexSet ( std::istream & is );
95 private:
96 // no copy constructor allowed
97 IndexStack( const IndexStack<T,length> & s) : maxIndex_ (0) , stack_(0) {}
98
99 // no assignment operator allowed
100 IndexStack<T,length> & operator = ( const IndexStack<T,length> & s)
101 {
102 DUNE_THROW(Exception, "IndexStack::operator = () not allowed!");
103 return *this;
104 }
105
106 // clear fullStacks
107 void clearStack ();
108
109 }; // end class IndexStack
110
111 //****************************************************************
112 // Inline implementation
113 // ***************************************************************
114 template <class T, int length>
116 : stack_ ( new StackType () ) , maxIndex_ (0) {}
117
118 template <class T, int length>
120 {
121 if(stack_) delete stack_;
122 stack_ = 0;
123
124 while( !fullStackList_.empty() )
125 {
126 StackType * st = fullStackList_.top();
127 if(st) delete st;
128 fullStackList_.pop();
129 }
130 while( !emptyStackList_.empty() )
131 {
132 StackType * st = emptyStackList_.top();
133 if(st) delete st;
134 emptyStackList_.pop();
135 }
136 }
137
138 template <class T, int length>
140 {
141 if((*stack_).empty())
142 {
143 if( fullStackList_.size() <= 0)
144 {
145 return maxIndex_++;
146 }
147 else
148 {
149 emptyStackList_.push( stack_ );
150 stack_ = fullStackList_.top();
151 fullStackList_.pop();
152 }
153 }
154 return (*stack_).topAndPop();
155 }
156
157 template <class T, int length>
158 inline void IndexStack<T,length>::freeIndex ( T index )
159 {
160 if((*stack_).full())
161 {
162 fullStackList_.push( stack_ );
163 if(emptyStackList_.size() <= 0)
164 {
165 stack_ = new StackType ();
166 }
167 else
168 {
169 stack_ = emptyStackList_.top();
170 emptyStackList_.pop();
171 }
172 }
173 (*stack_).push(index);
174 }
175
176 template <class T, int length>
178 {
179 T vec[2*length];
180
181 for(int i=0; i<2*length; i++)
182 vec[i] = getIndex();
183
184 for(int i=0; i<2*length; i++)
185 freeIndex(vec[i]);
186
187 for(int i=0; i<2*length; i++)
188 vec[i] = getIndex();
189
190 for(int i=0; i<2*length; i++)
191 printf(" index [%d] = %d \n",i,vec[i]);
192 }
193
194 template <class T, int length>
195 inline void IndexStack<T,length>::backupIndexSet ( std::ostream & os )
196 {
197 // holes are not stored at the moment
198 os.write( ((const char *) &maxIndex_ ), sizeof(int) ) ;
199 return ;
200 }
201
202 template <class T, int length>
203 inline void IndexStack<T,length>::restoreIndexSet ( std::istream & is )
204 {
205 is.read ( ((char *) &maxIndex_), sizeof(int) );
206 clearStack ();
207
208 return ;
209 }
210
211 template <class T, int length>
212 inline void IndexStack<T,length>::clearStack ()
213 {
214 if(stack_)
215 {
216 delete stack_;
217 stack_ = new StackType();
218 assert(stack_);
219 }
220
221 while( !fullStackList_.empty() )
222 {
223 StackType * st = fullStackList_.top();
224 if(st) delete st;
225 fullStackList_.pop();
226 }
227 return;
228 }
229
230} // end namespace Dune
231#endif
Definition: indexstack.hh:26
void checkAndSetMax(T index)
set index as maxIndex if index is bigger than maxIndex
Definition: indexstack.hh:70
~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
A Vector class with statically reserved memory.
Definition: reservedvector.hh:47
constexpr void push_back(const value_type &t) noexcept(std::is_nothrow_copy_assignable_v< value_type >)
Appends an element to the end of a vector, up to the maximum size n, O(1) time.
Definition: reservedvector.hh:194
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
constexpr auto back(const HybridTreePath< T... > &tp) -> decltype(treePathEntry< sizeof...(T) -1 >(tp))
Returns a copy of the last element of the HybridTreePath.
Definition: treepath.hh:257
constexpr auto pop_back(const HybridTreePath< T... > &tp)
Removes last index on a HybridTreePath.
Definition: treepath.hh:356
Dune namespace.
Definition: alignedallocator.hh:13
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)