/* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2002 * * Last modified: * $Date: 2006-08-04 16:05:34 +0200 (Fri, 04 Aug 2006) $ by $Author: schulte $ * $Revision: 3514 $ * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * See the file "LICENSE" for information on usage and * redistribution of this file, and for a * DISCLAIMER OF ALL WARRANTIES. * */ #ifndef __GECODE_SUPPORT_DYNAMIC_STACK_HH__ #define __GECODE_SUPPORT_DYNAMIC_STACK_HH__ #include "gecode/kernel.hh" namespace Gecode { namespace Support { /** * \brief Stack with arbitrary number of elements * * Requires \code #include "gecode/support/dynamic-stack.hh" \endcode * \ingroup FuncSupport */ template class DynamicStack { private: /// Current size of the stack int limit; /// Top of stack int tos; /// Elements on stack T* stack; /// Resize stack (increase size) void resize(void); public: /// Initialize stack with \a n elements DynamicStack(int n=64); /// Release memory ~DynamicStack(void); /// Test whether stack is empty bool empty(void) const; /// Pop topmost element from stack and return it T pop(void); /// Return top of stack T& top(void); /// Push element on stack void push(T); /// Return number of entries currently on stack int entries(void) const; /** \brief Return entry at position \a i * * Position 0 corresponds to the element first pushed, * whereas position \c entries()-1 corresponds to the * element pushed last. */ T& operator[](int i); /** \brief Return entry at position \a i * * Position 0 corresponds to the element first pushed, * whereas position \c entries()-1 corresponds to the * element pushed last. */ const T& operator [] (int i) const; /// Return size of stack size_t size(void) const; }; template void DynamicStack::resize(void) { int nl = (limit * 3) / 2; stack = Memory::brealloc(stack,limit,nl); limit = nl; } template forceinline DynamicStack::DynamicStack(int n) : limit(n), tos(0), stack(Memory::bmalloc(n)) {} template forceinline DynamicStack::~DynamicStack(void) { Memory::free(stack); } template forceinline T DynamicStack::pop(void) { return stack[--tos]; } template forceinline T& DynamicStack::top(void) { return stack[tos-1]; } template forceinline void DynamicStack::push(T x) { stack[tos++] = x; if (tos==limit) resize(); } template forceinline bool DynamicStack::empty(void) const { return tos==0; } template forceinline int DynamicStack::entries(void) const { return tos; } template forceinline T& DynamicStack::operator [] (int i) { return stack[i]; } template forceinline const T& DynamicStack::operator [] (int i) const { return stack[i]; } template forceinline size_t DynamicStack::size(void) const { return (limit * sizeof(T)) + sizeof(DynamicStack); } }} #endif // STATISTICS: support-any