private void Update(IEnumerable <T> list, bool isUp) { // ToList ensures it is a copy, as `state` is mutated during the iteration and `list` might depend on it. // Specifically, `state.Keys.Except(activeItems)` returns an IEnumerable that does, and has to be copied. foreach (var item in list.ToList()) { bool success = state.TryGetValue(item, out var priorState); if (!success) { // If not in dict, the item is considered to be Up. priorState = SignalState.Up; } var currentState = new SignalState(isUp, priorState.IsUp != isUp); if (currentState.Equals(SignalState.Up)) { state.Remove(item); //Console.WriteLine("item: " + item + " - state: " + currentState + " - Removed from dict!"); } else { state[item] = currentState; //Console.WriteLine("item: " + item + " - state: " + currentState); } } }
private static KeyDisjunction ParseConjunction(string disjunction) { KeyDisjunction keyDisjunction = new KeyDisjunction(); foreach (string conjunction in disjunction.Split('|')) { KeyConjunction keyConjunction = new KeyConjunction(); foreach (string term in conjunction.Split('+')) { string key = term.Trim(); bool isUp = false; bool isEdge = false; if (key.Contains("^")) { isUp = true; key = key.Replace("^", ""); } if (key.Contains("~")) { isEdge = true; key = key.Replace("~", ""); } SignalState state = new SignalState(isUp, isEdge); if (Keys.TryParse(key, true, out Keys keyboardKey)) { keyConjunction[keyboardKey] = state; } else if (MouseKeys.TryParse(key, true, out MouseKeys mouseKey)) { keyConjunction[mouseKey] = state; } else { Console.Error.WriteLine(String.Format("ERROR! - Unable to parse key: {0}", key)); } } keyDisjunction.Add(keyConjunction); } return(keyDisjunction); }
private void Update(IEnumerable <T> list, SignalState state) { Update(list, state.IsUp); }