• 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 PUBLIC_API 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 T>
51 class PUBLIC_API ChunkDeque : public std::deque<T, ChunkAllocator<T>> {
52 public:
ChunkDeque(Chunk * chunk)53     explicit ChunkDeque(Chunk *chunk) : std::deque<T, ChunkAllocator<T>>(ChunkAllocator<T>(chunk)) {}
54     ~ChunkDeque() = default;
55 };
56 
57 template<typename T>
58 class PUBLIC_API ChunkQueue : public std::queue<T, ChunkDeque<T>> {
59 public:
ChunkQueue(Chunk * chunk)60     explicit ChunkQueue(Chunk *chunk) : std::queue<T, ChunkDeque<T>>(ChunkDeque<T>(chunk)) {}
61     ~ChunkQueue() = default;
62 };
63 
64 template<typename T>
65 class PUBLIC_API ChunkStack : public std::stack<T, ChunkDeque<T>> {
66 public:
ChunkStack(Chunk * chunk)67     explicit ChunkStack(Chunk *chunk) : std::stack<T, ChunkDeque<T>>(ChunkDeque<T>(chunk)) {}
68     ~ChunkStack() = default;
69 };
70 
71 template<typename K, typename Compare = std::less<K>>
72 class PUBLIC_API ChunkSet : public std::set<K, Compare, ChunkAllocator<K>> {
73 public:
74     // Constructs an empty set.
ChunkSet(Chunk * chunk)75     explicit ChunkSet(Chunk *chunk)
76         : std::set<K, Compare, ChunkAllocator<K>>(Compare(), ChunkAllocator<K>(chunk))
77     {
78     }
79     ~ChunkSet() = default;
80 };
81 
82 template<typename K, typename V, typename Compare = std::less<K>>
83 class PUBLIC_API ChunkMap : public std::map<K, V, Compare, ChunkAllocator<std::pair<const K, V>>> {
84 public:
85     // Constructs an empty map.
ChunkMap(Chunk * chunk)86     explicit ChunkMap(Chunk *chunk)
87         : std::map<K, V, Compare, ChunkAllocator<std::pair<const K, V>>>(Compare(),
88                                                                          ChunkAllocator<std::pair<const K, V>>(chunk))
89     {
90     }
91     ~ChunkMap() = default;
92 };
93 
94 template<typename K, typename V, typename Hash = std::hash<K>, typename KeyEqual = std::equal_to<K>>
95 class PUBLIC_API ChunkUnorderedMap : public std::unordered_map<K, V, Hash, KeyEqual,
96                                                                ChunkAllocator<std::pair<const K, V>>> {
97 public:
98     // NOLINTNEXTLINE(readability-magic-numbers)
99     explicit ChunkUnorderedMap(Chunk *chunk, size_t bucket_count = 100)
100         : std::unordered_map<K, V, Hash, KeyEqual, ChunkAllocator<std::pair<const K, V>>>(
101               bucket_count, Hash(), KeyEqual(), ChunkAllocator<std::pair<const K, V>>(chunk))
102     {
103     }
104     ~ChunkUnorderedMap() = default;
105     NO_COPY_SEMANTIC(ChunkUnorderedMap);
106     NO_MOVE_SEMANTIC(ChunkUnorderedMap);
107 };
108 
109 template<typename K, typename V, typename Compare = std::less<K>>
110 class PUBLIC_API ChunkMultimap : public std::multimap<K, V, Compare, ChunkAllocator<std::pair<const K, V>>> {
111 public:
112     // Constructs an empty multimap.
ChunkMultimap(Chunk * chunk)113     explicit ChunkMultimap(Chunk *chunk)
114         : std::multimap<K, V, Compare, ChunkAllocator<std::pair<const K, V>>>(
115               Compare(), ChunkAllocator<std::pair<const K, V>>(chunk))
116     {
117     }
118     ~ChunkMultimap() = default;
119     NO_COPY_SEMANTIC(ChunkMultimap);
120     NO_MOVE_SEMANTIC(ChunkMultimap);
121 };
122 }  // namespace panda::ecmascript
123 
124 #endif  // LIBPANDABASE_ECMASCRIPT_MEM_CONTAINERS_H
125