1 class SCR_WeightedArray<Class TValue>: Managed
3 protected ref array<TValue> m_Values =
new array<TValue>();
4 protected ref array<float>
m_Weights =
new array<float>();
5 protected float m_TotalWeight;
7 int GetRandomValue(out TValue outValue)
9 float weightedValue = Math.RandomFloat(0, m_TotalWeight);
10 return GetWeightedValue(outValue, weightedValue);
12 int GetRandomValue(out TValue outValue, RandomGenerator randomGenerator)
14 float weightedValue = randomGenerator.RandFloatXY(0, m_TotalWeight);
15 return GetWeightedValue(outValue, weightedValue);
17 int GetWeightedValue(out TValue outValue,
float weightedValue)
20 for (
int i = 0, count = m_Values.Count(); i < count; i++)
23 if (weightedValue < weight)
25 outValue = m_Values[i];
31 float GetTotalWeight()
37 return m_Values.Get(n);
39 void Set(
int n, TValue value)
41 m_Values.Set(n, value);
43 int Insert(TValue value,
float weight)
46 m_TotalWeight += weight;
47 return m_Values.Insert(value);
55 void RemoveOrdered(
int i)
58 m_Values.RemoveOrdered(i);
61 float GetWeight(
int i)
71 return m_Values.Count();
75 return m_Values.IsEmpty();
79 return m_Values.Contains(value);
81 int Find(TValue value)
83 return m_Values.Find(value);
86 int CopyFrom(notnull SCR_WeightedArray<TValue> from)
89 int count = from.Count();
90 for (
int i = 0; i < count; i++)
93 m_Values.Insert(from.m_Values[i]);
102 int ToArray(out notnull array<TValue> outArray)
104 return outArray.Copy(m_Values);
108 PrintFormat(
"H_WeightedArray count: %1, total weight: %2", Count(), GetTotalWeight());
109 for (
int i, count = Count(); i < count; i++)
111 PrintFormat(
"[%1] => %2: %3", i,
m_Weights[i], m_Values[i]);