Skip to content

Commit 101bb92

Browse files
committed
karm-core: Fix copying empty hash table.
1 parent bff1017 commit 101bb92

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/karm-core/base/hash-table.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,14 @@ struct HashTable {
4848

4949
HashTable(HashTable const& other) {
5050
_cap = other._cap;
51-
_slots = new Slot[_cap];
51+
_slots = nullptr;
5252
_len = other._len;
5353
_dead = other._dead;
54+
55+
if (_cap == 0)
56+
return;
57+
58+
_slots = new Slot[_cap];
5459
for (usize i = 0; i < _cap; i++) {
5560
if (other._slots[i].state == State::USED) {
5661
_slots[i]._manual.ctor(other._slots[i].unwrap());
@@ -143,7 +148,7 @@ struct HashTable {
143148

144149
template <typename Self, Meta::Equatable<T> U>
145150
auto lookup(this Self& self, U const& u) -> Meta::CopyConst<Self, Slot>* {
146-
if (not self._slots)
151+
if (not self._slots or self._cap == 0)
147152
return nullptr;
148153

149154
usize start = hash(u) % self._cap;

src/karm-core/base/map.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ struct Map {
6767
_items.put(slot, key, std::forward<decltype(value)>(value));
6868
}
6969

70-
[[]] Opt<V> remove(Meta::Equatable<K> auto const& key) {
70+
[[nodiscard]] Opt<V> remove(Meta::Equatable<K> auto const& key) {
7171
if (auto* slot = _items.lookup(key); slot and slot->state == Items::USED) {
7272
V res = std::move(slot->unwrap().value);
7373
_items.clear(slot);

0 commit comments

Comments
 (0)