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

array.h

Go to the documentation of this file.
00001 // See the end of this file for license information.
00002 
00003 #ifndef TORSION_ARRAY_H
00004 #define TORSION_ARRAY_H
00005 
00006 #include "virtmem.h"
00007 
00009 
00010 template <class T>
00011 class Array {
00012 protected:
00013   T* data;                              
00014   unsigned int used_size;               
00015   unsigned int capacity;                
00016 
00017   static const unsigned int INITIAL_CAPACITY = 8;
00018 
00019 public:
00022   Array() {
00023     used_size = 0;
00024     capacity = 0;
00025     data = NULL;
00026   }
00027 
00029   inline unsigned int
00030   size() {
00031     return used_size;
00032   }
00033 
00036   void
00037   grow();
00038 
00040   inline void
00041   ensure_capacity() {
00042     if (used_size > capacity)
00043       grow();
00044   }
00045 
00047   inline void
00048   append(T element) {
00049     used_size++;
00050     ensure_capacity();
00051     data[used_size - 1] = element;
00052   }
00053 
00055   inline T
00056   operator [](unsigned int index) {
00057     return data[index];
00058   }
00059 
00061   inline void
00062   clear() {
00063     if (data)
00064       virtual_mem.free(data);
00065   }
00066 };
00067 
00068 template <class T>
00069 void
00070 Array<T>::grow() {
00071   if (capacity == 0) {                  // initially allocate a data array
00072     data = (T*)virtual_mem.alloc(INITIAL_CAPACITY * sizeof(T));
00073     capacity = INITIAL_CAPACITY;
00074   } else {                              // allocate a new, larger data array
00075     T* new_data = (T*)virtual_mem.alloc(capacity * 2 * sizeof(T));
00076                                         // copy the current data into it
00077     memcpy(new_data, data, capacity * sizeof(T));
00078     virtual_mem.free(data);             // free the original array
00079     data = new_data;
00080     capacity *= 2;
00081   }
00082 }
00083 
00084 #endif
00085 
00086 /* Torsion Operating System, Copyright (C) 2000-2002 Dan Helfman
00087  *
00088  * This program is free software; you can redistribute it and/or modify it
00089  * under the terms of the GNU General Public License as published by the
00090  * Free Software Foundation; either version 2 of the License, or (at your
00091  * option) any later version.
00092  * 
00093  * This program is distributed in the hope that it will be useful, but
00094  * WITHOUT ANY WARRANTY; without even the implied warranty of
00095  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00096  * General Public License for more details (in the COPYING file).
00097  * 
00098  * You should have received a copy of the GNU General Public License along
00099  * with this program; if not, write to the Free Software Foundation, Inc.,
00100  * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00101  */

Torsion Operating System, Copyright (C) 2000-2002 Dan Helfman