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