<commit_before>#pragma once
#ifndef UPDATE_H__
#include "common.h"

namespace sketch {
namespace update {
struct Increment {
    // Saturates
    template<typename T, typename IntType>
    void operator()(T &ref, IntType maxval) const {
        if(static_cast<IntType>(ref) < maxval)
            ref = ref + 1;
        //ref += (ref < maxval);
    }
    template<typename T, typename Container, typename IntType>
    void operator()(std::vector<T> &ref, Container &con, IntType nbits) const {
            unsigned count = con[ref[0]];
            ++count;
            if(range_check<typename std::decay_t<decltype(*(std::declval<Container>().cbegin()))>>(nbits, count) == 0) {
                for(const auto el: ref)
                    con[el] = count;
            }
    }
    template<typename... Args>
    Increment(Args &&... args) {}
    static uint64_t est_count(uint64_t val) {
        return val;
    }
    template<typename T1, typename T2>
    static auto combine(const T1 &i, const T2 &j) {
        using RetType = std::common_type_t<T1, T2>;
        return RetType(i) + RetType(j);
    }
};
struct PowerOfTwo {
    common::DefaultRNGType rng_;
    uint64_t  gen_;
    uint8_t nbits_;
    // Also saturates
    template<typename T, typename IntType>
    void operator()(T &ref, IntType maxval) {
#if !NDEBUG
        std::fprintf(stderr, "maxval: %zu. ref: %zu\n", size_t(maxval), size_t(ref));
#endif
        if(unsigned(ref) == 0) ref = 1;
        else {
            if(ref >= maxval) return;
            if(__builtin_expect(nbits_ < ref, 0)) gen_ = rng_(), nbits_ = 64;
            const unsigned oldref = ref;
            ref = ref + ((gen_ & (UINT64_C(-1) >> (64 - unsigned(ref)))) == 0);
            gen_ >>= oldref, nbits_ -= oldref;
        }
    }
    template<typename T, typename Container, typename IntType, typename Alloc>
    void operator()(std::vector<T, Alloc> &ref, Container &con, IntType nbits) {
        uint64_t val = con[ref[0]];
        if(val == 0) {
            for(const auto el: ref)
                con[el] = 1;
        } else {
            if(__builtin_expect(nbits_ < val, 0)) gen_ = rng_(), nbits_ = 64;
            auto oldval = val;
            if((gen_ & (UINT64_C(-1) >> (64 - val))) == 0) {
                ++val;
                if(range_check(nbits, val) == 0)
                    for(const auto el: ref)
                        con[el] = val;
            }
            gen_ >>= oldval;
            nbits_ -= oldval;
        }
    }
    template<typename T1, typename T2><commit_msg>update::PowerOfTwo<commit_after>#pragma once
#ifndef UPDATE_H__
#include "common.h"

namespace sketch {
namespace update {
struct Increment {
    // Saturates
    template<typename T, typename IntType>
    void operator()(T &ref, IntType maxval) const {
        if(static_cast<IntType>(ref) < maxval)
            ref = ref + 1;
        //ref += (ref < maxval);
    }
    template<typename T, typename Container, typename IntType>
    void operator()(std::vector<T> &ref, Container &con, IntType nbits) const {
            unsigned count = con[ref[0]];
            ++count;
            if(range_check<typename std::decay_t<decltype(*(std::declval<Container>().cbegin()))>>(nbits, count) == 0) {
                for(const auto el: ref)
                    con[el] = count;
            }
    }
    template<typename... Args>
    Increment(Args &&... args) {}
    static uint64_t est_count(uint64_t val) {
        return val;
    }
    template<typename T1, typename T2>
    static auto combine(const T1 &i, const T2 &j) {
        using RetType = std::common_type_t<T1, T2>;
        return RetType(i) + RetType(j);
    }
};
struct PowerOfTwo {
    common::DefaultRNGType rng_;
    uint64_t  gen_;
    uint8_t nbits_;
    // Also saturates
    template<typename T, typename IntType>
    void operator()(T &ref, IntType maxval) {
#if !NDEBUG
        std::fprintf(stderr, "maxval: %zu. ref: %zu\n", size_t(maxval), size_t(ref));
#endif
        if(static_cast<IntType>(ref) == 0) ref = 1;
        else {
            if(ref >= maxval) return;
            if(__builtin_expect(nbits_ < ref, 0)) gen_ = rng_(), nbits_ = 64;
            const IntType oldref = ref;
            ref = oldref + ((gen_ & (UINT64_C(-1) >> (64 - oldref))) == 0);
            gen_ >>= oldref, nbits_ -= oldref;
        }
    }
    template<typename T, typename Container, typename IntType, typename Alloc>
    void operator()(std::vector<T, Alloc> &ref, Container &con, IntType nbits) {
        uint64_t val = con[ref[0]];
        if(val == 0) {
            for(const auto el: ref)
                con[el] = 1;
        } else {
            if(__builtin_expect(nbits_ < val, 0)) gen_ = rng_(), nbits_ = 64;
            auto oldval = val;
            if((gen_ & (UINT64_C(-1) >> (64 - val))) == 0) {
                ++val;
                if(range_check(nbits, val) == 0)
                    for(const auto el: ref)
                        con[el] = val;
            }
            gen_ >>= oldval;
            nbits_ -= oldval;
        }
    }
    template<typename T1, typename T2>
{'fraction_non_alphanumeric': 0.13869188337273444, 'fraction_numerical': 0.017336485421591805, 'mean_word_length': 2.0274299344066784, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 50, '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': '3707247', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1649, 'n_words': 415}