Set.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Map.h>
  9. #include <LibJS/Runtime/Object.h>
  10. #include <LibJS/Runtime/Value.h>
  11. namespace JS {
  12. class Set : public Object {
  13. JS_OBJECT(Set, Object);
  14. public:
  15. static Set* create(GlobalObject&);
  16. explicit Set(Object& prototype);
  17. virtual ~Set() override = default;
  18. // NOTE: Unlike what the spec says, we implement Sets using an underlying map,
  19. // so all the functions below do not directly implement the operations as
  20. // defined by the specification.
  21. void set_clear() { m_values.map_clear(); }
  22. bool set_remove(Value const& value) { return m_values.map_remove(value); }
  23. bool set_has(Value const& key) const { return m_values.map_has(key); }
  24. void set_add(Value const& key) { m_values.map_set(key, js_undefined()); }
  25. size_t set_size() const { return m_values.map_size(); }
  26. auto begin() const { return m_values.begin(); }
  27. auto begin() { return m_values.begin(); }
  28. auto end() const { return m_values.end(); }
  29. private:
  30. virtual void visit_edges(Visitor& visitor) override;
  31. Map m_values;
  32. };
  33. }