Debian Codegraph @

texk/lcdf-typetools/lcdf-typetools-src/liblcdf/vectorv.cc

raw
1 /*
2 * vectorv.cc -- template specialization for Vector<void*>
3 * Eddie Kohler
4 *
5 * Copyright (c) 1999-2006 Massachusetts Institute of Technology
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, subject to the conditions
10 * listed in the Click LICENSE file. These conditions include: you must
11 * preserve this copyright notice, and you cannot mention the copyright
12 * holders in advertising related to the Software without their permission.
13 * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
14 * notice is a summary of the Click LICENSE file; the license in that file is
15 * legally binding.
16 */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 #include <lcdf/vector.hh>
22 #include <string.h>
23
24 Vector<void*>::Vector(const Vector<void*> &o)
25 : _l(0), _n(0), _capacity(0)
26 {
27 *this = o;
28 }
29
30 Vector<void*>::~Vector()
31 {
32 delete[] _l;
33 }
34
35 Vector<void*> &
36 Vector<void*>::operator=(const Vector<void*> &o)
37 {
38 if (&o != this) {
39 _n = 0;
40 if (reserve(o._n) && o._n) {
41 _n = o._n;
42 memcpy(_l, o._l, sizeof(void *) * _n);
43 }
44 }
45 return *this;
46 }
47
48 Vector<void*> &
49 Vector<void*>::assign(int n, void *e)
50 {
51 _n = 0;
52 resize(n, e);
53 return *this;
54 }
55
56 bool
57 Vector<void*>::reserve(int want)
58 {
59 if (want < 0)
60 want = (_capacity > 0 ? _capacity * 2 : 4);
61 if (want <= _capacity)
62 return true;
63
64 void **new_l = new void*[want];
65 if (!new_l)
66 return false;
67
68 if (_n) {
69 memcpy(new_l, _l, sizeof(void*) * _n);
70 }
71 delete[] _l;
72
73 _l = new_l;
74 _capacity = want;
75 return true;
76 }
77
78 Vector<void*>::iterator
79 Vector<void*>::erase(iterator a, iterator b)
80 {
81 if (b > a) {
82 assert(a >= begin() && b <= end());
83 memmove(a, b, (end() - b) * sizeof(void*));
84 _n -= b - a;
85 return a;
86 } else
87 return b;
88 }
89
90 void
91 Vector<void*>::resize(int nn, void *e)
92 {
93 if (nn <= _capacity || reserve(nn)) {
94 for (int i = _n; i < nn; i++)
95 _l[i] = e;
96 _n = nn;
97 }
98 }
99
100 void
101 Vector<void*>::swap(Vector<void*>& o)
102 {
103 void **l = _l;
104 int n = _n;
105 int cap = _capacity;
106 _l = o._l;
107 _n = o._n;
108 _capacity = o._capacity;
109 o._l = l;
110 o._n = n;
111 o._capacity = cap;
112 }