<commit_before>/***************************************************************************
 *  include/stxxl/bits/containers/pager.h
 *
 *  Part of the STXXL. See http://stxxl.sourceforge.net
 *
 *  Copyright (C) 2002, 2003, 2006 <NAME> <<EMAIL>>
 *  Copyright (C) 2011 <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_PAGER_HEADER
#define STXXL_PAGER_HEADER

#include <list>
#include <cassert>

#include <stxxl/bits/noncopyable.h>
#include <stxxl/bits/common/rand.h>
#include <stxxl/bits/common/simple_vector.h>


__STXXL_BEGIN_NAMESPACE

//! \addtogroup stlcontinternals
//! \{

enum pager_type
{
    random,
    lru
};

//! \brief Pager with \b random replacement strategy
template <unsigned npages_>
class random_pager
{
    typedef unsigned_type size_type;
    size_type num_pages;
    random_number<random_uniform_fast> rnd;

public:
    enum { n_pages = npages_ };
    random_pager(size_type num_pages = n_pages) : num_pages(num_pages) { }
    int_type kick()
    {
        return rnd(num_pages);
    }
    void hit(int_type ipage)
    {
        assert(ipage < int_type(num_pages));
        assert(ipage >= 0);
    }
    size_type size () const
    {
        return num_pages;
    }
};

//! \brief Pager with \b LRU replacement strategy
template <unsigned npages_ = 0>
class lru_pager : private noncopyable
{
    typedef unsigned_type size_type;
    typedef std::list<size_type> list_type;

    list_type history;
    simple_vector<list_type::iterator> history_entry;

public:
    enum { n_pages = npages_ };

    lru_pager(size_type num_pages = n_pages) : history_entry(num_pages)
    {
        for (size_type i = 0; i < history_entry.size(); ++i)
            history_entry[i] = history.insert(history.end(), i);
    }

    size_type kick()
    {
        return history.back();
    }

    void hit(size_type ipage)
    {
        assert(ipage < history_entry.size());
        history.splice(history.begin(), history, history_entry[ipage]);
    }

    void swap(lru_pager & obj)
    {
        history.swap(obj.history);
        history_entry.swap(obj.history_entry);
    }

    size_type size () const
    {
        return history_entry.size();<commit_msg>unify types, use size(), don't expose n_pages<commit_after>/***************************************************************************
 *  include/stxxl/bits/containers/pager.h
 *
 *  Part of the STXXL. See http://stxxl.sourceforge.net
 *
 *  Copyright (C) 2002, 2003, 2006 <NAME> <<EMAIL>>
 *  Copyright (C) 2011 <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_PAGER_HEADER
#define STXXL_PAGER_HEADER

#include <list>
#include <cassert>

#include <stxxl/bits/noncopyable.h>
#include <stxxl/bits/common/rand.h>
#include <stxxl/bits/common/simple_vector.h>


__STXXL_BEGIN_NAMESPACE

//! \addtogroup stlcontinternals
//! \{

enum pager_type
{
    random,
    lru
};

//! \brief Pager with \b random replacement strategy
template <unsigned npages_>
class random_pager
{
    enum { n_pages = npages_ };

    typedef unsigned_type size_type;

    size_type num_pages;
    random_number<random_uniform_fast> rnd;

public:
    random_pager(size_type num_pages = n_pages) : num_pages(num_pages) { }
    size_type kick()
    {
        return rnd(size());
    }

    void hit(size_type ipage)
    {
        assert(ipage < size());
    }

    size_type size() const
    {
        return num_pages;
    }
};

//! \brief Pager with \b LRU replacement strategy
template <unsigned npages_ = 0>
class lru_pager : private noncopyable
{
    enum { n_pages = npages_ };

    typedef unsigned_type size_type;
    typedef std::list<size_type> list_type;

    list_type history;
    simple_vector<list_type::iterator> history_entry;

public:
    lru_pager(size_type num_pages = n_pages) : history_entry(num_pages)
    {
        for (size_type i = 0; i < size(); ++i)
            history_entry[i] = history.insert(history.end(), i);
    }

    size_type kick()
    {
        return history.back();
    }

    void hit(size_type ipage)
    {
        assert(ipage < size());
        history.splice(history.begin(), history, history_entry[ipage]);
    }

    void swap(lru_pager & obj)
    {
        history.swap(obj.history);
        history_entry.swap(obj.history_entry);
    }

    size_type size() const
    {
        return history_entry.size();
{'fraction_non_alphanumeric': 0.17363751584283904, 'fraction_numerical': 0.010350654837346852, 'mean_word_length': 3.273465703971119, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 36, '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': '1350772', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1561, 'n_words': 333}