<commit_before>#ifndef HAND_ROLLED_VTABLE_INCLUDED__
#define HAND_ROLLED_VTABLE_INCLUDED__

#include <printable_types.hpp>

#include <array>
#include <cassert>
#include <functional>
#include <iostream>
#include <memory>
#include <type_traits>
#include <utility>
#include <vector>


#ifdef _MSC_VER
#define noexcept
#endif

class any_printable
{
public:
    // Contructors
    any_printable () :
        handle_ (nullptr),
        buffer_ {}
    {}

    template <typename T>
    any_printable (T value) :
        handle_ (nullptr),
        buffer_ {}
    { handle_ = clone_impl(std::forward<T>(value), buffer_); }

    any_printable (const any_printable & rhs) :
        handle_ (nullptr),
        buffer_ {}
    {
        if (rhs.handle_)
            handle_ = rhs.handle_->clone_into(buffer_);
    }

    any_printable (any_printable && rhs) noexcept :
        handle_ (nullptr),
        buffer_ (rhs.buffer_)
    { std::swap(rhs.handle_, handle_); }

    // Assignment
    template <typename T>
    any_printable & operator= (T value)
    {
        handle_ = clone_impl(std::forward<T>(value), buffer_);
        return *this;
    }

    any_printable & operator= (const any_printable & rhs)
    {
        any_printable temp(rhs);
        std::swap(temp.handle_, handle_);
        std::swap(temp.buffer_, buffer_);
        return *this;
    }

    any_printable & operator= (any_printable && rhs) noexcept
    {
        any_printable temp(std::move(rhs));
        std::swap(temp.handle_, handle_);
        std::swap(temp.buffer_, buffer_);
        return *this;
    }

    ~any_printable ()
    {
        if (handle_)
            handle_->destroy();
    }

    // Public interface
    void print () const
    {
        assert(handle_);
        handle_->print();
    }

private:
    typedef std::array<unsigned char, 24> buffer;

    struct handle_base
    {
        virtual ~handle_base () {}
        virtual handle_base * clone_into (buffer & buf) const = 0;
        virtual void destroy () = 0;

        // Public interface
        virtual void print () const = 0;
    };

    template <typename T, bool HeapAllocated>
    struct handle :
        handle_base
    {
        template <typename U = T>
        handle (T value,
                typename std::enable_if<
                    std::is_reference<U>::value
                >::type* = 0) :
            value_ (value)
        {}

        template <typename U = T>
        handle (T value,
                typename std::enable_if<
                    !std::is_reference<U>::value,
                    int
                >::type* = 0) noexcept :
            value_ (std::move(value))
        {}

        virtual handle_base * clone_into (buffer & buf) const
        { return clone_impl(value_, buf); }

        virtual void destroy ()
        {
            if (HeapAllocated)
                delete this;
            else
                this->~handle();
        }

        // Public interface
        virtual void print () const
        { value_.print(); }

        T value_;
    };

    template <typename T, bool HeapAllocated>
    struct handle<std::reference_wrapper<T>, HeapAllocated> :
        handle<T &, HeapAllocated>
    {
        handle (std::reference_wrapper<T> ref) :
            handle<T &, HeapAllocated> (ref.get())
        {}
    };

    template <typename T>
    static handle_base * clone_impl (T value, buffer & buf)
    {
        handle_base * retval = nullptr;
        typedef typename std::remove_reference<T>::type handle_t;
        const bool too_big = sizeof(buf) < sizeof(handle<handle_t, false>);
        if (too_big) {
            retval = new handle<handle_t, true>(std::forward<T>(value));
        } else {
            new (&buf) handle<handle_t, false>(std::forward<T>(value));
            retval =
                static_cast<handle_base *>(static_cast<void *>(&buf));
        }
        return retval;
    }

    handle_base * handle_;
    buffer buffer_;
};

#endif
<commit_msg>Align the values stored in the SBO version of hand_rolled.<commit_after>#ifndef HAND_ROLLED_VTABLE_INCLUDED__
#define HAND_ROLLED_VTABLE_INCLUDED__

#include <printable_types.hpp>

#include <array>
#include <cassert>
#include <functional>
#include <iostream>
#include <memory>
#include <type_traits>
#include <utility>
#include <vector>


#ifdef _MSC_VER
#define noexcept
#endif

class any_printable
{
public:
    // Contructors
    any_printable () :
        handle_ (nullptr),
        buffer_ {}
    {}

    template <typename T>
    any_printable (T value) :
        handle_ (nullptr),
        buffer_ {}
    { handle_ = clone_impl(std::forward<T>(value), buffer_); }

    any_printable (const any_printable & rhs) :
        handle_ (nullptr),
        buffer_ {}
    {
        if (rhs.handle_)
            handle_ = rhs.handle_->clone_into(buffer_);
    }

    any_printable (any_printable && rhs) noexcept :
        handle_ (nullptr),
        buffer_ (rhs.buffer_)
    { std::swap(rhs.handle_, handle_); }

    // Assignment
    template <typename T>
    any_printable & operator= (T value)
    {
        handle_ = clone_impl(std::forward<T>(value), buffer_);
        return *this;
    }

    any_printable & operator= (const any_printable & rhs)
    {
        any_printable temp(rhs);
        std::swap(temp.handle_, handle_);
        std::swap(temp.buffer_, buffer_);
        return *this;
    }

    any_printable & operator= (any_printable && rhs) noexcept
    {
        any_printable temp(std::move(rhs));
        std::swap(temp.handle_, handle_);
        std::swap(temp.buffer_, buffer_);
        return *this;
    }

    ~any_printable ()
    {
        if (handle_)
            handle_->destroy();
    }

    // Public interface
    void print () const
    {
        assert(handle_);
        handle_->print();
    }

private:
    typedef std::array<unsigned char, 24> buffer;

    struct handle_base
    {
        virtual ~handle_base () {}
        virtual handle_base * clone_into (buffer & buf) const = 0;
        virtual void destroy () = 0;

        // Public interface
        virtual void print () const = 0;
    };

    template <typename T, bool HeapAllocated>
    struct handle :
        handle_base
    {
        template <typename U = T>
        handle (T value,
                typename std::enable_if<
                    std::is_reference<U>::value
                >::type* = 0) :
            value_ (value)
        {}

        template <typename U = T>
        handle (T value,
                typename std::enable_if<
                    !std::is_reference<U>::value,
                    int
                >::type* = 0) noexcept :
            value_ (std::move(value))
        {}

        virtual handle_base * clone_into (buffer & buf) const
        { return clone_impl(value_, buf); }

        virtual void destroy ()
        {
            if (HeapAllocated)
                delete this;
            else
                this->~handle();
        }

        // Public interface
        virtual void print () const
        { value_.print(); }

        T value_;
    };

    template <typename T, bool HeapAllocated>
    struct handle<std::reference_wrapper<T>, HeapAllocated> :
        handle<T &, HeapAllocated>
    {
        handle (std::reference_wrapper<T> ref) :
            handle<T &, HeapAllocated> (ref.get())
        {}
    };

    template <typename T>
    static handle_base * clone_impl (T value, buffer & buf)
    {
        handle_base * retval = nullptr;
        typedef typename std::remove_reference<T>::type handle_t;
        void * buf_ptr = &buf;
        std::size_t buf_size = sizeof(buf);
        buf_ptr = std::align(
            alignof(handle<handle_t, false>),
            sizeof(handle<handle_t, false>),
            buf_ptr,
            buf_size
        );
        if (buf_ptr) {
            new (buf_ptr) handle<handle_t, false>(std::forward<T>(value));
            retval = static_cast<handle_base *>(buf_ptr);
        } else {
            retval = new handle<handle_t, true>(std::forward<T>(value));
        }
        return retval;
    }

    handle_base * handle_;
    buffer buffer_;
};

#endif

{'fraction_non_alphanumeric': 0.11649649403370649, 'fraction_numerical': 0.0017222290564645098, 'mean_word_length': 1.9812981298129813, 'pattern_counts': {'":': 0, '<': 76, '<?xml version=': 0, '>': 83, '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': '3844102', 'n_tokens_mistral': 2469, 'n_tokens_neox': 2401, 'n_words': 560}