Advertisement
radmickey

Untitled

Apr 8th, 2023 (edited)
1,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.99 KB | None | 0 0
  1. /* Я реализовал класс контейнера, реализующий интерфейс dictionary и способный выбросить исключение,
  2. реализующее интерфейс not_found_exception.
  3. Мой контейнер соответствует всем требованиям контейнера ( https://en.cppreference.com/w/cpp/named_req/Container ).
  4. А также я реализовал пару дополнительных методов для удобного использования контейнера в разработке.
  5.  
  6. Код в более удобном формате  https://pastebin.com/wY0JgSNp
  7. */
  8.  
  9. #include <unordered_map>
  10. #include <sstream>
  11.  
  12. template<class Key, class Value>
  13. class dictionary
  14. {
  15. public:
  16.     virtual ~dictionary() = default;
  17.     virtual const Value& get(const Key& key) const = 0;
  18.     virtual void set(const Key& key, const Value& value) = 0;
  19.     virtual bool is_set(const Key& key) const = 0;
  20. };
  21.  
  22. template<class Key>
  23. class not_found_exception : public std::exception
  24. {
  25. public:
  26.     virtual const Key& get_key() const noexcept = 0;
  27. };
  28.  
  29. template<class Key>
  30. class key_not_found_exception : public not_found_exception<Key>, public std::runtime_error
  31. {
  32. private:
  33.     std::string to_str(const Key& key) {
  34.         std::stringstream ss;
  35.         ss << key;
  36.         return ss.str();
  37.     }
  38. public:
  39.     explicit key_not_found_exception(const Key& key) :
  40.             std::runtime_error("Key " + to_str(key) + " not found"),
  41.             m_key(key)
  42.     {
  43.     }
  44.  
  45.     [[nodiscard]] const Key& get_key() const noexcept override
  46.     {
  47.         return m_key;
  48.     }
  49.  
  50.     [[nodiscard]] const char* what() const noexcept override {
  51.         return std::runtime_error::what();
  52.     }
  53. private:
  54.     Key m_key;
  55. };
  56.  
  57. template<class Key, class Value>
  58. class custom_dictionary : public dictionary<Key, Value>
  59. {
  60. public:
  61.     using iterator = typename std::unordered_map<Key, Value>::iterator;
  62.     using const_iterator = typename std::unordered_map<Key, Value>::const_iterator;
  63.     using value_type = typename std::pair<const Key, Value>;
  64.     using pointer = typename std::unordered_map<Key, Value>::pointer;
  65.     using reference = typename std::unordered_map<Key, Value>::reference;
  66.     using const_reference = typename std::unordered_map<Key, Value>::const_reference;
  67.     using size_type = size_t;
  68.  
  69.     // Base methods
  70.     ~custom_dictionary() override = default;
  71.  
  72.     custom_dictionary() noexcept
  73.         : storage_{}
  74.     {
  75.     }
  76.  
  77.     custom_dictionary(custom_dictionary&& other) noexcept
  78.             : storage_(std::move(other.storage_))
  79.     {
  80.     }
  81.  
  82.     custom_dictionary(std::initializer_list<value_type> init_list) noexcept
  83.             : storage_{init_list}
  84.     {
  85.     }
  86.  
  87.     custom_dictionary(const custom_dictionary& other)
  88.             : storage_{other.storage_}
  89.     {
  90.     }
  91.  
  92.     custom_dictionary& operator=(custom_dictionary&& other) noexcept
  93.     {
  94.         storage_ = std::move(other.storage_);
  95.         return *this;
  96.     }
  97.  
  98.     custom_dictionary& operator=(const custom_dictionary& other) noexcept
  99.     {
  100.         if (this == &other) {
  101.             return *this;
  102.         }
  103.         storage_ = other.storage_;
  104.         return *this;
  105.     }
  106.  
  107.     custom_dictionary& operator=(std::initializer_list<value_type> list) noexcept
  108.     {
  109.         storage_.clear();
  110.         storage_.insert(list.begin(), list.end());
  111.         return *this;
  112.     }
  113.  
  114.     // Getter's methods
  115.     [[nodiscard]] const Value& get(const Key& key) const override
  116.     {
  117.         auto it = storage_.find(key);
  118.         if (it == storage_.end())
  119.         {
  120.             throw key_not_found_exception<Key>(key);
  121.         }
  122.         return it->second;
  123.     }
  124.  
  125.     [[nodiscard]] bool is_set(const Key& key) const override
  126.     {
  127.         return storage_.find(key) != storage_.end();
  128.     }
  129.  
  130.     [[nodiscard]] size_type size() const noexcept
  131.     {
  132.         return storage_.size();
  133.     }
  134.  
  135.     [[nodiscard]] bool empty() const noexcept
  136.     {
  137.         return storage_.empty();
  138.     }
  139.  
  140.     [[nodiscard]] size_type max_size() const
  141.     {
  142.         return storage_.max_size();
  143.     }
  144.  
  145.     // Setters / the methods for changing container data
  146.     void set(const Key& key, const Value& value) override
  147.     {
  148.         storage_[key] = value;
  149.     }
  150.  
  151.     template<typename... Args>
  152.     std::pair<iterator, bool> emplace(Args&&... args)
  153.     {
  154.         return storage_.emplace(std::forward<Args>(args)...);
  155.     }
  156.  
  157.     void erase(const Key& key) noexcept
  158.     {
  159.         storage_.erase(key);
  160.     }
  161.  
  162.     void reserve(size_type count)
  163.     {
  164.         storage_.reserve(count);
  165.     }
  166.  
  167.     void clear() noexcept
  168.     {
  169.         storage_.clear();
  170.     }
  171.  
  172.     void swap(custom_dictionary& other) noexcept
  173.     {
  174.         std::swap(storage_, other.storage_);
  175.     }
  176.  
  177.     // Operator's methods
  178.     bool operator==(const custom_dictionary& other) {
  179.         if (this == &other) {
  180.             return true;
  181.         }
  182.  
  183.         if (size() != other.size()) {
  184.             return false;
  185.         }
  186.  
  187.         return storage_ == other.storage_;
  188.     }
  189.  
  190.     bool operator!=(const custom_dictionary& other) {
  191.         return !(*this == other);
  192.     }
  193.  
  194.     // Iterator's methods
  195.     [[nodiscard]] iterator begin() noexcept
  196.     {
  197.         return storage_.begin();
  198.     }
  199.  
  200.     [[nodiscard]] const_iterator begin() const noexcept
  201.     {
  202.         return storage_.cbegin();
  203.     }
  204.  
  205.     [[nodiscard]] const_iterator cbegin() const noexcept
  206.     {
  207.         return storage_.cbegin();
  208.     }
  209.  
  210.     [[nodiscard]] iterator end() noexcept
  211.     {
  212.         return storage_.end();
  213.     }
  214.  
  215.     [[nodiscard]] const_iterator end() const noexcept
  216.     {
  217.         return storage_.cend();
  218.     }
  219.  
  220.     [[nodiscard]] const_iterator cend() const noexcept
  221.     {
  222.         return storage_.cend();
  223.     }
  224.  
  225. private:
  226.     std::unordered_map<Key, Value> storage_;
  227. };
  228.  
  229. template<class Key, class Value>
  230. void swap(custom_dictionary<Key, Value>& lhs, custom_dictionary<Key, Value>& rhs) {
  231.     lhs.swap(rhs);
  232. }
  233.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement