Back to index

tetex-bin  3.0
GList.h
Go to the documentation of this file.
00001 //========================================================================
00002 //
00003 // GList.h
00004 //
00005 // Copyright 2001-2003 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 #ifndef GLIST_H
00010 #define GLIST_H
00011 
00012 #include <aconf.h>
00013 
00014 #ifdef USE_GCC_PRAGMAS
00015 #pragma interface
00016 #endif
00017 
00018 #include "gtypes.h"
00019 
00020 //------------------------------------------------------------------------
00021 // GList
00022 //------------------------------------------------------------------------
00023 
00024 class GList {
00025 public:
00026 
00027   // Create an empty list.
00028   GList();
00029 
00030   // Create an empty list with space for <size1> elements.
00031   GList(int sizeA);
00032 
00033   // Destructor - does not free pointed-to objects.
00034   ~GList();
00035 
00036   //----- general
00037 
00038   // Get the number of elements.
00039   int getLength() { return length; }
00040 
00041   //----- ordered list support
00042 
00043   // Return the <i>th element.
00044   // Assumes 0 <= i < length.
00045   void *get(int i) { return data[i]; }
00046 
00047   // Append an element to the end of the list.
00048   void append(void *p);
00049 
00050   // Append another list to the end of this one.
00051   void append(GList *list);
00052 
00053   // Insert an element at index <i>.
00054   // Assumes 0 <= i <= length.
00055   void insert(int i, void *p);
00056 
00057   // Deletes and returns the element at index <i>.
00058   // Assumes 0 <= i < length.
00059   void *del(int i);
00060 
00061   //----- control
00062 
00063   // Set allocation increment to <inc>.  If inc > 0, that many
00064   // elements will be allocated every time the list is expanded.
00065   // If inc <= 0, the list will be doubled in size.
00066   void setAllocIncr(int incA) { inc = incA; }
00067 
00068 private:
00069 
00070   void expand();
00071   void shrink();
00072 
00073   void **data;                     // the list elements
00074   int size;                 // size of data array
00075   int length;               // number of elements on list
00076   int inc;                  // allocation increment
00077 };
00078 
00079 #define deleteGList(list, T)                        \
00080   do {                                              \
00081     GList *_list = (list);                          \
00082     {                                               \
00083       int _i;                                       \
00084       for (_i = 0; _i < _list->getLength(); ++_i) { \
00085         delete (T*)_list->get(_i);                  \
00086       }                                             \
00087       delete _list;                                 \
00088     }                                               \
00089   } while (0)
00090 
00091 #endif