Skip to content

Commit f019165

Browse files
committed
Fix type of tombstone_traits::index. Fix tombstone_traits implementation for optional<T>
1 parent 7a71c2e commit f019165

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

source/bounded/integer_tombstone_traits.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ struct tombstone_traits<T> {
4848
}
4949
static constexpr auto index(T const & value) noexcept {
5050
auto const bounded_value = integer(value.m_value);
51-
return
51+
auto const result =
5252
BOUNDED_CONDITIONAL(bounded_value < numeric_traits::min_value<T>, bounded_value - underlying_min,
5353
BOUNDED_CONDITIONAL(bounded_value > numeric_traits::max_value<T>, bounded_value + spare_below - numeric_traits::max_value<T> - constant<1>,
5454
constant<-1>
5555
));
56+
return bounded::assume_in_range(result, constant<-1>, spare_representations - constant<1>);
5657
}
5758
};
5859

source/bounded/tombstone_traits.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
export module bounded.tombstone_traits;
77

8+
import bounded.arithmetic.operators;
89
import bounded.integer;
10+
import bounded.normalize;
911

1012
namespace bounded {
1113

@@ -35,9 +37,15 @@ struct tombstone_traits_composer<pointer> {
3537
static constexpr auto spare_representations = tombstone_traits<Inner>::spare_representations;
3638

3739
static constexpr auto make(auto const index) noexcept -> Outer {
38-
return Outer(tombstone_tag(), [=]() noexcept { return tombstone_traits<Inner>::make(index); });
40+
return Outer(
41+
tombstone_tag(),
42+
[=] noexcept { return tombstone_traits<Inner>::make(index); }
43+
);
3944
}
40-
static constexpr auto index(Outer const & value) noexcept {
45+
static constexpr auto index(Outer const & value) noexcept -> bounded::integer<
46+
-1,
47+
bounded::normalize<spare_representations - bounded::constant<1>>
48+
> {
4149
return tombstone_traits<Inner>::index(value.*pointer);
4250
}
4351
};

source/tv/optional.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,17 @@ struct optional_storage<T> {
127127
m_data(bounded::tombstone_traits<T>::make(index + 1_bi))
128128
{
129129
}
130-
constexpr auto tombstone_index() const noexcept {
131-
return bounded::tombstone_traits<T>::index(m_data) - 1_bi;
130+
using tombstone_index_t = bounded::integer<
131+
-1,
132+
bounded::normalize<spare_representations - 1_bi>
133+
>;
134+
constexpr auto tombstone_index() const noexcept -> tombstone_index_t {
135+
auto const underlying_index = bounded::tombstone_traits<T>::index(m_data);
136+
return bounded::assume_in_range<tombstone_index_t>(
137+
underlying_index == -1_bi ?
138+
-1_bi :
139+
underlying_index - 1_bi,
140+
);
132141
}
133142

134143
private:

source/tv/test/optional.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ struct bounded::tombstone_traits<two_spare_representations> {
250250
static constexpr auto make(auto const index) noexcept -> two_spare_representations {
251251
return two_spare_representations{index == 0_bi ? 12 : 19};
252252
}
253-
static constexpr auto index(two_spare_representations const & value) noexcept {
253+
static constexpr auto index(two_spare_representations const & value) noexcept -> bounded::integer<-1, 1> {
254254
return
255255
BOUNDED_CONDITIONAL(value.x == 12, 0_bi,
256256
BOUNDED_CONDITIONAL(value.x == 19, 1_bi,

0 commit comments

Comments
 (0)