<commit_before>/*!
    \file allocator.h
    \brief Memory allocator wrapper definition
    \author <NAME>
    \date 17.04.2017
    \copyright MIT License
*/

#ifndef CPPCOMMON_MEMORY_ALLOCATOR_WRAPPER_H
#define CPPCOMMON_MEMORY_ALLOCATOR_WRAPPER_H

#include "memory.h"

namespace CppCommon {

//! Memory allocator wrapper class
/*!
    Memory allocator wrapper implements standard allocator interface and
    wraps the memory manager provided as a template argument.

    Not thread-safe.
*/
template <typename T, class TMemoryManager, bool nothrow = false>
class Allocator
{
    template <typename U, class UMemoryManager, bool flag>
    friend class Allocator;

public:
    //! Element type
    typedef T value_type;
    //! Pointer to element
    typedef T* pointer;
    //! Reference to element
    typedef T& reference;
    //! Pointer to constant element
    typedef const T* const_pointer;
    //! Reference to constant element
    typedef const T& const_reference;
    //! Quantities of elements
    typedef size_t size_type;
    //! Difference between two pointers
    typedef ptrdiff_t difference_type;

    //! Initialize allocator with a given memory manager
    /*!
        \param manager - Memory manager
    */
    Allocator(TMemoryManager& manager) noexcept : _manager(manager) {}
    template <typename U>
    Allocator(const Allocator<U, TMemoryManager, nothrow>& alloc) noexcept : _manager(alloc._manager) {}
    Allocator(const Allocator<T, TMemoryManager, nothrow>& alloc) noexcept : _manager(alloc._manager) {}
    Allocator(Allocator&&) noexcept = default;
    ~Allocator() noexcept = default;

    template <typename U>
    Allocator& operator=(const Allocator<U, TMemoryManager, nothrow>& alloc) noexcept
    { _manager = alloc._manager; return *this; }
    Allocator& operator=(Allocator&&) noexcept = default;

    //! Get the address of the given reference
    /*!
        \param x - Reference to the element
        \return Pointer to the element
    */
    pointer address(reference x) const noexcept { return std::addressof(x); }
    //! Get the constant address of the given constant reference
    /*!
        \param x - Constant reference to the element
        \return Constant pointer to the element
    */
    const_pointer address(const_reference x) const noexcept { return std::addressof(x); }

    //! Get the maximum number of elements, that could potentially be allocated by the allocator
    /*!
        \return The number of elements that might be allocated as maximum by a call to the allocate() method
    */
    size_type max_size() const noexcept { return _manager.capacity(); }

    //! Allocate a block of storage suitable to contain the given count of elements
    /*!
        \param num - Number of elements to be allocated
        \param hint - Allocation hint (default is 0)
        \return A pointer to the initial element in the block of storage
    */
    pointer allocate(size_type num, const void* hint = 0);
    //! Release a block of storage previously allocated
    /*!
        \param ptr - Pointer to a block of storage
        \param num - Number of releasing elements
    */
    void deallocate(pointer ptr, size_type num);

    //! Reset the allocator
    void reset() { _manager.reset(); }

    //! Constructs an element object on the given location pointer
    /*!
        \param ptr - Pointer to a location with enough storage space to contain an element
        \param val - Value to initialize the construced element to
    */
    template <typename U, class... Args>
    void construct(U* ptr, Args&&... args) { new((void*)ptr) U(std::forward<Args>(args)...); }
    //! Destroys in-place the object pointed by the given location pointer
    /*!
        \param ptr - Pointer to the object to be destroyed
    */<commit_msg>build<commit_after>/*!
    \file allocator.h
    \brief Memory allocator wrapper definition
    \author <NAME>
    \date 17.04.2017
    \copyright MIT License
*/

#ifndef CPPCOMMON_MEMORY_ALLOCATOR_WRAPPER_H
#define CPPCOMMON_MEMORY_ALLOCATOR_WRAPPER_H

#include "memory.h"

namespace CppCommon {

//! Memory allocator wrapper class
/*!
    Memory allocator wrapper implements standard allocator interface and
    wraps the memory manager provided as a template argument.

    Not thread-safe.
*/
template <typename T, class TMemoryManager, bool nothrow = false>
class Allocator
{
    template <typename U, class UMemoryManager, bool flag>
    friend class Allocator;

public:
    //! Element type
    typedef T value_type;
    //! Pointer to element
    typedef T* pointer;
    //! Reference to element
    typedef T& reference;
    //! Pointer to constant element
    typedef const T* const_pointer;
    //! Reference to constant element
    typedef const T& const_reference;
    //! Quantities of elements
    typedef size_t size_type;
    //! Difference between two pointers
    typedef ptrdiff_t difference_type;

    //! Initialize allocator with a given memory manager
    /*!
        \param manager - Memory manager
    */
    Allocator(TMemoryManager& manager) noexcept : _manager(manager) {}
    template <typename U>
    Allocator(const Allocator<U, TMemoryManager, nothrow>& alloc) noexcept : _manager(alloc._manager) {}
    Allocator(const Allocator<T, TMemoryManager, nothrow>& alloc) noexcept : _manager(alloc._manager) {}
    Allocator(Allocator&&) noexcept = default;
    ~Allocator() noexcept = default;

    template <typename U>
    Allocator& operator=(const Allocator<U, TMemoryManager, nothrow>& alloc) noexcept
    { _manager = alloc._manager; return *this; }
    Allocator& operator=(Allocator&&) noexcept = default;

    //! Get the address of the given reference
    /*!
        \param x - Reference to the element
        \return Pointer to the element
    */
    pointer address(reference x) const noexcept { return std::addressof(x); }
    //! Get the constant address of the given constant reference
    /*!
        \param x - Constant reference to the element
        \return Constant pointer to the element
    */
    const_pointer address(const_reference x) const noexcept { return std::addressof(x); }

    //! Get the maximum number of elements, that could potentially be allocated by the allocator
    /*!
        \return The number of elements that might be allocated as maximum by a call to the allocate() method
    */
    size_type max_size() const noexcept { return _manager.max_size(); }

    //! Allocate a block of storage suitable to contain the given count of elements
    /*!
        \param num - Number of elements to be allocated
        \param hint - Allocation hint (default is 0)
        \return A pointer to the initial element in the block of storage
    */
    pointer allocate(size_type num, const void* hint = 0);
    //! Release a block of storage previously allocated
    /*!
        \param ptr - Pointer to a block of storage
        \param num - Number of releasing elements
    */
    void deallocate(pointer ptr, size_type num);

    //! Reset the allocator
    void reset() { _manager.reset(); }

    //! Constructs an element object on the given location pointer
    /*!
        \param ptr - Pointer to a location with enough storage space to contain an element
        \param val - Value to initialize the construced element to
    */
    template <typename U, class... Args>
    void construct(U* ptr, Args&&... args) { new((void*)ptr) U(std::forward<Args>(args)...); }
    //! Destroys in-place the object pointed by the given location pointer
    /*!
        \param ptr - Pointer to the object to be destroyed
    */
{'fraction_non_alphanumeric': 0.08654353562005276, 'fraction_numerical': 0.002638522427440633, 'mean_word_length': 2.897686375321337, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '3713118', 'n_tokens_mistral': 2025, 'n_tokens_neox': 1978, 'n_words': 806}