Skip to content

Commit a906160

Browse files
kainino0xDawn LUCI CQ
authored andcommitted
[dawn] Rewrite ityp::span on top of std::span
Allows removing the DAWN_CHECK which is now taken care of by libc++. Note that begin() and end() now return iterators instead of pointers. This doesn't affect anything except the tests. Also minor reformat of other ityp classes for consistency. Bug: none Change-Id: I070cbb7c03c460fb5cd697208a0cf2f69a4889d3 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/260380 Auto-Submit: Kai Ninomiya <[email protected]> Reviewed-by: Brandon Jones <[email protected]> Commit-Queue: Kai Ninomiya <[email protected]>
1 parent f7e0d60 commit a906160

File tree

4 files changed

+28
-77
lines changed

4 files changed

+28
-77
lines changed

src/dawn/common/ityp_array.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,24 @@ class array : private ::std::array<Value, Size> {
5757
constexpr array(Values&&... values) : Base{std::forward<Values>(values)...} {}
5858

5959
constexpr Value& operator[](Index i) { return Base::operator[](static_cast<I>(i)); }
60-
6160
constexpr const Value& operator[](Index i) const { return Base::operator[](static_cast<I>(i)); }
6261

6362
Value& at(Index i) { return Base::at(static_cast<I>(i)); }
64-
6563
constexpr const Value& at(Index i) const { return Base::at(static_cast<I>(i)); }
6664

6765
typename Base::iterator begin() noexcept { return Base::begin(); }
68-
6966
typename Base::const_iterator begin() const noexcept { return Base::begin(); }
7067

7168
typename Base::iterator end() noexcept { return Base::end(); }
72-
7369
typename Base::const_iterator end() const noexcept { return Base::end(); }
7470

7571
constexpr Index size() const { return Index(I(Size)); }
7672

77-
using Base::back;
7873
using Base::data;
7974
using Base::empty;
8075
using Base::fill;
76+
77+
using Base::back;
8178
using Base::front;
8279
};
8380

src/dawn/common/ityp_span.h

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,69 +28,39 @@
2828
#ifndef SRC_DAWN_COMMON_ITYP_SPAN_H_
2929
#define SRC_DAWN_COMMON_ITYP_SPAN_H_
3030

31+
#include <limits>
32+
#include <span>
33+
3134
#include "dawn/common/TypedInteger.h"
3235
#include "dawn/common/UnderlyingType.h"
3336

3437
namespace dawn::ityp {
3538

36-
// ityp::span is a helper class that wraps an unowned packed array of type |Value|.
37-
// It stores the size and pointer to first element. It has the restriction that
38-
// indices must be a particular type |Index|. This provides a type-safe way to index
39-
// raw pointers.
39+
// ityp::span is a helper class that wraps std::span<T, std::dynamic_extent>
40+
// with the restriction that indices must be a particular type |Index|.
4041
template <typename Index, typename Value>
41-
class span {
42+
class span : private ::std::span<Value> {
4243
using I = UnderlyingType<Index>;
44+
using Base = ::std::span<Value>;
4345

4446
public:
45-
constexpr span() : mData(nullptr), mSize(0) {}
46-
constexpr span(Value* data, Index size) : mData(data), mSize(size) {}
47-
48-
constexpr Value& operator[](Index i) const {
49-
DAWN_CHECK(i < mSize);
50-
return mData[static_cast<I>(i)];
51-
}
52-
53-
Value* data() noexcept { return mData; }
54-
55-
const Value* data() const noexcept { return mData; }
56-
57-
Value* begin() noexcept { return mData; }
58-
59-
const Value* begin() const noexcept { return mData; }
47+
constexpr span() = default;
48+
constexpr span(Value* data, Index size) : Base{data, static_cast<I>(size)} {}
6049

61-
Value* end() noexcept { return mData + static_cast<I>(mSize); }
50+
constexpr Value& operator[](Index i) const { return Base::operator[](static_cast<I>(i)); }
6251

63-
const Value* end() const noexcept { return mData + static_cast<I>(mSize); }
64-
65-
Value& front() {
66-
DAWN_ASSERT(mData != nullptr);
67-
DAWN_ASSERT(static_cast<I>(mSize) >= 0);
68-
return *mData;
69-
}
70-
71-
const Value& front() const {
72-
DAWN_ASSERT(mData != nullptr);
73-
DAWN_ASSERT(static_cast<I>(mSize) >= 0);
74-
return *mData;
52+
constexpr Index size() const {
53+
DAWN_ASSERT(std::numeric_limits<I>::max() >= Base::size());
54+
return Index(static_cast<I>(Base::size()));
7555
}
7656

77-
Value& back() {
78-
DAWN_ASSERT(mData != nullptr);
79-
DAWN_ASSERT(static_cast<I>(mSize) >= 0);
80-
return *(mData + static_cast<I>(mSize) - 1);
81-
}
82-
83-
const Value& back() const {
84-
DAWN_ASSERT(mData != nullptr);
85-
DAWN_ASSERT(static_cast<I>(mSize) >= 0);
86-
return *(mData + static_cast<I>(mSize) - 1);
87-
}
57+
using Base::data;
8858

89-
Index size() const { return mSize; }
59+
using Base::begin;
60+
using Base::end;
9061

91-
private:
92-
Value* mData;
93-
Index mSize;
62+
using Base::back;
63+
using Base::front;
9464
};
9565

9666
// ityp::SpanFromUntyped<Index>(myValues, myValueCount) creates a span<Index, Value> from a C-style

src/dawn/common/ityp_vector.h

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,42 +55,26 @@ class vector : public std::vector<Value> {
5555

5656
public:
5757
vector() : Base() {}
58-
5958
explicit vector(Index size) : Base(static_cast<I>(size)) {}
60-
6159
vector(Index size, const Value& init) : Base(static_cast<I>(size), init) {}
62-
6360
vector(const vector& rhs) : Base(static_cast<const Base&>(rhs)) {}
64-
6561
vector(vector&& rhs) : Base(static_cast<Base&&>(rhs)) {}
66-
6762
vector(std::initializer_list<Value> init) : Base(init) {}
6863

6964
vector& operator=(const vector& rhs) {
7065
Base::operator=(static_cast<const Base&>(rhs));
7166
return *this;
7267
}
73-
7468
vector& operator=(vector&& rhs) noexcept {
7569
Base::operator=(static_cast<Base&&>(rhs));
7670
return *this;
7771
}
7872

79-
Value& operator[](Index i) {
80-
return Base::operator[](static_cast<I>(i));
81-
}
82-
83-
constexpr const Value& operator[](Index i) const {
84-
return Base::operator[](static_cast<I>(i));
85-
}
73+
Value& operator[](Index i) { return Base::operator[](static_cast<I>(i)); }
74+
constexpr const Value& operator[](Index i) const { return Base::operator[](static_cast<I>(i)); }
8675

87-
Value& at(Index i) {
88-
return Base::at(static_cast<I>(i));
89-
}
90-
91-
constexpr const Value& at(Index i) const {
92-
return Base::at(static_cast<I>(i));
93-
}
76+
Value& at(Index i) { return Base::at(static_cast<I>(i)); }
77+
constexpr const Value& at(Index i) const { return Base::at(static_cast<I>(i)); }
9478

9579
constexpr Index size() const {
9680
DAWN_ASSERT(std::numeric_limits<I>::max() >= Base::size());

src/dawn/tests/unittests/ITypSpanTests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,16 @@ TEST_F(ITypSpanTest, BeginEndFrontBackData) {
8181
Span span(arr.data(), Key(arr.size()));
8282

8383
// non-const versions
84-
ASSERT_EQ(span.begin(), &span[Key(0)]);
85-
ASSERT_EQ(span.end(), &span[Key(0)] + static_cast<size_t>(span.size()));
84+
ASSERT_EQ(&*span.begin(), &span[Key(0)]);
85+
ASSERT_EQ(&*span.end(), &span[Key(0)] + static_cast<size_t>(span.size()));
8686
ASSERT_EQ(&span.front(), &span[Key(0)]);
8787
ASSERT_EQ(&span.back(), &span[Key(9)]);
8888
ASSERT_EQ(span.data(), &span[Key(0)]);
8989

9090
// const versions
9191
const Span& constSpan = span;
92-
ASSERT_EQ(constSpan.begin(), &constSpan[Key(0)]);
93-
ASSERT_EQ(constSpan.end(), &constSpan[Key(0)] + static_cast<size_t>(constSpan.size()));
92+
ASSERT_EQ(&*constSpan.begin(), &constSpan[Key(0)]);
93+
ASSERT_EQ(&*constSpan.end(), &constSpan[Key(0)] + static_cast<size_t>(constSpan.size()));
9494
ASSERT_EQ(&constSpan.front(), &constSpan[Key(0)]);
9595
ASSERT_EQ(&constSpan.back(), &constSpan[Key(9)]);
9696
ASSERT_EQ(constSpan.data(), &constSpan[Key(0)]);

0 commit comments

Comments
 (0)