blob: eae674b81dcb3eab56026dde2a8456affc723a8f [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/**@file Simplified Vector template with aliases. */
2/*
3* Copyright 2008 Free Software Foundation, Inc.
4*
5* This software is distributed under the terms of the GNU Affero Public License.
6* See the COPYING file in the main directory for details.
7*
8* This use of this software may be subject to additional restrictions.
9* See the LEGAL file in the main directory for details.
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU Affero General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU Affero General Public License for more details.
20
21 You should have received a copy of the GNU Affero General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24*/
25
26
27
28
29#ifndef VECTOR_H
30#define VECTOR_H
31
32#include <string.h>
33#include <iostream>
34#include <assert.h>
kurtis.heimerl5a872472013-05-31 21:47:25 +000035// We cant use Logger.h in this file...
36extern int gVectorDebug;
37#define BVDEBUG(msg) if (gVectorDebug) {std::cout << msg;}
38
dburgess82c46ff2011-10-07 02:40:51 +000039
40
41/**
42 A simplified Vector template with aliases.
43 Unlike std::vector, this class does not support dynamic resizing.
44 Unlike std::vector, this class does support "aliases" and subvectors.
45*/
46template <class T> class Vector {
47
48 // TODO -- Replace memcpy calls with for-loops.
49
50 public:
51
52 /**@name Iterator types. */
53 //@{
54 typedef T* iterator;
55 typedef const T* const_iterator;
56 //@}
57
58 protected:
59
60 T* mData; ///< allocated data block, if any
61 T* mStart; ///< start of useful data
62 T* mEnd; ///< end of useful data + 1
63
64 public:
65
kurtis.heimerl5a872472013-05-31 21:47:25 +000066 /****
67 char *inspect() {
68 static char buf[100];
69 sprintf(buf," mData=%p mStart=%p mEnd=%p ",mData,mStart,mEnd);
70 return buf;
71 }
72 ***/
73
dburgess82c46ff2011-10-07 02:40:51 +000074 /** Return the size of the Vector. */
75 size_t size() const
76 {
77 assert(mStart>=mData);
78 assert(mEnd>=mStart);
79 return mEnd - mStart;
80 }
81
82 /** Return size in bytes. */
83 size_t bytes() const { return size()*sizeof(T); }
84
85 /** Change the size of the Vector, discarding content. */
86 void resize(size_t newSize)
87 {
88 if (mData!=NULL) delete[] mData;
89 if (newSize==0) mData=NULL;
90 else mData = new T[newSize];
91 mStart = mData;
92 mEnd = mStart + newSize;
93 }
94
Alexander Chemerisf0189c42017-03-18 14:08:43 -070095 /** Reduce addressable size of the Vector, keeping content. */
96 void shrink(size_t newSize)
97 {
98 assert(newSize <= mEnd - mStart);
99 mEnd = mStart + newSize;
100 }
101
dburgess82c46ff2011-10-07 02:40:51 +0000102 /** Release memory and clear pointers. */
103 void clear() { resize(0); }
104
105
106 /** Copy data from another vector. */
107 void clone(const Vector<T>& other)
108 {
109 resize(other.size());
110 memcpy(mData,other.mStart,other.bytes());
111 }
112
113
114
115
116 //@{
117
118 /** Build an empty Vector of a given size. */
119 Vector(size_t wSize=0):mData(NULL) { resize(wSize); }
120
121 /** Build a Vector by shifting the data block. */
122 Vector(Vector<T>& other)
123 :mData(other.mData),mStart(other.mStart),mEnd(other.mEnd)
124 { other.mData=NULL; }
125
126 /** Build a Vector by copying another. */
127 Vector(const Vector<T>& other):mData(NULL) { clone(other); }
128
129 /** Build a Vector with explicit values. */
130 Vector(T* wData, T* wStart, T* wEnd)
131 :mData(wData),mStart(wStart),mEnd(wEnd)
132 { }
133
134 /** Build a vector from an existing block, NOT to be deleted upon destruction. */
135 Vector(T* wStart, size_t span)
136 :mData(NULL),mStart(wStart),mEnd(wStart+span)
137 { }
138
139 /** Build a Vector by concatenation. */
140 Vector(const Vector<T>& other1, const Vector<T>& other2)
141 :mData(NULL)
142 {
143 resize(other1.size()+other2.size());
144 memcpy(mStart, other1.mStart, other1.bytes());
145 memcpy(mStart+other1.size(), other2.mStart, other2.bytes());
146 }
147
148 //@}
149
150 /** Destroy a Vector, deleting held memory. */
151 ~Vector() { clear(); }
152
153
154
155
156 //@{
157
158 /** Assign from another Vector, shifting ownership. */
159 void operator=(Vector<T>& other)
160 {
161 clear();
162 mData=other.mData;
163 mStart=other.mStart;
164 mEnd=other.mEnd;
165 other.mData=NULL;
166 }
167
168 /** Assign from another Vector, copying. */
169 void operator=(const Vector<T>& other) { clone(other); }
170
171 //@}
172
173
174 //@{
175
176 /** Return an alias to a segment of this Vector. */
177 Vector<T> segment(size_t start, size_t span)
178 {
179 T* wStart = mStart + start;
180 T* wEnd = wStart + span;
181 assert(wEnd<=mEnd);
182 return Vector<T>(NULL,wStart,wEnd);
183 }
184
185 /** Return an alias to a segment of this Vector. */
186 const Vector<T> segment(size_t start, size_t span) const
187 {
188 T* wStart = mStart + start;
189 T* wEnd = wStart + span;
190 assert(wEnd<=mEnd);
191 return Vector<T>(NULL,wStart,wEnd);
192 }
193
194 Vector<T> head(size_t span) { return segment(0,span); }
195 const Vector<T> head(size_t span) const { return segment(0,span); }
196 Vector<T> tail(size_t start) { return segment(start,size()-start); }
197 const Vector<T> tail(size_t start) const { return segment(start,size()-start); }
198
199 /**
200 Copy part of this Vector to a segment of another Vector.
201 @param other The other vector.
202 @param start The start point in the other vector.
203 @param span The number of elements to copy.
204 */
205 void copyToSegment(Vector<T>& other, size_t start, size_t span) const
206 {
207 T* base = other.mStart + start;
208 assert(base+span<=other.mEnd);
209 assert(mStart+span<=mEnd);
210 memcpy(base,mStart,span*sizeof(T));
211 }
212
213 /** Copy all of this Vector to a segment of another Vector. */
214 void copyToSegment(Vector<T>& other, size_t start=0) const { copyToSegment(other,start,size()); }
215
216 void copyTo(Vector<T>& other) const { copyToSegment(other,0,size()); }
217
218 /**
219 Copy a segment of this vector into another.
220 @param other The other vector (to copt into starting at 0.)
221 @param start The start point in this vector.
222 @param span The number of elements to copy.
223 */
224 void segmentCopyTo(Vector<T>& other, size_t start, size_t span) const
225 {
226 const T* base = mStart + start;
227 assert(base+span<=mEnd);
228 assert(other.mStart+span<=other.mEnd);
229 memcpy(other.mStart,base,span*sizeof(T));
230 }
231
Alexander Chemerisc7088162017-03-18 13:43:22 -0700232 /**
233 Move (copy) a segment of this vector into a different position in the vector
234 @param from Start point from which to copy.
235 @param to Start point to which to copy.
236 @param span The number of elements to copy.
237 */
238 void segmentMove(size_t from, size_t to, size_t span)
239 {
240 const T* baseFrom = mStart + from;
241 T* baseTo = mStart + to;
242 assert(baseFrom+span<=mEnd);
243 assert(baseTo+span<=mEnd);
244 memmove(baseTo,baseFrom,span*sizeof(T));
245 }
246
dburgess82c46ff2011-10-07 02:40:51 +0000247 void fill(const T& val)
248 {
249 T* dp=mStart;
250 while (dp<mEnd) *dp++=val;
251 }
252
253 void fill(const T& val, unsigned start, unsigned length)
254 {
255 T* dp=mStart+start;
256 T* end=dp+length;
257 assert(end<=mEnd);
258 while (dp<end) *dp++=val;
259 }
260
261
262 //@}
263
264
265 //@{
266
267 T& operator[](size_t index)
268 {
269 assert(mStart+index<mEnd);
270 return mStart[index];
271 }
272
273 const T& operator[](size_t index) const
274 {
275 assert(mStart+index<mEnd);
276 return mStart[index];
277 }
278
279 const T* begin() const { return mStart; }
280 T* begin() { return mStart; }
281 const T* end() const { return mEnd; }
282 T* end() { return mEnd; }
kurtis.heimerl5a872472013-05-31 21:47:25 +0000283 bool isOwner() { return !!mData; } // Do we own any memory ourselves?
dburgess82c46ff2011-10-07 02:40:51 +0000284 //@}
285
286
287};
288
289
290
291
292/** Basic print operator for Vector objects. */
293template <class T>
294std::ostream& operator<<(std::ostream& os, const Vector<T>& v)
295{
296 for (unsigned i=0; i<v.size(); i++) os << v[i] << " ";
297 return os;
298}
299
300
301
302#endif
303// vim: ts=4 sw=4