From d7e6fa7e9ecc5cf5ec32a75a0e4c745e47f58494 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Tue, 25 May 2021 14:34:01 +0000 Subject: [PATCH 1/3] Setting up GitHub Classroom Feedback From 6b6a234c12e5d30b997d07f94c4ed72a9855eac8 Mon Sep 17 00:00:00 2001 From: Angielnani <80117994+Angielnani@users.noreply.github.com> Date: Tue, 25 May 2021 17:36:26 +0300 Subject: [PATCH 2/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 190b1c3..814c7bb 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ## Имя студента -*Пожалуйста, добавьте вместо курсивного текста своё ФИО.* +Суржиков Ярослав Сергеевич ## Описание задания From f7e53290ac8711ae2bde9e924a9e51f70d5b416c Mon Sep 17 00:00:00 2001 From: Angielnani Date: Fri, 28 May 2021 19:42:27 +0300 Subject: [PATCH 3/3] All done --- src/hash_table.cpp | 48 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/src/hash_table.cpp b/src/hash_table.cpp index a581cb3..73f8c0c 100644 --- a/src/hash_table.cpp +++ b/src/hash_table.cpp @@ -17,30 +17,60 @@ namespace itis { throw std::logic_error("hash table load factor must be in range [0...1]"); } - // Tip: allocate hash-table buckets + for (int i = 0; i < capacity; i++){ + buckets_.emplace_back(Bucket()); + } } std::optional HashTable::Search(int key) const { - // Tip: compute hash code (index) and use linear search + int index = hash(key); + for (const auto& bucket : buckets_[index]){ + if (bucket.first == key){ + return bucket.second; + } + } return std::nullopt; } void HashTable::Put(int key, const std::string &value) { - // Tip 1: compute hash code (index) to determine which bucket to use - // Tip 2: consider the case when the key exists (read the docs in the header file) + int index = hash(key); + for (auto &bucket : buckets_[index]){ + if (bucket.first == key) { + bucket.second = value; + return; + } + } + buckets_[index].push_back(std::pair(key, value)); + num_keys_ += 1; if (static_cast(num_keys_) / buckets_.size() >= load_factor_) { - // Tip 3: recompute hash codes (indices) for key-value pairs (create a new hash-table) - // Tip 4: use utils::hash(key, size) to compute new indices for key-value pairs + auto new_size = buckets_.size() * kGrowthCoefficient; + std::vector temp_buckets; + temp_buckets.resize(new_size); + for (auto &bucket: buckets_){ + for (std::pair &pair: bucket){ + int i = utils::hash(pair.first, new_size); + temp_buckets[i].push_back(pair); + } + } + buckets_ =temp_buckets; } } std::optional HashTable::Remove(int key) { - // Tip 1: compute hash code (index) to determine which bucket to use - // TIp 2: find the key-value pair to remove and make a copy of value to return - return std::nullopt; + int index = hash(key); + std::optional nullopt; + for (auto &bucket : buckets_[index]){ + if (bucket.first == key){ + nullopt = bucket.second; + buckets_[index].remove(bucket); + break; + } + } + return nullopt; } + bool HashTable::ContainsKey(int key) const { // Note: uses Search(key) which is not initially implemented return Search(key).has_value();