aboutsummaryrefslogtreecommitdiff
path: root/Занимательное программирование/5/3_context/context.cpp
blob: f65a205e9d7c1c69b00505bf5b9e5fd49c7e1852 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "utf8.h"
#include "context.hpp"

context_occurrence::context_occurrence(utf8::utfchar32_t character)
{
    this->counts.insert({ character, 1 });
}

void context_occurrence::add(utf8::utfchar32_t character)
{
    ++this->count;

    auto lookup_result = this->counts.find(character);
    if (lookup_result == this->counts.end())
    {
        this->counts.insert({ character, 1 });
    }
    else
    {
        lookup_result->second = lookup_result->second + 1;
    }
}

std::unordered_map<utf8::utfchar32_t, unsigned int>::iterator context_occurrence::begin()
{
    return this->counts.begin();
}

std::unordered_map<utf8::utfchar32_t, unsigned int>::iterator context_occurrence::end()
{
    return this->counts.end();
}

void context_model::add(utf8::utfchar32_t character)
{
    if (this->current_context.size() >= 2)
    {
        std::string context_key;

        for (const auto context_character: this->current_context)
        {
            utf8::append(context_character, context_key);
        }
        auto lookup_result = this->occurrences.find(context_key);

        if (lookup_result == this->occurrences.end())
        {
            this->occurrences.insert({ context_key, context_occurrence(character) });
        }
        else
        {
            lookup_result->second.add(character);
        }
        this->current_context.pop_front();
    }
    this->current_context.push_back(character);
}

std::unordered_map<std::string, context_occurrence>::iterator context_model::begin()
{
    return this->occurrences.begin();
}

std::unordered_map<std::string, context_occurrence>::iterator context_model::end()
{
    return this->occurrences.end();
}