Skip to content

Commit

Permalink
AK: Make HashTable and HashMap use size_t for size and capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Feb 24, 2020
1 parent 3252a69 commit b813b2f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
6 changes: 3 additions & 3 deletions AK/HashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class HashMap {
HashMap() {}

bool is_empty() const { return m_table.is_empty(); }
int size() const { return m_table.size(); }
int capacity() const { return m_table.capacity(); }
size_t size() const { return m_table.size(); }
size_t capacity() const { return m_table.capacity(); }
void clear() { m_table.clear(); }

void set(const K& key, const V& value) { m_table.set({ key, value }); }
Expand Down Expand Up @@ -92,7 +92,7 @@ class HashMap {
return m_table.find(hash, finder);
}

void ensure_capacity(int capacity) { m_table.ensure_capacity(capacity); }
void ensure_capacity(size_t capacity) { m_table.ensure_capacity(capacity); }

Optional<typename Traits<V>::PeekType> get(const K& key) const
{
Expand Down
42 changes: 21 additions & 21 deletions AK/HashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class HashTableIterator {
private:
friend HashTableType;

explicit HashTableIterator(HashTableType& table, bool is_end, BucketIteratorType bucket_iterator = BucketIteratorType::universal_end(), int bucket_index = 0)
explicit HashTableIterator(HashTableType& table, bool is_end, BucketIteratorType bucket_iterator = BucketIteratorType::universal_end(), size_t bucket_index = 0)
: m_table(table)
, m_bucket_index(bucket_index)
, m_is_end(is_end)
Expand All @@ -95,7 +95,7 @@ class HashTableIterator {
}

HashTableType& m_table;
int m_bucket_index { 0 };
size_t m_bucket_index { 0 };
bool m_is_end { false };
BucketIteratorType m_bucket_iterator;
};
Expand Down Expand Up @@ -148,10 +148,10 @@ class HashTable {

~HashTable() { clear(); }
bool is_empty() const { return !m_size; }
int size() const { return m_size; }
int capacity() const { return m_capacity; }
size_t size() const { return m_size; }
size_t capacity() const { return m_capacity; }

void ensure_capacity(int capacity)
void ensure_capacity(size_t capacity)
{
ASSERT(capacity >= size());
rehash(capacity);
Expand All @@ -177,7 +177,7 @@ class HashTable {
{
if (is_empty())
return end();
int bucket_index;
size_t bucket_index;
auto& bucket = lookup_with_hash(hash, &bucket_index);
auto bucket_iterator = bucket.find(finder);
if (bucket_iterator != bucket.end())
Expand All @@ -190,7 +190,7 @@ class HashTable {
{
if (is_empty())
return end();
int bucket_index;
size_t bucket_index;
auto& bucket = lookup_with_hash(hash, &bucket_index);
auto bucket_iterator = bucket.find(finder);
if (bucket_iterator != bucket.end())
Expand Down Expand Up @@ -218,34 +218,34 @@ class HashTable {
void remove(Iterator);

private:
Bucket& lookup(const T&, int* bucket_index = nullptr);
const Bucket& lookup(const T&, int* bucket_index = nullptr) const;
Bucket& lookup(const T&, size_t* bucket_index = nullptr);
const Bucket& lookup(const T&, size_t* bucket_index = nullptr) const;

Bucket& lookup_with_hash(unsigned hash, int* bucket_index)
Bucket& lookup_with_hash(unsigned hash, size_t* bucket_index)
{
if (bucket_index)
*bucket_index = hash % m_capacity;
return m_buckets[hash % m_capacity];
}

const Bucket& lookup_with_hash(unsigned hash, int* bucket_index) const
const Bucket& lookup_with_hash(unsigned hash, size_t* bucket_index) const
{
if (bucket_index)
*bucket_index = hash % m_capacity;
return m_buckets[hash % m_capacity];
}

void rehash(int capacity);
void rehash(size_t capacity);
void insert(const T&);
void insert(T&&);

Bucket& bucket(int index) { return m_buckets[index]; }
const Bucket& bucket(int index) const { return m_buckets[index]; }
Bucket& bucket(size_t index) { return m_buckets[index]; }
const Bucket& bucket(size_t index) const { return m_buckets[index]; }

Bucket* m_buckets { nullptr };

int m_size { 0 };
int m_capacity { 0 };
size_t m_size { 0 };
size_t m_capacity { 0 };
bool m_clearing { false };
bool m_rehashing { false };
};
Expand Down Expand Up @@ -293,17 +293,17 @@ void HashTable<T, TraitsForT>::set(const T& value)
}

template<typename T, typename TraitsForT>
void HashTable<T, TraitsForT>::rehash(int new_capacity)
void HashTable<T, TraitsForT>::rehash(size_t new_capacity)
{
TemporaryChange<bool> change(m_rehashing, true);
new_capacity *= 2;
auto* new_buckets = new Bucket[new_capacity];
auto* old_buckets = m_buckets;
int old_capacity = m_capacity;
size_t old_capacity = m_capacity;
m_buckets = new_buckets;
m_capacity = new_capacity;

for (int i = 0; i < old_capacity; ++i) {
for (size_t i = 0; i < old_capacity; ++i) {
for (auto& value : old_buckets[i]) {
insert(move(value));
}
Expand Down Expand Up @@ -360,7 +360,7 @@ void HashTable<T, TraitsForT>::remove(Iterator it)
}

template<typename T, typename TraitsForT>
auto HashTable<T, TraitsForT>::lookup(const T& value, int* bucket_index) -> Bucket&
auto HashTable<T, TraitsForT>::lookup(const T& value, size_t* bucket_index) -> Bucket&
{
unsigned hash = TraitsForT::hash(value);
if (bucket_index)
Expand All @@ -369,7 +369,7 @@ auto HashTable<T, TraitsForT>::lookup(const T& value, int* bucket_index) -> Buck
}

template<typename T, typename TraitsForT>
auto HashTable<T, TraitsForT>::lookup(const T& value, int* bucket_index) const -> const Bucket&
auto HashTable<T, TraitsForT>::lookup(const T& value, size_t* bucket_index) const -> const Bucket&
{
unsigned hash = TraitsForT::hash(value);
if (bucket_index)
Expand Down
2 changes: 1 addition & 1 deletion Games/Minesweeper/Field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ void Field::set_chord_preview(Square& square, bool chord_preview)
});
}

void Field::set_field_size(int rows, int columns, int mine_count)
void Field::set_field_size(int rows, int columns, size_t mine_count)
{
if (m_rows == rows && m_columns == columns && m_mine_count == mine_count)
return;
Expand Down
6 changes: 3 additions & 3 deletions Games/Minesweeper/Field.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ class Field final : public GUI::Frame {

int rows() const { return m_rows; }
int columns() const { return m_columns; }
int mine_count() const { return m_mine_count; }
size_t mine_count() const { return m_mine_count; }
int square_size() const { return 15; }
bool is_single_chording() const { return m_single_chording; }

void set_field_size(int rows, int columns, int mine_count);
void set_field_size(int rows, int columns, size_t mine_count);
void set_single_chording(bool new_val);

void reset();
Expand Down Expand Up @@ -105,7 +105,7 @@ class Field final : public GUI::Frame {

int m_rows { 0 };
int m_columns { 0 };
int m_mine_count { 0 };
size_t m_mine_count { 0 };
int m_unswept_empties { 0 };
Vector<OwnPtr<Square>> m_squares;
RefPtr<Gfx::Bitmap> m_mine_bitmap;
Expand Down

0 comments on commit b813b2f

Please sign in to comment.