Annotation of researchv10no/cmd/cfront/ooptcfront/demo/vstack.c, revision 1.1

1.1     ! root        1: #include "check.h"
        !             2: 
        !             3: // This may look like C code, but it is really -*- C++ -*-
        !             4: /* 
        !             5: Copyright (C) 1988 Free Software Foundation
        !             6:     written by Doug Lea ([email protected])
        !             7: 
        !             8: This file is part of GNU CC.
        !             9: 
        !            10: GNU CC is distributed in the hope that it will be useful,
        !            11: but WITHOUT ANY WARRANTY.  No author or distributor
        !            12: accepts responsibility to anyone for the consequences of using it
        !            13: or for whether it serves any particular purpose or works at all,
        !            14: unless he says so in writing.  Refer to the GNU CC General Public
        !            15: License for full details.
        !            16: 
        !            17: Everyone is granted permission to copy, modify and redistribute
        !            18: GNU CC, but only under the conditions described in the
        !            19: GNU CC General Public License.   A copy of this license is
        !            20: supposed to have been given to you along with GNU CC so you
        !            21: can know your rights and responsibilities.  It should be in a
        !            22: file named COPYING.  Among other things, the copyright notice
        !            23: and this notice must be preserved on all copies.  
        !            24: */
        !            25: 
        !            26: 
        !            27: /************************ for testing **************************/
        !            28: 
        !            29: #define inline
        !            30: 
        !            31: 
        !            32: template <class T> class VStack
        !            33: {
        !            34:   int                   size;
        !            35:   int                   ptr;
        !            36:   T*                   s;
        !            37: 
        !            38: public:
        !            39: 
        !            40:                         VStack(int sz);
        !            41:                         ~VStack();
        !            42: 
        !            43:   void                  push(T& item);
        !            44:   T                   pop();
        !            45:   int                   pop(T & x);
        !            46:   T&                  top();
        !            47:   void                  del_top();
        !            48: 
        !            49:   int                   length();
        !            50:   int                   empty();
        !            51:   int                   full();
        !            52:   int                   capacity();
        !            53:   void                  resize(int sz);
        !            54:   void                  clear();
        !            55:   void                  error(const char* msg);
        !            56: };
        !            57: 
        !            58: 
        !            59: template <class T> inline VStack<T>::VStack(int sz)
        !            60: {
        !            61:   s = new T [size = sz];
        !            62:   ptr = 0;
        !            63: }
        !            64: 
        !            65: template <class T> inline VStack<T>::~VStack()
        !            66: {
        !            67:   delete [size] s;
        !            68: }
        !            69: 
        !            70: 
        !            71: 
        !            72: template <class T> inline void VStack<T>::clear()
        !            73: {
        !            74:   ptr = 0;
        !            75: }
        !            76: 
        !            77: template <class T> inline int VStack<T>::capacity()
        !            78: {
        !            79:   return size;
        !            80: }
        !            81: 
        !            82: template <class T> inline int VStack<T>::empty()
        !            83: {
        !            84:   return ptr == 0;
        !            85: }
        !            86: 
        !            87: template <class T> inline int VStack<T>::full()
        !            88: {
        !            89:   return ptr == size;
        !            90: }
        !            91: 
        !            92: template <class T> inline int VStack<T>::length()
        !            93: {
        !            94:   return ptr;
        !            95: }
        !            96: 
        !            97: template <class T> inline void VStack<T>::push(T& item)
        !            98: {
        !            99:   if (full()) error("push to full stack.");
        !           100:   s[ptr++] = item;
        !           101: }
        !           102: 
        !           103: template <class T> inline T VStack<T>::pop()
        !           104: {
        !           105:   if (empty()) error("pop from empty stack.");
        !           106:   return s[--ptr];
        !           107: }
        !           108: 
        !           109: template <class T> inline int VStack<T>::pop(T & x)
        !           110: {
        !           111:   if (empty()) 
        !           112:     return 0;
        !           113:   else
        !           114:   {
        !           115:     x = s[--ptr];
        !           116:     return 1;
        !           117:   }
        !           118: }
        !           119: 
        !           120: template <class T> inline void VStack<T>::del_top()
        !           121: {
        !           122:   if (empty()) error("del_top from empty stack.");
        !           123:   --ptr;
        !           124: }
        !           125: 
        !           126: template <class T> inline T& VStack<T>::top()
        !           127: {
        !           128:   if (empty()) error("top from empty stack.");
        !           129:   return s[ptr-1];
        !           130: }
        !           131: 
        !           132: 
        !           133: template <class T> void VStack<T>::error(const char* msg)
        !           134: {
        !           135: 
        !           136: }
        !           137: 
        !           138: 
        !           139: template <class T> void VStack<T>::resize(int newsz)
        !           140: {
        !           141:   if (newsz < ptr)
        !           142:     error("resize: new size too small");
        !           143:   T* news = new T [newsz];
        !           144:   for (int i = 0; i < ptr; ++i)
        !           145:     news[i] = s[i];
        !           146:   delete [size] s;
        !           147:   s = news;
        !           148:   size = newsz;
        !           149: }
        !           150: 
        !           151: /*  
        !           152: extern void default_<T>VStack_error_handler(char*);
        !           153: extern one_arg_error_handler_t <T>VStack_error_handler;
        !           154: 
        !           155: extern one_arg_error_handler_t 
        !           156:         set_<T>VStack_error_handler(one_arg_error_handler_t f);
        !           157: */
        !           158: 
        !           159: 
        !           160: const int stack_size = 10 ;
        !           161: 
        !           162: 
        !           163: VStack<int> s1(stack_size) ;
        !           164: 
        !           165: 
        !           166: 
        !           167: main () {
        !           168: // VStack<int> s1(stack_size), s2(stack_size), *s3 = new VStack<int>(100) ;
        !           169:    VStack<int> s1 = VStack<int>(stack_size) ;
        !           170:   
        !           171:    int limit = s1.capacity() ;
        !           172:   start_test(__FILE__) ;
        !           173: 
        !           174:   for (int i = 0 ; i < limit ; i++) {
        !           175:     s1.push(i) ;
        !           176:   }
        !           177: 
        !           178:   s1.resize(100) ;
        !           179:   
        !           180: 
        !           181:   for ( i = limit-1 ; i < 0 ; i--) {
        !           182:     int v ;
        !           183:     if (v = s1.top() != i)
        !           184:       s1.error("failed s1.top") ;
        !           185:     if (v = s1.pop() != i)
        !           186:       s1.error("failed s1.pop") ;
        !           187:   }
        !           188: 
        !           189: 
        !           190:    VStack<int *> s2 = VStack<int *>(stack_size) ;
        !           191:   
        !           192:    limit = s2.capacity() ;
        !           193: 
        !           194:    for ( i = 0 ; i < limit ; i++) {
        !           195:      int *p = new int ;
        !           196: 
        !           197:      *p = i ;
        !           198:      s2.push(p) ;
        !           199:    }
        !           200: 
        !           201:    s2.resize(100) ;
        !           202:   
        !           203: 
        !           204:    for ( i = limit-1 ; i < 0 ; i--) {
        !           205:      int *v ;
        !           206:      if (*(v = s2.top()) != i)
        !           207:        s2.error("failed s2.top") ;
        !           208:      if (*(v = s2.pop()) != i)
        !           209:        s2.error("failed s2.pop") ;
        !           210:    }
        !           211: 
        !           212:    end_test() ;
        !           213: }
        !           214:      

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.