Path: blob/main/contrib/llvm-project/libc/src/__support/fixedvector.h
213766 views
//===-- A data structure for a fixed capacity data store --------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_SRC___SUPPORT_FIXEDVECTOR_H9#define LLVM_LIBC_SRC___SUPPORT_FIXEDVECTOR_H1011#include "src/__support/CPP/array.h"12#include "src/__support/CPP/iterator.h"13#include "src/__support/libc_assert.h"14#include "src/__support/macros/config.h"15#include "src/string/memory_utils/inline_memset.h"1617namespace LIBC_NAMESPACE_DECL {1819// A fixed size data store backed by an underlying cpp::array data structure. It20// supports vector like API but is not resizable like a vector.21template <typename T, size_t CAPACITY> class FixedVector {22cpp::array<T, CAPACITY> store;23size_t item_count = 0;2425public:26LIBC_INLINE constexpr FixedVector() = default;2728using iterator = typename cpp::array<T, CAPACITY>::iterator;29LIBC_INLINE constexpr FixedVector(iterator begin, iterator end)30: store{}, item_count{} {31LIBC_ASSERT(begin + CAPACITY >= end);32for (; begin != end; ++begin)33push_back(*begin);34}3536using const_iterator = typename cpp::array<T, CAPACITY>::const_iterator;37LIBC_INLINE constexpr FixedVector(const_iterator begin, const_iterator end)38: store{}, item_count{} {39LIBC_ASSERT(begin + CAPACITY >= end);40for (; begin != end; ++begin)41push_back(*begin);42}4344LIBC_INLINE constexpr FixedVector(size_t count, const T &value)45: store{}, item_count{} {46LIBC_ASSERT(count <= CAPACITY);47for (size_t i = 0; i < count; ++i)48push_back(value);49}5051LIBC_INLINE constexpr bool push_back(const T &obj) {52if (item_count == CAPACITY)53return false;54store[item_count] = obj;55++item_count;56return true;57}5859LIBC_INLINE constexpr const T &back() const {60LIBC_ASSERT(!empty());61return store[item_count - 1];62}6364LIBC_INLINE constexpr T &back() {65LIBC_ASSERT(!empty());66return store[item_count - 1];67}6869LIBC_INLINE constexpr bool pop_back() {70if (item_count == 0)71return false;72inline_memset(&store[item_count - 1], 0, sizeof(T));73--item_count;74return true;75}7677LIBC_INLINE constexpr T &operator[](size_t idx) {78LIBC_ASSERT(idx < item_count);79return store[idx];80}8182LIBC_INLINE constexpr const T &operator[](size_t idx) const {83LIBC_ASSERT(idx < item_count);84return store[idx];85}8687LIBC_INLINE constexpr bool empty() const { return item_count == 0; }8889LIBC_INLINE constexpr size_t size() const { return item_count; }9091// Empties the store for all practical purposes.92LIBC_INLINE constexpr void reset() {93inline_memset(store.data(), 0, sizeof(T) * item_count);94item_count = 0;95}9697// This static method does not free up the resources held by |store|,98// say by calling `free` or something similar. It just does the equivalent99// of the `reset` method. Considering that FixedVector is of fixed storage,100// a `destroy` method like this should not be required. However, FixedVector101// is used in a few places as an alternate for data structures which use102// dynamically allocated storate. So, the `destroy` method like this103// matches the `destroy` API of those other data structures so that users104// can easily swap one data structure for the other.105LIBC_INLINE static void destroy(FixedVector<T, CAPACITY> *store) {106store->reset();107}108109using reverse_iterator = typename cpp::array<T, CAPACITY>::reverse_iterator;110LIBC_INLINE constexpr reverse_iterator rbegin() {111return reverse_iterator{&store[item_count]};112}113LIBC_INLINE constexpr reverse_iterator rend() { return store.rend(); }114115LIBC_INLINE constexpr iterator begin() { return store.begin(); }116LIBC_INLINE constexpr iterator end() { return iterator{&store[item_count]}; }117118LIBC_INLINE constexpr const_iterator begin() const { return store.begin(); }119LIBC_INLINE constexpr const_iterator end() const {120return const_iterator{&store[item_count]};121}122};123124} // namespace LIBC_NAMESPACE_DECL125126#endif // LLVM_LIBC_SRC___SUPPORT_FIXEDVECTOR_H127128129