/////////////////////////////////////////////////////////////////////////////////////////////// private static IDictionary <long, string> Merge( IDictionary <long, IDictionary <char, long> > flags, bool sort ) { IDictionary <long, string> result = new LongStringDictionary(); foreach (KeyValuePair <long, IDictionary <char, long> > pair in flags) { result.Add(pair.Key, (string)DictionaryToChars( pair.Value, sort)); /* ADD ONLY */ } return(result); }
/////////////////////////////////////////////////////////////////////////////////////////////// public static IDictionary <long, string> Change( /* 1.2 */ IDictionary <long, string> flags, long key, string changeFlags, bool sort, ref Result error ) { if (flags != null) { if (changeFlags != null) { IDictionary <long, string> newFlags = new LongStringDictionary(flags); IDictionary <char, long> keyFlags = newFlags.ContainsKey(key) ? CharsToDictionary(newFlags[key]) : new CharLongDictionary(); bool add = true; /* NOTE: Default to add mode. */ for (int index = 0; index < changeFlags.Length; index++) { char character = changeFlags[index]; switch (character) { case AddCharacter: { add = true; break; } case RemoveCharacter: { add = false; break; } case SetCharacter: { keyFlags.Clear(); add = true; break; } case AllChars: case AllDigitChars: case AllAlphabetChars: case AllUpperAlphabetChars: case AllLowerAlphabetChars: { IEnumerable <char> characters = GetChars(character); if (characters != null) { foreach (char newCharacter in characters) { if (add) { if (keyFlags.ContainsKey(newCharacter)) { keyFlags[newCharacter]++; } else { keyFlags.Add(newCharacter, 1); } } else if (keyFlags.ContainsKey(newCharacter)) { keyFlags.Remove(newCharacter); } } } else { error = String.Format( "invalid change meta-character '{0}' at index {1}", character, index); return(null); } break; } default: { if (!CharIsValidValue(character)) { error = String.Format( "invalid change character '{0}' at index {1}", character, index); return(null); } if (add) { if (keyFlags.ContainsKey(character)) { keyFlags[character]++; } else { keyFlags.Add(character, 1); } } else if (keyFlags.ContainsKey(character)) { keyFlags.Remove(character); } break; } } } if (newFlags.ContainsKey(key)) { if (keyFlags.Count > 0) { newFlags[key] = (string)DictionaryToChars(keyFlags, sort); } else { newFlags.Remove(key); } } else if (keyFlags.Count > 0) { newFlags.Add(key, (string)DictionaryToChars( keyFlags, sort)); /* ADD ONLY */ } return(newFlags); } else { error = "invalid change flags"; } } else { error = "invalid flags"; } return(null); }