<commit_before>/***************************************************************************************
* file        : cached_allocator.hpp
* data        : 2016/03/05
* author      : <NAME>
* contact     : <EMAIL>
* copyright   : Copyright (C) 2016  <NAME>
*             :
* description : This header contains definition of cached_allocator class which can be used to speed-up rapid allocations
*             : and deallocations of memory buffers of the same size. It is best to use this allocator to allocate and deallocate
*             : single instances of objects (memory buffers of size 1).
*             :
* references  : Original (and actual) version of source code can be found here <http://www.github.com/cas4ey/shared_allocator>.
*             :
* license     : This file is part of SharedAllocator.
*             :
*             : SharedAllocator is free software: you can redistribute it and/or modify
*             : it under the terms of the GNU General Public License as published by
*             : the Free Software Foundation, either version 3 of the License, or
*             : (at your option) any later version.
*             :
*             : SharedAllocator is distributed in the hope that it will be useful,
*             : but WITHOUT ANY WARRANTY; without even the implied warranty of
*             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*             : GNU General Public License for more details.
*             :
*             : You should have received a copy of the GNU General Public License
*             : along with SharedAllocator. If not, see <http://www.gnu.org/licenses/>.
*             :
*             : A copy of the GNU General Public License can be found in file LICENSE.
****************************************************************************************/

#ifndef SHARED___ALLOCATOR__SINGLE_CACHED_ALLOCATOR___HPP___
#define SHARED___ALLOCATOR__SINGLE_CACHED_ALLOCATOR___HPP___

#include "shared_allocator/shared_allocator.hpp"
#include <vector>

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace salloc {

    /** \brief Allocator that caches memory for objects for further usage.

    It caches deallocated memory buffers and uses them later to provide fast allocation of memory buffers of the SAME SIZE (or lesser size).
    
    \note It is better to use this allocator to allocate memory buffers of the same size.

    \note BEST TO USE IF YOU WANT TO ALLOCATE AND DEALLOCATE BUFFERS OF SIZE 1 (SINGLE INSTANCES OF OBJECTS)
    (Best fit for std::list, std::set, std::map BUT NOT for std::vector).
    
    \ingroup salloc */
    template <class T, class TAlloc = shared_allocator<T> >
    class cached_allocator
    {
    protected:

        typedef cached_allocator<T, TAlloc> ThisType;
        typedef ::std::vector<T*, typename TAlloc::template rebind<T*>::other> MemoryCache;

        MemoryCache m_memoryCache; ///< Memory cache of objects
        TAlloc        m_allocator; ///< Allocator to allocate new instances of T

    public:

        typedef T                         value_type;

        typedef value_type*                  pointer;
        typedef const value_type*      const_pointer;
        typedef void*                   void_pointer;
        typedef const void*       const_void_pointer;

        typedef value_type&                reference;
        typedef const value_type&    const_reference;

        typedef size_t                     size_type;
        typedef ptrdiff_t            difference_type;

        /** \brief Auxiliary struct to convert this type to single_cached_allocator of other type. */
        template <class U>
        struct rebind
        {
            typedef cached_allocator<U, typename TAlloc::template rebind<U>::other> other;
        };

        /** \brief Returns an actual adress of value.

        \note Uses std::addressof */
        pointer address(reference _value) const throw()
        {
            return ::std::addressof(_value);
        }

        /** \brief Returns an actual adress of const value.

        \note Uses std::addressof */
        const_pointer address(const_reference _value) const throw()
        {
            return ::std::addressof(_value);
        }

        /** \brief Default constructor.

        Does nothing. */
        cached_allocator() throw()
        {
        }

        /** \brief Empty copy constructor.

        Does nothing. */
        template <class U>
        cached_allocator(const U&) throw()
        {
        }

        /** \brief Move constructor.

        Moves reserved memory. */
        cached_allocator(ThisType&& _rvalueAlloc) : m_memoryCache(::std::move(_rvalueAlloc.m_memoryCache))
        {
        }

        /** \brief Empty assignment operator.

        Does nothing and returns reference to this allocator. */
        template <class U>
        ThisType& operator=(const U&)
        {
            return *this;
        }

        ~cached_allocator()
        {
            private_clear();
        }

        /** \brief Clear memory cache.
        <commit_msg>building by gcc under linux<commit_after>/***************************************************************************************
* file        : cached_allocator.hpp
* data        : 2016/03/05
* author      : <NAME>
* contact     : <EMAIL>
* copyright   : Copyright (C) 2016  <NAME>
*             :
* description : This header contains definition of cached_allocator class which can be used to speed-up rapid allocations
*             : and deallocations of memory buffers of the same size. It is best to use this allocator to allocate and deallocate
*             : single instances of objects (memory buffers of size 1).
*             :
* references  : Original (and actual) version of source code can be found here <http://www.github.com/cas4ey/shared_allocator>.
*             :
* license     : This file is part of SharedAllocator.
*             :
*             : SharedAllocator is free software: you can redistribute it and/or modify
*             : it under the terms of the GNU General Public License as published by
*             : the Free Software Foundation, either version 3 of the License, or
*             : (at your option) any later version.
*             :
*             : SharedAllocator is distributed in the hope that it will be useful,
*             : but WITHOUT ANY WARRANTY; without even the implied warranty of
*             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*             : GNU General Public License for more details.
*             :
*             : You should have received a copy of the GNU General Public License
*             : along with SharedAllocator. If not, see <http://www.gnu.org/licenses/>.
*             :
*             : A copy of the GNU General Public License can be found in file LICENSE.
****************************************************************************************/

#ifndef SHARED___ALLOCATOR__SINGLE_CACHED_ALLOCATOR___HPP___
#define SHARED___ALLOCATOR__SINGLE_CACHED_ALLOCATOR___HPP___

#include "shared_allocator/shared_allocator.hpp"
#include <vector>

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace salloc {

    /** \brief Allocator that caches memory for objects for further usage.

    It caches deallocated memory buffers and uses them later to provide fast allocation of memory buffers of the SAME SIZE (or lesser size).
    
    \note It is better to use this allocator to allocate memory buffers of the same size.

    \note BEST TO USE IF YOU WANT TO ALLOCATE AND DEALLOCATE BUFFERS OF SIZE 1 (SINGLE INSTANCES OF OBJECTS)
    (Best fit for std::list, std::set, std::map BUT NOT for std::vector).
    
    \ingroup salloc */
    template <class T, class TAlloc = shared_allocator<T> >
    class cached_allocator
    {
    protected:

        typedef cached_allocator<T, TAlloc> ThisType;
        typedef ::std::vector<T*, typename TAlloc::template rebind<T*>::other> MemoryCache;

        MemoryCache m_memoryCache; ///< Memory cache of objects
        TAlloc        m_allocator; ///< Allocator to allocate new instances of T

    public:

        typedef T                         value_type;

        typedef value_type*                  pointer;
        typedef const value_type*      const_pointer;
        typedef void*                   void_pointer;
        typedef const void*       const_void_pointer;

        typedef value_type&                reference;
        typedef const value_type&    const_reference;

        typedef size_t                     size_type;
        typedef ptrdiff_t            difference_type;

        /** \brief Auxiliary struct to convert this type to single_cached_allocator of other type. */
        template <class U>
        struct rebind
        {
            typedef cached_allocator<U, typename TAlloc::template rebind<U>::other> other;
        };

        /** \brief Returns an actual adress of value.

        \note Uses std::addressof */
        pointer address(reference _value) const throw()
        {
            return ::std::addressof(_value);
        }

        /** \brief Returns an actual adress of const value.

        \note Uses std::addressof */
        const_pointer address(const_reference _value) const throw()
        {
            return ::std::addressof(_value);
        }

        /** \brief Default constructor.

        Does nothing. */
        cached_allocator() throw()
        {
        }

        /** \brief Empty copy constructor.

        Does nothing. */
        cached_allocator(const ThisType&) throw()
        {
        }

        /** \brief Empty copy constructor.

        Does nothing. */
        template <class U>
        cached_allocator(const U&) throw()
        {
        }

        /** \brief Move constructor.

        Moves reserved memory. */
        cached_allocator(ThisType&& _rvalueAlloc) : m_memoryCache(::std::move(_rvalueAlloc.m_memoryCache))
        {
        }

        /** \brief Empty assignment operator.

        Does nothing and returns reference to this allocator. */
        template <class U>
        ThisType& operator=(const U&)
        {
            return *this;
        }

        ~cached_allocator()
        {
            private_clear();
        }

        /** \brief Clear memory cache.
        
{'fraction_non_alphanumeric': 0.15013979496738117, 'fraction_numerical': 0.0029822926374650513, 'mean_word_length': 1.8040240397177947, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '5704935', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2487, 'n_words': 925}