<commit_before>﻿using System.Collections.Generic;
using System.Linq;

namespace CjClutter.Commons.Collections
{
    public class MultiValueDictionary<TKey, TValue>
    {
        private readonly IEqualityComparer<TValue> _equalityComparer;
        private readonly Dictionary<TKey, List<TValue>> _dictionary;

        public MultiValueDictionary()
            : this(EqualityComparer<TValue>.Default) { }

        public MultiValueDictionary(IEqualityComparer<TValue> equalityComparer)
        {
            _equalityComparer = equalityComparer;
            _dictionary = new Dictionary<TKey, List<TValue>>();
        }

        public void Add(TKey key, TValue value)
        {
            var containsKey = _dictionary.ContainsKey(key);
            if (containsKey)
            {
                _dictionary[key].Add(value);
            }
            else
            {
                var valueCollection = new List<TValue> { value };
                _dictionary.Add(key, valueCollection);
            }
        }

        public void Clear()
        {
            foreach (var value in _dictionary.Values)
            {
                value.Clear();
            }

            _dictionary.Clear();
        }

        public bool Contains(TKey key, TValue item)
        {
            if (!ContainsKey(key))
            {
                return false;
            }
<commit_msg>MultiValueDictionary: avoid double lookup when adding values<commit_after>﻿using System.Collections.Generic;
using System.Linq;

namespace CjClutter.Commons.Collections
{
    public class MultiValueDictionary<TKey, TValue>
    {
        private readonly IEqualityComparer<TValue> _equalityComparer;
        private readonly Dictionary<TKey, List<TValue>> _dictionary;

        public MultiValueDictionary()
            : this(EqualityComparer<TValue>.Default) { }

        public MultiValueDictionary(IEqualityComparer<TValue> equalityComparer)
        {
            _equalityComparer = equalityComparer;
            _dictionary = new Dictionary<TKey, List<TValue>>();
        }

        public void Add(TKey key, TValue value)
        {
            List<TValue> list;
            if (!_dictionary.TryGetValue(key, out list)) {
                list = new List<TValue> { value };
                _dictionary.Add(key, list);
            }
            else {
                list.Add(value);
            }
        }

        public void Clear()
        {
            foreach (var value in _dictionary.Values)
            {
                value.Clear();
            }

            _dictionary.Clear();
        }

        public bool Contains(TKey key, TValue item)
        {
            if (!ContainsKey(key))
            {
                return false;
            }

{'fraction_non_alphanumeric': 0.08375819373634377, 'fraction_numerical': 0.0, 'mean_word_length': 1.687866927592955, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6718674', 'n_tokens_mistral': 727, 'n_tokens_neox': 693, 'n_words': 148}