Polygon Crucher SDK - Documentation
Documentation
Loading...
Searching...
No Matches
OffsetArray.h
1#ifndef COFFSETARRAY_H
2#define COFFSETARRAY_H
3
4#ifdef _MSC_VER
5#pragma once
6#endif // _MSC_VER
7
8BEGIN_MOOTOOLS_NAMESPACE
9
10 #ifndef _DEBUG
11 #define SIZET_SPLIT_LIMIT ((unsigned int)-1)
12 #endif
13
14 // This class is used to store value that can be greater than the unsigned int limit.
15 // if the values are all below the unsigned int limit, they are stored as unsigned int.
16 // if some values are greater than the unsigned int limit, they are stores as size_t starting from the first concerned values.
17 //
18 // The class is useful particularly to store pointer offsets as it offer a compact way to store the offsets.
19 //
20 // Important Note: this class only store POSITIVE offset.
21 // Under Win32 sizet == unsigned int meaning that the class has no meaning
22 // Anyway we use it instead of a simple CXUIntArray because of the serialization process
23 // Using the same serialization between x32 / x64 allows data exchange between both type of application
24 class DLL_TOOLSFUNCTION COffsetArray
25 {
26 protected:
27 // Note:
28 // - size is user number of elements
29 // - intSize is the number of int elements
30 // - bufSize contains intBuffer size or sizetBuffer buffer size (if sizetBuf not empty)
31 // the total number of allocated elements (GetAllocatedSize() is egal to (bufSize or intSize+bufSize)
32 unsigned int growBy; // Granularity
33 unsigned int size, intSize, bufSize;
34 unsigned int *intBuffer;
35 SIZET *sizetBuffer;
36
37 #ifdef _DEBUG
38 unsigned int SIZET_SPLIT_LIMIT; // This is for debugging testing in W32 and to make testing easier with simple number
39 unsigned int splitOperation;
40 #endif
41
42 unsigned int SplitArray(unsigned int index);
43 unsigned int GetSizetSize() const; // Get the nbr of elements of SIZET buffer
44 unsigned int GetAllocatedSize() const; // Get the upper bound valid limit
45
46 public:
47 COffsetArray();
48 virtual ~COffsetArray();
49
50 unsigned int GetSize() const;
51 void SetSize(unsigned int size, int growBy = -1);
52 void PreAllocate(int size, int newGrowBy = -1);
53
54 void FreeExtra();
55 void RemoveAll();
56
57 SIZET operator[](unsigned int index) const;
58 SIZET GetAt(unsigned int index) const;
59 void SetAt(unsigned int index, SIZET value);
60 void SetAtGrow(unsigned int index, SIZET value);
61 void RemoveAt(unsigned int index);
62 #ifndef MOOTOOLS_NO_ARCHIVE_SUPPORT
63 void Serialize(CXArchive& ar);
64 #endif
65
66 #ifdef MOOTOOLS_PRIVATE_DEBUG
67 void SetSplitLimit(unsigned int limit);
68 #endif
69
70 #ifdef _DEBUG
71 void Dump() const;
72 #endif
73 };
74
75 inline unsigned int COffsetArray::GetAllocatedSize() const
76 {
77 return (GetSizetSize() > 0) ? (intSize+bufSize) : bufSize;
78 }
79
80 inline unsigned int COffsetArray::GetSizetSize() const
81 {
82 XASSERT(intSize <= size);
83 return size-intSize;
84 }
85
86 inline unsigned int COffsetArray::GetSize() const
87 {
88 return size;
89 }
90
91 inline SIZET COffsetArray::operator[](unsigned int index) const
92 {
93 XASSERT(index < size);
94 if (index >= size)
95 return 0;
96
97 if (index < intSize)
98 return intBuffer[index];
99
100 return sizetBuffer[index-intSize];
101 }
102
103 inline SIZET COffsetArray::GetAt(unsigned int index) const
104 {
105 return (*this)[index];
106 }
107
108 inline void COffsetArray::SetAt(unsigned int index, SIZET value)
109 {
110 XASSERT(index < size);
111 if (index >= size)
112 return;
113
114 if (value > SIZET_SPLIT_LIMIT)
115 SplitArray(index);
116
117 if (index < intSize)
118 {
119 intBuffer[index] = (unsigned int)value;
120 return;
121 }
122
123 sizetBuffer[index-intSize] = value;
124 }
125
126 inline void COffsetArray::SetAtGrow(unsigned int index, SIZET value)
127 {
128 if (index >= size)
129 SetSize(index+1, -1);
130
131 SetAt(index, value);
132 }
133
134 inline void COffsetArray::RemoveAt(unsigned int index)
135 {
136 if (index >= size)
137 return;
138
139 if (index < intSize)
140 {
141 xmemmove_s(intBuffer + index, (size_t)intSize*sizeof(unsigned int),
142 intBuffer+index+1, (intSize-(index+1))*sizeof(unsigned int));
143
144 size--;
145 intSize--;
146
147 if (intSize == 0)
148 {
149 xDeallocateArray(intBuffer);
150 intBuffer = NULL;
151
152 XASSERT(size != 0 || !sizetBuffer);
153 if (size == 0) // if size > 0, bufSize matches for the sizetBuf buffer size
154 bufSize = 0;
155 }
156 }
157 else
158 {
159 XASSERT(sizetBuffer);
160 xmemmove_s(sizetBuffer + (index-intSize), (size_t)GetSizetSize()*sizeof(SIZET),
161 sizetBuffer+(index-intSize)+1, (GetSizetSize()-((index-intSize)+1))*sizeof(SIZET));
162
163 size--;
164
165 if (GetSizetSize() == 0)
166 {
167 xDeallocateArray(sizetBuffer);
168 sizetBuffer = NULL;
169 bufSize = intSize; // bufSize matched the sizetBuffer
170 }
171 }
172 }
173
174END_MOOTOOLS_NAMESPACE
175
176#endif
The class defines an x, y, z 3D point which can use int, float or double.
Definition 3DPoint.h:27
Definition XArchive.h:17