Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef MY_OWN_HASH_TABLE_HASH_MAP_H
- #define MY_OWN_HASH_TABLE_HASH_MAP_H
- #include <unordered_map>
- #include <list>
- #include <iterator>
- #include <utility>
- template<class KeyType, class ValueType, class Hash = std::hash<KeyType> >
- class HashMap {
- public:
- class iterator {
- public:
- iterator() = default;
- explicit iterator(typename std::unordered_map<KeyType, ValueType, Hash>::iterator it) : it_(it) {}
- iterator& operator++() {
- ++it_;
- return *this;
- }
- iterator operator++(int) {
- iterator tmp = *this;
- ++it_;
- return tmp;
- }
- std::pair<const KeyType, ValueType>& operator*() {
- return *it_;
- }
- std::pair<const KeyType, ValueType>* operator->() {
- return &*it_;
- }
- bool operator==(const iterator& other) {
- return other.it_ == it_;
- }
- bool operator!=(const iterator& other) {
- return !(*this == other);
- }
- private:
- typename std::unordered_map<KeyType, ValueType, Hash>::iterator it_;
- };
- class const_iterator {
- public:
- const_iterator() = default;
- explicit const_iterator(typename std::unordered_map<KeyType, ValueType, Hash>::const_iterator it) : it_(it) {}
- const_iterator& operator++() {
- ++it_;
- return *this;
- }
- const_iterator operator++(int) {
- const_iterator tmp = *this;
- ++it_;
- return tmp;
- }
- const std::pair<const KeyType, ValueType>& operator*() {
- return *it_;
- }
- const std::pair<const KeyType, ValueType>* operator->() {
- return &*it_;
- }
- bool operator==(const const_iterator& other) {
- return other.it_ == it_;
- }
- bool operator!=(const const_iterator& other) {
- return !(*this == other);
- }
- private:
- typename std::unordered_map<KeyType, ValueType, Hash>::const_iterator it_;
- };
- explicit HashMap(Hash hasher = Hash()) : map_(1, hasher) {}
- template <class InputIterator>
- HashMap(InputIterator begin,
- InputIterator end,
- Hash hasher = Hash()) : map_(1, hasher) {
- for (auto it = begin; it != end; ++it) {
- map_.insert(*it);
- }
- }
- HashMap(std::initializer_list<std::pair<KeyType, ValueType>> list, Hash hasher = Hash()) : map_(1, hasher) {
- for (auto &item : list) {
- map_.insert(item);
- }
- }
- size_t size() const {
- return map_.size();
- }
- bool empty() const {
- return map_.empty();
- }
- Hash hash_function() const {
- return map_.hash_function();
- }
- void insert(std::pair<KeyType, ValueType> element) {
- map_.insert(element);
- }
- void erase(KeyType key) {
- map_.erase(key);
- }
- const_iterator find(KeyType key) const {
- return const_iterator(map_.find(key));
- }
- iterator find(KeyType key) {
- return iterator(map_.find(key));
- }
- ValueType &operator[](KeyType key) {
- return map_[key];
- }
- const ValueType &at(KeyType key) const {
- return map_.at(key);
- }
- iterator begin() {
- return iterator(map_.begin());
- }
- iterator end() {
- return iterator(map_.end());
- }
- const_iterator begin() const {
- return const_iterator(map_.cbegin());
- }
- const_iterator end() const {
- return const_iterator(map_.cend());
- }
- void clear() {
- map_.clear();
- }
- private:
- std::unordered_map<KeyType, ValueType, Hash> map_;
- };
- #endif //MY_OWN_HASH_TABLE_HASH_MAP_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement