<commit_before>#ifndef PRIORITY_BUFFER_H
#define PRIORITY_BUFFER_H

#include <vector>


template <typename T>
class PriorityBuffer {
  public:
    void Push(const T& t) {
        objects.push_back(t);
    }

    T& Pop() {
        auto& object = objects.back();
        objects.pop_back();
        return object;
    }

  private:
    std::vector<T> objects;
};<commit_msg>First crack at priority buffer<commit_after>#ifndef PRIORITY_BUFFER_H
#define PRIORITY_BUFFER_H

#include <algorithm>
#include <chrono>
#include <functional>
#include <deque>


template <typename T>
class PriorityBuffer {
    typedef std::function<unsigned long long(const T&)> PriorityFunction;
    typedef std::pair<unsigned long long, T> PriorityObject;

  public:
    PriorityBuffer(PriorityFunction get_priority=&PriorityBuffer::epoch_priority)
        : get_priority_{get_priority} {}

    void Push(const T& t) {
        auto priority = get_priority_(t);
        auto find = std::find_if(objects_.begin(), objects_.end(),
                [priority] (const PriorityObject& o) {
                    return priority >= o.first;
                });
        if (find == objects_.begin()) {
            objects_.emplace_front(priority, t);
        } else if (find == objects_.end()) {
            objects_.emplace_back(priority, t);
        } else {
            auto position = find - objects_.begin();
            std::rotate(objects_.begin(), objects_.begin() + position, objects_.end());
            objects_.emplace_front(priority, t);
            std::rotate(objects_.begin(), objects_.end() - position, objects_.end());
        }
    }

    T Pop() {
        auto object = objects_.front();
        objects_.pop_front();
        return object.second;
    }

  private:
    static unsigned long long epoch_priority(const T& t) {
        return std::chrono::steady_clock::now().time_since_epoch().count();
    }

    PriorityFunction get_priority_;
    std::deque<PriorityObject> objects_;
};
{'fraction_non_alphanumeric': 0.12385786802030457, 'fraction_numerical': 0.0, 'mean_word_length': 2.7976878612716765, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 15, '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': '3353468', 'n_tokens_mistral': 583, 'n_tokens_neox': 557, 'n_words': 131}