~ubuntu-branches/ubuntu/precise/ipe/precise

« back to all changes in this revision

Viewing changes to src/xpdflib/glist.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve M. Robbins
  • Date: 2004-06-08 00:44:02 UTC
  • Revision ID: james.westby@ubuntu.com-20040608004402-72yu51xlh7vt6p9m
Tags: upstream-6.0pre16
ImportĀ upstreamĀ versionĀ 6.0pre16

Show diffs side-by-side

added added

removed removed

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