-
Notifications
You must be signed in to change notification settings - Fork 38
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
Add constexpr_vector class #415
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Fizzy: A fast WebAssembly interpreter | ||
// Copyright 2019-2020 The Fizzy Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <utility> | ||
|
||
namespace fizzy | ||
{ | ||
/// Constexpr vector is used to represent compile time constant value lists. | ||
/// It allows to have a collection (constexpr array) of such lists, where list lengths vary across | ||
/// the collection, but each length is still known at compile time (and doesn't exceed Capacity | ||
/// limit). | ||
template <typename T, std::size_t Capacity> | ||
class constexpr_vector | ||
{ | ||
private: | ||
const T m_array[Capacity]; | ||
const std::size_t m_size; | ||
|
||
public: | ||
template <typename... U> | ||
constexpr constexpr_vector(U&&... u) noexcept | ||
: m_array{std::forward<U>(u)...}, m_size(sizeof...(U)) | ||
{} | ||
|
||
constexpr const T* data() const noexcept { return m_array; } | ||
constexpr std::size_t size() const noexcept { return m_size; } | ||
|
||
constexpr const T* begin() const noexcept { return m_array; } | ||
constexpr const T* end() const noexcept { return m_array + m_size; } | ||
}; | ||
|
||
} // namespace fizzy |
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,61 @@ | ||
// Fizzy: A fast WebAssembly interpreter | ||
// Copyright 2019-2020 The Fizzy Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "constexpr_vector.hpp" | ||
#include "span.hpp" | ||
#include <gmock/gmock.h> | ||
#include <vector> | ||
|
||
using namespace fizzy; | ||
using namespace testing; | ||
|
||
static_assert(constexpr_vector<int, 5>{1, 2, 3}.size() == 3); | ||
static_assert(*constexpr_vector<int, 5>{1, 2, 3}.data() == 1); | ||
|
||
TEST(constexpr_vector, size) | ||
{ | ||
constexpr constexpr_vector<int, 5> v1 = {}; | ||
EXPECT_EQ(v1.size(), 0); | ||
|
||
constexpr constexpr_vector<int, 5> v2 = {1, 2, 3}; | ||
EXPECT_EQ(v2.size(), 3); | ||
EXPECT_EQ(*v2.data(), 1); | ||
|
||
constexpr constexpr_vector<int, 5> v3 = {1, 2, 3, 4, 5}; | ||
EXPECT_EQ(v3.size(), 5); | ||
EXPECT_EQ(*v3.data(), 1); | ||
} | ||
|
||
TEST(constexpr_vector, iterator) | ||
{ | ||
constexpr constexpr_vector<int, 5> v = {1, 2, 3}; | ||
int expected = 1; | ||
for (const auto i : v) | ||
{ | ||
EXPECT_EQ(i, expected); | ||
++expected; | ||
} | ||
EXPECT_EQ(expected, 4); | ||
} | ||
|
||
TEST(constexpr_vector, span) | ||
{ | ||
constexpr constexpr_vector<int, 5> v = {1, 2, 3}; | ||
int expected = 1; | ||
for (const auto i : span<const int>(v)) | ||
{ | ||
EXPECT_EQ(i, expected); | ||
++expected; | ||
} | ||
EXPECT_EQ(expected, 4); | ||
} | ||
|
||
TEST(constexpr_vector, array) | ||
{ | ||
constexpr constexpr_vector<int, 5> arr[] = {{1, 2, 3}, {4, 5, 6, 7, 8}, {9}, {}}; | ||
EXPECT_THAT(std::vector<int>(arr[0].begin(), arr[0].end()), ElementsAre(1, 2, 3)); | ||
EXPECT_THAT(std::vector<int>(arr[1].begin(), arr[1].end()), ElementsAre(4, 5, 6, 7, 8)); | ||
EXPECT_THAT(std::vector<int>(arr[2].begin(), arr[2].end()), ElementsAre(9)); | ||
EXPECT_THAT(std::vector<int>(arr[3].begin(), arr[3].end()), IsEmpty()); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be also named
static_vector
as in https://github.com/gnzlbg/static_vector#3-existing-practice.Also found a precedence of
constexpr_vector
: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0597r0.html