Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12126 from JosJuice/small-vector
Move SmallVector to Common
- Loading branch information
Showing
5 changed files
with
55 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// SPDX-License-Identifier: CC0-1.0 | ||
|
||
#pragma once | ||
|
||
#include <array> | ||
#include <cstddef> | ||
#include <utility> | ||
|
||
namespace Common | ||
{ | ||
|
||
// An std::vector-like container that uses no heap allocations but is limited to a maximum size. | ||
template <typename T, size_t MaxSize> | ||
class SmallVector final | ||
{ | ||
public: | ||
SmallVector() = default; | ||
explicit SmallVector(size_t size) : m_size(size) {} | ||
|
||
void push_back(const T& x) { m_array[m_size++] = x; } | ||
void push_back(T&& x) { m_array[m_size++] = std::move(x); } | ||
|
||
template <typename... Args> | ||
T& emplace_back(Args&&... args) | ||
{ | ||
return m_array[m_size++] = T{std::forward<Args>(args)...}; | ||
} | ||
|
||
T& operator[](size_t i) { return m_array[i]; } | ||
const T& operator[](size_t i) const { return m_array[i]; } | ||
|
||
auto begin() { return m_array.begin(); } | ||
auto end() { return m_array.begin() + m_size; } | ||
|
||
auto begin() const { return m_array.begin(); } | ||
auto end() const { return m_array.begin() + m_size; } | ||
|
||
size_t size() const { return m_size; } | ||
bool empty() const { return m_size == 0; } | ||
|
||
private: | ||
std::array<T, MaxSize> m_array{}; | ||
size_t m_size = 0; | ||
}; | ||
|
||
} // namespace Common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters