Skip to content

[libc][__support] Add constexpr to FixedVector members #95315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions libc/src/__support/fixedvector.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,45 @@ template <typename T, size_t CAPACITY> class FixedVector {
constexpr FixedVector() = default;

using iterator = typename cpp::array<T, CAPACITY>::iterator;
constexpr FixedVector(iterator begin, iterator end) {
constexpr FixedVector(iterator begin, iterator end) : store{}, item_count{} {
for (; begin != end; ++begin)
push_back(*begin);
}

constexpr FixedVector(size_t count, const T &value) {
constexpr FixedVector(size_t count, const T &value) : store{}, item_count{} {
for (size_t i = 0; i < count; ++i)
push_back(value);
}

bool push_back(const T &obj) {
constexpr bool push_back(const T &obj) {
if (item_count == CAPACITY)
return false;
store[item_count] = obj;
++item_count;
return true;
}

const T &back() const { return store[item_count - 1]; }
constexpr const T &back() const { return store[item_count - 1]; }

T &back() { return store[item_count - 1]; }
constexpr T &back() { return store[item_count - 1]; }

bool pop_back() {
constexpr bool pop_back() {
if (item_count == 0)
return false;
--item_count;
return true;
}

T &operator[](size_t idx) { return store[idx]; }
constexpr T &operator[](size_t idx) { return store[idx]; }

const T &operator[](size_t idx) const { return store[idx]; }
constexpr const T &operator[](size_t idx) const { return store[idx]; }

bool empty() const { return item_count == 0; }
constexpr bool empty() const { return item_count == 0; }

size_t size() const { return item_count; }
constexpr size_t size() const { return item_count; }

// Empties the store for all practical purposes.
void reset() { item_count = 0; }
constexpr void reset() { item_count = 0; }

// This static method does not free up the resources held by |store|,
// say by calling `free` or something similar. It just does the equivalent
Expand Down
Loading