private static void MergeRepeatedEntries(List <WeightedState> weightedStates) { // Merge repeated entries for the same state var dst = 0; for (var i = 1; i < weightedStates.Count; ++i) { var srcState = weightedStates[i]; var dstState = weightedStates[dst]; if (srcState.Index == dstState.Index) { weightedStates[dst] = new WeightedState(dstState.Index, dstState.Weight + srcState.Weight); } else { ++dst; if (dst != i) { weightedStates[dst] = srcState; } } } // truncate excess ++dst; if (dst < weightedStates.Count) { weightedStates.RemoveRange(dst, weightedStates.Count - dst); } }
/// <summary> /// Creates a new instance of /// <see cref="WeightableVertexCost"/> /// with the cost of <paramref name="startCost"/>. /// Weighted cost is set to the same value /// </summary> /// <param name="startCost"></param> public WeightableVertexCost(int startCost) : base(startCost) { WeightedCost = startCost; Status = new WeightedState(); UnweightedCostView = string.Empty; }
/// <summary> /// Turns weighted state set into an array. This is convenient for writing LINQ queries /// in tests. /// </summary> public WeightedState[] ToArray() { var result = new WeightedState[this.Count]; for (var i = 0; i < this.Count; ++i) { result[i] = this[i]; } return(result); }
private static Weight NormalizeWeights(List <WeightedState> weightedStates) { var maxWeight = weightedStates[0].Weight; for (var i = 1; i < weightedStates.Count; ++i) { if (weightedStates[i].Weight > maxWeight) { maxWeight = weightedStates[i].Weight; } } var normalizer = Weight.Inverse(maxWeight); for (var i = 0; i < weightedStates.Count; ++i) { var state = weightedStates[i]; weightedStates[i] = new WeightedState(state.Index, state.Weight * normalizer); } return(maxWeight); }
public (WeightedStateSet, Weight) Get() { Debug.Assert(this.weightedStates.Count > 0); var sortedStates = this.weightedStates.ToArray(); if (sortedStates.Length == 1) { var state = sortedStates[0]; sortedStates[0] = new WeightedState(state.Index, Weight.One); return(new WeightedStateSet(sortedStates), state.Weight); } else { Array.Sort(sortedStates); var maxWeight = sortedStates[0].Weight; for (var i = 1; i < sortedStates.Length; ++i) { if (sortedStates[i].Weight > maxWeight) { maxWeight = sortedStates[i].Weight; } } var normalizer = Weight.Inverse(maxWeight); for (var i = 0; i < sortedStates.Length; ++i) { var state = sortedStates[i]; sortedStates[i] = new WeightedState(state.Index, state.Weight * normalizer); } return(new WeightedStateSet(sortedStates), maxWeight); } }
/// <summary> /// Sets <see cref="WeightableVertexCost"/> /// to weighted status. That means that /// the current cost of vertex will be /// set to its normal value /// </summary> public void MakeWeighted() { CurrentCost = WeightedCost; Status = new WeightedState(); }