File: simplevector.h

package info (click to toggle)
povray 1%3A3.7.0.10-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 147,232 kB
  • sloc: cpp: 845,011; ansic: 122,118; sh: 34,204; pascal: 6,420; asm: 3,355; ada: 1,681; makefile: 1,389; cs: 879; awk: 590; perl: 245; xml: 95
file content (565 lines) | stat: -rw-r--r-- 13,563 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*******************************************************************************
 * simplevector.h
 *
 * Very simple, basic vector-like classes containing just enough functionality
 * for their intended uses within POV. Flexibility is sacrificed for performance
 * as these classes will typically be used in places where they may constructed
 * and destroyed many millions of times per render. (For example, mediasky.pov
 * rendered at only 160x120 with no AA results in over 16 million instances of
 * construction of a FixedSimpleVector).
 *
 * These classes were added after extensive profiling pointed to a number of
 * instances of our use of std::vector causing slowdowns, particularly when
 * multiple threads were in use (due to locks in the RTL memory management used
 * to prevent heap corruption). Experiments with non-heap-based allocators (e.g.
 * refpools or thread-local storage) did improve the situation somewhat but weren't
 * enough, hence this file. At the time of writing we get about a 10% improvement
 * as compared to the old code.
 *
 * NOTE NOTE NOTE NOTE
 * -------------------
 * Be aware that these classes do NOT run destructors on contained objects.
 * This is intentional as we currently do not store any objects in them that
 * require this functionality.
 *
 * Author: Christopher J. Cason.
 *
 * ---------------------------------------------------------------------------
 * Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
 * Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
 *
 * POV-Ray is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * POV-Ray is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------------
 * POV-Ray is based on the popular DKB raytracer version 2.12.
 * DKBTrace was originally written by David K. Buck.
 * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
 * ---------------------------------------------------------------------------
 * $File: //depot/public/povray/3.x/source/backend/support/simplevector.h $
 * $Revision: #1 $
 * $Change: 6069 $
 * $DateTime: 2013/11/06 11:59:40 $
 * $Author: chrisc $
 *******************************************************************************/

#ifndef __SIMPLEVECTOR__
#define __SIMPLEVECTOR__

#include <stdexcept>
#include "base/pov_err.h"

namespace pov
{

////////////////////////////////////////////////////////////////////////////
// Works like std::vector in some ways, but very limited and not at all as
// flexible. Does not implement all the methods of std::vector (just what
// is needed in POV). Has different allocation behaviour, which will probably
// need to be tweaked over time for best performance.
//
// Be aware that this class does NOT run destructors on contained objects.
// This is intentional as we currently do not store any objects in it that
// require this functionality. // TODO FIXME
////////////////////////////////////////////////////////////////////////////
template<class ContainerType, class Allocator = std::allocator<ContainerType> >
class SimpleVector
{
public:
	typedef SimpleVector<ContainerType> MyType;
	typedef size_t size_type;
	typedef size_t difference_type;
	typedef ContainerType *pointer;
	typedef ContainerType& reference;
	typedef ContainerType value_type;
	typedef const ContainerType *const_pointer;
	typedef const ContainerType& const_reference;
	typedef const ContainerType *const_iterator;
	typedef ContainerType *iterator;
	typedef Allocator allocator;
	typedef std::reverse_iterator<iterator> reverse_iterator;
	typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

	SimpleVector()
	{
		m_First = m_Last = m_End = NULL;
	}

	SimpleVector(size_type nItems, const ContainerType& InitialVal)
	{
		m_First = m_Last = m_End = NULL;
		if (nItems)
			allocate (nItems, InitialVal);
	}

	SimpleVector(const MyType& RHS)
	{
		if (RHS.m_First != RHS.m_Last)
		{
			allocate (RHS.capacity());
			for (pointer p = RHS.m_First ; p != RHS.m_Last ; )
				*m_Last++ = *p++;
		}
		else
			m_First = m_Last = m_End = NULL;
	}

	~SimpleVector()
	{
		// we don't call destructors, even if they exist
		if (m_First != NULL)
			deallocate ();
	}

	MyType& operator=(const MyType& RHS)
	{
		if (RHS.size() > capacity())
		{
			if (m_First != NULL)
				deallocate ();
			allocate (RHS.size());
		}
		m_Last = m_First;
		for (pointer p = RHS.m_First ; p != RHS.m_Last ; )
			*m_Last++ = *p++;
		return (*this);
	}

	size_type capacity() const
	{
		return (m_End - m_First);
	}

	iterator begin()
	{
		return (m_First);
	}

	const_iterator begin() const
	{
		return (m_First);
	}

	iterator end()
	{
		return (m_Last);
	}

	const_iterator end() const
	{
		return (m_Last);
	}

	reverse_iterator rbegin()
	{
		return (reverse_iterator (m_Last));
	}

	const_reverse_iterator rbegin() const
	{
		return (const_reverse_iterator (m_Last));
	}

	reverse_iterator rend()
	{
		return (reverse_iterator (m_First));
	}

	const_reverse_iterator rend() const
	{
		return (const_reverse_iterator (m_First));
	}

	size_type size() const
	{
		return (m_Last - m_First);
	}

	size_type max_size() const
	{
		return (alloc.max_size ());
	}

	bool empty() const
	{
		return (m_First == m_Last);
	}

	const_reference at(size_type Index) const
	{
		if (Index > size())
			throw std::out_of_range ("index out of range in SimpleVector::at");
		return (m_First [Index]);
	}

	reference at(size_type Index)
	{
		if (Index > size())
			throw std::out_of_range ("index out of range in SimpleVector::at");
		return (m_First [Index]);
	}

	const_reference operator[](size_type Index) const
	{
		return (m_First [Index]);
	}

	reference operator[](size_type Index)
	{
		return (m_First [Index]);
	}

	reference front()
	{
		return (*m_First);
	}

	const_reference front() const
	{
		return (*m_First);
	}

	reference back()
	{
		return (*(m_Last - 1));
	}

	const_reference back() const
	{
		return (*(m_Last - 1));
	}

	void push_back(const ContainerType& NewVal)
	{
		if (m_Last < m_End)
		{
			*m_Last++ = NewVal;
			return;
		}
		insert(m_Last, NewVal);
	}

	void pop_back()
	{
		if (m_Last > m_First)
			--m_Last;
	}

	iterator insert(iterator Where, const ContainerType& NewVal)
	{
		size_type Index = 0;

		if (m_Last > m_First)
			Index = Where - m_First;

		if (m_Last == m_End)
		{
			size_type c = size() + 1;
			size_type n = capacity();
			size_type nc = n * 2;
			if (nc < 8)
				nc = 8;
			pointer p = alloc.allocate (nc);
			p [Index] = NewVal;
			for (size_type i = 0 ; i < Index ; i++)
				p [i] = m_First [i];
			for (size_type i = Index + 1 ; i < c ; i++)
				p [i] = m_First [i];
			if (m_First != NULL)
				alloc.deallocate (m_First, n);
			m_First = p;
			m_End = m_First + nc;
			m_Last = m_First + c;
			return (m_First + Index);
		}

		if (Index == size())
		{
			*m_Last = NewVal;
			return (m_Last++);
		}

		for (size_type i = size() ; i > Index ; i--)
			m_First [i] = m_First [i - 1];
		m_Last++;
		m_First [Index] = NewVal;
		return (m_First + Index);
	}

	iterator erase(iterator What)
	{
		size_type Index = What - begin();

		if (What == m_Last - 1)
			return (m_Last--);

		for (pointer p1 = What, p2 = What + 1 ; p2 < m_Last ; )
			*p1++ = *p2++;
		m_Last--;
		return (++What);
	}

	void clear()
	{
		m_Last = m_First;
	}

private:
	void allocate (size_type nItems)
	{
		m_Last = m_First = alloc.allocate (nItems);
		m_End = m_First + nItems;
	}

	void allocate (size_type nItems, const ContainerType& InitialVal)
	{
		m_Last = m_First = alloc.allocate (nItems);
		m_End = m_First + nItems;
		while (nItems--)
			*m_Last++ = InitialVal;
	}

	void deallocate ()
	{
		alloc.deallocate (m_First, m_End - m_First);
	}

	allocator alloc;
	pointer m_First;
	pointer m_End;
	pointer m_Last;
};

////////////////////////////////////////////////////////////////////////////
// This template class requires a maximum size (ElementCount) and maintains
// its storage internally (typically therefore this will end up on the stack
// rather than being allocated upon request). The up side to this behaviour
// is that no time is spent obtaining memory from a pool, or for that matter
// copying data if a reallocation is needed. The down side is that firstly
// it cannot expand beyond the maximum size specified, and secondly that
// binary copies of the object take longer because they contain the entire
// storage (even if no entries are allocated).
//
// Be aware that this class does NOT run destructors on contained objects.
// This is intentional as we currently do not store any objects in it that
// require this functionality.
////////////////////////////////////////////////////////////////////////////
template<class ContainerType, int ElementCount>
class FixedSimpleVector
{
public:
	typedef FixedSimpleVector<ContainerType, ElementCount> MyType;
	typedef size_t size_type;
	typedef size_t difference_type;
	typedef ContainerType *pointer;
	typedef ContainerType& reference;
	typedef ContainerType value_type;
	typedef const ContainerType *const_pointer;
	typedef const ContainerType& const_reference;
	typedef const ContainerType *const_iterator;
	typedef ContainerType *iterator;
	typedef std::reverse_iterator<iterator> reverse_iterator;
	typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

	FixedSimpleVector() :
		m_Last ((pointer) m_Data),
		m_End (pointer (m_Data) + ElementCount)
	{
	}

	FixedSimpleVector(size_type nItems, const ContainerType& InitialVal) :
		m_Last ((pointer) m_Data),
		m_End (pointer (m_Data) + ElementCount)
	{
		if (nItems > ElementCount)
			throw POV_EXCEPTION(pov_base::kInternalLimitErr, "Internal limit exceeded in FixedSimpleVector");
		while (nItems--)
			*m_Last++ = InitialVal;
	}

	FixedSimpleVector(const MyType& RHS) :
		m_Last ((pointer) m_Data),
		m_End (pointer (m_Data) + ElementCount)
	{
		for (pointer p = pointer (RHS.m_Data) ; p != RHS.m_Last ; )
			*m_Last++ = *p++;
	}

	~FixedSimpleVector()
	{
		// we don't call destructors, even if they exist
	}

	MyType& operator=(const MyType& RHS)
	{
		if (RHS.size() > ElementCount)
			throw POV_EXCEPTION(pov_base::kInternalLimitErr, "Internal limit exceeded in FixedSimpleVector");
		m_Last = pointer (m_Data);
		for (pointer p = pointer (RHS.m_Data) ; p != RHS.m_Last ; )
			*m_Last++ = *p++;
		return (*this);
	}

	size_type capacity() const
	{
		return (ElementCount);
	}

	iterator begin()
	{
		return (reinterpret_cast<pointer>(&(m_Data[0])));
	}

	const_iterator begin() const
	{
		return (reinterpret_cast<const_pointer>(&(m_Data[0])));
	}

	iterator end()
	{
		return (m_Last);
	}

	const_iterator end() const
	{
		return (m_Last);
	}

	reverse_iterator rbegin()
	{
		return (reverse_iterator (m_Last));
	}

	const_reverse_iterator rbegin() const
	{
		return (const_reverse_iterator (m_Last));
	}

	reverse_iterator rend()
	{
		return (reverse_iterator (pointer (m_Data)));
	}

	const_reverse_iterator rend() const
	{
		return (const_reverse_iterator (pointer (m_Data)));
	}

	size_type size() const
	{
		return (m_Last - const_pointer (m_Data));
	}

	size_type max_size() const
	{
		return (ElementCount);
	}

	bool empty() const
	{
		return (const_pointer (m_Data) == m_Last);
	}

	const_reference at(size_type Index) const
	{
		if (Index > size())
			throw std::out_of_range ("index out of range in FixedSimpleVector::at");
		return (pointer (m_Data) [Index]);
	}

	reference at(size_type Index)
	{
		if (Index > size())
			throw std::out_of_range ("index out of range in FixedSimpleVector::at");
		return (pointer (m_Data) [Index]);
	}

	const_reference operator[](size_type Index) const
	{
		return (pointer (m_Data) [Index]);
	}

	reference operator[](size_type Index)
	{
		return (pointer (m_Data) [Index]);
	}

	reference front()
	{
		return (*pointer (m_Data));
	}

	const_reference front() const
	{
		return (*pointer (m_Data));
	}

	reference back()
	{
		return (*(m_Last - 1));
	}

	const_reference back() const
	{
		return (*(m_Last - 1));
	}

	void push_back(const ContainerType& NewVal)
	{
		if (m_Last == m_End)
			throw POV_EXCEPTION(pov_base::kInternalLimitErr, "Internal limit exceeded in FixedSimpleVector");
		*m_Last++ = NewVal;
	}

	void pop_back()
	{
		if (m_Last > pointer (m_Data))
			--m_Last;
	}

	iterator insert(iterator Where, const ContainerType& NewVal)
	{
		if (m_Last == m_End)
			throw POV_EXCEPTION(pov_base::kInternalLimitErr, "Internal limit exceeded in FixedSimpleVector");
		for (pointer p1 = Where, p2 = Where + 1 ; p1 < m_Last ; )
			*p2++ = *p1++;
		m_Last++;
		*Where = NewVal;
		return (Where);
	}

	iterator erase(iterator Where)
	{
		if (Where == m_End)
			throw POV_EXCEPTION(pov_base::kInternalLimitErr, "Attempt to erase past end of vector");
		if (Where == m_Last - 1)
			return (m_Last--);
		for (pointer p1 = Where, p2 = Where + 1 ; p2 < m_Last ; )
			*p1++ = *p2++;
		m_Last--;
		return (++Where);
	}

	void clear()
	{
		m_Last = pointer (m_Data);
	}

private:
	unsigned char m_Data [sizeof (value_type) * ElementCount];
	const pointer m_End;
	pointer m_Last;
};

}

#endif