<commit_before>﻿namespace AgileObjects.AgileMapper.Caching
{
    using System;
    using System.Collections.Generic;

    internal class ArrayCache<TKey, TValue> : ICache<TKey, TValue>
    {
        private const int DefaultCapacity = 10;
        private readonly object _keyLock = new object();

        private TKey[] _keys;
        private TValue[] _values;
        private int _capacity;
        private int _length;

        public ArrayCache()
        {
            _capacity = DefaultCapacity;
            _length = 0;
            _keys = new TKey[DefaultCapacity];
            _values = new TValue[DefaultCapacity];
        }

        public IEnumerable<TValue> Values
        {
            get
            {
                for (var i = 0; i < _length; i++)
                {
                    yield return _values[i];
                }
            }
        }

        public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
            => GetOrAdd(key, valueFactory, k => k);

        public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory, Func<TKey, TKey> keyFactory)
        {
            TValue value;

            var currentLength = _length;

            if (TryGetValue(key, 0, out value))
            {
                return value;
            }

            lock (_keyLock)
            {
                if (TryGetValue(key, currentLength, out value))
                {
                    return value;
                }

                EnsureCapacity();

                value = valueFactory.Invoke(key);
                key = keyFactory.Invoke(key);

                _keys[_length] = key;
                _values[_length] = value;

                ++_length;
            }

            return value;
        }

        private bool TryGetValue(TKey key, int startIndex, out TValue value)
        {
            for (var i = startIndex; i < _length; i++)
            {
                if (_keys[i].Equals(key))
                {
                    value = _values[i];
                    return true;
                }
            }

            value = default(TValue);
            return false;
        }

        private void EnsureCapacity()
        {<commit_msg>Short-circuiting cache check if keys are the same object<commit_after>﻿namespace AgileObjects.AgileMapper.Caching
{
    using System;
    using System.Collections.Generic;

    internal class ArrayCache<TKey, TValue> : ICache<TKey, TValue>
    {
        private const int DefaultCapacity = 10;
        private readonly object _keyLock = new object();

        private TKey[] _keys;
        private TValue[] _values;
        private int _capacity;
        private int _length;

        public ArrayCache()
        {
            _capacity = DefaultCapacity;
            _length = 0;
            _keys = new TKey[DefaultCapacity];
            _values = new TValue[DefaultCapacity];
        }

        public IEnumerable<TValue> Values
        {
            get
            {
                for (var i = 0; i < _length; i++)
                {
                    yield return _values[i];
                }
            }
        }

        public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
            => GetOrAdd(key, valueFactory, k => k);

        public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory, Func<TKey, TKey> keyFactory)
        {
            TValue value;

            var currentLength = _length;

            if (TryGetValue(key, 0, out value))
            {
                return value;
            }

            lock (_keyLock)
            {
                if (TryGetValue(key, currentLength, out value))
                {
                    return value;
                }

                EnsureCapacity();

                value = valueFactory.Invoke(key);
                key = keyFactory.Invoke(key);

                _keys[_length] = key;
                _values[_length] = value;

                ++_length;
            }

            return value;
        }

        private bool TryGetValue(TKey key, int startIndex, out TValue value)
        {
            for (var i = startIndex; i < _length; i++)
            {
                var thisKey = _keys[i];

                if (ReferenceEquals(thisKey, key) || thisKey.Equals(key))
                {
                    value = _values[i];
                    return true;
                }
            }

            value = default(TValue);
            return false;
        }

        private void EnsureCapacity()
        {
{'fraction_non_alphanumeric': 0.08234258036107442, 'fraction_numerical': 0.002201673271686482, 'mean_word_length': 1.2423494570582427, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '342967', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1140, 'n_words': 301}