<commit_before>#include <cassert>
#include <fstream>
#include <stdexcept>
#include <iterator>
#include "reader.h"

using namespace std;

template<class T, class CharT = char, class Traits = char_traits<CharT>,
         class Distance = ptrdiff_t >
struct theresnoescapefromthisiterator
    : public iterator<input_iterator_tag, T, Distance, const T*, const T&>
{
public:
    theresnoescapefromthisiterator() = default;

    theresnoescapefromthisiterator(basic_istream<CharT, Traits> &stream) :
        stream(&stream)
    {
        stream.seekg(0, ios_base::end);
        file_size = stream.tellg();
        if (file_size)
            fill();
        else
            this->stream = NULL;
    }

    theresnoescapefromthisiterator &operator++()
    {
        assert(stream);
        pos += 1;
        if (pos == file_size)
            stream = NULL;
        else
            fill();
        return *this;
    }

    bool operator==(const theresnoescapefromthisiterator &rhs)
    {
        return (stream && rhs.stream) ? (pos == rhs.pos)
            : (stream == rhs.stream);
    }

    bool operator!=(const theresnoescapefromthisiterator &rhs)
    {
        return !(*this == rhs);
    }

    T operator*() const
    {
        return current.value;
    }

private:
    void fill()
    {
        stream->seekg(pos);
        for (unsigned i = 0;i != sizeof(T);++i) {
            char c;
            stream->read(&c, 1);
            current.pieces[i] = c;
        }
    }

    basic_istream<CharT, Traits> *stream = NULL;
    typename basic_istream<CharT, Traits>::pos_type pos = 0;
    typename basic_istream<CharT, Traits>::pos_type file_size;
    mutable union
    {
        T value;
        uint8_t pieces[sizeof(T)];
    } current;
};

class Reader {
public:
    Reader(istream &stream);
    theresnoescapefromthisiterator<uint8_t> begin();
    theresnoescapefromthisiterator<uint8_t> end();
private:
    theresnoescapefromthisiterator<uint8_t> begin_;
};

Reader::Reader(istream &stream)
    : begin_(stream) {
}

theresnoescapefromthisiterator<uint8_t> Reader::begin() {
    return begin_;
}

theresnoescapefromthisiterator<uint8_t> Reader::end() {
    return {};
}

map<uint8_t, unsigned int> read(string file) {
    ifstream stream(file);
    if (stream.rdstate() & ios_base::failbit)
        throw runtime_error("File do not exist");

    map<uint8_t, unsigned int> map;

    for (auto character : Reader(stream)) {
        ++map[character];
    }

    stream.close();
    return map;
}<commit_msg>Rename theresnoescapefromthisiterator to readeriterator for readability proporse<commit_after>#include <cassert>
#include <fstream>
#include <stdexcept>
#include <iterator>
#include "reader.h"

using namespace std;

template<class T, class CharT = char, class Traits = char_traits<CharT>,
         class Distance = ptrdiff_t >
struct readeriterator
    : public iterator<input_iterator_tag, T, Distance, const T*, const T&>
{
public:
    readeriterator() = default;

    readeriterator(basic_istream<CharT, Traits> &stream) :
        stream(&stream)
    {
        stream.seekg(0, ios_base::end);
        file_size = stream.tellg();
        if (file_size)
            fill();
        else
            this->stream = NULL;
    }

    readeriterator &operator++()
    {
        assert(stream);
        pos += 1;
        if (pos == file_size)
            stream = NULL;
        else
            fill();
        return *this;
    }

    bool operator==(const readeriterator &rhs)
    {
        return (stream && rhs.stream) ? (pos == rhs.pos)
            : (stream == rhs.stream);
    }

    bool operator!=(const readeriterator &rhs)
    {
        return !(*this == rhs);
    }

    T operator*() const
    {
        return current.value;
    }

private:
    void fill()
    {
        stream->seekg(pos);
        for (unsigned i = 0;i != sizeof(T);++i) {
            char c;
            stream->read(&c, 1);
            current.pieces[i] = c;
        }
    }

    basic_istream<CharT, Traits> *stream = NULL;
    typename basic_istream<CharT, Traits>::pos_type pos = 0;
    typename basic_istream<CharT, Traits>::pos_type file_size;
    mutable union
    {
        T value;
        uint8_t pieces[sizeof(T)];
    } current;
};

class Reader {
public:
    Reader(istream &stream);
    readeriterator<uint8_t> begin();
    readeriterator<uint8_t> end();
private:
    readeriterator<uint8_t> begin_;
};

Reader::Reader(istream &stream)
    : begin_(stream) {
}

readeriterator<uint8_t> Reader::begin() {
    return begin_;
}

readeriterator<uint8_t> Reader::end() {
    return {};
}

map<uint8_t, unsigned int> read(string file) {
    ifstream stream(file);
    if (stream.rdstate() & ios_base::failbit)
        throw runtime_error("File do not exist");

    map<uint8_t, unsigned int> map;

    for (auto character : Reader(stream)) {
        ++map[character];
    }

    stream.close();
    return map;
}
{'fraction_non_alphanumeric': 0.12635011208477684, 'fraction_numerical': 0.005298553087426126, 'mean_word_length': 2.574654042243263, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 45, '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': '3475852', 'n_tokens_mistral': 1637, 'n_tokens_neox': 1563, 'n_words': 350}