AMXX-BG.INFO ndimarray.inc Raw include

ndimarray.inc

Original include source with line numbers.

Back Download .inc
1 #if defined _ndim_array_included
2 #endinput
3 #endif
4
5 #define _ndim_array_included
6
7 #include <cellarray>
8
9 enum NDim
10 {
11 Invalid_NDim = 0,
12 }
13 /**
14 * These arrays are intended to be used for a form of global storage without
15 * limiting how many dimensions a person may need.
16 * These are not designed to be used as a replacement for normal arrays, as
17 * normal arrays are faster and should be used whenever possible.
18 */
19
20 /**
21 * Creates an N Dimensional Array
22 *
23 * @param cellsize The cellsize for the last dimension.
24 * @param ... Each param is the size for a dimension.
25 *
26 * @note The cellsize should only be changed if working with strings or arrays.
27 * @note2 NDim Arrays start zeroed.
28 *
29 * @return The NDim handle.
30 */
31 stock NDim:NDimCreate( const cellsize=1, ... )
32 {
33 new argnum = numargs();
34 if( !argnum )
35 return Invalid_NDim;
36
37 new i, total_size = 1;
38 for( i=1; i<argnum; i++ )
39 total_size *= getarg(i);
40
41 if( total_size <= 0 )
42 return Invalid_NDim;
43
44 new Array:actual_array = ArrayCreate(cellsize,1);
45 for( i=0; i<total_size; i++ )
46 ArrayPushCell( actual_array, 0 );
47
48 new Array:tracker = ArrayCreate(1,1);
49 ArrayPushCell( tracker, actual_array );
50 for( i=1; i<argnum; i++ )
51 ArrayPushCell( tracker, getarg(i) );
52
53 return NDim:tracker;
54 }
55 /**
56 * Destroys the NDim Array, and resets the handle to 0 to prevent accidental usage after it is destroyed.
57 *
58 * @param tracker The array to destroy.
59 */
60 stock NDimDestroy( &NDim:tracker )
61 {
62 ArrayDestroy( Array:ArrayGetCell(Array:tracker, 0) );
63 ArrayDestroy( Array:tracker );
64 tracker = Invalid_NDim;
65 }
66
67 /**
68 * Returns a single cell of data from an NDim Array.
69 *
70 * @param tracker The array to retrieve the item from.
71 * @param ... Each param is the element for the corresponding dimension.
72 *
73 * @return The value of the cell.
74 */
75 stock any:NDimGetCell( const NDim:tracker, ... )
76 {
77 new temp_position, position, Array:actual_array = Array:ArrayGetCell(Array:tracker, 0);
78 new argnum = numargs()-1;
79 for( new i=1,j; i<argnum; i++ )
80 {
81 temp_position = getarg(i);
82 for( j=i; j<argnum; j++ )
83 temp_position *= _:ArrayGetCell(Array:tracker, j+1);
84 position += temp_position;
85 }
86 position += getarg(argnum);
87 return ArrayGetCell(actual_array, position);
88 }
89 /**
90 * Returns data within an NDim Array.
91 * Make sure the output buffer matches the size the array was created with!
92 *
93 * @param tracker The array to retrieve the item from.
94 * @param data The output buffer to write.
95 * @param ... Each param is the element for the corresponding dimension.
96 */
97 stock NDimGetArray( const NDim:tracker, data[], ... )
98 {
99 new temp_position, position, Array:actual_array = Array:ArrayGetCell(Array:tracker, 0);
100 new argnum = numargs()-1;
101 for( new i=2,j; i<argnum; i++ )
102 {
103 temp_position = getarg(i);
104 for( j=i; j<argnum; j++ )
105 temp_position *= _:ArrayGetCell(Array:tracker, j);
106 position += temp_position;
107 }
108 position += getarg(argnum);
109 return ArrayGetArray(actual_array, position, data);
110 }
111 /**
112 * Returns a string value from an NDim Array.
113 *
114 * @param tracker The array to retrieve the item from.
115 * @param output The variable to store the value in.
116 * @param size Character size of the output buffer.
117 * @param ... Each param is the element for the corresponding dimension.
118 */
119 stock NDimGetString( const NDim:tracker, output[], const size, ... )
120 {
121 new temp_position, position, Array:actual_array = Array:ArrayGetCell(Array:tracker, 0);
122 new argnum = numargs()-1;
123 for( new i=3,j; i<argnum; i++ )
124 {
125 temp_position = getarg(i);
126 for( j=i-1; j<argnum-1; j++ )
127 temp_position *= _:ArrayGetCell(Array:tracker, j);
128 position += temp_position;
129 }
130 position += getarg(argnum);
131 return ArrayGetString(actual_array, position, output, size);
132 }
133
134 /**
135 * Sets an NDim Array's single cell value. Use this only on array that were created with a cellsize of 1!
136 *
137 * @param tracker The array to set the item from within.
138 * @param input The value to set.
139 * @param ... Each param is the element for the corresponding dimension.
140 */
141 stock NDimSetCell( const NDim:tracker, const any:input, ... )
142 {
143 new temp_position, position, Array:actual_array = Array:ArrayGetCell(Array:tracker, 0);
144 new argnum = numargs()-1;
145 for( new i=2,j; i<argnum; i++ )
146 {
147 temp_position = getarg(i);
148 for( j=i; j<argnum; j++ )
149 temp_position *= _:ArrayGetCell(Array:tracker, j);
150 position += temp_position;
151 }
152 position += getarg(argnum);
153 return ArraySetCell(actual_array, position, input);
154 }
155 /**
156 * Sets an item's data with that of a local buffer.
157 * The buffer size must match what the cellsize that the array was created with!
158 *
159 * @param tracker The array to set the item from within.
160 * @param input The input buffer to store.
161 * @param ... Each param is the element for the corresponding dimension.
162 */
163 stock NDimSetArray( const NDim:tracker, const any:input[], ... )
164 {
165 new temp_position, position, Array:actual_array = Array:ArrayGetCell(Array:tracker, 0);
166 new argnum = numargs()-1;
167 for( new i=2,j; i<argnum; i++ )
168 {
169 temp_position = getarg(i);
170 for( j=i; j<argnum; j++ )
171 temp_position *= _:ArrayGetCell(Array:tracker, j);
172 position += temp_position;
173 }
174 position += getarg(argnum);
175 return ArraySetArray(actual_array, position, input);
176 }
177 /**
178 * Sets a string value for an NDim Array.
179 * The stored string will be truncated if it is longer than the cellsize the array was created with!
180 *
181 * @param tracker The array to set the item from within.
182 * @param input The string to set the item as.
183 * @param ... Each param is the element for the corresponding dimension.
184 */
185 stock NDimSetString( const NDim:tracker, const input[], ... )
186 {
187 new temp_position, position, Array:actual_array = Array:ArrayGetCell(Array:tracker, 0);
188 new argnum = numargs()-1;
189 for( new i=2,j; i<argnum; i++ )
190 {
191 temp_position = getarg(i);
192 for( j=i; j<argnum; j++ )
193 temp_position *= _:ArrayGetCell(Array:tracker, j);
194 position += temp_position;
195 }
196 position += getarg(argnum);
197 return ArraySetString(actual_array, position, input);
198 }
199
200 /**
201 * Sets all of an NDim Array's single cell values. Use this only on array that were created with a cellsize of 1!
202 *
203 * @param tracker The array to set the item from within.
204 * @param input The value to set.
205 */
206 stock NDimFillCell( const NDim:tracker, const any:input )
207 {
208 new i, total_size = 1;
209 new argnum = ArraySize(Array:tracker) - 1;
210 for( i=0; i<argnum; i++ )
211 total_size *= ArrayGetCell(Array:tracker, i+1);
212
213 new Array:actual_array = ArrayGetCell(Array:tracker, 0);
214 for( i=0; i<total_size; i++ )
215 ArraySetCell(actual_array, i, input);
216 }
217 /**
218 * Sets all items' data with that of a local buffer.
219 * The buffer size must match what the cellsize that the array was created with!
220 *
221 * @param tracker The array to set the item from within.
222 * @param input The input buffer to store.
223 */
224 stock NDimFillArray( const NDim:tracker, const any:input[] )
225 {
226 new i, total_size = 1;
227 new argnum = ArraySize(Array:tracker) - 1;
228 for( i=0; i<argnum; i++ )
229 total_size *= ArrayGetCell(Array:tracker, i+1);
230
231 new Array:actual_array = ArrayGetCell(Array:tracker, 0);
232 for( i=0; i<total_size; i++ )
233 ArraySetArray(actual_array, i, input);
234 }
235 /**
236 * Sets all string values for an NDim Array.
237 * The stored string will be truncated if it is longer than the cellsize the array was created with!
238 *
239 * @param tracker The array to set the item from within.
240 * @param input The string to set the item as.
241 */
242 stock NDimFillString( const NDim:tracker, const input[] )
243 {
244 new i, total_size = 1;
245 new argnum = ArraySize(Array:tracker) - 1;
246 for( i=0; i<argnum; i++ )
247 total_size *= ArrayGetCell(Array:tracker, i+1);
248
249 new Array:actual_array = ArrayGetCell(Array:tracker, 0);
250 for( i=0; i<total_size; i++ )
251 ArraySetString(actual_array, i, input);
252 }
253