Back to index

tetex-bin  3.0
Array.cc
Go to the documentation of this file.
00001 //========================================================================
00002 //
00003 // Array.cc
00004 //
00005 // Copyright 1996-2003 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 #include <aconf.h>
00010 
00011 #ifdef USE_GCC_PRAGMAS
00012 #pragma implementation
00013 #endif
00014 
00015 #include <stdlib.h>
00016 #include <stddef.h>
00017 #include "gmem.h"
00018 #include "Object.h"
00019 #include "Array.h"
00020 
00021 //------------------------------------------------------------------------
00022 // Array
00023 //------------------------------------------------------------------------
00024 
00025 Array::Array(XRef *xrefA) {
00026   xref = xrefA;
00027   elems = NULL;
00028   size = length = 0;
00029   ref = 1;
00030 }
00031 
00032 Array::~Array() {
00033   int i;
00034 
00035   for (i = 0; i < length; ++i)
00036     elems[i].free();
00037   gfree(elems);
00038 }
00039 
00040 void Array::add(Object *elem) {
00041   if (length == size) {
00042     if (length == 0) {
00043       size = 8;
00044     } else {
00045       size *= 2;
00046     }
00047     elems = (Object *)grealloc(elems, size * sizeof(Object));
00048   }
00049   elems[length] = *elem;
00050   ++length;
00051 }
00052 
00053 Object *Array::get(int i, Object *obj) {
00054   if (i < 0 || i >= length) {
00055 #ifdef DEBUG_MEM
00056     abort();
00057 #else
00058     return obj->initNull();
00059 #endif
00060   }
00061   return elems[i].fetch(xref, obj);
00062 }
00063 
00064 Object *Array::getNF(int i, Object *obj) {
00065   if (i < 0 || i >= length) {
00066 #ifdef DEBUG_MEM
00067     abort();
00068 #else
00069     return obj->initNull();
00070 #endif
00071   }
00072   return elems[i].copy(obj);
00073 }