1 /*
2  * vectorv.cc -- template specialization for Vector<void*>
3  * Eddie Kohler
4  *
5  * Copyright (c) 1999-2004 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 
Vector(const Vector<void * > & o)24 Vector<void*>::Vector(const Vector<void*> &o)
25     : _l(0), _n(0), _capacity(0)
26 {
27     *this = o;
28 }
29 
~Vector()30 Vector<void*>::~Vector()
31 {
32     delete[] _l;
33 }
34 
35 Vector<void*> &
operator =(const Vector<void * > & o)36 Vector<void*>::operator=(const Vector<void*> &o)
37 {
38     if (&o != this) {
39 	_n = 0;
40 	if (reserve(o._n)) {
41 	    _n = o._n;
42 	    memcpy(_l, o._l, sizeof(void *) * _n);
43 	}
44     }
45     return *this;
46 }
47 
48 Vector<void*> &
assign(int n,void * e)49 Vector<void*>::assign(int n, void *e)
50 {
51     _n = 0;
52     resize(n, e);
53     return *this;
54 }
55 
56 bool
reserve(int want)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     memcpy(new_l, _l, sizeof(void*) * _n);
69     delete[] _l;
70 
71     _l = new_l;
72     _capacity = want;
73     return true;
74 }
75 
76 Vector<void*>::iterator
erase(iterator a,iterator b)77 Vector<void*>::erase(iterator a, iterator b)
78 {
79     if (b > a) {
80 	assert(a >= begin() && b <= end());
81 	memmove(a, b, (end() - b) * sizeof(void*));
82 	_n -= b - a;
83 	return a;
84     } else
85 	return b;
86 }
87 
88 void
resize(int nn,void * e)89 Vector<void*>::resize(int nn, void *e)
90 {
91     if (nn <= _capacity || reserve(nn)) {
92 	for (int i = _n; i < nn; i++)
93 	    _l[i] = e;
94 	_n = nn;
95     }
96 }
97 
98 void
swap(Vector<void * > & o)99 Vector<void*>::swap(Vector<void*>& o)
100 {
101     void **l = _l;
102     int n = _n;
103     int cap = _capacity;
104     _l = o._l;
105     _n = o._n;
106     _capacity = o._capacity;
107     o._l = l;
108     o._n = n;
109     o._capacity = cap;
110 }
111