<commit_before>/*
  Copyright (C) 2018 <NAME> <<EMAIL>>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published
  by the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef PROGRESSBAR_H
#define PROGRESSBAR_H

#include <unistd.h>
#include <atomic>
#include <chrono>
#include <cmath>
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <mutex>
#include <string>
#include <vector>

#ifdef _WINDOWS
#include <windows.h>
#else
#include <sys/ioctl.h>
#endif

namespace progressbar {

class ProgressBar {
  protected:
    using ticks = std::chrono::steady_clock::rep;
    std::vector<char> buf;
    bool is_tty;
    bool closed = false;
    bool subbar;
    std::mutex mutex_m;
    std::atomic<std::size_t> current;
    std::size_t reprint_next = 1;
    std::chrono::time_point<std::chrono::steady_clock> start_time;
    std::size_t eta_from_iter = 0;
    std::chrono::time_point<std::chrono::steady_clock> eta_from_time;
    std::size_t last_reprint_iter = 0;
    std::chrono::time_point<std::chrono::steady_clock> last_reprint_time;
    const ticks min_reprint_time;
    std::FILE* out;

    void update(std::size_t n) noexcept {
#ifndef PROGRESSBAR_SILENT
        current = std::min(current + n, total);
        if (current >= reprint_next) {
            std::lock_guard<std::mutex> guard(mutex_m);
            if (!closed) {
                recalc_and_print();
            }
        }
#endif
    }

    void recalc_and_print(bool force = false) noexcept {
        auto now = std::chrono::steady_clock::now();
        auto duration = (now - last_reprint_time).count();
        reprint_next = current + (current - last_reprint_iter) * min_reprint_time / std::max(duration, min_reprint_time) + 1;
        if (duration >= min_reprint_time || force) {
            auto freq = std::chrono::steady_clock::period::den
                        * ((1 - smoothing) * (current - last_reprint_iter) / static_cast<float>(duration)
                           + smoothing * (current - eta_from_iter) / static_cast<float>((now - eta_from_time).count()))
                        / std::chrono::steady_clock::period::num;
            auto etr = std::lrint((total - current)
                                  * ((1 - smoothing) * duration / static_cast<float>((current - last_reprint_iter))
                                     + smoothing * (now - eta_from_time).count() / static_cast<float>(current - eta_from_iter)));

            print_bar(freq, (now - start_time).count(), etr, true);

            last_reprint_time = now;
            last_reprint_iter = current;
        }
    }

    template<typename... Args>
    static constexpr bool safe_print(char*& c, std::size_t& buf_remaining, const char* fmt, Args... args) noexcept {
        const auto res = std::snprintf(c, buf_remaining, fmt, args...);  // NOLINT(hicpp-vararg,cppcoreguidelines-pro-type-vararg)
        if (res < 0 || buf_remaining <= static_cast<std::size_t>(res)) {
            return false;
        }
        buf_remaining -= res;
        c += res;
        return true;
    }

    static inline bool safe_print(char*& c, std::size_t& buf_remaining, const char* str) noexcept {
        const auto res = std::snprintf(c, buf_remaining, "%s", str);  // NOLINT(hicpp-vararg,cppcoreguidelines-pro-type-vararg)
        if (res < 0 || buf_remaining <= static_cast<std::size_t>(res)) {
            return false;
        }
        buf_remaining -= res;
        c += res;
        return true;
    }

    inline static bool safe_print_duration(char*& c, std::size_t& buf_remaining, ticks t) noexcept {
        auto dur = std::chrono::steady_clock::duration(t);
        auto d = std::chrono::duration_cast<std::chrono::duration<ticks, std::ratio<3600 * 24>>>(dur);
        auto h = std::chrono::duration_cast<std::chrono::hours>(dur -= d);
        auto m = std::chrono::duration_cast<std::chrono::minutes>(dur -= h);
        auto s = std::chrono::duration_cast<std::chrono::seconds>(dur -= m);
        if (d.count() > 0) {<commit_msg>Squashed 'lib/cpp-library/' changes from e3f1ba8..e3add8e<commit_after>/*
  Copyright (C) 2018 <NAME> <<EMAIL>>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published
  by the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef PROGRESSBAR_H
#define PROGRESSBAR_H

#include <unistd.h>
#include <atomic>
#include <chrono>
#include <cmath>
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <mutex>
#include <string>
#include <vector>

#ifdef _WINDOWS
#include <windows.h>
#else
#include <sys/ioctl.h>
#endif

namespace progressbar {

class ProgressBar {
  protected:
    using ticks = std::chrono::steady_clock::rep;
    std::vector<char> buf;
    bool is_tty;
    bool closed = false;
    bool subbar;
    std::mutex mutex_m;
    std::atomic<std::size_t> current;
    std::size_t reprint_next = 1;
    std::chrono::time_point<std::chrono::steady_clock> start_time;
    std::size_t eta_from_iter = 0;
    std::chrono::time_point<std::chrono::steady_clock> eta_from_time;
    std::size_t last_reprint_iter = 0;
    std::chrono::time_point<std::chrono::steady_clock> last_reprint_time;
    const ticks min_reprint_time;
    std::FILE* out;

    void update(std::size_t n) noexcept {
#ifndef PROGRESSBAR_SILENT
        current = std::min(current + n, total);
        if (current >= reprint_next) {
            std::lock_guard<std::mutex> guard(mutex_m);
            if (!closed) {
                recalc_and_print();
            }
        }
#endif
    }

    void recalc_and_print(bool force = false) noexcept {
        auto now = std::chrono::steady_clock::now();
        auto duration = (now - last_reprint_time).count();
        reprint_next = current + (current - last_reprint_iter) * min_reprint_time / std::max(duration, min_reprint_time) + 1;
        if (duration >= min_reprint_time || force) {
            auto freq = std::chrono::steady_clock::period::den
                        * ((1 - smoothing) * (current - last_reprint_iter) / static_cast<float>(duration)
                           + smoothing * (current - eta_from_iter) / static_cast<float>((now - eta_from_time).count()))
                        / std::chrono::steady_clock::period::num;
            auto etr = std::lrint((total - current)
                                  * ((1 - smoothing) * duration / static_cast<float>((current - last_reprint_iter))
                                     + smoothing * (now - eta_from_time).count() / static_cast<float>(current - eta_from_iter)));

            print_bar(freq, (now - start_time).count(), etr, true);

            last_reprint_time = now;
            last_reprint_iter = current;
        }
    }

    template<typename... Args>
    static inline bool safe_print(char*& c, std::size_t& buf_remaining, const char* fmt, Args... args) noexcept {
        const auto res = std::snprintf(c, buf_remaining, fmt, args...);  // NOLINT(hicpp-vararg,cppcoreguidelines-pro-type-vararg)
        if (res < 0 || buf_remaining <= static_cast<std::size_t>(res)) {
            return false;
        }
        buf_remaining -= res;
        c += res;
        return true;
    }

    static inline bool safe_print(char*& c, std::size_t& buf_remaining, const char* str) noexcept {
        const auto res = std::snprintf(c, buf_remaining, "%s", str);  // NOLINT(hicpp-vararg,cppcoreguidelines-pro-type-vararg)
        if (res < 0 || buf_remaining <= static_cast<std::size_t>(res)) {
            return false;
        }
        buf_remaining -= res;
        c += res;
        return true;
    }

    inline static bool safe_print_duration(char*& c, std::size_t& buf_remaining, ticks t) noexcept {
        auto dur = std::chrono::steady_clock::duration(t);
        auto d = std::chrono::duration_cast<std::chrono::duration<ticks, std::ratio<3600 * 24>>>(dur);
        auto h = std::chrono::duration_cast<std::chrono::hours>(dur -= d);
        auto m = std::chrono::duration_cast<std::chrono::minutes>(dur -= h);
        auto s = std::chrono::duration_cast<std::chrono::seconds>(dur -= m);
        if (d.count() > 0) {
{'fraction_non_alphanumeric': 0.12569985728400482, 'fraction_numerical': 0.004940169063563509, 'mean_word_length': 2.9267241379310347, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '451644', 'n_tokens_mistral': 2991, 'n_tokens_neox': 2894, 'n_words': 728}