00001
00002
00003
00004
00005
00006
00007 #ifndef FIXVEC_H
00008 #define FIXVEC_H
00009
00010 #include <cstdarg>
00011 #include <cstring>
00012
00013 #include "debug.h"
00014
00015
00016
00017
00018 template <class TYPE, int SIZE> class FixedVector
00019 {
00020
00021
00022
00023
00024 public:
00025 typedef TYPE value_type;
00026 typedef TYPE& reference;
00027 typedef const TYPE& const_reference;
00028 typedef TYPE* pointer;
00029 typedef const TYPE* const_pointer;
00030
00031 typedef unsigned long size_type;
00032 typedef long difference_type;
00033
00034 typedef TYPE* iterator;
00035 typedef const TYPE* const_iterator;
00036
00037
00038
00039
00040 public:
00041 ~FixedVector() {}
00042
00043 FixedVector() {}
00044
00045 FixedVector(TYPE def) : mData()
00046 {
00047 init(def);
00048 }
00049
00050 FixedVector(TYPE value0, TYPE value1, ...);
00051
00052
00053
00054
00055
00056
00057
00058 public:
00059
00060 bool empty() const { return SIZE == 0; }
00061 size_t size() const { return SIZE; }
00062
00063
00064 TYPE& operator[](unsigned long index) {
00065 ASSERT(index < SIZE);
00066 return mData[index];
00067 }
00068
00069 const TYPE& operator[](unsigned long index) const {
00070 ASSERT(index < SIZE);
00071 return mData[index];
00072 }
00073
00074 const TYPE* buffer() const { return mData; }
00075 TYPE* buffer() { return mData; }
00076
00077
00078 iterator begin() { return mData; }
00079 const_iterator begin() const { return mData; }
00080
00081 iterator end() { return this->begin() + this->size(); }
00082 const_iterator end() const { return this->begin() + this->size(); }
00083 void init(const TYPE& def);
00084
00085
00086
00087
00088 protected:
00089 TYPE mData[SIZE];
00090 };
00091
00092
00093
00094
00095
00096 template <class TYPE, int SIZE>
00097 FixedVector<TYPE, SIZE>::FixedVector(TYPE value0, TYPE value1, ...)
00098 {
00099 mData[0] = value0;
00100 mData[1] = value1;
00101
00102 va_list ap;
00103 va_start(ap, value1);
00104
00105 for (int index = 2; index < SIZE; index++)
00106 {
00107 TYPE value = va_arg(ap, TYPE);
00108 mData[index] = value;
00109 }
00110
00111 va_end(ap);
00112 }
00113
00114 template <class TYPE, int SIZE>
00115 void FixedVector<TYPE, SIZE>::init(const TYPE& def)
00116 {
00117 for (int i = 0; i < SIZE; ++i)
00118 mData[i] = def;
00119 }
00120
00121 #endif // FIXVEC_H