filters

GList.h

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