Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_WeightedArray.c
Go to the documentation of this file.
1class SCR_WeightedArray<Class TValue>
2{
3 protected ref array<TValue> m_aValues = {};
4 protected ref array<float> m_aWeights = {};
5 protected float m_fTotalWeight;
6
7 //------------------------------------------------------------------------------------------------
10 int GetRandomValue(out TValue outValue)
11 {
12 return GetWeightedValue(outValue, Math.RandomFloat(0, m_fTotalWeight));
13 }
14
15 //------------------------------------------------------------------------------------------------
19 int GetRandomValue(out TValue outValue, notnull RandomGenerator randomGenerator)
20 {
21 return GetWeightedValue(outValue, randomGenerator.RandFloatXY(0, m_fTotalWeight));
22 }
23
24 //------------------------------------------------------------------------------------------------
28 int GetWeightedValue(out TValue outValue, float weightedValue)
29 {
30 float weight;
31 for (int i, count = m_aValues.Count(); i < count; i++)
32 {
33 weight += m_aWeights[i];
34 if (weightedValue < weight)
35 {
36 outValue = m_aValues[i];
37 return i;
38 }
39 }
40
41 return -1;
42 }
43
44 //------------------------------------------------------------------------------------------------
47 {
48 return m_fTotalWeight;
49 }
50
51 //------------------------------------------------------------------------------------------------
54 TValue Get(int n)
55 {
56 return m_aValues.Get(n);
57 }
58
59 //------------------------------------------------------------------------------------------------
62 void Set(int n, TValue value)
63 {
64 m_aValues.Set(n, value);
65 }
66
67 //------------------------------------------------------------------------------------------------
72 int Insert(TValue value, float weight)
73 {
74 m_aWeights.Insert(weight);
75 m_fTotalWeight += weight;
76 return m_aValues.Insert(value);
77 }
78
79 //------------------------------------------------------------------------------------------------
82 void Remove(int i)
83 {
85 m_aValues.Remove(i);
86 m_aWeights.Remove(i);
87 }
88
89 //------------------------------------------------------------------------------------------------
92 void RemoveOrdered(int i)
93 {
95 m_aValues.RemoveOrdered(i);
96 m_aWeights.RemoveOrdered(i);
97 }
98
99 //------------------------------------------------------------------------------------------------
102 float GetWeight(int i)
103 {
104 return m_aWeights[i];
105 }
106
107 //------------------------------------------------------------------------------------------------
110 TValue GetValue(int i)
111 {
112 return m_aValues[i];
113 }
114
115 //------------------------------------------------------------------------------------------------
118 int Count()
119 {
120 return m_aValues.Count();
121 }
122
123 //------------------------------------------------------------------------------------------------
125 bool IsEmpty()
126 {
127 return m_aValues.IsEmpty();
128 }
129
130 //------------------------------------------------------------------------------------------------
134 bool Contains(TValue value)
135 {
136 return m_aValues.Contains(value);
137 }
138
139 //------------------------------------------------------------------------------------------------
143 int Find(TValue value)
144 {
145 return m_aValues.Find(value);
146 }
147
148 //------------------------------------------------------------------------------------------------
152 int CopyFrom(notnull SCR_WeightedArray<TValue> from)
153 {
154 Clear();
155 int count = from.Count();
156 for (int i; i < count; i++)
157 {
158 m_aWeights.Insert(from.m_aWeights[i]);
159 m_aValues.Insert(from.m_aValues[i]);
160 }
161
162 m_fTotalWeight = from.m_fTotalWeight;
163
164 return count;
165 }
166
167 //------------------------------------------------------------------------------------------------
169 void Clear()
170 {
171 m_aWeights.Clear();
172 m_aValues.Clear();
173 m_fTotalWeight = 0;
174 }
175
176 //------------------------------------------------------------------------------------------------
180 int ToArray(out notnull array<TValue> outArray)
181 {
182 return outArray.Copy(m_aValues);
183 }
184
185 //------------------------------------------------------------------------------------------------
187 void Debug()
188 {
189 PrintFormat("SCR_WeightedArray<%1> count: %2, total weight: %3", TValue, Count(), GetTotalWeight(), level: LogLevel.NORMAL);
190 foreach (int i, TValue value : m_aValues)
191 {
192 PrintFormat("[%1] => %2: %3", i, m_aWeights[i], value, level: LogLevel.NORMAL);
193 }
194 }
195}
Super root of all classes in Enforce script.
Definition Types.c:35
Definition Math.c:13
int CopyFrom(notnull SCR_WeightedArray< TValue > from)
int Insert(TValue value, float weight)
int GetRandomValue(out TValue outValue, notnull RandomGenerator randomGenerator)
int ToArray(out notnull array< TValue > outArray)
int GetRandomValue(out TValue outValue)
int GetWeightedValue(out TValue outValue, float weightedValue)
void Set(int n, TValue value)
LogLevel
Enum with severity of the logging message.
Definition LogLevel.c:14
proto void PrintFormat(string fmt, void param1=NULL, void param2=NULL, void param3=NULL, void param4=NULL, void param5=NULL, void param6=NULL, void param7=NULL, void param8=NULL, void param9=NULL, LogLevel level=LogLevel.NORMAL)
proto native void Clear()
Remove all calls from list.
TypeID handle64 Count()