Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_ArrayHelper.c
Go to the documentation of this file.
2{
3 //------------------------------------------------------------------------------------------------
17 static int GetWeightedIndex(notnull array<float> weights, float value01)
18 {
19 if (weights.IsEmpty())
20 return -1;
21
22 if (value01 < 0 || value01 > 1)
23 value01 = Math.Mod(value01, 1);
24
25 float probabilitySum;
26 for (int i, count = weights.Count(); i < count; i++)
27 {
28 probabilitySum += weights[i];
29 }
30
31 float add = 0;
32 probabilitySum *= value01;
33
34 for (int i, count = weights.Count(); i < count; i++)
35 {
36 add += weights[i];
37 if (add > probabilitySum)
38 return i;
39 }
40
41 return 0;
42 }
43}
44
48class SCR_ArrayHelperT<Class T>
49{
50 //------------------------------------------------------------------------------------------------
55 static bool AreEqual(notnull array<T> array1, notnull array<T> array2)
56 {
57 if (array1.Count() != array2.Count())
58 return false;
59
60 foreach (int i, T value : array1)
61 {
62 if (array2[i] != value)
63 return false;
64 }
65
66 return true;
67 }
68
69 //------------------------------------------------------------------------------------------------
72 static array<T> GetCopy(array<T> source)
73 {
74 if (!source)
75 return null;
76
77 array<T> result = {};
78 result.Copy(source);
79 return result;
80 }
81
82 //------------------------------------------------------------------------------------------------
87 static void Intersect(notnull array<T> array1, notnull array<T> array2, notnull out array<T> result/*, bool unique = true */)
88 {
89 result.Clear();
90 int count1 = array1.Count();
91 int count2 = array2.Count();
92 if (count1 > count2)
93 {
94 for (int i = array2.Count() - 1; i >= 0; i--)
95 {
96 if (array1.Contains(array2[i]))
97 result.Insert(array2[i]);
98 }
99 }
100 else
101 {
102 for (int i = array1.Count() - 1; i >= 0; i--)
103 {
104 if (array2.Contains(array1[i]))
105 result.Insert(array1[i]);
106 }
107 }
108 }
109
110 //------------------------------------------------------------------------------------------------
113 static void RemoveDuplicates(notnull inout array<T> items)
114 {
115 for (int i = items.Count() - 1; i >= 0; --i)
116 {
117 if (items.Find(items[i]) != i)
118 items.RemoveOrdered(i);
119 }
120 }
121
122 //------------------------------------------------------------------------------------------------
131 static void Reverse(notnull inout array<T> items)
132 {
133 int itemsCount = items.Count();
134 if (itemsCount < 2)
135 return;
136
137 int flooredMiddle = itemsCount * 0.5;
138 itemsCount--;
139
140 // 4 indices
141 // flooredMiddle = 2
142 // 0 <-> 3
143 // 1 <-> 2
144 // { 0 1 2 3 }
145 // { 3 2 1 0 }
146
147 // 5 indices
148 // flooredMiddle = 2
149 // 0 <-> 4
150 // 1 <-> 3
151 // 2 is central
152 // { 0 1 2 3 4 }
153 // { 4 3 2 1 0 }
154
155 for (int i; i < flooredMiddle; i++)
156 {
157 items.SwapItems(i, itemsCount - i);
158 }
159 }
160
161 //------------------------------------------------------------------------------------------------
170 static void Shuffle(notnull inout array<T> items, int shuffles = 1)
171 {
172 if (items.Count() < 2)
173 return;
174
175 // two checks are faster than Math.ClampInt
176 if (shuffles < 1)
177 shuffles = 1;
178
179 if (shuffles > 10)
180 shuffles = 10;
181
182 while (shuffles > 0)
183 {
184 for (int i, count = items.Count(); i < count; ++i)
185 {
186 int index1 = items.GetRandomIndex();
187 int index2 = items.GetRandomIndex();
188 if (index1 != index2)
189 items.SwapItems(index1, index2);
190 }
191
192 shuffles--;
193 }
194 }
195
196// //------------------------------------------------------------------------------------------------
197// //!
198// //! \param[in] toConvert
199// //! \param[in] result
200// // NATIVE version
201// static void ArrayToSet(notnull array<T> toConvert, notnull set<T> result)
202// {
203// result.Clear();
204// foreach (T item : toConvert)
205// {
206// result.Insert(item);
207// }
208// }
209//
210// //------------------------------------------------------------------------------------------------
211// //! \param[in] toConvert
212// //! \param[in] result
213// // NATIVE version
214// static void SetToArray(notnull set<T> toConvert, notnull array<T> result)
215// {
216// result.Clear();
217// foreach (T item : toConvert)
218// {
219// result.Insert(item);
220// }
221// }
222}
223
225class SCR_ArrayHelperRefT<Class T>
226{
227 //------------------------------------------------------------------------------------------------
232 static bool AreEqual(notnull array<ref T> array1, notnull array<ref T> array2)
233 {
234 if (array1.Count() != array2.Count())
235 return false;
236
237 foreach (int i, T value : array1)
238 {
239 if (array2[i] != value)
240 return false;
241 }
242
243 return true;
244 }
245
246 //------------------------------------------------------------------------------------------------
251 static void CopyReferencesFromTo(notnull array<ref T> source, notnull array<ref T> destination)
252 {
253 destination.Clear();
254 foreach (T sourceRef : source)
255 {
256 destination.Insert(sourceRef);
257 }
258 }
259
260 //------------------------------------------------------------------------------------------------
263 static array<ref T> GetCopy(array<ref T> source)
264 {
265 if (!source)
266 return null;
267
268 array<ref T> result = {};
269 foreach (T sourceRef : source)
270 {
271 result.Insert(sourceRef);
272 }
273
274 return result;
275 }
276
277 //------------------------------------------------------------------------------------------------
280 static array<T> GetNonRefCopy(array<ref T> source)
281 {
282 if (!source)
283 return null;
284
285 array<T> result = {};
286 foreach (T sourceRef : source)
287 {
288 result.Insert(sourceRef);
289 }
290
291 return result;
292 }
293
294 //------------------------------------------------------------------------------------------------
299 static void Intersect(notnull array<ref T> array1, notnull array<ref T> array2, notnull out array<ref T> result/*, bool unique = true */)
300 {
301 result.Clear();
302 int count1 = array1.Count();
303 int count2 = array2.Count();
304 if (count1 > count2)
305 {
306 for (int i = array2.Count() - 1; i >= 0; i--)
307 {
308 if (array1.Contains(array2[i]))
309 result.Insert(array2[i]);
310 }
311 }
312 else
313 {
314 for (int i = array1.Count() - 1; i >= 0; i--)
315 {
316 if (array2.Contains(array1[i]))
317 result.Insert(array1[i]);
318 }
319 }
320 }
321
322 //------------------------------------------------------------------------------------------------
325 static void RemoveDuplicates(notnull inout array<ref T> items)
326 {
327 for (int i = items.Count() - 1; i >= 0; --i)
328 {
329 if (items.Find(items[i]) != i)
330 items.RemoveOrdered(i);
331 }
332 }
333
334 //------------------------------------------------------------------------------------------------
343 static void Reverse(notnull inout array<ref T> items)
344 {
345 int itemsCount = items.Count();
346 if (itemsCount < 2)
347 return;
348
349 int flooredMiddle = itemsCount * 0.5;
350 itemsCount--;
351
352 // 4 indices
353 // flooredMiddle = 2
354 // 0 <-> 3
355 // 1 <-> 2
356 // { 0 1 2 3 }
357 // { 3 2 1 0 }
358
359 // 5 indices
360 // flooredMiddle = 2
361 // 0 <-> 4
362 // 1 <-> 3
363 // 2 is central
364 // { 0 1 2 3 4 }
365 // { 4 3 2 1 0 }
366
367 for (int i; i < flooredMiddle; i++)
368 {
369 items.SwapItems(i, itemsCount - i);
370 }
371 }
372
373 //------------------------------------------------------------------------------------------------
382 static void ShuffleRef(notnull inout array<ref T> items, int shuffles = 1)
383 {
384 if (items.Count() < 2)
385 return;
386
387 // two checks are faster than Math.ClampInt
388 if (shuffles < 1)
389 shuffles = 1;
390
391 if (shuffles > 10)
392 shuffles = 10;
393
394 while (shuffles > 0)
395 {
396 for (int i = 0, count = items.Count(); i < count; i++)
397 {
398 int index1 = Math.RandomInt(0, count);
399 int index2 = Math.RandomInt(0, count);
400 if (index1 != index2)
401 items.SwapItems(index1, index2);
402 }
403
404 shuffles--;
405 }
406 }
407
408 //------------------------------------------------------------------------------------------------
412 static void InsertAll(notnull array<ref T> destination, notnull array<ref T> source)
413 {
414 foreach (T entry : source)
415 {
416 destination.Insert(entry);
417 }
418 }
419
420// //------------------------------------------------------------------------------------------------
421// //!
422// //! \param[in] toConvert
423// //! \param[in] result
424// // REFERENCE version
425// static void ArrayToSet(notnull array<ref T> toConvert, notnull set<ref T> result)
426// {
427// result.Clear();
428// foreach (T item : toConvert)
429// {
430// result.Insert(item);
431// }
432// }
433//
434// //------------------------------------------------------------------------------------------------
435// //! \param[in] toConvert
436// //! \param[in] result
437// // REFERENCE version
438// static void SetToArray(notnull set<ref T> toConvert, notnull array<ref T> result)
439// {
440// result.Clear();
441// foreach (T item : toConvert)
442// {
443// result.Insert(item);
444// }
445// }
446}
@ Reverse
Definition ELightType.c:14
class SCR_ArrayHelper AreEqual(notnull array< T > array1, notnull array< T > array2)
class SCR_MapHelperT< Class T, Class U > T
Super root of all classes in Enforce script.
Definition Types.c:35
Definition Math.c:13
void InsertAll(notnull array< T > from)
Definition Types.c:250