From fafdda8902e25e39c0e8484252a8e17271ed4538 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 6 May 2019 13:28:52 +0200 Subject: [PATCH] AK: Change HashTable and HashMap size/capacity to be ints. --- AK/HashMap.h | 4 ++-- AK/HashTable.h | 38 +++++++++++++++++++------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/AK/HashMap.h b/AK/HashMap.h index df802f6702b..a2f54cf216b 100644 --- a/AK/HashMap.h +++ b/AK/HashMap.h @@ -48,8 +48,8 @@ public: } bool is_empty() const { return m_table.is_empty(); } - unsigned size() const { return m_table.size(); } - unsigned capacity() const { return m_table.capacity(); } + int size() const { return m_table.size(); } + int capacity() const { return m_table.capacity(); } void clear() { m_table.clear(); } void set(const K&, const V&); diff --git a/AK/HashTable.h b/AK/HashTable.h index 1ae17b6b628..7a020601418 100644 --- a/AK/HashTable.h +++ b/AK/HashTable.h @@ -46,8 +46,8 @@ public: ~HashTable() { clear(); } bool is_empty() const { return !m_size; } - unsigned size() const { return m_size; } - unsigned capacity() const { return m_capacity; } + int size() const { return m_size; } + int capacity() const { return m_capacity; } void set(const T&); void set(T&&); @@ -108,7 +108,7 @@ public: } private: friend class HashTable; - explicit Iterator(HashTable& table, bool is_end, typename DoublyLinkedList::Iterator bucket_iterator = DoublyLinkedList::Iterator::universal_end(), unsigned bucket_index = 0) + explicit Iterator(HashTable& table, bool is_end, typename DoublyLinkedList::Iterator bucket_iterator = DoublyLinkedList::Iterator::universal_end(), int bucket_index = 0) : m_table(table) , m_bucket_index(bucket_index) , m_is_end(is_end) @@ -125,7 +125,7 @@ public: } HashTable& m_table; - unsigned m_bucket_index { 0 }; + int m_bucket_index { 0 }; bool m_is_end { false }; typename DoublyLinkedList::Iterator m_bucket_iterator; }; @@ -186,7 +186,7 @@ public: } private: friend class HashTable; - ConstIterator(const HashTable& table, bool is_end, typename DoublyLinkedList::ConstIterator bucket_iterator = DoublyLinkedList::ConstIterator::universal_end(), unsigned bucket_index = 0) + ConstIterator(const HashTable& table, bool is_end, typename DoublyLinkedList::ConstIterator bucket_iterator = DoublyLinkedList::ConstIterator::universal_end(), int bucket_index = 0) : m_table(table) , m_bucket_index(bucket_index) , m_is_end(is_end) @@ -204,7 +204,7 @@ public: } const HashTable& m_table; - unsigned m_bucket_index { 0 }; + int m_bucket_index { 0 }; bool m_is_end { false }; typename DoublyLinkedList::ConstIterator m_bucket_iterator; }; @@ -225,16 +225,16 @@ public: void remove(Iterator); private: - Bucket& lookup(const T&, unsigned* bucket_index = nullptr); - const Bucket& lookup(const T&, unsigned* bucket_index = nullptr) const; - void rehash(unsigned capacity); + Bucket& lookup(const T&, int* bucket_index = nullptr); + const Bucket& lookup(const T&, int* bucket_index = nullptr) const; + void rehash(int capacity); void insert(const T&); void insert(T&&); Bucket* m_buckets { nullptr }; - unsigned m_size { 0 }; - unsigned m_capacity { 0 }; + int m_size { 0 }; + int m_capacity { 0 }; }; template @@ -281,7 +281,7 @@ void HashTable::set(const T& value) template -void HashTable::rehash(unsigned new_capacity) +void HashTable::rehash(int new_capacity) { new_capacity *= 2; #ifdef HASHTABLE_DEBUG @@ -289,14 +289,14 @@ void HashTable::rehash(unsigned new_capacity) #endif auto* new_buckets = new Bucket[new_capacity]; auto* old_buckets = m_buckets; - unsigned old_capacity = m_capacity; + int old_capacity = m_capacity; m_buckets = new_buckets; m_capacity = new_capacity; #ifdef HASHTABLE_DEBUG kprintf("reinsert %u buckets\n", old_capacity); #endif - for (unsigned i = 0; i < old_capacity; ++i) { + for (int i = 0; i < old_capacity; ++i) { for (auto& value : old_buckets[i].chain) { insert(move(value)); } @@ -348,7 +348,7 @@ auto HashTable::find(const T& value) -> Iterator { if (is_empty()) return end(); - unsigned bucket_index; + int bucket_index; auto& bucket = lookup(value, &bucket_index); auto bucket_iterator = bucket.chain.find(value); if (bucket_iterator != bucket.chain.end()) @@ -361,7 +361,7 @@ auto HashTable::find(const T& value) const -> ConstIterator { if (is_empty()) return end(); - unsigned bucket_index; + int bucket_index; auto& bucket = lookup(value, &bucket_index); auto bucket_iterator = bucket.chain.find(value); if (bucket_iterator != bucket.chain.end()) @@ -378,7 +378,7 @@ void HashTable::remove(Iterator it) } template -typename HashTable::Bucket& HashTable::lookup(const T& value, unsigned* bucket_index) +typename HashTable::Bucket& HashTable::lookup(const T& value, int* bucket_index) { unsigned hash = TraitsForT::hash(value); #ifdef HASHTABLE_DEBUG @@ -392,7 +392,7 @@ typename HashTable::Bucket& HashTable::lookup(cons } template -const typename HashTable::Bucket& HashTable::lookup(const T& value, unsigned* bucket_index) const +const typename HashTable::Bucket& HashTable::lookup(const T& value, int* bucket_index) const { unsigned hash = TraitsForT::hash(value); #ifdef HASHTABLE_DEBUG @@ -409,7 +409,7 @@ template void HashTable::dump() const { kprintf("HashTable{%p} m_size=%u, m_capacity=%u, m_buckets=%p\n", this, m_size, m_capacity, m_buckets); - for (unsigned i = 0; i < m_capacity; ++i) { + for (int i = 0; i < m_capacity; ++i) { auto& bucket = m_buckets[i]; kprintf("Bucket %u\n", i); for (auto& e : bucket.chain) {