source: GTP/trunk/Lib/Vis/Preprocessing/src/mixkit/MxStack.h @ 1097

Revision 1097, 1.5 KB checked in by mattausch, 18 years ago (diff)
Line 
1#ifndef MXSTACK_INCLUDED // -*- C++ -*-
2#define MXSTACK_INCLUDED
3#if !defined(__GNUC__)
4#  pragma once
5#endif
6
7/************************************************************************
8
9  This provides a very simple typed-access stack class.  It's really
10  just a convenience wrapper for the underlying MxDynBlock class.
11
12  Copyright (C) 1998 Michael Garland.  See "COPYING.txt" for details.
13 
14  $Id: MxStack.h,v 1.1 2002/09/24 16:53:54 wimmer Exp $
15
16 ************************************************************************/
17
18#include "MxDynBlock.h"
19
20template<class T>
21class MxStack : private MxDynBlock<T>
22{
23public:
24    MxStack(unsigned int n) : MxDynBlock<T>(n)
25        { }
26    MxStack(const T& val, unsigned int n) : MxDynBlock<T>(n)
27        { push(val); }
28
29    T&       top()       { return last(); }
30    const T& top() const { return last(); }
31
32    bool is_empty() { return length()==0; }
33
34    T& pop() { return drop(); }
35
36    void push(const T& val) { add(val); }
37    //
38    // NOTE: In this code, it is *crucial* that we do the add() and
39    //       assignment in separate steps.  The obvious alternative
40    //       is something like { add(top()); }.  But this is subtly
41    //       broken!  The top() will grab a pointer into the block,
42    //       but the add() may reallocate the block before doing the
43    //       assignment.  Thus, the pointer will become invalid.
44    void push() { add(); top() = raw(length()-2); }
45};
46
47// MXSTACK_INCLUDED
48#endif
Note: See TracBrowser for help on using the repository browser.