This repository was archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap.hpp
126 lines (98 loc) · 3.57 KB
/
map.hpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2022 The Jule Programming Language.
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.
#ifndef __JULE_MAP_HPP
#define __JULE_MAP_HPP
#include <initializer_list>
#include <ostream>
#include <unordered_map>
#include "types.hpp"
#include "str.hpp"
#include "slice.hpp"
namespace jule {
class MapKeyHasher;
// Built-in map type.
template<typename Key, typename Value>
class Map;
class MapKeyHasher {
public:
size_t operator()(const jule::Str &key) const noexcept {
size_t hash{ 0 };
for (jule::Int i{ 0 }; i < key.len(); ++i)
hash += key[i] % 7;
return hash;
}
template<typename T>
inline size_t operator()(const T &obj) const noexcept
{ return this->operator()(jule::to_str<T>(obj)); }
};
template<typename Key, typename Value>
class Map {
public:
mutable std::unordered_map<Key, Value, MapKeyHasher> buffer{};
Map<Key, Value>(void) noexcept {}
Map<Key, Value>(const std::nullptr_t) noexcept {}
Map<Key, Value>(const std::initializer_list<std::pair<Key, Value>> &src) noexcept {
for (const std::pair<Key, Value> &pair: src)
this->buffer.insert(pair);
}
inline constexpr
auto begin(void) noexcept
{ return this->buffer.begin(); }
inline constexpr
auto begin(void) const noexcept
{ return this->buffer.begin(); }
inline constexpr
auto end(void) noexcept
{ return this->buffer.end(); }
inline constexpr
auto end(void) const noexcept
{ return this->buffer.end(); }
inline void clear(void) noexcept
{ this->buffer.clear(); }
jule::Slice<Key> keys(void) const noexcept {
jule::Slice<Key> keys(this->len());
jule::Uint index{ 0 };
for (const auto &pair: *this)
keys._slice[index++] = pair.first;
return keys;
}
jule::Slice<Value> values(void) const noexcept {
jule::Slice<Value> keys(this->len());
jule::Uint index{ 0 };
for (const auto &pair: *this)
keys._slice[index++] = pair.second;
return keys;
}
inline constexpr
jule::Bool has(const Key &key) const noexcept
{ return this->buffer.find(key) != this->end(); }
inline jule::Int len(void) const noexcept
{ return this->buffer.size(); }
inline void del(const Key &key) noexcept
{ this->buffer.erase(key); }
inline jule::Bool operator==(const std::nullptr_t) const noexcept
{ return this->buffer.empty(); }
inline jule::Bool operator!=(const std::nullptr_t) const noexcept
{ return !this->operator==(nullptr); }
Value &operator[](const Key &key)
{ return this->buffer[key]; }
Value &operator[](const Key &key) const
{ return this->buffer[key]; }
friend std::ostream &operator<<(std::ostream &stream,
const Map<Key, Value> &src) noexcept {
stream << '{';
jule::Int length{ src.len() };
for (const auto pair: src) {
stream << pair.first;
stream << ':';
stream << pair.second;
if (--length > 0)
stream << ", ";
}
stream << '}';
return stream;
}
};
} // namespace jule
#endif // #ifndef __JULE_MAP_HPP