Generated on Mon Aug 25 11:35:44 2008 for Gecode by doxygen 1.5.6

dynamic-stack.icc

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: 2007-09-19 09:51:28 +0200 (Wed, 19 Sep 2007) $ by $Author: schulte $
00011  *     $Revision: 5048 $
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>
00046   class DynamicStack {
00047   private:
00049     int limit;
00051     int tos;
00053     T*  stack;
00055     void resize(void);
00056   public:
00058     DynamicStack(int n=64);
00060     ~DynamicStack(void);
00061 
00063     bool empty(void) const;
00065     T pop(void);
00067     T& top(void);
00069     void push(T);
00070 
00071 
00073     int entries(void) const;
00080     T& operator[](int i);
00087     const T& operator[](int i) const;
00088 
00090     size_t size(void) const;
00091   };
00092 
00093 
00094   template <class T>
00095   void
00096   DynamicStack<T>::resize(void) {
00097     int nl = (limit * 3) / 2;
00098     stack = Memory::brealloc<T>(stack,
00099                                 static_cast<size_t>(limit),
00100                                 static_cast<size_t>(nl));
00101     limit = nl;
00102   }
00103 
00104   template <class T>
00105   forceinline
00106   DynamicStack<T>::DynamicStack(int n)
00107     : limit(n), tos(0), stack(Memory::bmalloc<T>(static_cast<size_t>(n))) {}
00108 
00109   template <class T>
00110   forceinline
00111   DynamicStack<T>::~DynamicStack(void) {
00112     Memory::free(stack);
00113   }
00114 
00115   template <class T>
00116   forceinline T
00117   DynamicStack<T>::pop(void) {
00118     return stack[--tos];
00119   }
00120 
00121   template <class T>
00122   forceinline T&
00123   DynamicStack<T>::top(void) {
00124     return stack[tos-1];
00125   }
00126 
00127   template <class T>
00128   forceinline void
00129   DynamicStack<T>::push(T x) {
00130     stack[tos++] = x;
00131     if (tos==limit)
00132       resize();
00133   }
00134 
00135   template <class T>
00136   forceinline bool
00137   DynamicStack<T>::empty(void) const {
00138     return tos==0;
00139   }
00140 
00141   template <class T>
00142   forceinline int
00143   DynamicStack<T>::entries(void) const {
00144     return tos;
00145   }
00146 
00147   template <class T>
00148   forceinline T&
00149   DynamicStack<T>::operator[](int i) {
00150     return stack[i];
00151   }
00152 
00153   template <class T>
00154   forceinline const T&
00155   DynamicStack<T>::operator[](int i) const {
00156     return stack[i];
00157   }
00158 
00159   template <class T>
00160   forceinline size_t
00161   DynamicStack<T>::size(void) const {
00162     return (static_cast<size_t>(limit) * sizeof(T)) + sizeof(DynamicStack<T>);
00163   }
00164 
00165 }}
00166 
00167 // STATISTICS: support-any