mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:48:11 +00:00
AK: Add values() method in HashTable
Add HashTable::values() method that returns all values.
This commit is contained in:
parent
c9c8f2413f
commit
4c6564e3c1
2 changed files with 25 additions and 0 deletions
|
@ -423,6 +423,15 @@ public:
|
||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Vector<T> values() const
|
||||||
|
{
|
||||||
|
Vector<T> list;
|
||||||
|
list.ensure_capacity(size());
|
||||||
|
for (auto& value : *this)
|
||||||
|
list.unchecked_append(value);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool should_grow() const { return ((m_size + 1) * 100) >= (m_capacity * grow_at_load_factor_percent); }
|
bool should_grow() const { return ((m_size + 1) * 100) >= (m_capacity * grow_at_load_factor_percent); }
|
||||||
static constexpr size_t size_in_bytes(size_t capacity) { return sizeof(BucketType) * capacity; }
|
static constexpr size_t size_in_bytes(size_t capacity) { return sizeof(BucketType) * capacity; }
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include <AK/DeprecatedString.h>
|
#include <AK/DeprecatedString.h>
|
||||||
#include <AK/HashTable.h>
|
#include <AK/HashTable.h>
|
||||||
#include <AK/NonnullOwnPtr.h>
|
#include <AK/NonnullOwnPtr.h>
|
||||||
|
#include <AK/Vector.h>
|
||||||
|
|
||||||
TEST_CASE(construct)
|
TEST_CASE(construct)
|
||||||
{
|
{
|
||||||
|
@ -434,3 +435,18 @@ TEST_CASE(ordered_infinite_loop_clang_regression)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE(values)
|
||||||
|
{
|
||||||
|
OrderedHashTable<int> table;
|
||||||
|
table.set(10);
|
||||||
|
table.set(30);
|
||||||
|
table.set(20);
|
||||||
|
|
||||||
|
Vector<int> values = table.values();
|
||||||
|
|
||||||
|
EXPECT_EQ(values.size(), table.size());
|
||||||
|
EXPECT_EQ(values[0], 10);
|
||||||
|
EXPECT_EQ(values[1], 30);
|
||||||
|
EXPECT_EQ(values[2], 20);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue