Punchcard

BiMap

Mar 25th, 2024
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | Source Code | 0 0
  1. #include "bimap.h"
  2.  
  3. struct BiMap::Impl {   
  4.  
  5.     Impl() {};    
  6.     Impl(const Impl& other) = default;
  7.     Impl& operator=(const Impl& other) = default;
  8.    
  9.     bool Add(std::string_view key, std::string_view value) {
  10.         // TODO: Реализуйте метод самостоятельно
  11.         if (k2v_.find(key) != k2v_.end() || v2k_.find(value) != v2k_.end()) {
  12.             return false;
  13.         }        
  14.         try {
  15.             dictionary_.push_back(key);
  16.             v2k_[value] = dictionary_.back();      
  17.            
  18.             dictionary_.push_back(value);
  19.             k2v_[key] = dictionary_.back();
  20.         } catch (std::bad_alloc& ba) {
  21.             throw new std::bad_alloc;
  22.         }
  23.         return true;
  24.     }
  25.  
  26.     std::optional<std::string_view> FindValue(std::string_view key) const noexcept {
  27.         // TODO: Реализуйте метод самостоятельно
  28.         try {
  29.             return k2v_.at(key);
  30.         }
  31.         catch (std::out_of_range& oor) {
  32.             return std::nullopt;
  33.         }
  34.     }
  35.  
  36.     std::optional<std::string_view> FindKey(std::string_view value) const noexcept {
  37.         // TODO: Реализуйте метод самостоятельно
  38.         try {
  39.             return v2k_.at(value);
  40.         }      
  41.         catch (std::out_of_range& oor) {
  42.             return std::nullopt;
  43.         }
  44.     }
  45. private:
  46.     std::deque<std::string_view> dictionary_;
  47.     std::unordered_map<std::string_view, std::string_view> k2v_;
  48.     std::unordered_map<std::string_view, std::string_view> v2k_;
  49. };
  50.  
  51. BiMap::BiMap()
  52.     : impl_(std::make_unique<Impl>()) {
  53. }
  54.  
  55. BiMap::~BiMap() = default;
  56.  
  57. BiMap::BiMap(BiMap&&) noexcept = default;
  58.  
  59. BiMap& BiMap::operator=(BiMap&&) noexcept = default;
  60.  
  61. BiMap::BiMap(const BiMap& other)
  62.     : impl_(other.impl_ ? std::make_unique<Impl>(*other.impl_) : nullptr) {
  63. }
  64.  
  65. BiMap& BiMap::operator=(const BiMap& other) {
  66.     if (this != std::addressof(other)) {
  67.         impl_ = std::make_unique<Impl>(*other.impl_);
  68.         if (!other.impl_) {     // Правый аргумент пуст?
  69.             impl_.reset();
  70.         } else if (impl_) {     // Левый и правый аргументы не пустые?
  71.             assert(other.impl_);
  72.             *impl_ = *other.impl_;
  73.         } else {                // Левый аргумент пуст, а правый не пуст
  74.             assert(!impl_ && other.impl_);            
  75.         }
  76.     }
  77.     return *this;
  78. }
  79.  
  80. bool BiMap::Add(std::string_view key, std::string_view value) {
  81.     return impl_->Add(key, value);
  82. }
  83.  
  84. std::optional<std::string_view> BiMap::FindValue(std::string_view key) const noexcept {
  85.     return impl_->FindValue(key);
  86. }
  87.  
  88. std::optional<std::string_view> BiMap::FindKey(std::string_view value) const noexcept {
  89.     return impl_->FindKey(value);
  90. }
Advertisement
Add Comment
Please, Sign In to add comment