@@ -1030,6 +1030,12 @@ NDARRAY_HOST_DEVICE const DimsSrc& assert_dims_compatible(const DimsSrc& src) {
1030
1030
return src;
1031
1031
}
1032
1032
1033
+ /* * Return a tuple of generic `dims` with same min and extents and all strides set to `unresolved`. */
1034
+ template <class Dims , size_t ... Is>
1035
+ auto bounds_tuple (const Dims& dims, index_sequence<Is...>) {
1036
+ return std::make_tuple (dim<>(std::get<Is>(dims).min (), std::get<Is>(dims).extent (), unresolved)...);
1037
+ }
1038
+
1033
1039
} // namespace internal
1034
1040
1035
1041
template <class ... Dims>
@@ -1293,6 +1299,13 @@ class shape {
1293
1299
NDARRAY_HOST_DEVICE index_t rows () const { return i ().extent (); }
1294
1300
NDARRAY_HOST_DEVICE index_t columns () const { return j ().extent (); }
1295
1301
1302
+ /* * Returns a shape with dynamic dims, with the same min and extents but
1303
+ * strides initialized to `nda::unresolved`. This can be used to create
1304
+ * an array with the same dimensions but different compile-time constraints. */
1305
+ NDARRAY_HOST_DEVICE auto bounds () const {
1306
+ return make_shape_from_tuple (internal::bounds_tuple (dims_, dim_indices ()));
1307
+ }
1308
+
1296
1309
/* * A shape is equal to another shape if the dim objects of each
1297
1310
* dimension from both shapes are equal. */
1298
1311
template <class ... OtherDims, class = enable_if_same_rank<OtherDims...>>
@@ -2115,6 +2128,15 @@ class array_ref {
2115
2128
}
2116
2129
const nda::dim<> dim (size_t d) const { return shape_.dim (d); }
2117
2130
NDARRAY_HOST_DEVICE size_type size () const { return shape_.size (); }
2131
+ /* * Returns a shape with dynamic dims, with the same min and extents but
2132
+ * strides initialized to `nda::unresolved`. This can be used to create
2133
+ * an array with the same dimensions but different compile-time constraints:
2134
+ *
2135
+ * nda::array_ref<T, SrcShape> ref(...);
2136
+ * nda::array<T, DstShape> y(ref.bounds()); // Compact array with the same `min`
2137
+ * // and `extents` as `ref`.
2138
+ */
2139
+ NDARRAY_HOST_DEVICE auto bounds () const { return shape_.bounds (); }
2118
2140
NDARRAY_HOST_DEVICE bool empty () const { return base () != nullptr ? shape_.empty () : true ; }
2119
2141
NDARRAY_HOST_DEVICE bool is_compact () const { return shape_.is_compact (); }
2120
2142
@@ -2577,6 +2599,15 @@ class array {
2577
2599
}
2578
2600
const nda::dim<> dim (size_t d) const { return shape_.dim (d); }
2579
2601
size_type size () const { return shape_.size (); }
2602
+ /* * Returns a shape with dynamic dims, with the same min and extents but
2603
+ * strides initialized to `nda::unresolved`. This can be used to create
2604
+ * an array with the same dimensions but different compile-time constraints:
2605
+ *
2606
+ * nda::array<T, SrcShape> other(...);
2607
+ * nda::array<T, DstShape> y(other.bounds()); // Compact array with the same `min`
2608
+ * // and `extents` as `other`.
2609
+ */
2610
+ auto bounds () const { return shape_.bounds (); }
2580
2611
bool empty () const { return shape_.empty (); }
2581
2612
bool is_compact () const { return shape_.is_compact (); }
2582
2613
0 commit comments