• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 LIBPANDABASE_ECMASCRIPT_MEM_CONTAINERS_H
17 #define LIBPANDABASE_ECMASCRIPT_MEM_CONTAINERS_H
18 
19 #include <deque>
20 #include <list>
21 #include <stack>
22 #include <queue>
23 #include <vector>
24 #include <set>
25 #include <string>
26 #include <map>
27 #include <unordered_map>
28 #include <unordered_set>
29 #include <functional>
30 
31 #include "ecmascript/mem/chunk_allocator.h"
32 
33 namespace panda::ecmascript {
34 template<typename T>
35 class ChunkVector : public std::vector<T, ChunkAllocator<T>> {
36 public:
ChunkVector(Chunk * chunk)37     explicit ChunkVector(Chunk *chunk) : std::vector<T, ChunkAllocator<T>>(ChunkAllocator<T>(chunk)) {}
38 
ChunkVector(size_t size,Chunk * chunk)39     ChunkVector(size_t size, Chunk *chunk) : std::vector<T, ChunkAllocator<T>>(size, T(), ChunkAllocator<T>(chunk)) {}
40 
ChunkVector(size_t size,T def,Chunk * chunk)41     ChunkVector(size_t size, T def, Chunk *chunk)
42         : std::vector<T, ChunkAllocator<T>>(size, def, ChunkAllocator<T>(chunk))
43     {
44     }
45     ~ChunkVector() = default;
46     NO_COPY_SEMANTIC(ChunkVector);
47     NO_MOVE_SEMANTIC(ChunkVector);
48 };
49 
50 template<typename K, typename V, typename Compare = std::less<K>>
51 class ChunkMap : public std::map<K, V, Compare, ChunkAllocator<std::pair<const K, V>>> {
52 public:
53     // Constructs an empty map.
ChunkMap(Chunk * chunk)54     explicit ChunkMap(Chunk *chunk)
55         : std::map<K, V, Compare, ChunkAllocator<std::pair<const K, V>>>(Compare(),
56                                                                          ChunkAllocator<std::pair<const K, V>>(chunk))
57     {
58     }
59     ~ChunkMap() = default;
60     NO_COPY_SEMANTIC(ChunkMap);
61     NO_MOVE_SEMANTIC(ChunkMap);
62 };
63 
64 template<typename K, typename V, typename Hash = std::hash<K>, typename KeyEqual = std::equal_to<K>>
65 class ChunkUnorderedMap : public std::unordered_map<K, V, Hash, KeyEqual, ChunkAllocator<std::pair<const K, V>>> {
66 public:
67     // NOLINTNEXTLINE(readability-magic-numbers)
68     explicit ChunkUnorderedMap(Chunk *chunk, size_t bucket_count = 100)
69         : std::unordered_map<K, V, Hash, KeyEqual, ChunkAllocator<std::pair<const K, V>>>(
70               bucket_count, Hash(), KeyEqual(), ChunkAllocator<std::pair<const K, V>>(chunk))
71     {
72     }
73     ~ChunkUnorderedMap() = default;
74     NO_COPY_SEMANTIC(ChunkUnorderedMap);
75     NO_MOVE_SEMANTIC(ChunkUnorderedMap);
76 };
77 
78 template<typename K, typename V, typename Compare = std::less<K>>
79 class ChunkMultimap : public std::multimap<K, V, Compare, ChunkAllocator<std::pair<const K, V>>> {
80 public:
81     // Constructs an empty multimap.
ChunkMultimap(Chunk * chunk)82     explicit ChunkMultimap(Chunk *chunk)
83         : std::multimap<K, V, Compare, ChunkAllocator<std::pair<const K, V>>>(
84               Compare(), ChunkAllocator<std::pair<const K, V>>(chunk))
85     {
86     }
87     ~ChunkMultimap() = default;
88     NO_COPY_SEMANTIC(ChunkMultimap);
89     NO_MOVE_SEMANTIC(ChunkMultimap);
90 };
91 }  // namespace panda::ecmascript
92 
93 #endif  // LIBPANDABASE_ECMASCRIPT_MEM_CONTAINERS_H
94