filters

GList.cc

00001 //========================================================================
00002 //
00003 // GList.cc
00004 //
00005 // Copyright 2001-2002 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 #include <aconf.h>
00010 
00011 
00012 #include <string.h>
00013 #include "gmem.h"
00014 #include "GList.h"
00015 
00016 //------------------------------------------------------------------------
00017 // GList
00018 //------------------------------------------------------------------------
00019 
00020 GList::GList() {
00021   size = 8;
00022   data = (void **)gmalloc(size * sizeof(void*));
00023   length = 0;
00024   inc = 0;
00025 }
00026 
00027 GList::GList(int sizeA) {
00028   size = sizeA;
00029   data = (void **)gmalloc(size * sizeof(void*));
00030   length = 0;
00031   inc = 0;
00032 }
00033 
00034 GList::~GList() {
00035   gfree(data);
00036 }
00037 
00038 void GList::append(void *p) {
00039   if (length >= size) {
00040     expand();
00041   }
00042   data[length++] = p;
00043 }
00044 
00045 void GList::append(GList *list) {
00046   int i;
00047 
00048   while (length + list->length > size) {
00049     expand();
00050   }
00051   for (i = 0; i < list->length; ++i) {
00052     data[length++] = list->data[i];
00053   }
00054 }
00055 
00056 void GList::insert(int i, void *p) {
00057   if (length >= size) {
00058     expand();
00059   }
00060   if (i < length) {
00061     memmove(data+i+1, data+i, (length - i) * sizeof(void *));
00062   }
00063   data[i] = p;
00064   ++length;
00065 }
00066 
00067 void *GList::del(int i) {
00068   void *p;
00069 
00070   p = data[i];
00071   if (i < length - 1) {
00072     memmove(data+i, data+i+1, (length - i - 1) * sizeof(void *));
00073   }
00074   --length;
00075   if (size - length >= ((inc > 0) ? inc : size/2)) {
00076     shrink();
00077   }
00078   return p;
00079 }
00080 
00081 void GList::expand() {
00082   size += (inc > 0) ? inc : size;
00083   data = (void **)grealloc(data, size * sizeof(void*));
00084 }
00085 
00086 void GList::shrink() {
00087   size -= (inc > 0) ? inc : size/2;
00088   data = (void **)grealloc(data, size * sizeof(void*));
00089 }
KDE Home | KDE Accessibility Home | Description of Access Keys