Generated on Tue Apr 18 10:22:14 2017 for Gecode by doxygen 1.6.3

dynamic-stack.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2002
00008  *
00009  *  Last modified:
00010  *     $Date: 2013-07-11 12:30:18 +0200 (Thu, 11 Jul 2013) $ by $Author: schulte $
00011  *     $Revision: 13840 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 namespace Gecode { namespace Support {
00039 
00045   template<class T, class A>
00046   class DynamicStack {
00047   private:
00049     A& a;
00051     int limit;
00053     int tos;
00055     T*  stack;
00057     void resize(void);
00058   public:
00060     DynamicStack(A& a, int n=64);
00062     ~DynamicStack(void);
00063 
00065     bool empty(void) const;
00067     int entries(void) const;
00068 
00070     T pop(void);
00072     T& top(void) const;
00074     T& last(void) const;
00076     void push(const T& x);
00077 
00078 
00085     T& operator [](int i);
00092     const T& operator [](int i) const;
00093   private:
00095     static void* operator new(size_t s) throw() { (void) s; return NULL; }
00097     static void  operator delete(void* p) { (void) p; };
00099     DynamicStack(const DynamicStack& s) : a(s.a) {}
00101     const DynamicStack& operator =(const DynamicStack&) { return *this; }
00102   };
00103 
00104 
00105   template<class T, class A>
00106   void
00107   DynamicStack<T,A>::resize(void) {
00108     int nl = (limit * 3) / 2;
00109     stack = a.template realloc<T>(stack,limit,nl);
00110     limit = nl;
00111   }
00112 
00113   template<class T, class A>
00114   forceinline
00115   DynamicStack<T,A>::DynamicStack(A& a0, int n)
00116     : a(a0), limit(n), tos(0), stack(a.template alloc<T>(n)) {}
00117 
00118   template<class T, class A>
00119   forceinline
00120   DynamicStack<T,A>::~DynamicStack(void) {
00121     a.free(stack,limit);
00122   }
00123 
00124   template<class T, class A>
00125   forceinline T
00126   DynamicStack<T,A>::pop(void) {
00127     return stack[--tos];
00128   }
00129 
00130   template<class T, class A>
00131   forceinline T&
00132   DynamicStack<T,A>::top(void) const {
00133     return stack[tos-1];
00134   }
00135 
00136   template<class T, class A>
00137   forceinline T&
00138   DynamicStack<T,A>::last(void) const {
00139     return stack[tos];
00140   }
00141 
00142   template<class T, class A>
00143   forceinline void
00144   DynamicStack<T,A>::push(const T& x) {
00145     stack[tos++] = x;
00146     if (tos==limit)
00147       resize();
00148   }
00149 
00150   template<class T, class A>
00151   forceinline bool
00152   DynamicStack<T,A>::empty(void) const {
00153     return tos==0;
00154   }
00155 
00156   template<class T, class A>
00157   forceinline int
00158   DynamicStack<T,A>::entries(void) const {
00159     return tos;
00160   }
00161 
00162   template<class T, class A>
00163   forceinline T&
00164   DynamicStack<T,A>::operator [](int i) {
00165     return stack[i];
00166   }
00167 
00168   template<class T, class A>
00169   forceinline const T&
00170   DynamicStack<T,A>::operator [](int i) const {
00171     return stack[i];
00172   }
00173 
00174 }}
00175 
00176 // STATISTICS: support-any