1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 08:17:34 +00:00

LibJS: Implement Sets using Maps

This implements ordered sets using Maps with a sentinel value, and
includes some extra set tests.
Fixes #11004.

Co-Authored-By: davidot <davidot@serenityos.org>
This commit is contained in:
Ali Mohammad Pur 2022-02-09 18:34:16 +03:30 committed by Linus Groh
parent 4a73ec07c5
commit 3bfcd7b52d
8 changed files with 133 additions and 21 deletions

View file

@ -6,8 +6,8 @@
#pragma once
#include <AK/HashTable.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Map.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/Value.h>
@ -22,13 +22,24 @@ public:
explicit Set(Object& prototype);
virtual ~Set() override;
OrderedHashTable<Value, ValueTraits> const& values() const { return m_values; };
OrderedHashTable<Value, ValueTraits>& values() { return m_values; };
// NOTE: Unlike what the spec says, we implement Sets using an underlying map,
// so all the functions below do not directly implement the operations as
// defined by the specification.
void set_clear() { m_values.map_clear(); }
bool set_remove(Value const& value) { return m_values.map_remove(value); }
bool set_has(Value const& key) const { return m_values.map_has(key); }
void set_add(Value const& key) { m_values.map_set(key, js_undefined()); }
size_t set_size() const { return m_values.map_size(); }
auto begin() const { return m_values.begin(); }
auto begin() { return m_values.begin(); }
auto end() const { return m_values.end(); }
private:
virtual void visit_edges(Visitor& visitor) override;
OrderedHashTable<Value, ValueTraits> m_values;
Map m_values;
};
}