upgraded to xpdf-3.01pl1
[swftools.git] / pdf2swf / xpdf / GList.cc
1 //========================================================================
2 //
3 // GList.cc
4 //
5 // Copyright 2001-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #include <aconf.h>
10
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
13 #endif
14
15 #include <stdlib.h>
16 #include <string.h>
17 #include "gmem.h"
18 #include "GList.h"
19
20 //------------------------------------------------------------------------
21 // GList
22 //------------------------------------------------------------------------
23
24 GList::GList() {
25   size = 8;
26   data = (void **)gmallocn(size, sizeof(void*));
27   length = 0;
28   inc = 0;
29 }
30
31 GList::GList(int sizeA) {
32   size = sizeA;
33   data = (void **)gmallocn(size, sizeof(void*));
34   length = 0;
35   inc = 0;
36 }
37
38 GList::~GList() {
39   gfree(data);
40 }
41
42 void GList::append(void *p) {
43   if (length >= size) {
44     expand();
45   }
46   data[length++] = p;
47 }
48
49 void GList::append(GList *list) {
50   int i;
51
52   while (length + list->length > size) {
53     expand();
54   }
55   for (i = 0; i < list->length; ++i) {
56     data[length++] = list->data[i];
57   }
58 }
59
60 void GList::insert(int i, void *p) {
61   if (length >= size) {
62     expand();
63   }
64   if (i < length) {
65     memmove(data+i+1, data+i, (length - i) * sizeof(void *));
66   }
67   data[i] = p;
68   ++length;
69 }
70
71 void *GList::del(int i) {
72   void *p;
73
74   p = data[i];
75   if (i < length - 1) {
76     memmove(data+i, data+i+1, (length - i - 1) * sizeof(void *));
77   }
78   --length;
79   if (size - length >= ((inc > 0) ? inc : size/2)) {
80     shrink();
81   }
82   return p;
83 }
84
85 void GList::sort(int (*cmp)(const void *obj1, const void *obj2)) {
86   qsort(data, length, sizeof(void *), cmp);
87 }
88
89 void GList::expand() {
90   size += (inc > 0) ? inc : size;
91   data = (void **)greallocn(data, size, sizeof(void*));
92 }
93
94 void GList::shrink() {
95   size -= (inc > 0) ? inc : size/2;
96   data = (void **)greallocn(data, size, sizeof(void*));
97 }