<commit_before>/***************************************************************************
 *  include/stxxl/bits/mng/block_alloc.h
 *
 *  Part of the STXXL. See http://stxxl.sourceforge.net
 *
 *  Copyright (C) 2002-2007 <NAME> <<EMAIL>>
 *  Copyright (C) 2008-2009 <NAME> <<EMAIL>>
 *
 *  Distributed under the Boost Software License, Version 1.0.
 *  (See accompanying file LICENSE_1_0.txt or copy at
 *  http://www.boost.org/LICENSE_1_0.txt)
 **************************************************************************/

#ifndef STXXL_MNG__BLOCK_ALLOC_H
#define STXXL_MNG__BLOCK_ALLOC_H

#include <algorithm>
#include <stxxl/bits/parallel.h>
#include <stxxl/bits/common/rand.h>
#include <stxxl/bits/mng/config.h>


__STXXL_BEGIN_NAMESPACE

//! \ingroup mnglayer

//! \weakgroup alloc Allocation functors
//! Standard allocation strategies encapsulated in functors
//! \{

//! \brief example disk allocation scheme functor
//! \remarks model of \b allocation_strategy concept
struct basic_allocation_strategy
{
    basic_allocation_strategy(int disks_begin, int disks_end);
    basic_allocation_strategy();
    int operator () (int i) const;
    static const char * name();
};

//! \brief striping disk allocation scheme functor
//! \remarks model of \b allocation_strategy concept
struct striping
{
    int begin, diff;

    striping(int b, int e) : begin(b), diff(e - b)
    { }

    striping() : begin(0)
    {
        diff = config::get_instance()->disks_number();
    }

    int operator () (int i) const
    {
        return begin + i % diff;
    }

    static const char * name()
    {
        return "striping";
    }
/*
    // FIXME WHY?
    virtual ~striping()
    { }
*/
};

//! \brief fully randomized disk allocation scheme functor
//! \remarks model of \b allocation_strategy concept
struct FR : public striping
{
    random_number<random_uniform_fast> rnd;

    FR(int b, int e) : striping(b, e)
    { }

    FR() : striping()
    { }

    int operator () (int /*i*/) const
    {
        return begin + rnd(diff);
    }

    static const char * name()
    {
        return "fully randomized striping";
    }
};

//! \brief simple randomized disk allocation scheme functor
//! \remarks model of \b allocation_strategy concept
struct SR : public striping
{
    random_number<random_uniform_fast> rnd;

    int offset;

    SR(int b, int e) : striping(b, e)
    {
        offset = rnd(diff);
    }

    SR() : striping()
    {
        offset = rnd(diff);
    }

    int operator () (int i) const
    {
        return begin + (i + offset) % diff;
    }

    static const char * name()
    {
        return "simple randomized striping";
    }
};

//! \brief randomized cycling disk allocation scheme functor
//! \remarks model of \b allocation_strategy concept
struct RC : public striping
{
    std::vector<int> perm;

    RC(int b, int e) : striping(b, e), perm(diff)
    {
        for (int i = 0; i < diff; i++)
            perm[i] = i;

        stxxl::random_number<random_uniform_fast> rnd;
        std::random_shuffle(perm.begin(), perm.end(), rnd __STXXL_FORCE_SEQUENTIAL);
    }

    RC() : striping(), perm(diff)
    {
        for (int i = 0; i < diff; i++)
            perm[i] = i;

        random_number<random_uniform_fast> rnd;
        std::random_shuffle(perm.begin(), perm.end(), rnd __STXXL_FORCE_SEQUENTIAL);
    }

    int operator () (int i) const
    {
        return begin + perm[i % diff];
    }

    static const char * name()
    {
        return "randomized cycling striping";
    }
};

struct RC_disk : public RC
{
    RC_disk(int b, int e) : RC(b, e)
    { }

    RC_disk() : RC(config::get_instance()->regular_disk_range().first, config::get_instance()->regular_disk_range().second)
    { }<commit_msg>avoid duplication of initialization code<commit_after>/***************************************************************************
 *  include/stxxl/bits/mng/block_alloc.h
 *
 *  Part of the STXXL. See http://stxxl.sourceforge.net
 *
 *  Copyright (C) 2002-2007 <NAME> <<EMAIL>>
 *  Copyright (C) 2008-2009 <NAME> <<EMAIL>>
 *
 *  Distributed under the Boost Software License, Version 1.0.
 *  (See accompanying file LICENSE_1_0.txt or copy at
 *  http://www.boost.org/LICENSE_1_0.txt)
 **************************************************************************/

#ifndef STXXL_MNG__BLOCK_ALLOC_H
#define STXXL_MNG__BLOCK_ALLOC_H

#include <algorithm>
#include <stxxl/bits/parallel.h>
#include <stxxl/bits/common/rand.h>
#include <stxxl/bits/mng/config.h>


__STXXL_BEGIN_NAMESPACE

//! \ingroup mnglayer

//! \weakgroup alloc Allocation functors
//! Standard allocation strategies encapsulated in functors
//! \{

//! \brief example disk allocation scheme functor
//! \remarks model of \b allocation_strategy concept
struct basic_allocation_strategy
{
    basic_allocation_strategy(int disks_begin, int disks_end);
    basic_allocation_strategy();
    int operator () (int i) const;
    static const char * name();
};

//! \brief striping disk allocation scheme functor
//! \remarks model of \b allocation_strategy concept
struct striping
{
protected:
    int begin, diff;

public:
    striping(int b, int e) : begin(b), diff(e - b)
    { }

    striping() : begin(0)
    {
        diff = config::get_instance()->disks_number();
    }

    int operator () (int i) const
    {
        return begin + i % diff;
    }

    static const char * name()
    {
        return "striping";
    }
};

//! \brief fully randomized disk allocation scheme functor
//! \remarks model of \b allocation_strategy concept
struct FR : public striping
{
private:
    random_number<random_uniform_fast> rnd;

public:
    FR(int b, int e) : striping(b, e)
    { }

    FR() : striping()
    { }

    int operator () (int /*i*/) const
    {
        return begin + rnd(diff);
    }

    static const char * name()
    {
        return "fully randomized striping";
    }
};

//! \brief simple randomized disk allocation scheme functor
//! \remarks model of \b allocation_strategy concept
struct SR : public striping
{
private:
    int offset;

    void init()
    {
        random_number<random_uniform_fast> rnd;
        offset = rnd(diff);
    }

public:
    SR(int b, int e) : striping(b, e)
    {
        init();
    }

    SR() : striping()
    {
        init();
    }

    int operator () (int i) const
    {
        return begin + (i + offset) % diff;
    }

    static const char * name()
    {
        return "simple randomized striping";
    }
};

//! \brief randomized cycling disk allocation scheme functor
//! \remarks model of \b allocation_strategy concept
struct RC : public striping
{
private:
    std::vector<int> perm;

    void init()
    {
        for (int i = 0; i < diff; i++)
            perm[i] = i;

        stxxl::random_number<random_uniform_fast> rnd;
        std::random_shuffle(perm.begin(), perm.end(), rnd __STXXL_FORCE_SEQUENTIAL);
    }

public:
    RC(int b, int e) : striping(b, e), perm(diff)
    {
        init();
    }

    RC() : striping(), perm(diff)
    {
        init();
    }

    int operator () (int i) const
    {
        return begin + perm[i % diff];
    }

    static const char * name()
    {
        return "randomized cycling striping";
    }
};

struct RC_disk : public RC
{
    RC_disk(int b, int e) : RC(b, e)
    { }

    RC_disk() : RC(config::get_instance()->regular_disk_range().first, config::get_instance()->regular_disk_range().second)
    { }
{'fraction_non_alphanumeric': 0.16379310344827586, 'fraction_numerical': 0.0066002155172413795, 'mean_word_length': 2.7786259541984735, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '231728', 'n_tokens_mistral': 2486, 'n_tokens_neox': 2342, 'n_words': 633}