aboutsummaryrefslogtreecommitdiff
path: root/Занимательное программирование/5/3_context/context.cpp
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2026-02-27 17:49:09 +0100
committerEugen Wissner <belka@caraus.de>2026-02-27 17:49:09 +0100
commit1768f21de7b75c98c932fcd734218d348b1b0060 (patch)
treee517cc9b792f376b211bbd37a496269913d833d5 /Занимательное программирование/5/3_context/context.cpp
parent82ae29cb8a59a1e093190d14f8c5097658545a12 (diff)
downloadbook-exercises-1768f21de7b75c98c932fcd734218d348b1b0060.tar.gz
Добавлена третья задача пятой главы
Diffstat (limited to 'Занимательное программирование/5/3_context/context.cpp')
-rw-r--r--Занимательное программирование/5/3_context/context.cpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/Занимательное программирование/5/3_context/context.cpp b/Занимательное программирование/5/3_context/context.cpp
new file mode 100644
index 0000000..f65a205
--- /dev/null
+++ b/Занимательное программирование/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<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();
+}