14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021-2023 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_DFX_HPROF_STRING_HASHMAP_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_DFX_HPROF_STRING_HASHMAP_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h"
204514f5e3Sopenharmony_ci#include "ecmascript/mem/c_containers.h"
214514f5e3Sopenharmony_ci#include "ecmascript/mem/c_string.h"
224514f5e3Sopenharmony_ci
234514f5e3Sopenharmony_cinamespace panda::ecmascript {
244514f5e3Sopenharmony_ciusing StringKey = uint64_t;
254514f5e3Sopenharmony_ciusing StringId = uint64_t;
264514f5e3Sopenharmony_ci
274514f5e3Sopenharmony_ci// An Implementation for Native StringTable without Auto Mem-Management
284514f5e3Sopenharmony_ci// To make sure when using String, it still stays where it was.
294514f5e3Sopenharmony_ciclass StringHashMap {
304514f5e3Sopenharmony_cipublic:
314514f5e3Sopenharmony_ci    explicit StringHashMap(const EcmaVM *vm) : vm_(vm)
324514f5e3Sopenharmony_ci    {
334514f5e3Sopenharmony_ci        ASSERT(vm_ != nullptr);
344514f5e3Sopenharmony_ci    }
354514f5e3Sopenharmony_ci    ~StringHashMap()
364514f5e3Sopenharmony_ci    {
374514f5e3Sopenharmony_ci        Clear();
384514f5e3Sopenharmony_ci    }
394514f5e3Sopenharmony_ci    NO_MOVE_SEMANTIC(StringHashMap);
404514f5e3Sopenharmony_ci    NO_COPY_SEMANTIC(StringHashMap);
414514f5e3Sopenharmony_ci    /*
424514f5e3Sopenharmony_ci     * The ID is the seat number in JSON file Range from 0~string_table_.size()
434514f5e3Sopenharmony_ci     */
444514f5e3Sopenharmony_ci    StringId GetStringId(const CString *cstr) const;
454514f5e3Sopenharmony_ci    /*
464514f5e3Sopenharmony_ci     * Get all keys sorted by insert order
474514f5e3Sopenharmony_ci     */
484514f5e3Sopenharmony_ci    const CVector<StringKey> &GetOrderedKeyStorage() const
494514f5e3Sopenharmony_ci    {
504514f5e3Sopenharmony_ci        return orderedKey_;
514514f5e3Sopenharmony_ci    }
524514f5e3Sopenharmony_ci    /*
534514f5e3Sopenharmony_ci     * Get string by its hash key
544514f5e3Sopenharmony_ci     */
554514f5e3Sopenharmony_ci    CString *GetStringByKey(StringKey key) const;
564514f5e3Sopenharmony_ci    std::pair<uint64_t, CString *> GetStringAndIdPair(StringKey key) const;
574514f5e3Sopenharmony_ci    StringId InsertStrAndGetStringId(const CString &cstrArg);
584514f5e3Sopenharmony_ci    size_t GetCapcity() const
594514f5e3Sopenharmony_ci    {
604514f5e3Sopenharmony_ci        ASSERT(orderedKey_.size() == hashmap_.size());
614514f5e3Sopenharmony_ci        return orderedKey_.size();
624514f5e3Sopenharmony_ci    }
634514f5e3Sopenharmony_ci    /*
644514f5e3Sopenharmony_ci     * For external call to use this StringTable
654514f5e3Sopenharmony_ci     */
664514f5e3Sopenharmony_ci    CString *GetString(const CString &cstr);
674514f5e3Sopenharmony_ci
684514f5e3Sopenharmony_ciprivate:
694514f5e3Sopenharmony_ci    StringKey GenerateStringKey(const CString *cstr) const;
704514f5e3Sopenharmony_ci    CString *FindOrInsertString(const CString *cstr);
714514f5e3Sopenharmony_ci    /*
724514f5e3Sopenharmony_ci     * Free all memory
734514f5e3Sopenharmony_ci     */
744514f5e3Sopenharmony_ci    void Clear();
754514f5e3Sopenharmony_ci    const EcmaVM *vm_;
764514f5e3Sopenharmony_ci    CVector<StringKey> orderedKey_;  // Used for Serialize Order
774514f5e3Sopenharmony_ci    size_t index_ {2};  // 2: Offset the String-Table Header
784514f5e3Sopenharmony_ci    CUnorderedMap<StringKey, StringId> indexMap_;
794514f5e3Sopenharmony_ci    CUnorderedMap<StringKey, CString *> hashmap_;
804514f5e3Sopenharmony_ci};
814514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
824514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_DFX_HPROF_STRING_HASHMAP_H
83