Advertisement
radmickey

Untitled

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