rayzor

annotate src/stl/vector.h @ 14:a9a948809c6f

starting the renderer screen, plus misc stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 13 Apr 2014 08:06:21 +0300
parents 70e332156d02
children 5380ff64e83f
rev   line source
nuclear@4 1 /* vi:set ft=cpp: */
nuclear@4 2 #ifndef VECTOR_H_
nuclear@4 3 #define VECTOR_H_
nuclear@4 4
nuclear@12 5 #include <stdlib.h>
nuclear@12 6
nuclear@4 7 template <class T>
nuclear@4 8 class vector {
nuclear@4 9 private:
nuclear@4 10 T *data;
nuclear@4 11 size_t num_items, max_items;
nuclear@4 12
nuclear@4 13 public:
nuclear@4 14 vector()
nuclear@4 15 {
nuclear@4 16 data = 0;
nuclear@4 17 num_items = max_items = 0;
nuclear@4 18 }
nuclear@4 19
nuclear@4 20 vector(const vector &v)
nuclear@4 21 {
nuclear@4 22 data = 0;
nuclear@4 23 num_items = max_items = 0;
nuclear@4 24 resize(v.size());
nuclear@4 25
nuclear@4 26 for(size_t i=0; i<v.size(); i++) {
nuclear@4 27 data[i] = v.data[i];
nuclear@4 28 }
nuclear@4 29 }
nuclear@4 30
nuclear@4 31 vector &operator =(const vector &v)
nuclear@4 32 {
nuclear@4 33 if(&v != this) {
nuclear@4 34 clear();
nuclear@4 35 resize(v.size());
nuclear@4 36
nuclear@4 37 for(size_t i=0; i<v.size(); i++) {
nuclear@4 38 data[i] = v.data[i];
nuclear@4 39 }
nuclear@4 40 }
nuclear@4 41 return *this;
nuclear@4 42 }
nuclear@4 43
nuclear@4 44 ~vector()
nuclear@4 45 {
nuclear@4 46 clear();
nuclear@4 47 }
nuclear@4 48
nuclear@4 49 void clear()
nuclear@4 50 {
nuclear@4 51 delete [] data;
nuclear@9 52 data = 0;
nuclear@4 53 max_items = num_items = 0;
nuclear@4 54 }
nuclear@4 55
nuclear@4 56 bool empty() const
nuclear@4 57 {
nuclear@4 58 return num_items == 0;
nuclear@4 59 }
nuclear@4 60
nuclear@4 61 size_t size() const
nuclear@4 62 {
nuclear@4 63 return num_items;
nuclear@4 64 }
nuclear@4 65
nuclear@4 66 void resize(size_t nsz)
nuclear@4 67 {
nuclear@4 68 if(nsz > 0) {
nuclear@4 69 T *newdata = new T[nsz];
nuclear@4 70 if(!newdata) return;
nuclear@4 71
nuclear@4 72 size_t num = num_items < nsz ? num_items : nsz;
nuclear@4 73 for(size_t i=0; i<num; i++) {
nuclear@4 74 newdata[i] = data[i];
nuclear@4 75 }
nuclear@4 76
nuclear@4 77 delete [] data;
nuclear@4 78 data = newdata;
nuclear@4 79 max_items = nsz;
nuclear@4 80 num_items = num;
nuclear@4 81 } else {
nuclear@4 82 delete [] data;
nuclear@4 83 max_items = num_items = 0;
nuclear@4 84 }
nuclear@4 85 }
nuclear@4 86
nuclear@4 87 void push_back(const T &item)
nuclear@4 88 {
nuclear@4 89 if(num_items >= max_items) {
nuclear@4 90 resize(max_items > 0 ? max_items * 2 : 8);
nuclear@4 91 }
nuclear@4 92 data[num_items++] = item;
nuclear@4 93 }
nuclear@4 94
nuclear@9 95 void pop_back()
nuclear@9 96 {
nuclear@9 97 if(--num_items <= 0) {
nuclear@9 98 num_items = 0;
nuclear@9 99 }
nuclear@9 100
nuclear@9 101 if(num_items < max_items / 3) {
nuclear@9 102 resize(max_items / 2);
nuclear@9 103 }
nuclear@9 104 }
nuclear@9 105
nuclear@9 106 T &back()
nuclear@9 107 {
nuclear@9 108 return data[num_items - 1];
nuclear@9 109 }
nuclear@9 110
nuclear@9 111 const T &back() const
nuclear@9 112 {
nuclear@9 113 return data[num_items - 1];
nuclear@9 114 }
nuclear@9 115
nuclear@4 116 T &operator [](int idx)
nuclear@4 117 {
nuclear@4 118 return data[idx];
nuclear@4 119 }
nuclear@4 120
nuclear@4 121 const T &operator [](int idx) const
nuclear@4 122 {
nuclear@4 123 return data[idx];
nuclear@4 124 }
nuclear@4 125 };
nuclear@4 126
nuclear@4 127 #endif // VECTOR_H_