1 #ifndef DALI_INDEXED_MAP_BASE_H
2 #define DALI_INDEXED_MAP_BASE_H
5 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
26 #include <dali/public-api/common/dali-common.h>
33 * @brief Specific key-element container which can access by index.
34 * Register element by key. and Get element by key or index.
36 * This basical member API that indexed map container needs.
37 * Indexed map container can only Register and not allow Unregister in current implement spec.
39 * This container hold std::pair<KeyType, ElementType>.
40 * Iterator will iterate this pair container.
44 * TrieContainerBase<KeyType, SearchKeyType, KeyIndexConverterType, ElementType> container;
45 * for(auto && elements : container)
47 * elements.first == KeyType();
48 * elements.second == ElementType();
52 * Contain elements in registered order.
53 * You can access that data by this order as 'index'
57 * container.Register(10001, 111);
58 * container.Register(20002, 222);
59 * container[10001] == 111;
60 * container[20002] == 222;
61 * container.GetElementByIndex(0) == 111;
62 * container.GetKeyByIndex(0) == 10001;
63 * container.GetKeyElementPairByIndex(1) == KeyElementPairType(20002, 222);
66 * Get API return iterator of container.
70 * container.Register(10001, 111);
71 * const auto& iter = container.Get(10001);
72 * iter->first == 10001;
73 * iter->second == 111;
74 * container.Get(30003) == container.End();
77 * @tparam KeyType The type of the key that the container holds.
78 * @tparam SearchKeyType The type of the key when the container register or get.
79 * @tparam ElementType The type of the data that the container holds.
81 template<typename KeyType, typename SearchKeyType, typename ElementType>
86 * @brief Type definitions.
88 using KeyElementPairType = std::pair<KeyType, ElementType>;
89 using iterator = typename std::vector<KeyElementPairType>::iterator;
90 using const_iterator = typename std::vector<KeyElementPairType>::const_iterator;
92 public: // Virtual API
94 * @brief Register element by the key.
96 * @param[in] key The key that this container will hold. Duplicated key doesn't allow.
97 * @param[in] element The element pairwise with key.
98 * @return True if Register success. Otherwise, return false.
100 virtual bool Register(const SearchKeyType& key, const ElementType& element) = 0;
103 * @brief Register moved element by the key.
105 * @param[in] key The key that this container will hold. Duplicated key doesn't allow.
106 * @param[in] element The element pairwise with key.
107 * @return True if Register success. Otherwise, return false.
109 virtual bool Register(const SearchKeyType& key, ElementType&& element) = 0;
112 * @brief Get element by the key.
114 * @param[in] key The key that this container will hold.
115 * @return If exist, iterator of container. Otherwise, return End().
117 virtual iterator Get(const SearchKeyType& key) = 0;
120 * @brief Get element by the key.
122 * @param[in] key The key that this container will hold.
123 * @return If exist, iterator of container. Otherwise, return End().
125 virtual const_iterator Get(const SearchKeyType& key) const = 0;
129 * @brief Constructor.
138 virtual ~IndexedMapBase()
143 * @brief Clear this container.
147 mKeyElementPool.clear();
151 * @brief Get the number of elements that this container hold.
153 * @return The number of elements that this container hold.
155 std::size_t Count() const
157 return mKeyElementPool.size();
161 * @brief Check whether container is empty or not.
163 * @return Whether elemnt is empty or not.
167 return mKeyElementPool.empty();
171 * @brief Reserve KeyElementPool container size.
172 * Reserve only if current container size is smaller than input size.
174 * @param[in] size Reserve size.
176 void Reserve(const std::size_t& size)
178 if(mKeyElementPool.size() < size)
180 mKeyElementPool.reserve(size);
185 * @brief Get Element by the key.
186 * @note Assert throw when try to use unregistered key.
188 * @param[in] key The key what we want to get.
189 * @return registered element.
191 const ElementType& operator[](const SearchKeyType& key) const
193 const_iterator iter = Get(key);
194 DALI_ASSERT_ALWAYS(iter != End() && "const operator[] doesn't allow non-exist key access");
199 * @brief Get Element by the key.
200 * @note Assert throw when try to use unregistered key.
202 * @param[in] key The key what we want to get.
203 * @return registered element.
205 ElementType& operator[](const SearchKeyType& key)
207 iterator iter = Get(key);
208 DALI_ASSERT_ALWAYS(iter != End() && "operator[] doesn't allow non-exist key access");
213 * @brief Get Element by the index.
215 * @param[in] index The index what we want to get.
216 * @return index'th registered element.
217 * @note mTrieKeyElementPool emplace_back the elements ordered by Register function called.
219 const ElementType& GetElementByIndex(const std::uint32_t& index) const
221 DALI_ASSERT_ALWAYS((index < mKeyElementPool.size()) && "operator[] index >= Count()");
222 return mKeyElementPool[index].second;
226 * @brief Get Key by the index.
228 * @param[in] index The index what we want to get.
229 * @return index'th registered key.
230 * @note mTrieKeyElementPool emplace_back the elements ordered by Register function called.
232 const KeyType& GetKeyByIndex(const std::uint32_t& index) const
234 DALI_ASSERT_ALWAYS((index < mKeyElementPool.size()) && "operator[] index >= Count()");
235 return mKeyElementPool[index].first;
239 * @brief Get Key and Element by the index.
241 * @param[in] index The index what we want to get.
242 * @return index'th registered key element pair.
243 * @note mTrieKeyElementPool emplace_back the elements ordered by Register function called.
245 const KeyElementPairType& GetKeyElementPairByIndex(const std::uint32_t& index) const
247 DALI_ASSERT_ALWAYS((index < mKeyElementPool.size()) && "operator[] index >= Count()");
248 return mKeyElementPool[index];
252 * @brief Iterator to the beginning of the data.
253 * @return Iterator to the beginning of the data
257 return mKeyElementPool.begin();
260 * @brief Const Iterator to the beginning of the data.
261 * @return Const Iterator to the beginning of the data
263 const_iterator CBegin() const
265 return mKeyElementPool.cbegin();
268 * @brief Const Iterator to the beginning of the data.
269 * @return Const Iterator to the beginning of the data
271 const_iterator Begin() const
273 return mKeyElementPool.begin();
277 * @brief Iterator to the end of the data (one past last element).
278 * @return Iterator to the end of the data (one past last element)
282 return mKeyElementPool.end();
285 * @brief Const iterator to the end of the data (one past last element).
286 * @return Const iterator to the end of the data (one past last element)
288 const_iterator CEnd() const
290 return mKeyElementPool.cend();
293 * @brief Const iterator to the end of the data (one past last element).
294 * @return Const iterator to the end of the data (one past last element)
296 const_iterator End() const
298 return mKeyElementPool.end();
301 public: // API for C++11 std style functions.
303 * Support for C++11 std style function call.
312 * Support for C++11 std style function call.
314 * @return The number of elements that this container hold.
317 std::size_t size() const
323 * Support for C++11 std style function call.
325 * @return Whether elemnt is empty or not.
334 * Support for C++11 std style function call.
336 * @param[in] size Reserve size.
339 void reserve(const std::size_t size)
345 * Support for C++11 Range-based for loop: for( item : container ).
346 * @return The start iterator
353 * Support for C++11 Range-based for loop: for( item : container ).
354 * @return The start const iterator
356 const_iterator cbegin() const
362 * Support for C++11 Range-based for loop: for( item : container ).
363 * @return The start const iterator
365 const_iterator begin() const
371 * Support for C++11 Range-based for loop: for( item : container ).
372 * @return The end iterator
380 * Support for C++11 Range-based for loop: for( item : container ).
381 * @return The end const iterator
383 const_iterator cend() const
389 * Support for C++11 Range-based for loop: for( item : container ).
390 * @return The end const iterator
392 const_iterator end() const
398 std::vector<KeyElementPairType> mKeyElementPool{};
401 } // namespace Internal
405 #endif // DALI_INDEXED_MAP_BASE_H