Generated on Thu Apr 11 13:59:20 2019 for Gecode by doxygen 1.6.3

static-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  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 namespace Gecode { namespace Support {
00035 
00041   template<class T, class A>
00042   class StaticStack {
00043   private:
00045     A& a;
00047     int n;
00049     unsigned int tos;
00051     T* stack;
00052   public:
00054     StaticStack(A& a, int n);
00056     ~StaticStack(void);
00057 
00059     void reset(void);
00061     bool empty(void) const;
00063     int entries(void) const;
00064 
00066     T pop(void);
00068     T& top(void) const;
00070     T& last(void) const;
00072     void push(const T& x);
00073 
00074   private:
00076     static void* operator new(size_t s) throw() { (void) s; return NULL; }
00078     static void  operator delete(void* p) { (void) p; };
00080     StaticStack(const StaticStack& s) : a(s.a) {}
00082     const StaticStack& operator =(const StaticStack&) { return *this; }
00083   };
00084 
00085   template<class T, class A>
00086   forceinline
00087   StaticStack<T,A>::StaticStack(A& a0, int n0)
00088     : a(a0), n(n0), tos(0), stack(a.template alloc<T>(n)) {}
00089 
00090   template<class T, class A>
00091   forceinline
00092   StaticStack<T,A>::~StaticStack(void) {
00093     a.free(stack,n);
00094   }
00095 
00096   template<class T, class A>
00097   forceinline bool
00098   StaticStack<T,A>::empty(void) const {
00099     return tos==0;
00100   }
00101 
00102   template<class T, class A>
00103   forceinline int
00104   StaticStack<T,A>::entries(void) const {
00105     return tos;
00106   }
00107 
00108   template<class T, class A>
00109   forceinline void
00110   StaticStack<T,A>::reset(void) {
00111     tos = 0;
00112   }
00113 
00114   template<class T, class A>
00115   forceinline T
00116   StaticStack<T,A>::pop(void) {
00117     assert((tos > 0) && (tos <= static_cast<unsigned int>(n)));
00118     return stack[--tos];
00119   }
00120 
00121   template<class T, class A>
00122   forceinline T&
00123   StaticStack<T,A>::top(void) const {
00124     assert((tos > 0) && (tos <= static_cast<unsigned int>(n)));
00125     return stack[tos-1];
00126   }
00127 
00128   template<class T, class A>
00129   forceinline T&
00130   StaticStack<T,A>::last(void) const {
00131     assert((tos >= 0) && (tos < static_cast<unsigned int>(n)));
00132     return stack[tos];
00133   }
00134 
00135   template<class T, class A>
00136   forceinline void
00137   StaticStack<T,A>::push(const T& x) {
00138     assert(tos < static_cast<unsigned int>(n));
00139     stack[tos++] = x;
00140   }
00141 
00142 }}
00143 
00144 // STATISTICS: support-any