Click
Loading...
Searching...
No Matches
vector.cc
1/*
2 * vector.{cc,hh} -- simple array template class
3 * Eddie Kohler
4 *
5 * Copyright (c) 1999-2000 Massachusetts Institute of Technology
6 * Copyright (c) 2006 Regents of the University of California
7 * Copyright (c) 2011 Eddie Kohler
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, subject to the conditions
12 * listed in the Click LICENSE file. These conditions include: you must
13 * preserve this copyright notice, and you cannot mention the copyright
14 * holders in advertising related to the Software without their permission.
15 * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
16 * notice is a summary of the Click LICENSE file; the license in that file is
17 * legally binding.
18 */
19
20#ifndef CLICK_VECTOR_CC
21#define CLICK_VECTOR_CC
22#include <click/glue.hh>
23#include <click/vector.hh>
24CLICK_DECLS
27template <typename AM, size_t ALIGNMENTM>
28vector_memory<AM,ALIGNMENTM>::~vector_memory()
29{
30 AM::destroy(l_, n_);
31 CLICK_LFREE(l_, capacity_ * sizeof(type));
32}
33
34template <typename AM, size_t ALIGNMENTM>
35void vector_memory<AM,ALIGNMENTM>::assign(const vector_memory<AM,ALIGNMENTM> &x)
36{
37 if (&x != this) {
38 AM::destroy(l_, n_);
39 AM::mark_noaccess(l_, n_);
40 n_ = 0;
41 if (reserve_and_push_back(x.n_, 0)) {
42 n_ = x.n_;
43 AM::mark_undefined(l_, n_);
44 AM::copy(l_, x.l_, n_);
45 }
46 }
47}
48
49template <typename AM, size_t ALIGNMENTM>
50void vector_memory<AM,ALIGNMENTM>::assign(size_type n, const type *vp)
51{
52 if (unlikely(need_argument_copy(vp))) {
53 type v_copy(*vp);
54 return assign(n, &v_copy);
55 }
56
57 resize(0, vp);
58 resize(n, vp);
59}
60
61template <typename AM, size_t ALIGNMENTM>
62typename vector_memory<AM,ALIGNMENTM>::iterator vector_memory<AM,ALIGNMENTM>::insert(iterator it, const type *vp)
63{
64 assert(it >= begin() && it <= end());
65 if (unlikely(need_argument_copy(vp))) {
66 type v_copy(*vp);
67 return insert(it, &v_copy);
68 }
69
70 if (n_ == capacity_) {
71 size_type pos = it - begin();
72 if (!reserve_and_push_back(-1, 0))
73 return end();
74 it = begin() + pos;
75 }
76 AM::mark_undefined(l_ + n_, 1);
77 AM::move(it + 1, it, end() - it);
78 AM::mark_undefined(it, 1);
79 AM::fill(it, 1, vp);
80 ++n_;
81 return it;
82}
83
84template <typename AM, size_t ALIGNMENTM>
85typename vector_memory<AM,ALIGNMENTM>::iterator vector_memory<AM,ALIGNMENTM>::erase(iterator a, iterator b)
86{
87 if (a < b) {
88 assert(a >= begin() && b <= end());
89 AM::move_onto(a, b, end() - b);
90 n_ -= b - a;
91 AM::destroy(end(), b - a);
92 AM::mark_noaccess(end(), b - a);
93 return a;
94 } else
95 return b;
96}
97
98template <typename AM, size_t ALIGNMENTM>
99bool vector_memory<AM,ALIGNMENTM>::reserve_and_push_back(size_type want, const type *push_vp)
100{
101 if (unlikely(push_vp && need_argument_copy(push_vp))) {
102 type push_v_copy(*push_vp);
103 return reserve_and_push_back(want, &push_v_copy);
104 }
105
106 if (want < 0)
107 want = (capacity_ > 0 ? capacity_ * 2 : 4);
108
109 if (want > capacity_) {
110 type *new_l = (type *) CLICK_ALIGNED_ALLOC_T(want * sizeof(type),ALIGNMENTM);
111 if (!new_l)
112 return false;
113 AM::mark_noaccess(new_l + n_, want - n_);
114 AM::move(new_l, l_, n_);
115 CLICK_ALIGNED_FREE(l_, capacity_ * sizeof(type));
116 l_ = new_l;
117 capacity_ = want;
118 }
119
120 if (unlikely(push_vp))
121 push_back(push_vp);
122 return true;
123}
124
125template <typename AM, size_t ALIGNMENTM>
126void vector_memory<AM,ALIGNMENTM>::resize(size_type n, const type *vp)
127{
128 if (unlikely(need_argument_copy(vp))) {
129 type v_copy(*vp);
130 return resize(n, &v_copy);
131 }
132
133 if (n <= capacity_ || reserve_and_push_back(n, 0)) {
134 assert(n >= 0);
135 if (n < n_) {
136 AM::destroy(l_ + n, n_ - n);
137 AM::mark_noaccess(l_ + n, n_ - n);
138 }
139 if (n_ < n) {
140 AM::mark_undefined(l_ + n_, n - n_);
141 AM::fill(l_ + n_, n - n_, vp);
142 }
143 n_ = n;
144 }
145}
146
147template <typename AM, size_t ALIGNMENTM>
148void vector_memory<AM,ALIGNMENTM>::swap(vector_memory<AM,ALIGNMENTM> &x)
149{
150 type *l = l_;
151 l_ = x.l_;
152 x.l_ = l;
153
154 size_type n = n_;
155 n_ = x.n_;
156 x.n_ = n;
157
158 size_type capacity = capacity_;
159 capacity_ = x.capacity_;
160 x.capacity_ = capacity;
161}
162
164CLICK_ENDDECLS
165#endif
Click's vector container template.