From 1768f21de7b75c98c932fcd734218d348b1b0060 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Fri, 27 Feb 2026 17:49:09 +0100 Subject: =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0?= =?UTF-8?q?=20=D1=82=D1=80=D0=B5=D1=82=D1=8C=D1=8F=20=D0=B7=D0=B0=D0=B4?= =?UTF-8?q?=D0=B0=D1=87=D0=B0=20=D0=BF=D1=8F=D1=82=D0=BE=D0=B9=20=D0=B3?= =?UTF-8?q?=D0=BB=D0=B0=D0=B2=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../5/3_context/context.cpp" | 67 ++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 "\320\227\320\260\320\275\320\270\320\274\320\260\321\202\320\265\320\273\321\214\320\275\320\276\320\265 \320\277\321\200\320\276\320\263\321\200\320\260\320\274\320\274\320\270\321\200\320\276\320\262\320\260\320\275\320\270\320\265/5/3_context/context.cpp" (limited to 'Занимательное программирование/5/3_context/context.cpp') diff --git "a/\320\227\320\260\320\275\320\270\320\274\320\260\321\202\320\265\320\273\321\214\320\275\320\276\320\265 \320\277\321\200\320\276\320\263\321\200\320\260\320\274\320\274\320\270\321\200\320\276\320\262\320\260\320\275\320\270\320\265/5/3_context/context.cpp" "b/\320\227\320\260\320\275\320\270\320\274\320\260\321\202\320\265\320\273\321\214\320\275\320\276\320\265 \320\277\321\200\320\276\320\263\321\200\320\260\320\274\320\274\320\270\321\200\320\276\320\262\320\260\320\275\320\270\320\265/5/3_context/context.cpp" new file mode 100644 index 0000000..f65a205 --- /dev/null +++ "b/\320\227\320\260\320\275\320\270\320\274\320\260\321\202\320\265\320\273\321\214\320\275\320\276\320\265 \320\277\321\200\320\276\320\263\321\200\320\260\320\274\320\274\320\270\321\200\320\276\320\262\320\260\320\275\320\270\320\265/5/3_context/context.cpp" @@ -0,0 +1,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::iterator context_occurrence::begin() +{ + return this->counts.begin(); +} + +std::unordered_map::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::iterator context_model::begin() +{ + return this->occurrences.begin(); +} + +std::unordered_map::iterator context_model::end() +{ + return this->occurrences.end(); +} -- cgit v1.2.3