Start work on span

This commit is contained in:
2024-11-04 13:19:11 +01:00
parent 6726a96f0c
commit 5682cb422c
4 changed files with 81 additions and 7 deletions

14
MODULE.bazel.lock generated
View File

@ -64,20 +64,20 @@
"@@apple_support~//crosstool:setup.bzl%apple_cc_configure_extension": { "@@apple_support~//crosstool:setup.bzl%apple_cc_configure_extension": {
"general": { "general": {
"bzlTransitiveDigest": "PjIds3feoYE8SGbbIq2SFTZy3zmxeO2tQevJZNDo7iY=", "bzlTransitiveDigest": "PjIds3feoYE8SGbbIq2SFTZy3zmxeO2tQevJZNDo7iY=",
"usagesDigest": "+hz7IHWN6A1oVJJWNDB6yZRG+RYhF76wAYItpAeIUIg=", "usagesDigest": "aLmqbvowmHkkBPve05yyDNGN7oh7QE9kBADr3QIZTZs=",
"recordedFileInputs": {}, "recordedFileInputs": {},
"recordedDirentsInputs": {}, "recordedDirentsInputs": {},
"envVariables": {}, "envVariables": {},
"generatedRepoSpecs": { "generatedRepoSpecs": {
"local_config_apple_cc_toolchains": {
"bzlFile": "@@apple_support~//crosstool:setup.bzl",
"ruleClassName": "_apple_cc_autoconf_toolchains",
"attributes": {}
},
"local_config_apple_cc": { "local_config_apple_cc": {
"bzlFile": "@@apple_support~//crosstool:setup.bzl", "bzlFile": "@@apple_support~//crosstool:setup.bzl",
"ruleClassName": "_apple_cc_autoconf", "ruleClassName": "_apple_cc_autoconf",
"attributes": {} "attributes": {}
},
"local_config_apple_cc_toolchains": {
"bzlFile": "@@apple_support~//crosstool:setup.bzl",
"ruleClassName": "_apple_cc_autoconf_toolchains",
"attributes": {}
} }
}, },
"recordedRepoMappingEntries": [ "recordedRepoMappingEntries": [
@ -92,7 +92,7 @@
"@@platforms//host:extension.bzl%host_platform": { "@@platforms//host:extension.bzl%host_platform": {
"general": { "general": {
"bzlTransitiveDigest": "xelQcPZH8+tmuOHVjL9vDxMnnQNMlwj0SlvgoqBkm4U=", "bzlTransitiveDigest": "xelQcPZH8+tmuOHVjL9vDxMnnQNMlwj0SlvgoqBkm4U=",
"usagesDigest": "pCYpDQmqMbmiiPI1p2Kd3VLm5T48rRAht5WdW0X2GlA=", "usagesDigest": "meSzxn3DUCcYEhq4HQwExWkWtU4EjriRBQLsZN+Q0SU=",
"recordedFileInputs": {}, "recordedFileInputs": {},
"recordedDirentsInputs": {}, "recordedDirentsInputs": {},
"envVariables": {}, "envVariables": {},

View File

@ -14,6 +14,7 @@ cc_library(
"meta.hpp", "meta.hpp",
"option.hpp", "option.hpp",
"print.hpp", "print.hpp",
"span.hpp",
"utility.hpp", "utility.hpp",
], ],
srcs = [ srcs = [
@ -40,5 +41,6 @@ cc_library(
"maybe_uninit", "maybe_uninit",
"meta", "meta",
"option", "option",
"span",
"utility", "utility",
]] ]]

44
asl/span.hpp Normal file
View File

@ -0,0 +1,44 @@
#pragma once
#include "asl/meta.hpp"
#include "asl/annotations.hpp"
#include "asl/layout.hpp"
namespace asl
{
static constexpr int64_t dynamic_size = -1;
template<is_object T, int64_t kSize = dynamic_size>
class span
{
static constexpr bool kIsDynamic = kSize < 0;
using SizeType = select_t<kIsDynamic, int64_t, empty>;
T* m_data{};
ASL_NO_UNIQUE_ADDRESS SizeType m_size{};
public:
constexpr span() = default;
constexpr span(const span&) = default;
constexpr span(span&&) = default;
constexpr span& operator=(const span&) = default;
constexpr span& operator=(span&&) = default;
~span() = default;
constexpr int64_t size() const
{
if constexpr (kIsDynamic) { return m_size; }
else { return kSize; }
}
constexpr int64_t size_bytes() const { return size() * size_of<T>; }
constexpr bool is_empty() const { return size() == 0; }
};
} // namespace asl

28
asl/tests/span_tests.cpp Normal file
View File

@ -0,0 +1,28 @@
#include "asl/span.hpp"
#include "asl/testing/testing.hpp"
#include "asl/tests/test_types.hpp"
static_assert(asl::trivially_destructible<asl::span<int>>);
static_assert(asl::trivially_destructible<asl::span<HasDestructor>>);
static_assert(asl::trivially_copyable<asl::span<int>>);
static_assert(asl::trivially_copyable<asl::span<NonCopyConstructible>>);
static_assert(asl::size_of<asl::span<int>> == asl::size_of<void*> * 2);
static_assert(asl::size_of<asl::span<int, 2>> == asl::size_of<void*>);
ASL_TEST(empty_dynamic)
{
asl::span<int> s;
ASL_TEST_EXPECT(s.size() == 0);
ASL_TEST_EXPECT(s.size_bytes() == 0);
ASL_TEST_EXPECT(s.is_empty());
}
ASL_TEST(empty_static)
{
asl::span<int, 0> s;
ASL_TEST_EXPECT(s.size() == 0);
ASL_TEST_EXPECT(s.size_bytes() == 0);
ASL_TEST_EXPECT(s.is_empty());
}