Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Я реализовал класс контейнера, реализующий интерфейс dictionary и способный выбросить исключение,
- реализующее интерфейс not_found_exception.
- Мой контейнер соответствует всем требованиям контейнера ( https://en.cppreference.com/w/cpp/named_req/Container ).
- А также я реализовал пару дополнительных методов для удобного использования контейнера в разработке.
- Код в более удобном формате https://pastebin.com/wY0JgSNp
- */
- #include <unordered_map>
- #include <sstream>
- template<class Key, class Value>
- class dictionary
- {
- public:
- virtual ~dictionary() = default;
- virtual const Value& get(const Key& key) const = 0;
- virtual void set(const Key& key, const Value& value) = 0;
- virtual bool is_set(const Key& key) const = 0;
- };
- template<class Key>
- class not_found_exception : public std::exception
- {
- public:
- virtual const Key& get_key() const noexcept = 0;
- };
- template<class Key>
- class key_not_found_exception : public not_found_exception<Key>, public std::runtime_error
- {
- private:
- std::string to_str(const Key& key) {
- std::stringstream ss;
- ss << key;
- return ss.str();
- }
- public:
- explicit key_not_found_exception(const Key& key) :
- std::runtime_error("Key " + to_str(key) + " not found"),
- m_key(key)
- {
- }
- [[nodiscard]] const Key& get_key() const noexcept override
- {
- return m_key;
- }
- [[nodiscard]] const char* what() const noexcept override {
- return std::runtime_error::what();
- }
- private:
- Key m_key;
- };
- template<class Key, class Value>
- class custom_dictionary : public dictionary<Key, Value>
- {
- public:
- using iterator = typename std::unordered_map<Key, Value>::iterator;
- using const_iterator = typename std::unordered_map<Key, Value>::const_iterator;
- using value_type = typename std::pair<const Key, Value>;
- using pointer = typename std::unordered_map<Key, Value>::pointer;
- using reference = typename std::unordered_map<Key, Value>::reference;
- using const_reference = typename std::unordered_map<Key, Value>::const_reference;
- using size_type = size_t;
- // Base methods
- ~custom_dictionary() override = default;
- custom_dictionary() noexcept
- : storage_{}
- {
- }
- custom_dictionary(custom_dictionary&& other) noexcept
- : storage_(std::move(other.storage_))
- {
- }
- custom_dictionary(std::initializer_list<value_type> init_list) noexcept
- : storage_{init_list}
- {
- }
- custom_dictionary(const custom_dictionary& other)
- : storage_{other.storage_}
- {
- }
- custom_dictionary& operator=(custom_dictionary&& other) noexcept
- {
- storage_ = std::move(other.storage_);
- return *this;
- }
- custom_dictionary& operator=(const custom_dictionary& other) noexcept
- {
- if (this == &other) {
- return *this;
- }
- storage_ = other.storage_;
- return *this;
- }
- custom_dictionary& operator=(std::initializer_list<value_type> list) noexcept
- {
- storage_.clear();
- storage_.insert(list.begin(), list.end());
- return *this;
- }
- // Getter's methods
- [[nodiscard]] const Value& get(const Key& key) const override
- {
- auto it = storage_.find(key);
- if (it == storage_.end())
- {
- throw key_not_found_exception<Key>(key);
- }
- return it->second;
- }
- [[nodiscard]] bool is_set(const Key& key) const override
- {
- return storage_.find(key) != storage_.end();
- }
- [[nodiscard]] size_type size() const noexcept
- {
- return storage_.size();
- }
- [[nodiscard]] bool empty() const noexcept
- {
- return storage_.empty();
- }
- [[nodiscard]] size_type max_size() const
- {
- return storage_.max_size();
- }
- // Setters / the methods for changing container data
- void set(const Key& key, const Value& value) override
- {
- storage_[key] = value;
- }
- template<typename... Args>
- std::pair<iterator, bool> emplace(Args&&... args)
- {
- return storage_.emplace(std::forward<Args>(args)...);
- }
- void erase(const Key& key) noexcept
- {
- storage_.erase(key);
- }
- void reserve(size_type count)
- {
- storage_.reserve(count);
- }
- void clear() noexcept
- {
- storage_.clear();
- }
- void swap(custom_dictionary& other) noexcept
- {
- std::swap(storage_, other.storage_);
- }
- // Operator's methods
- bool operator==(const custom_dictionary& other) {
- if (this == &other) {
- return true;
- }
- if (size() != other.size()) {
- return false;
- }
- return storage_ == other.storage_;
- }
- bool operator!=(const custom_dictionary& other) {
- return !(*this == other);
- }
- // Iterator's methods
- [[nodiscard]] iterator begin() noexcept
- {
- return storage_.begin();
- }
- [[nodiscard]] const_iterator begin() const noexcept
- {
- return storage_.cbegin();
- }
- [[nodiscard]] const_iterator cbegin() const noexcept
- {
- return storage_.cbegin();
- }
- [[nodiscard]] iterator end() noexcept
- {
- return storage_.end();
- }
- [[nodiscard]] const_iterator end() const noexcept
- {
- return storage_.cend();
- }
- [[nodiscard]] const_iterator cend() const noexcept
- {
- return storage_.cend();
- }
- private:
- std::unordered_map<Key, Value> storage_;
- };
- template<class Key, class Value>
- void swap(custom_dictionary<Key, Value>& lhs, custom_dictionary<Key, Value>& rhs) {
- lhs.swap(rhs);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement