synch wiht TL 37823
[luatex.git] / source / libs / poppler / poppler-0.34.0 / goo / GooList.cc
blob6ce4952dc6ac440d3842e439471e60ecf89fa32b
1 //========================================================================
2 //
3 // GooList.cc
4 //
5 // Copyright 2001-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
9 #include <config.h>
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
13 #endif
15 #include <stdlib.h>
16 #include <string.h>
17 #include "gmem.h"
18 #include "GooList.h"
20 //------------------------------------------------------------------------
21 // GooList
22 //------------------------------------------------------------------------
24 GooList::GooList() {
25 size = 8;
26 data = (void **)gmallocn(size, sizeof(void*));
27 length = 0;
28 inc = 0;
31 GooList::GooList(int sizeA) {
32 size = sizeA ? sizeA : 8;
33 data = (void **)gmallocn(size, sizeof(void*));
34 length = 0;
35 inc = 0;
38 GooList::~GooList() {
39 gfree(data);
42 GooList *GooList::copy() {
43 GooList *ret;
45 ret = new GooList(length);
46 ret->length = length;
47 memcpy(ret->data, data, length * sizeof(void *));
48 ret->inc = inc;
49 return ret;
52 void GooList::append(void *p) {
53 if (length >= size) {
54 expand();
56 data[length++] = p;
59 void GooList::append(GooList *list) {
60 int i;
62 while (length + list->length > size) {
63 expand();
65 for (i = 0; i < list->length; ++i) {
66 data[length++] = list->data[i];
70 void GooList::insert(int i, void *p) {
71 if (length >= size) {
72 expand();
74 if (i < 0) {
75 i = 0;
77 if (i < length) {
78 memmove(data+i+1, data+i, (length - i) * sizeof(void *));
80 data[i] = p;
81 ++length;
84 void *GooList::del(int i) {
85 void *p;
87 p = data[i];
88 if (i < length - 1) {
89 memmove(data+i, data+i+1, (length - i - 1) * sizeof(void *));
91 --length;
92 if (size - length >= ((inc > 0) ? inc : size/2)) {
93 shrink();
95 return p;
98 void GooList::sort(int (*cmp)(const void *obj1, const void *obj2)) {
99 qsort(data, length, sizeof(void *), cmp);
102 void GooList::reverse() {
103 void *t;
104 int n, i;
106 n = length / 2;
107 for (i = 0; i < n; ++i) {
108 t = data[i];
109 data[i] = data[length - 1 - i];
110 data[length - 1 - i] = t;
114 void GooList::expand() {
115 size += (inc > 0) ? inc : size;
116 data = (void **)greallocn(data, size, sizeof(void*));
119 void GooList::shrink() {
120 size -= (inc > 0) ? inc : size/2;
121 data = (void **)greallocn(data, size, sizeof(void*));