From f65b039c442bb28451bb4bcfc6a91bff76bd0e66 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 21 Jul 2021 18:18:29 +0200 Subject: [PATCH] AK: Sprinkle [[nodiscard]] on HashMap and HashTable --- AK/HashMap.h | 20 ++++++++++---------- AK/HashTable.h | 26 +++++++++++++------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/AK/HashMap.h b/AK/HashMap.h index c1823b9f4e..e5e9bb66fd 100644 --- a/AK/HashMap.h +++ b/AK/HashMap.h @@ -71,26 +71,26 @@ public: using IteratorType = typename HashTableType::Iterator; using ConstIteratorType = typename HashTableType::ConstIterator; - IteratorType begin() { return m_table.begin(); } - IteratorType end() { return m_table.end(); } - IteratorType find(const K& key) + [[nodiscard]] IteratorType begin() { return m_table.begin(); } + [[nodiscard]] IteratorType end() { return m_table.end(); } + [[nodiscard]] IteratorType find(const K& key) { return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(key, entry.key); }); } template - IteratorType find(unsigned hash, TUnaryPredicate predicate) + [[nodiscard]] IteratorType find(unsigned hash, TUnaryPredicate predicate) { return m_table.find(hash, predicate); } - ConstIteratorType begin() const { return m_table.begin(); } - ConstIteratorType end() const { return m_table.end(); } - ConstIteratorType find(const K& key) const + [[nodiscard]] ConstIteratorType begin() const { return m_table.begin(); } + [[nodiscard]] ConstIteratorType end() const { return m_table.end(); } + [[nodiscard]] ConstIteratorType find(const K& key) const { return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(key, entry.key); }); } template - ConstIteratorType find(unsigned hash, TUnaryPredicate predicate) const + [[nodiscard]] ConstIteratorType find(unsigned hash, TUnaryPredicate predicate) const { return m_table.find(hash, predicate); } @@ -121,7 +121,7 @@ public: return (*it).value; } - bool contains(const K& key) const + [[nodiscard]] bool contains(const K& key) const { return find(key) != end(); } @@ -139,7 +139,7 @@ public: return find(key)->value; } - Vector keys() const + [[nodiscard]] Vector keys() const { Vector list; list.ensure_capacity(size()); diff --git a/AK/HashTable.h b/AK/HashTable.h index 8ad3566f84..de34a85882 100644 --- a/AK/HashTable.h +++ b/AK/HashTable.h @@ -197,7 +197,7 @@ public: rehash(capacity * 2); } - bool contains(const T& value) const + [[nodiscard]] bool contains(T const& value) const { return find(value) != end(); } @@ -206,7 +206,7 @@ public: OrderedHashTableIterator, HashTableIterator>; - Iterator begin() + [[nodiscard]] Iterator begin() { if constexpr (IsOrdered) return Iterator(m_collection_data.head); @@ -218,7 +218,7 @@ public: return end(); } - Iterator end() + [[nodiscard]] Iterator end() { return Iterator(nullptr); } @@ -227,7 +227,7 @@ public: OrderedHashTableIterator, HashTableIterator>; - ConstIterator begin() const + [[nodiscard]] ConstIterator begin() const { if constexpr (IsOrdered) return ConstIterator(m_collection_data.head); @@ -239,7 +239,7 @@ public: return end(); } - ConstIterator end() const + [[nodiscard]] ConstIterator end() const { return ConstIterator(nullptr); } @@ -282,23 +282,23 @@ public: } template - Iterator find(unsigned hash, TUnaryPredicate predicate) + [[nodiscard]] Iterator find(unsigned hash, TUnaryPredicate predicate) { return Iterator(lookup_with_hash(hash, move(predicate))); } - Iterator find(const T& value) + [[nodiscard]] Iterator find(T const& value) { return find(TraitsForT::hash(value), [&](auto& other) { return TraitsForT::equals(value, other); }); } template - ConstIterator find(unsigned hash, TUnaryPredicate predicate) const + [[nodiscard]] ConstIterator find(unsigned hash, TUnaryPredicate predicate) const { return ConstIterator(lookup_with_hash(hash, move(predicate))); } - ConstIterator find(const T& value) const + [[nodiscard]] ConstIterator find(T const& value) const { return find(TraitsForT::hash(value), [&](auto& other) { return TraitsForT::equals(value, other); }); } @@ -360,7 +360,7 @@ private: } } - static size_t size_in_bytes(size_t capacity) + [[nodiscard]] static size_t size_in_bytes(size_t capacity) { if constexpr (IsOrdered) { return sizeof(BucketType) * capacity; @@ -406,7 +406,7 @@ private: } template - BucketType* lookup_with_hash(unsigned hash, TUnaryPredicate predicate) const + [[nodiscard]] BucketType* lookup_with_hash(unsigned hash, TUnaryPredicate predicate) const { if (is_empty()) return nullptr; @@ -424,12 +424,12 @@ private: } } - const BucketType* lookup_for_reading(const T& value) const + [[nodiscard]] BucketType const* lookup_for_reading(T const& value) const { return lookup_with_hash(TraitsForT::hash(value), [&value](auto& entry) { return TraitsForT::equals(entry, value); }); } - BucketType& lookup_for_writing(const T& value) + [[nodiscard]] BucketType& lookup_for_writing(T const& value) { if (should_grow()) rehash(capacity() * 2);