<commit_before>//============================================================================
// include/vsmc/thread/thread_num.hpp
//----------------------------------------------------------------------------
//
//                         vSMC: Scalable Monte Carlo
//
// This file is distribured under the 2-clauses BSD License.
// See LICENSE for details.
//============================================================================

#ifndef VSMC_THREAD_THREAD_NUM_HPP
#define VSMC_THREAD_THREAD_NUM_HPP

#include <vsmc/internal/common.hpp>
#include <cstdlib>
#include <thread>
#include <vector>

namespace vsmc {

/// \brief Number of thread used by algorithms
/// \ingroup Thread
class ThreadNum
{
    public :

    static ThreadNum &instance ()
    {
        static ThreadNum num;

        return num;
    }

    std::size_t thread_num () const {return thread_num_;}

    std::size_t thread_num (std::size_t num)
    {
        std::size_t old_num = thread_num_;
        thread_num_ = num;

        return old_num;
    }

    template <typename Range>
    std::vector<Range> partition (const Range &range) const
    {
        typedef typename Range::const_iterator size_type;
        size_type N = range.end() - range.begin();
        size_type tn = static_cast<size_type>(thread_num());
        size_type block_size =  0;

        if (N < tn)
            block_size = 1;
        else if (N % tn)
            block_size = N / tn + 1;
        else
            block_size = N / tn;

        std::vector<Range> range_vec;
        range_vec.reserve(thread_num());
        size_type B = range.begin();
        while (N > 0) {
            size_type next = N < block_size ? N : block_size;
            range_vec.push_back(Range(B, B + next));<commit_msg>doc thread_num member<commit_after>//============================================================================
// include/vsmc/thread/thread_num.hpp
//----------------------------------------------------------------------------
//
//                         vSMC: Scalable Monte Carlo
//
// This file is distribured under the 2-clauses BSD License.
// See LICENSE for details.
//============================================================================

#ifndef VSMC_THREAD_THREAD_NUM_HPP
#define VSMC_THREAD_THREAD_NUM_HPP

#include <vsmc/internal/common.hpp>
#include <cstdlib>
#include <thread>
#include <vector>

namespace vsmc {

/// \brief Number of threads used by algorithms
/// \ingroup Thread
class ThreadNum
{
    public :

    static ThreadNum &instance ()
    {
        static ThreadNum num;

        return num;
    }

    std::size_t thread_num () const {return thread_num_;}

    /// \brief Set a new number of threads, return the old number
    std::size_t thread_num (std::size_t num)
    {
        std::size_t old_num = thread_num_;
        thread_num_ = num;

        return old_num;
    }

    template <typename Range>
    std::vector<Range> partition (const Range &range) const
    {
        typedef typename Range::const_iterator size_type;
        size_type N = range.end() - range.begin();
        size_type tn = static_cast<size_type>(thread_num());
        size_type block_size =  0;

        if (N < tn)
            block_size = 1;
        else if (N % tn)
            block_size = N / tn + 1;
        else
            block_size = N / tn;

        std::vector<Range> range_vec;
        range_vec.reserve(thread_num());
        size_type B = range.begin();
        while (N > 0) {
            size_type next = N < block_size ? N : block_size;
            range_vec.push_back(Range(B, B + next));
{'fraction_non_alphanumeric': 0.22119944211994422, 'fraction_numerical': 0.002789400278940028, 'mean_word_length': 2.15391380826737, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, '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': '3932929', 'n_tokens_mistral': 1051, 'n_tokens_neox': 1004, 'n_words': 266}