Skip to content

Commit

Permalink
AK: Sprinkle [[nodiscard]] on HashMap and HashTable
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Jul 21, 2021
1 parent 3d0c581 commit f65b039
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
20 changes: 10 additions & 10 deletions AK/HashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,26 @@ class HashMap {
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<typename TUnaryPredicate>
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<typename TUnaryPredicate>
ConstIteratorType find(unsigned hash, TUnaryPredicate predicate) const
[[nodiscard]] ConstIteratorType find(unsigned hash, TUnaryPredicate predicate) const
{
return m_table.find(hash, predicate);
}
Expand Down Expand Up @@ -121,7 +121,7 @@ class HashMap {
return (*it).value;
}

bool contains(const K& key) const
[[nodiscard]] bool contains(const K& key) const
{
return find(key) != end();
}
Expand All @@ -139,7 +139,7 @@ class HashMap {
return find(key)->value;
}

Vector<K> keys() const
[[nodiscard]] Vector<K> keys() const
{
Vector<K> list;
list.ensure_capacity(size());
Expand Down
26 changes: 13 additions & 13 deletions AK/HashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class HashTable {
rehash(capacity * 2);
}

bool contains(const T& value) const
[[nodiscard]] bool contains(T const& value) const
{
return find(value) != end();
}
Expand All @@ -206,7 +206,7 @@ class HashTable {
OrderedHashTableIterator<HashTable, T, BucketType>,
HashTableIterator<HashTable, T, BucketType>>;

Iterator begin()
[[nodiscard]] Iterator begin()
{
if constexpr (IsOrdered)
return Iterator(m_collection_data.head);
Expand All @@ -218,7 +218,7 @@ class HashTable {
return end();
}

Iterator end()
[[nodiscard]] Iterator end()
{
return Iterator(nullptr);
}
Expand All @@ -227,7 +227,7 @@ class HashTable {
OrderedHashTableIterator<const HashTable, const T, const BucketType>,
HashTableIterator<const HashTable, const T, const BucketType>>;

ConstIterator begin() const
[[nodiscard]] ConstIterator begin() const
{
if constexpr (IsOrdered)
return ConstIterator(m_collection_data.head);
Expand All @@ -239,7 +239,7 @@ class HashTable {
return end();
}

ConstIterator end() const
[[nodiscard]] ConstIterator end() const
{
return ConstIterator(nullptr);
}
Expand Down Expand Up @@ -282,23 +282,23 @@ class HashTable {
}

template<typename TUnaryPredicate>
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<typename TUnaryPredicate>
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); });
}
Expand Down Expand Up @@ -360,7 +360,7 @@ class HashTable {
}
}

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;
Expand Down Expand Up @@ -406,7 +406,7 @@ class HashTable {
}

template<typename TUnaryPredicate>
BucketType* lookup_with_hash(unsigned hash, TUnaryPredicate predicate) const
[[nodiscard]] BucketType* lookup_with_hash(unsigned hash, TUnaryPredicate predicate) const
{
if (is_empty())
return nullptr;
Expand All @@ -424,12 +424,12 @@ class HashTable {
}
}

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);
Expand Down

0 comments on commit f65b039

Please sign in to comment.