3 * Summary: Fixed size vector class that asserts if you do something bad.
4 * Written by: Jesse Jones
14 // ==========================================================================
16 // ==========================================================================
18 template <class TYPE
, int SIZE
> class FixedVector
21 //-----------------------------------
25 typedef TYPE value_type
;
26 typedef TYPE
& reference
;
27 typedef const TYPE
& const_reference
;
28 typedef TYPE
* pointer
;
29 typedef const TYPE
* const_pointer
;
31 typedef unsigned long size_type
;
32 typedef long difference_type
;
34 typedef TYPE
* iterator
;
35 typedef const TYPE
* const_iterator
;
37 //-----------------------------------
38 // Initialization/Destruction
45 FixedVector(TYPE def
) : mData()
50 FixedVector(TYPE value0
, TYPE value1
, ...);
51 // Allows for something resembling C array initialization, eg
52 // instead of "int a[3] = {0, 1, 2}" you'd use "FixedVector<int, 3>
53 // a(0, 1, 2)". Note that there must be SIZE arguments.
55 //-----------------------------------
60 bool empty() const { return SIZE
== 0; }
61 size_t size() const { return SIZE
; }
64 TYPE
& operator[](unsigned long index
)
69 // Intentionally printed as signed, it's very, very unlikely we'd
70 // have a genuine big number here, but underflows are common.
71 die("FixedVector out of bounds (%ld / %ld)", (signed long)index
,
78 const TYPE
& operator[](unsigned long index
) const
83 die("FixedVector out of bounds (%ld / %ld)", (signed long)index
,
90 const TYPE
* buffer() const { return mData
; }
91 TYPE
* buffer() { return mData
; }
93 // ----- Iterating -----
94 iterator
begin() { return mData
; }
95 const_iterator
begin() const { return mData
; }
97 iterator
end() { return this->begin() + this->size(); }
98 const_iterator
end() const { return this->begin() + this->size(); }
99 void init(const TYPE
& def
);
101 //-----------------------------------
109 // ==========================================================================
111 // ==========================================================================
112 template <class TYPE
, int SIZE
>
113 FixedVector
<TYPE
, SIZE
>::FixedVector(TYPE value0
, TYPE value1
, ...)
119 va_start(ap
, value1
); // second argument is last fixed parameter
121 for (int index
= 2; index
< SIZE
; index
++)
123 TYPE value
= va_arg(ap
, TYPE
);
124 mData
[index
] = value
;
130 template <class TYPE
, int SIZE
>
131 void FixedVector
<TYPE
, SIZE
>::init(const TYPE
& def
)
133 for (int i
= 0; i
< SIZE
; ++i
)