5 * Copyright (c) 2018 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.
22 #include <cstdint> // uint32_t
25 #include <dali/public-api/common/dali-vector.h>
34 * FreeList operates by connecting unused elements of a vector together in a linked list using the
35 * value of each unused cell as a pointer to the next. When a new element is added, it will be added
36 * to the first free index of the vector and the new free index will be the value which was on that
52 ~FreeList() = default;
55 * Adds a new item to the list. If there is no more space in the vector it will
56 * allocate more space, otherwise, it will use the first free cell to store the
57 * new value and will update the first free index.
59 * @param[in] value The value to add
60 * @return The index where the value has been added
62 uint32_t Add( uint32_t value )
64 const uint32_t size = static_cast<uint32_t>( mData.Size() ); // 4,294,967,295 entries is enough
65 if( mData.Empty() || mFirstFreeIndex == size )
67 //Make room for another item
68 mData.PushBack( size + 1 );
69 mFirstFreeIndex = size;
72 //Update first free index
73 uint32_t index = mFirstFreeIndex;
74 mFirstFreeIndex = mData[mFirstFreeIndex];
81 * Removes the item at position "index" from the list and
82 * updates the first free index
84 * @param[in] index The index of the element to remove
86 void Remove( uint32_t index )
88 mData[index] = mFirstFreeIndex;
89 mFirstFreeIndex = index;
95 * @param[in] index Index of the element.
96 * @return Reference to the element for given index.
98 uint32_t& operator[]( uint32_t index )
104 * Subscript operator (const).
106 * @param[in] index Index of the element.
107 * @return Reference to the element for given index.
109 uint32_t operator[]( uint32_t index ) const
115 Dali::Vector< uint32_t > mData; ///< data
116 uint32_t mFirstFreeIndex; ///< Index where a new element will be added
122 #endif /* FREE_LIST_H_ */