private void AddToDictionary(ref ItemData itemData) { string key = itemData.NormalizedItemValue; if (!_dictionaryBuilder.TryGetValue(key, out var dictionaryValue)) { dictionaryValue = new ItemDataCollectionValue <I>(itemData.Item); } else { dictionaryValue.Add(itemData.Item); } _dictionaryBuilder[key] = dictionaryValue; }
public ItemData this[int index] { get { return(_listBuilder[index]); } set { // Update the dictionary if it exists. if (_dictionaryBuilder is not null) { ItemData oldItemData = _listBuilder[index]; string oldNormalizedValue = oldItemData.NormalizedItemValue; string newNormalizedValue = value.NormalizedItemValue; if (!string.Equals(oldNormalizedValue, newNormalizedValue, StringComparison.OrdinalIgnoreCase)) { // Normalized values are different - delete from the old entry and add to the new entry. ItemDataCollectionValue <I> oldDictionaryEntry = _dictionaryBuilder[oldNormalizedValue]; oldDictionaryEntry.Delete(oldItemData.Item); if (oldDictionaryEntry.IsEmpty) { _dictionaryBuilder.Remove(oldNormalizedValue); } else { _dictionaryBuilder[oldNormalizedValue] = oldDictionaryEntry; } ItemDataCollectionValue <I> newDictionaryEntry = _dictionaryBuilder[newNormalizedValue]; newDictionaryEntry.Add(value.Item); _dictionaryBuilder[newNormalizedValue] = newDictionaryEntry; } else { // Normalized values are the same - replace the item in the entry. ItemDataCollectionValue <I> dictionaryEntry = _dictionaryBuilder[newNormalizedValue]; dictionaryEntry.Replace(oldItemData.Item, value.Item); _dictionaryBuilder[newNormalizedValue] = dictionaryEntry; } } _listBuilder[index] = value; } }