Generated on Wed Nov 1 15:04:49 2006 for Gecode by doxygen 1.4.5

static-stack.hh

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Christian Schulte <schulte@gecode.org>
00004  *
00005  *  Copyright:
00006  *     Christian Schulte, 2002
00007  *
00008  *  Last modified:
00009  *     $Date: 2006-04-11 15:58:37 +0200 (Tue, 11 Apr 2006) $ by $Author: tack $
00010  *     $Revision: 3188 $
00011  *
00012  *  This file is part of Gecode, the generic constraint
00013  *  development environment:
00014  *     http://www.gecode.org
00015  *
00016  *  See the file "LICENSE" for information on usage and
00017  *  redistribution of this file, and for a
00018  *     DISCLAIMER OF ALL WARRANTIES.
00019  *
00020  */
00021 
00022 #ifndef __GECODE_SUPPORT_STATIC_STACK_HH__
00023 #define __GECODE_SUPPORT_STATIC_STACK_HH__
00024 
00025 #include "gecode/kernel.hh"
00026 
00027 namespace Gecode { namespace Support {
00028 
00035   template <class T>
00036   class StaticStack {
00037   private:
00039     T* stack;
00041     unsigned int tos;
00042   public:
00044     StaticStack(const int n);
00046     ~StaticStack(void);
00047 
00049     void reset(void);
00051     bool empty(void) const;
00052 
00054     T pop(void);
00056     T& top(void);
00058     T& last(void);
00060     void push(T x);
00061 
00062   };
00063 
00064   template <class T>
00065   forceinline
00066   StaticStack<T>::StaticStack(const int n)
00067     : tos(0) {
00068     stack = reinterpret_cast<T*>(Memory::malloc((n+1)*sizeof(T)));
00069   }
00070 
00071   template <class T>
00072   forceinline
00073   StaticStack<T>::~StaticStack(void) {
00074     Memory::free(stack);
00075   }
00076 
00077   template <class T>
00078   forceinline bool
00079   StaticStack<T>::empty(void) const {
00080     return tos==0;
00081   }
00082 
00083   template <class T>
00084   forceinline void
00085   StaticStack<T>::reset(void) {
00086     tos = 0;
00087   }
00088 
00089   template <class T>
00090   forceinline T
00091   StaticStack<T>::pop(void) {
00092     return stack[--tos];
00093   }
00094 
00095   template <class T>
00096   forceinline T&
00097   StaticStack<T>::top(void) {
00098     return stack[tos-1];
00099   }
00100 
00101   template <class T>
00102   forceinline T&
00103   StaticStack<T>::last(void) {
00104     return stack[tos];
00105   }
00106 
00107   template <class T>
00108   forceinline void
00109   StaticStack<T>::push(T x) {
00110     stack[tos++] = x;
00111   }
00112 
00113 }}
00114 
00115 #endif
00116 
00117 // STATISTICS: support-any