public LayoutScore Times(double weightScale) { LayoutScore product = new LayoutScore(); foreach (ListItemStats <double, double> item in this.components.AllItems) { product.addComponent(item.Key, item.Value * weightScale); } return(product); }
public LayoutScore ComponentRange(int minIndexInclusive, int maxIndexExclusive) { LayoutScore range = new LayoutScore(); List <ListItemStats <double, double> > subComponents = this.components.ItemsBetweenIndices(minIndexInclusive, maxIndexExclusive); foreach (ListItemStats <double, double> component in subComponents) { range.addComponent(component.Key, component.Value); } return(range); }
public LayoutScore Plus(LayoutScore other) { int ourIndex = 0; int theirIndex = 0; LayoutScore sum = new LayoutScore(); while (true) { double priority; double weight = 0; if (ourIndex < this.components.NumItems) { if (theirIndex < other.components.NumItems) { // both coordinates exist ListItemStats <double, double> ourComponent = this.components.GetValueAtIndex(ourIndex); ListItemStats <double, double> theirComponent = other.components.GetValueAtIndex(theirIndex); priority = Math.Min(ourComponent.Key, theirComponent.Key); if (ourComponent.Key == priority) { weight += ourComponent.Value; ourIndex++; } if (theirComponent.Key == priority) { if (double.IsInfinity(weight) && double.IsInfinity(theirComponent.Value) && double.IsPositiveInfinity(weight) != double.IsPositiveInfinity(theirComponent.Value)) { // Treat negative infinity plus positive infinity as zero weight = 0; } else { weight += theirComponent.Value; } theirIndex++; } } else { // only our coordinate exists ListItemStats <double, double> ourComponent = this.components.GetValueAtIndex(ourIndex); priority = ourComponent.Key; weight = ourComponent.Value; ourIndex++; } } else { if (theirIndex < other.components.NumItems) { // only their coordinate exists ListItemStats <double, double> theirComponent = other.components.GetValueAtIndex(theirIndex); priority = theirComponent.Key; weight = theirComponent.Value; theirIndex++; } else { // no more components are left // debug-check: make sure there are no duplicate coordinates int i; for (i = 1; i < sum.components.NumItems; i++) { if (sum.components.GetValueAtIndex(i - 1).Key == sum.components.GetValueAtIndex(i).Key) { System.Diagnostics.Debug.WriteLine("error: layout score has a duplicate key"); } } return(sum); } } // only add the coordinate if it is nonzero, because zeros are always provided by default if (weight != 0) { sum.addComponent(priority, weight); } } }