<commit_before>﻿// Copyright (c) 2013 <NAME>
// See the file COPYING for license details.

using System;
using System.Collections.Generic;
using System.Threading;

namespace x2
{
    public class RefCounted
    {
        private int refCount;

        protected void IncrementRefCount()
        {
            Interlocked.Increment(ref refCount);
        }

        protected int DecrementRefCount()
        {
            return Interlocked.Decrement(ref refCount);
        }
    }

    /// <summary>
    /// Minimal generic object pool.
    /// </summary>
    /// <typeparam name="T">
    /// Specifies the type of objects in the pool.
    /// </typeparam>
    public class Pool<T> where T : class
    {
        private readonly Stack<T> store;

        public Pool()
        {
            store = new Stack<T>();
            Diag = new Diagnostics(this);
        }

        public T Acquire()
        {
            Diag.IncrementAcquireCount();

            lock (store)
            {
                if (store.Count != 0)
                {
                    return store.Pop();
                }
            }
            return null;
        }

        public void Release(T item)
        {
            Diag.IncrementReleaseCount();

            lock (store)
            {
                store.Push(item);
            }
        }

        #region Diagnostics

        /// <summary>
        /// Gets the Diagnostics object for this Pool.
        /// </summary>
        public Diagnostics Diag { get; private set; }

        /// <summary>
        /// Internal diagnostics helper class for Pool.
        /// </summary>
        public class Diagnostics
        {
            private readonly Pool<T> owner;

            private int acquireCount;
            private int releaseCount;

            public int AcquireCount { get { return acquireCount; } }
            public int ReleaseCount { get { return releaseCount; } }

            public int StoreSize {
                get
                {
                    lock (owner.store)
                    {
                        return owner.store.Count;
                    }
                }
            }

            internal Diagnostics(Pool<T> owner)
            {
                this.owner = owner;
            }
<commit_msg>added abstract class PooledObject<commit_after>﻿// Copyright (c) 2013 <NAME>
// See the file COPYING for license details.

using System;
using System.Collections.Generic;
using System.Threading;

namespace x2
{
    /// <summary>
    /// Common base class for any pooled object with custom lifecycle based on
    /// reference counting.
    /// </summary>
    public abstract class PooledObject
    {
        private int refCount;

        /// <summary>
        /// Releases this object and returns it to the pool.
        /// </summary>
        public abstract void Release();

        /// <summary>
        /// Increases the reference count to this object.
        /// </summary>
        public void AcquireReference()
        {
            Interlocked.Increment(ref refCount);
        }

        /// <summary>
        /// Decreases the reference count to this object, and releases this
        /// object if the reference count reached zero.
        /// </summary>
        public void ReleaseReference()
        {
            if (Interlocked.Decrement(ref refCount) <= 0)
            {
                Release();
            }
        }
    }

    /// <summary>
    /// Minimal generic object pool.
    /// </summary>
    /// <typeparam name="T">
    /// Specifies the type of objects in the pool.
    /// </typeparam>
    public class Pool<T> where T : class
    {
        private readonly Stack<T> store;

        public Pool()
        {
            store = new Stack<T>();
            Diag = new Diagnostics(this);
        }

        public T Acquire()
        {
            Diag.IncrementAcquireCount();

            lock (store)
            {
                if (store.Count != 0)
                {
                    return store.Pop();
                }
            }
            return null;
        }

        public void Release(T item)
        {
            Diag.IncrementReleaseCount();

            lock (store)
            {
                store.Push(item);
            }
        }

        #region Diagnostics

        /// <summary>
        /// Gets the diagnostics object for this Pool.
        /// </summary>
        public Diagnostics Diag { get; private set; }

        /// <summary>
        /// Internal diagnostics helper class for Pool.
        /// </summary>
        public class Diagnostics
        {
            private readonly Pool<T> owner;

            private int acquireCount;
            private int releaseCount;

            public int AcquireCount { get { return acquireCount; } }
            public int ReleaseCount { get { return releaseCount; } }

            public int StoreSize {
                get
                {
                    lock (owner.store)
                    {
                        return owner.store.Count;
                    }
                }
            }

            internal Diagnostics(Pool<T> owner)
            {
                this.owner = owner;
            }

{'fraction_non_alphanumeric': 0.09330783938814531, 'fraction_numerical': 0.00248565965583174, 'mean_word_length': 1.1795833333333334, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 39, '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': '4897258', 'n_tokens_mistral': 1314, 'n_tokens_neox': 1239, 'n_words': 377}