ConservativeVector.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <AK/IntrusiveList.h>
  9. #include <AK/Vector.h>
  10. #include <LibGC/Cell.h>
  11. #include <LibGC/Forward.h>
  12. #include <LibGC/HeapRoot.h>
  13. namespace GC {
  14. class ConservativeVectorBase {
  15. public:
  16. virtual ReadonlySpan<FlatPtr> possible_values() const = 0;
  17. protected:
  18. explicit ConservativeVectorBase(Heap&);
  19. ~ConservativeVectorBase();
  20. ConservativeVectorBase& operator=(ConservativeVectorBase const&);
  21. Heap* m_heap { nullptr };
  22. IntrusiveListNode<ConservativeVectorBase> m_list_node;
  23. public:
  24. using List = IntrusiveList<&ConservativeVectorBase::m_list_node>;
  25. };
  26. template<typename T, size_t inline_capacity>
  27. class ConservativeVector final
  28. : public ConservativeVectorBase
  29. , public Vector<T, inline_capacity> {
  30. public:
  31. explicit ConservativeVector(Heap& heap)
  32. : ConservativeVectorBase(heap)
  33. {
  34. }
  35. virtual ~ConservativeVector() = default;
  36. ConservativeVector(ConservativeVector const& other)
  37. : ConservativeVectorBase(*other.m_heap)
  38. , Vector<T, inline_capacity>(other)
  39. {
  40. }
  41. ConservativeVector(ConservativeVector&& other)
  42. : ConservativeVectorBase(*other.m_heap)
  43. , Vector<T, inline_capacity>(move(static_cast<Vector<T, inline_capacity>&>(other)))
  44. {
  45. }
  46. ConservativeVector& operator=(ConservativeVector const& other)
  47. {
  48. Vector<T, inline_capacity>::operator=(other);
  49. ConservativeVectorBase::operator=(other);
  50. return *this;
  51. }
  52. virtual ReadonlySpan<FlatPtr> possible_values() const override
  53. {
  54. static_assert(sizeof(T) >= sizeof(FlatPtr));
  55. return ReadonlySpan<FlatPtr> {
  56. reinterpret_cast<FlatPtr const*>(this->data()),
  57. this->size() * sizeof(T) / sizeof(FlatPtr),
  58. };
  59. }
  60. };
  61. }