Generated on Tue Apr 18 10:22:15 2017 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  *  Last modified:
00010  *     $Date: 2009-09-08 21:10:29 +0200 (Tue, 08 Sep 2009) $ by $Author: schulte $
00011  *     $Revision: 9692 $
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 StaticStack {
00047   private:
00049     A& a;
00051     int n;
00053     unsigned int tos;
00055     T* stack;
00056   public:
00058     StaticStack(A& a, int n);
00060     ~StaticStack(void);
00061 
00063     void reset(void);
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   private:
00080     static void* operator new(size_t s) throw() { (void) s; return NULL; }
00082     static void  operator delete(void* p) { (void) p; };
00084     StaticStack(const StaticStack& s) : a(s.a) {}
00086     const StaticStack& operator =(const StaticStack&) { return *this; }
00087   };
00088 
00089   template<class T, class A>
00090   forceinline
00091   StaticStack<T,A>::StaticStack(A& a0, int n0)
00092     : a(a0), n(n0), tos(0), stack(a.template alloc<T>(n)) {}
00093 
00094   template<class T, class A>
00095   forceinline
00096   StaticStack<T,A>::~StaticStack(void) {
00097     a.free(stack,n);
00098   }
00099 
00100   template<class T, class A>
00101   forceinline bool
00102   StaticStack<T,A>::empty(void) const {
00103     return tos==0;
00104   }
00105 
00106   template<class T, class A>
00107   forceinline int
00108   StaticStack<T,A>::entries(void) const {
00109     return tos;
00110   }
00111 
00112   template<class T, class A>
00113   forceinline void
00114   StaticStack<T,A>::reset(void) {
00115     tos = 0;
00116   }
00117 
00118   template<class T, class A>
00119   forceinline T
00120   StaticStack<T,A>::pop(void) {
00121     assert((tos > 0) && (tos <= static_cast<unsigned int>(n)));
00122     return stack[--tos];
00123   }
00124 
00125   template<class T, class A>
00126   forceinline T&
00127   StaticStack<T,A>::top(void) const {
00128     assert((tos > 0) && (tos <= static_cast<unsigned int>(n)));
00129     return stack[tos-1];
00130   }
00131 
00132   template<class T, class A>
00133   forceinline T&
00134   StaticStack<T,A>::last(void) const {
00135     assert((tos >= 0) && (tos < static_cast<unsigned int>(n)));
00136     return stack[tos];
00137   }
00138 
00139   template<class T, class A>
00140   forceinline void
00141   StaticStack<T,A>::push(const T& x) {
00142     assert(tos < static_cast<unsigned int>(n));
00143     stack[tos++] = x;
00144   }
00145 
00146 }}
00147 
00148 // STATISTICS: support-any