Odamex
Setting the Standard in Multiplayer Doom
common/tarray.h
Go to the documentation of this file.
00001 // Emacs style mode select   -*- C++ -*- 
00002 //-----------------------------------------------------------------------------
00003 //
00004 // $Id: tarray.h 1788 2010-08-24 04:42:57Z russellrice $
00005 //
00006 // Copyright (C) 1998-2006 by Randy Heit (ZDoom).
00007 // Copyright (C) 2006-2010 by The Odamex Team.
00008 //
00009 // This program is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU General Public License
00011 // as published by the Free Software Foundation; either version 2
00012 // of the License, or (at your option) any later version.
00013 //
00014 // This program is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // DESCRIPTION:
00020 //      TARRAY
00021 //
00022 //-----------------------------------------------------------------------------
00023 
00024 
00025 #ifndef __TARRAY_H__
00026 #define __TARRAY_H__
00027 
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include "m_alloc.h"
00031 
00032 template <class T>
00033 class TArray
00034 {
00035 public:
00036         TArray ()
00037         {
00038                 Most = 0;
00039                 Count = 0;
00040                 Array = NULL;
00041         }
00042         TArray (int max)
00043         {
00044                 Most = max;
00045                 Count = 0;
00046                 Array = (T *)Malloc (sizeof(T)*max);
00047         }
00048         ~TArray ()
00049         {
00050                 M_Free (Array);
00051         }
00052         T &operator[] (size_t index) const
00053         {
00054                 return Array[index];
00055         }
00056         size_t Push (T item)
00057         {
00058                 if (Count >= Most)
00059                 {
00060                         Most = Most ? Most * 2 : 16;
00061                         Array = (T *)Realloc (Array, sizeof(T)*Most);
00062                 }
00063                 Array[Count] = item;
00064                 return Count++;
00065         }
00066         bool Pop (T &item)
00067         {
00068                 if (Count > 0)
00069                 {
00070                         item = Array[--Count];
00071                         return true;
00072                 }
00073                 return false;
00074         }
00075         size_t Size ()
00076         {
00077                 return Count;
00078         }
00079         size_t Max ()
00080         {
00081                 return Most;
00082         }
00083         void Clear ()
00084         {
00085                 Count = 0;
00086         }
00087 private:
00088         T *Array;
00089         size_t Most;
00090         size_t Count;
00091 };
00092 
00093 #endif //__TARRAY_H__
00094 
00095 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends