Set.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. JS_DECLARE_ALLOCATOR(Set);
  15. public:
  16. static NonnullGCPtr<Set> create(Realm&);
  17. virtual void initialize(Realm&) override;
  18. virtual ~Set() override = default;
  19. // NOTE: Unlike what the spec says, we implement Sets using an underlying map,
  20. // so all the functions below do not directly implement the operations as
  21. // defined by the specification.
  22. void set_clear() { m_values->map_clear(); }
  23. bool set_remove(Value const& value) { return m_values->map_remove(value); }
  24. bool set_has(Value const& key) const { return m_values->map_has(key); }
  25. void set_add(Value const& key) { m_values->map_set(key, js_undefined()); }
  26. size_t set_size() const { return m_values->map_size(); }
  27. auto begin() const { return const_cast<Map const&>(*m_values).begin(); }
  28. auto begin() { return m_values->begin(); }
  29. auto end() const { return m_values->end(); }
  30. NonnullGCPtr<Set> copy() const;
  31. private:
  32. explicit Set(Object& prototype);
  33. virtual void visit_edges(Visitor& visitor) override;
  34. GCPtr<Map> m_values;
  35. };
  36. }