1/*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef ECMASCRIPT_JS_API_JS_API_LIGHTWEIGHTSET_H
17#define ECMASCRIPT_JS_API_JS_API_LIGHTWEIGHTSET_H
18
19#include "ecmascript/js_object.h"
20#include "ecmascript/js_tagged_value-inl.h"
21
22namespace panda::ecmascript {
23class JSAPILightWeightSet : public JSObject {
24public:
25    static constexpr int DEFAULT_CAPACITY_LENGTH = 8;
26    static constexpr int32_t HASH_REBELLION = 0xFFFFFFFF;
27    static JSAPILightWeightSet *Cast(TaggedObject *object)
28    {
29        ASSERT(JSTaggedValue(object).IsJSAPILightWeightSet());
30        return static_cast<JSAPILightWeightSet *>(object);
31    }
32    static bool Add(JSThread *thread, const JSHandle<JSAPILightWeightSet> &obj, const JSHandle<JSTaggedValue> &value);
33    static JSHandle<TaggedArray> CreateSlot(const JSThread *thread, const uint32_t capacity);
34    static void EnsureCapacity(const JSThread *thread, const JSHandle<JSAPILightWeightSet> &obj,
35                               uint32_t minimumCapacity);
36    static void SizeCopy(const JSThread *thread, const JSHandle<JSAPILightWeightSet> &obj, uint32_t capacity,
37                         uint32_t newCapacity);
38    static bool AddAll(JSThread *thread, const JSHandle<JSAPILightWeightSet> &obj,
39                       const JSHandle<JSTaggedValue> &value);
40    static bool Equal(JSThread *thread, const JSHandle<JSAPILightWeightSet> &obj,
41                      const JSHandle<JSTaggedValue> &value);
42    static void IncreaseCapacityTo(JSThread *thread, const JSHandle<JSAPILightWeightSet> &obj, int32_t minCapacity);
43    static JSTaggedValue ToString(JSThread *thread, const JSHandle<JSAPILightWeightSet> &obj);
44    static JSTaggedValue ForEach(JSThread *thread, const JSHandle<JSTaggedValue> &thisHandle,
45                                 const JSHandle<JSTaggedValue> &callbackFn, const JSHandle<JSTaggedValue> &thisArg);
46    static JSHandle<JSTaggedValue> GetIteratorObj(JSThread *thread, const JSHandle<JSAPILightWeightSet> &obj,
47                                                  IterationKind kind);
48    void AdjustArray(JSThread *thread, JSHandle<TaggedArray> srcArray, uint32_t fromIndex, uint32_t toIndex,
49                     bool direction);
50    void Clear(JSThread *thread);
51    JSTaggedValue Get(const uint32_t index);
52    JSTaggedValue GetHashAt(int32_t index);
53    JSTaggedValue GetValueAt(int32_t index);
54    JSTaggedValue Remove(JSThread *thread, JSHandle<JSTaggedValue> &value);
55    bool Has(const JSThread *thread, const JSHandle<JSTaggedValue> &value);
56    bool HasHash(const JSHandle<JSTaggedValue> &hashCode);
57    bool HasAll(const JSHandle<JSTaggedValue> &value);
58    bool RemoveAt(JSThread *thread, int32_t index);
59    void RemoveValue(const JSThread *thread, JSHandle<TaggedArray> &taggedArray,
60                     uint32_t index, bool isHash = false);
61    bool IsEmpty();
62    int32_t GetIndexOf(const JSThread *thread, JSHandle<JSTaggedValue> &value);
63    int32_t BinarySearchHashes(uint32_t hash, int32_t size);
64    int32_t GetHashIndex(const JSThread *thread, const JSHandle<JSTaggedValue> &value, int32_t size);
65    uint32_t Hash(const JSThread *thread, JSTaggedValue key);
66    inline uint32_t GetSize() const
67    {
68        return GetLength();
69    }
70    static void CheckAndCopyValues(const JSThread *thread, JSHandle<JSAPILightWeightSet> obj);
71    static constexpr size_t HASHES_OFFSET = JSObject::SIZE;
72    ACCESSORS(Hashes, HASHES_OFFSET, VALUES_OFFSET);
73    ACCESSORS(Values, VALUES_OFFSET, LENGTH_OFFSET);
74    ACCESSORS_PRIMITIVE_FIELD(Length, uint32_t, LENGTH_OFFSET, LAST_OFFSET);
75    DEFINE_ALIGN_SIZE(LAST_OFFSET);
76
77    DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, HASHES_OFFSET, LENGTH_OFFSET)
78    DECL_DUMP()
79};
80}  // namespace panda::ecmascript
81#endif  // ECMASCRIPT_JS_API_JS_API_LIGHTWEIGHTSET_H
82