From b813b2f871756bd150bc06e1d74adcfa179a7e56 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 24 Feb 2020 09:42:52 +0100 Subject: [PATCH] AK: Make HashTable and HashMap use size_t for size and capacity --- AK/HashMap.h | 6 +++--- AK/HashTable.h | 42 ++++++++++++++++++------------------- Games/Minesweeper/Field.cpp | 2 +- Games/Minesweeper/Field.h | 6 +++--- 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/AK/HashMap.h b/AK/HashMap.h index d521667e4b..9533b3042e 100644 --- a/AK/HashMap.h +++ b/AK/HashMap.h @@ -50,8 +50,8 @@ public: 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 }); } @@ -92,7 +92,7 @@ public: 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::PeekType> get(const K& key) const { diff --git a/AK/HashTable.h b/AK/HashTable.h index c3eae131aa..f4f8f28f64 100644 --- a/AK/HashTable.h +++ b/AK/HashTable.h @@ -79,7 +79,7 @@ public: 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) @@ -95,7 +95,7 @@ private: } HashTableType& m_table; - int m_bucket_index { 0 }; + size_t m_bucket_index { 0 }; bool m_is_end { false }; BucketIteratorType m_bucket_iterator; }; @@ -148,10 +148,10 @@ public: ~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); @@ -177,7 +177,7 @@ public: { 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()) @@ -190,7 +190,7 @@ public: { 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()) @@ -218,34 +218,34 @@ public: 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 }; }; @@ -293,17 +293,17 @@ void HashTable::set(const T& value) } template -void HashTable::rehash(int new_capacity) +void HashTable::rehash(size_t new_capacity) { TemporaryChange 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)); } @@ -360,7 +360,7 @@ void HashTable::remove(Iterator it) } template -auto HashTable::lookup(const T& value, int* bucket_index) -> Bucket& +auto HashTable::lookup(const T& value, size_t* bucket_index) -> Bucket& { unsigned hash = TraitsForT::hash(value); if (bucket_index) @@ -369,7 +369,7 @@ auto HashTable::lookup(const T& value, int* bucket_index) -> Buck } template -auto HashTable::lookup(const T& value, int* bucket_index) const -> const Bucket& +auto HashTable::lookup(const T& value, size_t* bucket_index) const -> const Bucket& { unsigned hash = TraitsForT::hash(value); if (bucket_index) diff --git a/Games/Minesweeper/Field.cpp b/Games/Minesweeper/Field.cpp index 90cef6454a..cff67823a6 100644 --- a/Games/Minesweeper/Field.cpp +++ b/Games/Minesweeper/Field.cpp @@ -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; diff --git a/Games/Minesweeper/Field.h b/Games/Minesweeper/Field.h index 97a84f015d..db6ee64fbb 100644 --- a/Games/Minesweeper/Field.h +++ b/Games/Minesweeper/Field.h @@ -65,11 +65,11 @@ public: 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(); @@ -105,7 +105,7 @@ private: 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> m_squares; RefPtr m_mine_bitmap;