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