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

Stack.h

Go to the documentation of this file.
00001 /***************************************************************************
00002 *cr 
00003 *cr (C) Copyright 1995-2019 The Board of Trustees of the 
00004 *cr University of Illinois 
00005 *cr All Rights Reserved 
00006 *cr 
00007 ***************************************************************************/
00008 
00009 /***************************************************************************
00010 * RCS INFORMATION:
00011 *
00012 * $RCSfile: Stack.h,v $
00013 * $Author: johns $ $Locker: $ $State: Exp $
00014 * $Revision: 1.30 $ $Date: 2021年09月23日 16:11:23 $
00015 *
00016 ***************************************************************************
00017 * DESCRIPTION:
00018 * A template class which implements a simple stack of arbitrary data.
00019 * Items are pushed onto and popped off of the stack, or may just be
00020 * copied from the stack.
00021 *
00022 ***************************************************************************/
00023 #ifndef STACK_TEMPLATE_H
00024 #define STACK_TEMPLATE_H
00025 
00029 template<class T>
00030 class Stack {
00031 
00032 private:
00033 T *data; 
00034 T *curr; 
00035 int sz; 
00036 int items; 
00037 
00038 public:
00040 Stack(int s) {
00041 items = 0;
00042 data = curr = new T[sz = (s > 0 ? s : 1)];
00043 }
00044 
00045 
00047 ~Stack(void) {
00048 if(data)
00049 delete [] data;
00050 }
00051 
00052 int max_stack_size(void) { return sz; } 
00053 int stack_size(void) { return items; } 
00054 int num(void) { return items; } 
00055 
00057 int push(const T& a) {
00058 if (items < sz) {
00059 *curr++ = a;
00060 items++;
00061 } else {
00062 return -1; // stack size exceede
00063 }
00064 return 0;
00065 }
00066 
00067 
00069 int dup(void) {
00070 if (items > 0) {
00071 return push(top());
00072 } else if(sz > 0) {
00073 curr++;
00074 items++;
00075 }
00076 return 0; // success
00077 }
00078 
00079 
00081 T& pop(void) {
00082 if (items > 0) {
00083 items--;
00084 return (*--curr);
00085 } else {
00086 return *data;
00087 }
00088 }
00089 
00090 
00092 T& top(void) {
00093 if (items > 0) {
00094 return *(curr - 1);
00095 } else {
00096 return *data;
00097 }
00098 }
00099 
00100 
00102 //
00103 // a cleaner O(1) alternative to something like...
00104 // while (transMat.num()) { stack.pop(); }
00105 //
00106 void clear(void) {
00107 items = 0;
00108 curr = data;
00109 // std::fill_n(data, sz, 0);
00110 memset(data, 0, sizeof(T)*sz);
00111 }
00112 
00113 };
00114 
00115 #endif
00116 

Generated on Tue Nov 18 02:48:06 2025 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002

AltStyle によって変換されたページ (->オリジナル) /