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 Get element by the key.
105 * @param[in] key The key that this container will hold.
106 * @return If exist, iterator of container. Otherwise, return End().
108 virtual iterator Get(const SearchKeyType& key) = 0;
111 * @brief Get element by the key.
113 * @param[in] key The key that this container will hold.
114 * @return If exist, iterator of container. Otherwise, return End().
116 virtual const_iterator Get(const SearchKeyType& key) const = 0;
120 * @brief Constructor.
130 virtual ~IndexedMapBase()
132 mKeyElementPool.clear();
136 * @brief Clear this container.
140 mKeyElementPool.clear();
144 * @brief Get the number of elements that this container hold.
146 * @return The number of elements that this container hold.
148 std::size_t Count() const
150 return mKeyElementPool.size();
154 * @brief Check whether container is empty or not.
156 * @return Whether elemnt is empty or not.
160 return mKeyElementPool.empty();
164 * @brief Reserve KeyElementPool container size.
165 * Reserve only if current container size is smaller than input size.
167 * @param[in] size Reserve size.
169 void Reserve(const std::size_t& size)
171 if(mKeyElementPool.size() < size)
173 mKeyElementPool.reserve(size);
178 * @brief Get Element by the key.
179 * @note Assert throw when try to use unregistered key.
181 * @param[in] key The key what we want to get.
182 * @return registered element.
184 const ElementType& operator[](const SearchKeyType& key) const
186 const_iterator iter = Get(key);
187 DALI_ASSERT_ALWAYS(iter != End() && "const operator[] doesn't allow non-exist key access");
192 * @brief Get Element by the key.
193 * @note Assert throw when try to use unregistered key.
195 * @param[in] key The key what we want to get.
196 * @return registered element.
198 ElementType& operator[](const SearchKeyType& key)
200 iterator iter = Get(key);
201 DALI_ASSERT_ALWAYS(iter != End() && "operator[] doesn't allow non-exist key access");
206 * @brief Get Element by the index.
208 * @param[in] index The index what we want to get.
209 * @return index'th registered element.
210 * @note mTrieKeyElementPool emplace_back the elements ordered by Register function called.
212 const ElementType& GetElementByIndex(const std::uint32_t& index) const
214 DALI_ASSERT_ALWAYS((index < mKeyElementPool.size()) && "operator[] index >= Count()");
215 return mKeyElementPool[index].second;
219 * @brief Get Key by the index.
221 * @param[in] index The index what we want to get.
222 * @return index'th registered key.
223 * @note mTrieKeyElementPool emplace_back the elements ordered by Register function called.
225 const KeyType& GetKeyByIndex(const std::uint32_t& index) const
227 DALI_ASSERT_ALWAYS((index < mKeyElementPool.size()) && "operator[] index >= Count()");
228 return mKeyElementPool[index].first;
232 * @brief Get Key and Element by the index.
234 * @param[in] index The index what we want to get.
235 * @return index'th registered key element pair.
236 * @note mTrieKeyElementPool emplace_back the elements ordered by Register function called.
238 const KeyElementPairType& GetKeyElementPairByIndex(const std::uint32_t& index) const
240 DALI_ASSERT_ALWAYS((index < mKeyElementPool.size()) && "operator[] index >= Count()");
241 return mKeyElementPool[index];
245 * @brief Iterator to the beginning of the data.
246 * @return Iterator to the beginning of the data
250 return mKeyElementPool.begin();
253 * @brief Const Iterator to the beginning of the data.
254 * @return Const Iterator to the beginning of the data
256 const_iterator CBegin() const
258 return mKeyElementPool.cbegin();
261 * @brief Const Iterator to the beginning of the data.
262 * @return Const Iterator to the beginning of the data
264 const_iterator Begin() const
266 return mKeyElementPool.begin();
270 * @brief Iterator to the end of the data (one past last element).
271 * @return Iterator to the end of the data (one past last element)
275 return mKeyElementPool.end();
278 * @brief Const iterator to the end of the data (one past last element).
279 * @return Const iterator to the end of the data (one past last element)
281 const_iterator CEnd() const
283 return mKeyElementPool.cend();
286 * @brief Const iterator to the end of the data (one past last element).
287 * @return Const iterator to the end of the data (one past last element)
289 const_iterator End() const
291 return mKeyElementPool.end();
294 public: // API for C++11 std style functions.
296 * Support for C++11 std style function call.
305 * Support for C++11 std style function call.
307 * @return The number of elements that this container hold.
310 std::size_t size() const
316 * Support for C++11 std style function call.
318 * @return Whether elemnt is empty or not.
327 * Support for C++11 std style function call.
329 * @param[in] size Reserve size.
332 void reserve(const std::size_t size)
338 * Support for C++11 Range-based for loop: for( item : container ).
339 * @return The start iterator
346 * Support for C++11 Range-based for loop: for( item : container ).
347 * @return The start const iterator
349 const_iterator cbegin() const
355 * Support for C++11 Range-based for loop: for( item : container ).
356 * @return The start const iterator
358 const_iterator begin() const
364 * Support for C++11 Range-based for loop: for( item : container ).
365 * @return The end iterator
373 * Support for C++11 Range-based for loop: for( item : container ).
374 * @return The end const iterator
376 const_iterator cend() const
382 * Support for C++11 Range-based for loop: for( item : container ).
383 * @return The end const iterator
385 const_iterator end() const
391 std::vector<KeyElementPairType> mKeyElementPool;
394 } // namespace Internal
398 #endif // DALI_INDEXED_MAP_BASE_H