/src/xpdf-4.05/goo/GList.cc
Line | Count | Source (jump to first uncovered line) |
1 | | //======================================================================== |
2 | | // |
3 | | // GList.cc |
4 | | // |
5 | | // Copyright 2001-2003 Glyph & Cog, LLC |
6 | | // |
7 | | //======================================================================== |
8 | | |
9 | | #include <aconf.h> |
10 | | |
11 | | #include <stdlib.h> |
12 | | #include <string.h> |
13 | | #include "gmem.h" |
14 | | #include "gmempp.h" |
15 | | #include "GList.h" |
16 | | |
17 | | //------------------------------------------------------------------------ |
18 | | // GList |
19 | | //------------------------------------------------------------------------ |
20 | | |
21 | 0 | GList::GList() { |
22 | 0 | size = 8; |
23 | 0 | data = (void **)gmallocn(size, sizeof(void*)); |
24 | 0 | length = 0; |
25 | 0 | inc = 0; |
26 | 0 | } |
27 | | |
28 | 0 | GList::GList(int sizeA) { |
29 | 0 | size = sizeA ? sizeA : 8; |
30 | 0 | data = (void **)gmallocn(size, sizeof(void*)); |
31 | 0 | length = 0; |
32 | 0 | inc = 0; |
33 | 0 | } |
34 | | |
35 | 0 | GList::~GList() { |
36 | 0 | gfree(data); |
37 | 0 | } |
38 | | |
39 | 0 | GList *GList::copy() { |
40 | 0 | GList *ret; |
41 | |
|
42 | 0 | ret = new GList(length); |
43 | 0 | ret->length = length; |
44 | 0 | memcpy(ret->data, data, length * sizeof(void *)); |
45 | 0 | ret->inc = inc; |
46 | 0 | return ret; |
47 | 0 | } |
48 | | |
49 | 0 | void GList::append(void *p) { |
50 | 0 | if (length >= size) { |
51 | 0 | expand(); |
52 | 0 | } |
53 | 0 | data[length++] = p; |
54 | 0 | } |
55 | | |
56 | 0 | void GList::append(GList *list) { |
57 | 0 | int i; |
58 | |
|
59 | 0 | while (length + list->length > size) { |
60 | 0 | expand(); |
61 | 0 | } |
62 | 0 | for (i = 0; i < list->length; ++i) { |
63 | 0 | data[length++] = list->data[i]; |
64 | 0 | } |
65 | 0 | } |
66 | | |
67 | 0 | void GList::insert(int i, void *p) { |
68 | 0 | if (length >= size) { |
69 | 0 | expand(); |
70 | 0 | } |
71 | 0 | if (i < 0) { |
72 | 0 | i = 0; |
73 | 0 | } |
74 | 0 | if (i < length) { |
75 | 0 | memmove(data+i+1, data+i, (length - i) * sizeof(void *)); |
76 | 0 | } |
77 | 0 | data[i] = p; |
78 | 0 | ++length; |
79 | 0 | } |
80 | | |
81 | 0 | void *GList::del(int i) { |
82 | 0 | void *p; |
83 | |
|
84 | 0 | p = data[i]; |
85 | 0 | if (i < length - 1) { |
86 | 0 | memmove(data+i, data+i+1, (length - i - 1) * sizeof(void *)); |
87 | 0 | } |
88 | 0 | --length; |
89 | 0 | if (size - length >= ((inc > 0) ? inc : size/2)) { |
90 | 0 | shrink(); |
91 | 0 | } |
92 | 0 | return p; |
93 | 0 | } |
94 | | |
95 | 0 | void GList::sort(int (*cmp)(const void *obj1, const void *obj2)) { |
96 | 0 | qsort(data, length, sizeof(void *), cmp); |
97 | 0 | } |
98 | | |
99 | | void GList::sort(int first, int n, |
100 | 0 | int (*cmp)(const void *ptr1, const void *ptr2)) { |
101 | 0 | qsort(data + first, n, sizeof(void *), cmp); |
102 | 0 | } |
103 | | |
104 | 0 | void GList::reverse() { |
105 | 0 | void *t; |
106 | 0 | int n, i; |
107 | |
|
108 | 0 | n = length / 2; |
109 | 0 | for (i = 0; i < n; ++i) { |
110 | 0 | t = data[i]; |
111 | 0 | data[i] = data[length - 1 - i]; |
112 | 0 | data[length - 1 - i] = t; |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | 0 | void GList::expand() { |
117 | 0 | size += (inc > 0) ? inc : size; |
118 | 0 | data = (void **)greallocn(data, size, sizeof(void*)); |
119 | 0 | } |
120 | | |
121 | 0 | void GList::shrink() { |
122 | 0 | size -= (inc > 0) ? inc : size/2; |
123 | 0 | data = (void **)greallocn(data, size, sizeof(void*)); |
124 | 0 | } |