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

namespace SwissArmyKnife.Pools
{
    /// <summary>
    /// A simple <see cref="ObjectPool"/>
    /// </summary>
    /// <remarks>
    /// This type is threadsafe
    /// </remarks>
    public sealed class SimpleObjectPool : ObjectPool
    {
        private readonly object _lock;
        private readonly Dictionary<Type, Stack> _stacks;

        /// <summary>
        /// Initializes a new instance of the <see cref="SimpleObjectPool"/>
        /// </summary>
        public SimpleObjectPool()
        {
            this._lock = new object();
            this._stacks = new Dictionary<Type, Stack>(5);
        }

        /// <summary>
        /// Add a <see cref="PoolableObject"/> to the pool
        /// </summary>
        /// <param name="objectToAdd">
        /// The object to add to the pool
        /// </param>
        public sealed override void Add<T>(T objectToAdd)
        {
            if (objectToAdd == null)
                throw new ArgumentNullException(nameof(objectToAdd));

            lock (this._lock)
            {
                Type originType = objectToAdd.GetType();
                Stack typeStack;

                if (!this._stacks.TryGetValue(originType, out typeStack))
                {
                    typeStack = new Stack();
                    this._stacks.Add(originType, typeStack);
                }

                objectToAdd.Pool = this;
                typeStack.Push(objectToAdd);
            }
        }

        /// <summary>
        /// Get a <see cref="PoolableObject"/> from the pool
        /// </summary>
        /// <returns>
        /// A <see cref="PoolableObject"/> from the pool or null
        /// when the pool is exhausted
        /// </returns>
        public sealed override T Get<T>()
        {
            lock (this._lock)
            {
                T result = null;

                Type originType = typeof(T);
                Stack typeStack;
                if (this._stacks.TryGetValue(originType, out typeStack) 
                    && typeStack.Peek() != null)
                {
                    result = (T)typeStack.Pop();
                }

                return result;
            }
        }

        /// <summary>
        /// Try to get a <see cref="PoolableObject"/> from the pool
        /// </summary>
        /// <typeparam name="T">
        /// The type of the object to get from the pool<commit_msg>Removed useless lock from SimpleObjectPool<commit_after>﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace SwissArmyKnife.Pools
{
    /// <summary>
    /// A simple <see cref="ObjectPool"/>
    /// </summary>
    /// <remarks>
    /// This type is threadsafe
    /// </remarks>
    public sealed class SimpleObjectPool : ObjectPool
    {
        private readonly object _lock;
        private readonly Dictionary<Type, Stack> _stacks;

        /// <summary>
        /// Initializes a new instance of the <see cref="SimpleObjectPool"/>
        /// </summary>
        public SimpleObjectPool()
        {
            this._lock = new object();
            this._stacks = new Dictionary<Type, Stack>(5);
        }

        /// <summary>
        /// Add a <see cref="PoolableObject"/> to the pool
        /// </summary>
        /// <param name="objectToAdd">
        /// The object to add to the pool
        /// </param>
        public sealed override void Add<T>(T objectToAdd)
        {
            if (objectToAdd == null)
                throw new ArgumentNullException(nameof(objectToAdd));

            lock (this._lock)
            {
                Type originType = objectToAdd.GetType();
                Stack typeStack;

                if (!this._stacks.TryGetValue(originType, out typeStack))
                {
                    typeStack = new Stack();
                    this._stacks.Add(originType, typeStack);
                }

                objectToAdd.Pool = this;
                typeStack.Push(objectToAdd);
            }
        }

        /// <summary>
        /// Get a <see cref="PoolableObject"/> from the pool
        /// </summary>
        /// <returns>
        /// A <see cref="PoolableObject"/> from the pool or null
        /// when the pool is exhausted
        /// </returns>
        public sealed override T Get<T>()
        {
            T result = null;

            Type originType = typeof(T);
            Stack typeStack;
            if (this._stacks.TryGetValue(originType, out typeStack) 
                && typeStack.Peek() != null)
            {
                result = (T)typeStack.Pop();
            }

            return result;
        }

        /// <summary>
        /// Try to get a <see cref="PoolableObject"/> from the pool
        /// </summary>
        /// <typeparam name="T">
        /// The type of the object to get from the pool
{'fraction_non_alphanumeric': 0.11943031536113936, 'fraction_numerical': 0.0004069175991861648, 'mean_word_length': 1.377176015473888, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, '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': '7477481', 'n_tokens_mistral': 1281, 'n_tokens_neox': 1231, 'n_words': 351}