Changeset 1244
- Timestamp:
- Apr 21, 2012, 10:18:14 AM (11 years ago)
- Location:
- trunk/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/Makefile.am
r1237 r1244 3 3 4 4 liblol_a_SOURCES = \ 5 core.h tiler.cpp tiler.h dict.cpp dict.h \5 core.h tiler.cpp tiler.h dict.cpp dict.h array.h \ 6 6 audio.cpp audio.h scene.cpp scene.h font.cpp font.h layer.cpp layer.h \ 7 7 map.cpp map.h entity.cpp entity.h ticker.cpp ticker.h lolgl.h \ -
trunk/src/array.h
r1243 r1244 23 23 { 24 24 25 template<typename T> class Array 25 template<typename T1, typename T2 = void, typename T3 = void> class Array 26 { 27 public: 28 typedef struct { T1 m1; T2 m2; T3 m3; } Element; 29 30 inline Array() : m_data(0), m_count(0), m_reserved(0) {} 31 inline ~Array() { delete[] m_data; } 32 33 inline Element& operator[](int n) 34 { 35 return m_data[n]; 36 } 37 38 inline Element const& operator[](int n) const 39 { 40 return m_data[n]; 41 } 42 43 inline Array<T1, T2, T3> const& operator+=(Element const &x) 44 { 45 Append(x.m1, x.m2, x.m3); 46 return *this; 47 } 48 49 inline void Append(T1 const &m1, T2 const &m2, T3 const &m3) 50 { 51 if (m_count >= m_reserved) 52 Reserve(m_count * 13 / 8 + 8); 53 m_data[m_count].m1 = m1; 54 m_data[m_count].m2 = m2; 55 m_data[m_count].m3 = m3; 56 ++m_count; 57 } 58 59 void Remove(int pos) 60 { 61 memmove(m_data + pos, m_data + pos + 1, m_count - pos - 1); 62 m_count--; 63 } 64 65 void Remove(int pos, int count) 66 { 67 memmove(m_data + pos, m_data + pos + count, m_count - pos - count); 68 m_count -= count; 69 } 70 71 void Reserve(int count) 72 { 73 if (count <= (int)m_reserved) 74 return; 75 76 Element *tmp = new Element[count]; 77 if (m_data) 78 { 79 memcpy(tmp, m_data, m_count * sizeof(Element)); 80 delete[] m_data; 81 } 82 m_data = tmp; 83 m_reserved = count; 84 } 85 86 inline int Count() const { return m_count; } 87 inline int Bytes() const { return m_count * sizeof(Element); } 88 89 private: 90 Element *m_data; 91 int m_count, m_reserved; 92 }; 93 94 template<typename T1, typename T2> class Array<T1, T2, void> 95 { 96 public: 97 typedef struct { T1 m1; T2 m2; } Element; 98 99 inline Array() : m_data(0), m_count(0), m_reserved(0) {} 100 inline ~Array() { delete[] m_data; } 101 102 inline Element& operator[](int n) 103 { 104 return m_data[n]; 105 } 106 107 inline Element const& operator[](int n) const 108 { 109 return m_data[n]; 110 } 111 112 inline Array<T1, T2> const& operator+=(Element const &x) 113 { 114 Append(x.m1, x.m2); 115 return *this; 116 } 117 118 inline void Append(T1 const &m1, T2 const &m2) 119 { 120 if (m_count >= m_reserved) 121 Reserve(m_count * 13 / 8 + 8); 122 m_data[m_count].m1 = m1; 123 m_data[m_count].m2 = m2; 124 ++m_count; 125 } 126 127 void Remove(int pos) 128 { 129 memmove(m_data + pos, m_data + pos + 1, m_count - pos - 1); 130 m_count--; 131 } 132 133 void Remove(int pos, int count) 134 { 135 memmove(m_data + pos, m_data + pos + count, m_count - pos - count); 136 m_count -= count; 137 } 138 139 void Reserve(int count) 140 { 141 if (count <= (int)m_reserved) 142 return; 143 144 Element *tmp = new Element[count]; 145 if (m_data) 146 { 147 memcpy(tmp, m_data, m_count * sizeof(Element)); 148 delete[] m_data; 149 } 150 m_data = tmp; 151 m_reserved = count; 152 } 153 154 inline int Count() const { return m_count; } 155 inline int Bytes() const { return m_count * sizeof(Element); } 156 157 private: 158 Element *m_data; 159 int m_count, m_reserved; 160 }; 161 162 template<typename T1> class Array<T1, void, void> 26 163 { 27 164 public: … … 29 166 inline ~Array() { delete[] m_data; } 30 167 31 inline T & operator[](int n)32 { 33 return m_data[n]; 34 } 35 36 inline T const& operator[](int n) const37 { 38 return m_data[n]; 39 } 40 41 inline Array<T > const& operator+=(Tconst &x)168 inline T1& operator[](int n) 169 { 170 return m_data[n]; 171 } 172 173 inline T1 const& operator[](int n) const 174 { 175 return m_data[n]; 176 } 177 178 inline Array<T1> const& operator+=(T1 const &x) 42 179 { 43 180 if (m_count >= m_reserved) … … 64 201 return; 65 202 66 T *tmp = new T[count];203 T1 *tmp = new T1[count]; 67 204 if (m_data) 68 205 { 69 memcpy(tmp, m_data, m_count * sizeof(T ));206 memcpy(tmp, m_data, m_count * sizeof(T1)); 70 207 delete[] m_data; 71 208 } … … 74 211 } 75 212 76 inline size_t Count() const { return m_count; }77 inline size_t Bytes() const { return m_count * sizeof(T); }213 inline int Count() const { return m_count; } 214 inline int Bytes() const { return m_count * sizeof(T1); } 78 215 79 216 private: 80 T *m_data;81 size_t m_count, m_reserved;217 T1 *m_data; 218 int m_count, m_reserved; 82 219 }; 83 220
Note: See TracChangeset
for help on using the changeset viewer.