Generated on Tue Mar 5 2013 22:37:31 for Gecode by doxygen 1.8.3.1
dynamic-stack.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2002
8  *
9  * Last modified:
10  * $Date: 2009-09-09 05:10:29 +1000 (Wed, 09 Sep 2009) $ by $Author: schulte $
11  * $Revision: 9692 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 namespace Gecode { namespace Support {
39 
45  template<class T, class A>
46  class DynamicStack {
47  private:
49  A& a;
51  int limit;
53  int tos;
55  T* stack;
57  void resize(void);
58  public:
60  DynamicStack(A& a, int n=64);
62  ~DynamicStack(void);
63 
65  bool empty(void) const;
67  int entries(void) const;
68 
70  T pop(void);
72  T& top(void) const;
74  T& last(void) const;
76  void push(const T& x);
77 
78 
85  T& operator [](int i);
92  const T& operator [](int i) const;
93 
95  size_t size(void) const;
96  private:
98  static void* operator new(size_t s) throw() { (void) s; return NULL; }
100  static void operator delete(void* p) { (void) p; };
102  DynamicStack(const DynamicStack& s) : a(s.a) {}
104  const DynamicStack& operator =(const DynamicStack&) { return *this; }
105  };
106 
107 
108  template<class T, class A>
109  void
111  int nl = (limit * 3) / 2;
112  stack = a.template realloc<T>(stack,limit,nl);
113  limit = nl;
114  }
115 
116  template<class T, class A>
119  : a(a0), limit(n), tos(0), stack(a.template alloc<T>(n)) {}
120 
121  template<class T, class A>
124  a.free(stack,limit);
125  }
126 
127  template<class T, class A>
128  forceinline T
130  return stack[--tos];
131  }
132 
133  template<class T, class A>
134  forceinline T&
136  return stack[tos-1];
137  }
138 
139  template<class T, class A>
140  forceinline T&
142  return stack[tos];
143  }
144 
145  template<class T, class A>
146  forceinline void
148  stack[tos++] = x;
149  if (tos==limit)
150  resize();
151  }
152 
153  template<class T, class A>
154  forceinline bool
156  return tos==0;
157  }
158 
159  template<class T, class A>
160  forceinline int
162  return tos;
163  }
164 
165  template<class T, class A>
166  forceinline T&
168  return stack[i];
169  }
170 
171  template<class T, class A>
172  forceinline const T&
174  return stack[i];
175  }
176 
177  template<class T, class A>
178  forceinline size_t
180  return (static_cast<size_t>(limit) * sizeof(T)) +
181  sizeof(DynamicStack<T,A>);
182  }
183 
184 }}
185 
186 // STATISTICS: support-any