rayzor

annotate src/stl/vector.h @ 10:235c8b764c0b

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