Main Page   Class Hierarchy   Compound List   File List   Compound Members   Related Pages  

stack.h

00001 //
00002 // stack.h
00003 //
00004 // Copyright (C) 1996 Limit Point Systems, Inc.
00005 //
00006 // Author: Curtis Janssen <cljanss@limitpt.com>
00007 // Maintainer: LPS
00008 //
00009 // This file is part of the SC Toolkit.
00010 //
00011 // The SC Toolkit is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Library General Public License as published by
00013 // the Free Software Foundation; either version 2, or (at your option)
00014 // any later version.
00015 //
00016 // The SC Toolkit is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 // GNU Library General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Library General Public License
00022 // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
00023 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
00024 //
00025 // The U.S. Government is granted a limited license as per AL 91-7.
00026 //
00027 
00028 #ifndef _util_render_stack_h
00029 #define _util_render_stack_h
00030 
00031 #include <iostream>
00032 
00033 #define STACK_MAX_STACK_SIZE 20
00034 template <class T>
00035 class Stack {
00036   private:
00037     T objects[STACK_MAX_STACK_SIZE];
00038     int nobjects;
00039   public:
00040     Stack(): nobjects(0) {}
00041     void push(const T&a) {
00042         if (nobjects >= STACK_MAX_STACK_SIZE) {
00043             ExEnv::err() << "Stack: overflow" << std::endl;
00044             abort();
00045           }
00046         objects[nobjects++] = a;
00047       }
00048     T pop() {
00049         if (!nobjects) {
00050             ExEnv::err() << "Stack: underflow" << std::endl;
00051             abort();
00052           }
00053         nobjects -= 1;
00054         return objects[nobjects];
00055       }
00056     T top() const {
00057         if (!nobjects) {
00058             ExEnv::err() << "Stack: underflow" << std::endl;
00059             abort();
00060           }
00061         return objects[nobjects - 1];
00062       }
00063     int n() const { return nobjects; }
00064     T operator[](int i) { return objects[i]; }
00065     void print(std::ostream& os = ExEnv::out()) {
00066         os << "Stack (depth = " << nobjects << "):" << std::endl;
00067         for (int i=0; i<nobjects; i++) {
00068             os << "  object " << i << ":" << std::endl;
00069             objects[i]->print(os);
00070           }
00071       }
00072 };
00073 
00074 #endif
00075 
00076 // Local Variables:
00077 // mode: c++
00078 // c-file-style: "CLJ"
00079 // End:

Generated at Thu Oct 4 18:08:47 2001 for MPQC 2.0.0 using the documentation package Doxygen 1.2.5.