14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_TEMPLATE_MAP_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_TEMPLATE_MAP_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/js_array.h"
204514f5e3Sopenharmony_ci#include "ecmascript/tagged_hash_table.h"
214514f5e3Sopenharmony_ci
224514f5e3Sopenharmony_cinamespace panda::ecmascript {
234514f5e3Sopenharmony_ciclass TemplateMap : public TaggedHashTable<TemplateMap> {
244514f5e3Sopenharmony_cipublic:
254514f5e3Sopenharmony_ci    using HashTable = TaggedHashTable<TemplateMap>;
264514f5e3Sopenharmony_ci    static inline bool IsMatch(const JSTaggedValue &key, const JSTaggedValue &other)
274514f5e3Sopenharmony_ci    {
284514f5e3Sopenharmony_ci        return key == other;
294514f5e3Sopenharmony_ci    }
304514f5e3Sopenharmony_ci    static inline int Hash(const JSTaggedValue &obj)
314514f5e3Sopenharmony_ci    {
324514f5e3Sopenharmony_ci        ASSERT(obj.IsJSArray());
334514f5e3Sopenharmony_ci        JSArray *array = JSArray::Cast(obj.GetTaggedObject());
344514f5e3Sopenharmony_ci        uint32_t len = array->GetArrayLength();
354514f5e3Sopenharmony_ci        return len;
364514f5e3Sopenharmony_ci    }
374514f5e3Sopenharmony_ci    inline static int GetKeyIndex(int entry)
384514f5e3Sopenharmony_ci    {
394514f5e3Sopenharmony_ci        return HashTable::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_KEY_INDEX;
404514f5e3Sopenharmony_ci    }
414514f5e3Sopenharmony_ci    inline static int GetValueIndex(int entry)
424514f5e3Sopenharmony_ci    {
434514f5e3Sopenharmony_ci        return HashTable::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_VALUE_INDEX;
444514f5e3Sopenharmony_ci    }
454514f5e3Sopenharmony_ci    inline static int GetEntryIndex(int entry)
464514f5e3Sopenharmony_ci    {
474514f5e3Sopenharmony_ci        return HashTable::TABLE_HEADER_SIZE + entry * GetEntrySize();
484514f5e3Sopenharmony_ci    }
494514f5e3Sopenharmony_ci    inline static int GetEntrySize()
504514f5e3Sopenharmony_ci    {
514514f5e3Sopenharmony_ci        return ENTRY_SIZE;
524514f5e3Sopenharmony_ci    }
534514f5e3Sopenharmony_ci    static TemplateMap *Cast(TaggedObject *object)
544514f5e3Sopenharmony_ci    {
554514f5e3Sopenharmony_ci        return reinterpret_cast<TemplateMap *>(object);
564514f5e3Sopenharmony_ci    }
574514f5e3Sopenharmony_ci    static const int DEFAULT_ELEMENTS_NUMBER = 16;
584514f5e3Sopenharmony_ci    static JSHandle<TemplateMap> Create(JSThread *thread, int numberOfElements = DEFAULT_ELEMENTS_NUMBER)
594514f5e3Sopenharmony_ci    {
604514f5e3Sopenharmony_ci        return HashTable::Create(thread, numberOfElements);
614514f5e3Sopenharmony_ci    }
624514f5e3Sopenharmony_ci    static int ComputeCompactSize([[maybe_unused]] const JSHandle<TemplateMap> &table, int computeHashTableSize,
634514f5e3Sopenharmony_ci        [[maybe_unused]] int tableSize, [[maybe_unused]] int addedElements)
644514f5e3Sopenharmony_ci    {
654514f5e3Sopenharmony_ci        return computeHashTableSize;
664514f5e3Sopenharmony_ci    }
674514f5e3Sopenharmony_ci    static const int ENTRY_SIZE = 2;
684514f5e3Sopenharmony_ci    static const int ENTRY_KEY_INDEX = 0;
694514f5e3Sopenharmony_ci    static const int ENTRY_VALUE_INDEX = 1;
704514f5e3Sopenharmony_ci};
714514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
724514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_TEMPLATE_MAP_H