File: NstVector.hpp

package info (click to toggle)
libretro-nestopia 1.52.0%2B20230102.gitcb1e24e-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,960 kB
  • sloc: cpp: 107,513; xml: 27,221; python: 1,329; ansic: 772; makefile: 634
file content (342 lines) | stat: -rw-r--r-- 7,752 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
////////////////////////////////////////////////////////////////////////////////////////
//
// Nestopia - NES/Famicom emulator written in C++
//
// Copyright (C) 2003-2008 Martin Freij
//
// This file is part of Nestopia.
//
// Nestopia is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Nestopia is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Nestopia; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
////////////////////////////////////////////////////////////////////////////////////////

#ifndef NST_VECTOR_H
#define NST_VECTOR_H

#ifndef NST_ASSERT_H
#include "NstAssert.hpp"
#endif

#ifdef NST_PRAGMA_ONCE
#pragma once
#endif

namespace Nes
{
	namespace Core
	{
		template<typename T>
		class Vector;

		template<>
		class Vector<void>
		{
		public:

			static void* Malloc(dword);
			static void* Realloc(void*,dword);
			static void  Free(void*);
			static void  Copy(void*,const void*,dword);
			static void  Move(void*,const void*,dword);

			template<typename T>
			static void Copy(T& dst,const T& src)
			{
				Copy( &dst, &src, sizeof(T) );
			}
		};

		template<> inline void Vector<void>::Copy(char&   dst,const char&   src) { dst = src; }
		template<> inline void Vector<void>::Copy(schar&  dst,const schar&  src) { dst = src; }
		template<> inline void Vector<void>::Copy(uchar&  dst,const uchar&  src) { dst = src; }
		template<> inline void Vector<void>::Copy(short&  dst,const short&  src) { dst = src; }
		template<> inline void Vector<void>::Copy(ushort& dst,const ushort& src) { dst = src; }
		template<> inline void Vector<void>::Copy(int&    dst,const int&    src) { dst = src; }
		template<> inline void Vector<void>::Copy(uint&   dst,const uint&   src) { dst = src; }
		template<> inline void Vector<void>::Copy(long&   dst,const long&   src) { dst = src; }
		template<> inline void Vector<void>::Copy(ulong&  dst,const ulong&  src) { dst = src; }

		template<typename T>
		class Vector
		{
			T* data;
			dword size;
			dword capacity;

			void MakeRoom(dword);

			typedef Vector<void> Allocator;

		public:

			typedef T Type;

			Vector();
			explicit Vector(dword);
			Vector(const Vector<T>&);
			Vector(const T*,dword);

			bool operator == (const Vector<T>&) const;

			void Reserve(dword);
			void Resize(dword);
			void Expand(dword);
			void Assign(const T*,dword);
			void Append(const T&);
			void Append(const T*,dword);
			T*   Insert(T*,const T&);
			void Erase(T*,dword=1);
			void Destroy();
			void Defrag();

			static void Swap(Vector<T>&,Vector<T>&);

			~Vector()
			{
				Allocator::Free( data );
			}

			void operator = (const Vector<T>& vector)
			{
				Assign( vector.data, vector.size );
			}

			void operator += (const Vector<T>& vector)
			{
				Append( vector.data, vector.size );
			}

			T& operator [] (dword i) const
			{
				NST_ASSERT( i < size );
				return data[i];
			}

			T* Begin() const
			{
				return data;
			}

			T* End() const
			{
				return data + size;
			}

			dword Size() const
			{
				return size;
			}

			dword Capacity() const
			{
				return capacity;
			}

			T& Front() const
			{
				NST_ASSERT( size );
				return data[0];
			}

			T& Back() const
			{
				NST_ASSERT( size );
				return data[size - 1];
			}

			const T& Pop()
			{
				NST_ASSERT( size );
				return data[--size];
			}

			void SetTo(dword count)
			{
				NST_ASSERT( count <= capacity );
				size = count;
			}

			void Clear()
			{
				size = 0;
			}
		};

		template<typename T>
		Vector<T>::Vector()
		: data(NULL), size(0), capacity(0) {}

		template<typename T>
		Vector<T>::Vector(const dword count)
		: data(count ? static_cast<T*>(Allocator::Malloc(count * sizeof(T))) : NULL), size(count), capacity(count)
		{
		}

		template<typename T>
		Vector<T>::Vector(const T* in,const dword count)
		: data(count ? static_cast<T*>(Allocator::Malloc(count * sizeof(T))) : NULL), size(count), capacity(count)
		{
			Allocator::Copy( data, in, count * sizeof(T) );
		}

		template<typename T>
		Vector<T>::Vector(const Vector<T>& v)
		: data(v.size ? static_cast<T*>(Allocator::Malloc(v.size * sizeof(T))) : NULL), size(v.size), capacity(v.size)
		{
			Allocator::Copy( data, v.data, v.size * sizeof(T) );
		}

		template<typename T>
		void Vector<T>::MakeRoom(const dword count)
		{
			NST_ASSERT( count );
			data = static_cast<T*>(Allocator::Realloc( data, count * sizeof(T) ));
			capacity = count;
		}

		template<typename T>
		void Vector<T>::Append(const T& value)
		{
			if (size == capacity)
				MakeRoom( (size + 1) * 2 );

			Allocator::Copy( data[size++], value );
		}

		template<typename T>
		void Vector<T>::Assign(const T* const NST_RESTRICT inData,const dword inSize)
		{
			if (capacity < inSize)
				MakeRoom( inSize );

			Allocator::Copy( data, inData, (size=inSize) * sizeof(T) );
		}

		template<typename T>
		void Vector<T>::Append(const T* const NST_RESTRICT inData,const dword inSize)
		{
			if (capacity < size + inSize)
				MakeRoom( (size * 2) + inSize );

			void* const tmp = data + size;
			size += inSize;

			Allocator::Copy( tmp, inData, inSize * sizeof(T) );
		}

		template<typename T>
		T* Vector<T>::Insert(T* it,const T& value)
		{
			const dword pos = it - data;

			if (size++ == capacity)
				MakeRoom( size * 2 );

			Allocator::Move( data+pos+1, data+pos, (size - (pos+1)) * sizeof(T) );
			Allocator::Copy( data[pos], value );

			return data+pos;
		}

		template<typename T>
		void Vector<T>::Erase(T* it,dword count)
		{
			NST_ASSERT( size >= count );

			const dword s = size;
			size -= count;
			Allocator::Move( it, it + count, (s - ((it-data) + count)) * sizeof(T) );
		}

		template<typename T>
		void Vector<T>::Reserve(dword count)
		{
			if (capacity < count)
				MakeRoom( count );
		}

		template<typename T>
		void Vector<T>::Resize(dword count)
		{
			Reserve( count );
			size = count;
		}

		template<typename T>
		void Vector<T>::Expand(dword count)
		{
			Reserve( size + count );
			size += count;
		}

		template<typename T>
		void Vector<T>::Defrag()
		{
			if (size)
			{
				MakeRoom( size );
			}
			else if (void* const tmp = data)
			{
				data = NULL;
				capacity = 0;
				Allocator::Free( tmp );
			}
		}

		template<typename T>
		bool Vector<T>::operator == (const Vector<T>& vector) const
		{
			if (size != vector.size)
				return false;

			for (const T *a=data, *b=vector.data, *const end=data+size; a != end; ++a, ++b)
			{
				if (!(*a == *b))
					return false;
			}

			return true;
		}

		template<typename T>
		void Vector<T>::Destroy()
		{
			if (void* const tmp = data)
			{
				data = NULL;
				size = 0;
				capacity = 0;
				Allocator::Free( tmp );
			}
		}

		template<typename T>
		void Vector<T>::Swap(Vector<T>& a,Vector<T>& b)
		{
			T* t = a.data;
			a.data = b.data;
			b.data = t;
			dword u = a.size;
			a.size = b.size;
			b.size = u;
			u = a.capacity;
			a.capacity = b.capacity;
			b.capacity = u;
		}
	}
}

#endif