Repository navigation
Expand file tree
/
Copy pathArray.h
More file actions
137 lines (98 loc) · 3.36 KB
/
Copy pathArray.h
File metadata and controls
137 lines (98 loc) · 3.36 KB
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
// Array.h: interface for the CArray class.
//
//////////////////////////////////////////////////////////////////////
#ifndef ARRAY_INCLUDE
#define ARRAY_INCLUDE
// array class uses heap to store items
template <class T> class CArray
{
public:
#ifdef VANILLA_MEMORYMANAGER
CArray(int sz) :mSize(sz) { mItems = (T*) malloc(sizeof(T)*sz); }
~CArray() { free(mItems); }
#else
CArray(int sz) :mSize(sz) { mItems = new (MEMTYPE_ARRAY) T[sz] ; }
~CArray() { delete [] mItems ; }
#endif
CArray<T>& operator =(CArray<T>& copy) {int i =0; for (i =0;i< mSize;i++) mItems[i] = copy[i] ; return *this; }
void SetAll(T val) { int i =0 ; for (i =0;i< mSize;i++) mItems[i] = val ; }
int Size() { return mSize ;}
#ifndef PARTICLE_ED
#ifndef EDITORBUILD2
#ifndef VANILLA_MEMORYMANAGER
void ReSize(int size) { mItems = (T*)MEM_MANAGER.ReAlloc(mItems, (sizeof(T))*size) ; mSize = size;}
#else
void ReSize(int size) { mItems = (T*)realloc(mItems, (sizeof(T))*size) ; mSize = size;}
#endif
#else
void ReSize(int size) { mItems = (T*)realloc(mItems, (sizeof(T))*size) ; mSize = size;}
#endif
#else
void ReSize(int size) { mItems = (T*)realloc(mItems, (sizeof(T))*size) ; mSize = size;}
#endif
#ifdef _DEBUG
T& operator [] (int item) ;
private:
#else
T& operator [] (int item) { return (T&)mItems[item] ; }
#endif
protected:
T* mItems ;
int mSize ;
};
// array class uses stack to store items
template <class T, int size> class CSArray
{
public:
CSArray<T, size>& operator =(CSArray<T, size>& copy) {int i =0; for (i =0;i< size;i++) mItems[i] = copy[i] ; return *this; }
void SetAll(T val) { int i =0 ; for (i =0;i< size;i++) mItems[i] = val ; }
int Size() { return size ; }
#ifdef _DEBUG
T& operator [] (int item) ;
#else
T& operator [] (int item) { return mItems[item] ; }
#endif
private:
T mItems[size] ;
};
template <class T> class COSet : public CArray<T>
{
public:
COSet(int initial_size = 1) : CArray<T>(initial_size), mEntries(0) {}
COSet<T>& operator =(COSet<T>& copy) { if (mSize != copy.mSize) ReSize(copy.mSize) ; CArray<T>::operator =(copy) ; mEntries = copy.mEntries; mIterator = copy.mIterator; return *this;}
int Add(const T& item) { mEntries++; if (mEntries>mSize) ReSize(mEntries*2); mItems[mEntries-1] = item ; return mEntries-1 ; }
T* First() { mIterator = 0 ; if (mEntries==0) return NULL ;return &mItems[mIterator] ; }
T* Next() { mIterator++; if (mIterator >=mEntries) return NULL ; return &mItems[mIterator] ; }
void Finalise() { ReSize(mEntries) ;}
int Size() { return mEntries ; }
protected:
int mEntries;
int mIterator;
};
// These operators are used in debug build and do bounds checking
#ifdef _DEBUG
#ifndef SIMPLE_MESHES
#include "debuglog.h"
#endif
template <class T> T& CArray<T>::operator [] (int item)
{
if (item < 0 || item >= mSize)
{
LOG.AddMessage("FATAL ERROR: Outside array bounds (index = %d, size = %d)", item, mSize) ;
ASSERT(0);
return mItems[0] ;
}
return mItems[item] ;
}
template <class T, int size> T& CSArray<T,size>::operator [] (int item)
{
if (item < 0 || item >= size)
{
LOG.AddMessage("FATAL ERROR: Outside array bounds (index = %d, size = %d)", item, size) ;
ASSERT(0);
return mItems[0] ;
}
return mItems[item] ;
}
#endif
#endif