private void AddChangeToSolution(MyDefinitionId removedComponentId, MyDefinitionId addedComponentId, int numChanged) { for (int i = 0; i < m_solution.Count; ++i) { ComponentChange change = m_solution[i]; if ((change.IsChange() || change.IsAddition()) && change.ToAdd == removedComponentId) { int difference = change.Amount - numChanged; int toChange = Math.Min(numChanged, change.Amount); numChanged -= change.Amount; if (difference > 0) { change.Amount = difference; m_solution[i] = change; } else { m_solution.RemoveAtFast(i); } if (change.IsChange()) { m_solution.Add(ComponentChange.CreateChange(change.ToRemove, addedComponentId, toChange)); } else // change.IsAddition() { m_solution.Add(ComponentChange.CreateAddition(addedComponentId, toChange)); } if (numChanged <= 0) { break; } } } if (numChanged > 0) { m_solution.Add(ComponentChange.CreateChange(removedComponentId, addedComponentId, numChanged)); } }
private void AddRemovalToSolution(MyDefinitionId removedComponentId, int removeCount) { // First search through the changes, whether some of them didn't add the given component. If so, change the change to removal for (int i = 0; i < m_solution.Count; ++i) { ComponentChange change = m_solution[i]; if ((change.IsChange() || change.IsAddition()) && change.ToAdd == removedComponentId) { int difference = change.Amount - removeCount; int toRemove = Math.Min(removeCount, change.Amount); removeCount -= change.Amount; if (difference > 0) { change.Amount = difference; m_solution[i] = change; } else { m_solution.RemoveAtFast(i); } if (change.IsChange()) { m_solution.Add(ComponentChange.CreateRemoval(change.ToRemove, toRemove)); } if (removeCount <= 0) { break; } } } if (removeCount > 0) { m_solution.Add(ComponentChange.CreateRemoval(removedComponentId, removeCount)); } }