Unordered Map and Set

basically wrapper on top of hash table

for unordered_map, pair<Key, Value> is the value_type
for unordered_set, Key is the value_type

unordered_map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef MY_UNORDERED_MAP
#define MY_UNORDERED_MAP


#include "my_hashtable.h"

namespace mystl {

template <
typename Key,
typename Value,
typename Hash = std::hash<Key>,
typename KeyEqual = mystl::equal_to<Key>,
typename Alloc = mystl::MyAllocator<hash_node<Key, Value>>>
class MyUnorderedMap{
public:
using key_type = Key;
using mapped_type = Value;
using value_type = mystl::pair<Key, Value>;
using size_type = std::size_t;
using allocator_type = Alloc;
using table_type = MyHashTable<Key, Value, Hash, KeyEqual, Alloc>;

private:
table_type table_;

public:
explicit MyUnorderedMap(size_type bucket_count = 8)
: table_(bucket_count) {}

bool empty() const noexcept {return table_.empty();}

size_type size() const {return table_.size();}

size_type bucket_count() const noexcept {return table_.bucket_count();}

float load_factor() const noexcept {return table_.load_factor();}

void clear() {table_.clear();}

void insert(const key_type& key, const mapped_type& value){
table_.insert(key, value);
}

bool erase(const key_type& key){
return table_.erase(key);
}

mapped_type* find(const key_type& key){
return table_.find(key);
}

mapped_type& operator[](const key_type& key){
return table_[key];
}

void rehash(size_type new_count){
table_.rehash(new_count);
}

void print_debug() const {
table_.print_debug();
}

};
} // namespace mystl
#endif // MY_UNORDERED_MAP

unordered_set

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef MY_UNORDERED_SET
#define MY_UNORDERED_SET

#include "my_hashtable.h"

namespace mystl{

template <
typename Key,
typename Hash = std::hash<Key>,
typename KeyEqual = mystl::equal_to<Key>,
typename Alloc = mystl::MyAllocator<hash_node<Key, Key>>>
class MyUnorderedSet{
public:
using key_type = Key;
using value_type = Key;
using size_type = std::size_t;
using hasher = Hash;
using key_equal = KeyEqual;
using allocator_type = Alloc;

using table_type = MyHashTable<Key, Key, Hash, KeyEqual, Alloc>;

private:
table_type table_;

public:
explicit MyUnorderedSet(size_type bucket_count = 8)
: table_(bucket_count){}

bool empty() const noexcept {return table_.empty();}
size_type size() const noexcept {return table_.size();}
size_type bucket_count() const noexcept {return table_.bucket_count();}

float load_factor() const noexcept {return table_.load_factor();}

void clear() {table_.clear();}

void insert(const key_type& key){
table_.insert(key,key);
}

bool contains(const key_type& key) const {
return const_cast<table_type&>(table_).find(key) != nullptr;
}

void rehash(size_type new_count){
table_.rehash(new_count);
}

void print_debug() const {
table_.print_debug();
}


};
} // namespace mystl
#endif // MY_UNORDERED_SET