array
C++ library for multi-dimensional arrays
absl.h
1 #ifndef NDARRAY_ABSL_ABSL_H
2 #define NDARRAY_ABSL_ABSL_H
3 
4 #include "absl/strings/str_format.h"
5 #include "absl/strings/str_join.h"
6 #include "array/array.h"
7 
8 // Adds Abseil Stringify support (https://abseil.io/blog/20221115-stringify).
9 
10 namespace nda {
11 
12 // interval -> string as the half-open interval [begin, end).
13 //
14 // Stringifies only the values, not whether they are static or dynamic.
15 template <typename Sink, index_t Min, index_t Extent>
16 void AbslStringify(Sink& sink, const interval<Min, Extent>& i) {
17  absl::Format(&sink, "[%v, %v)", i.min(), i.min() + i.extent());
18 }
19 
20 // dim -> string as "dim(min, extent, stride)".
21 //
22 // Stringifies only the values, not whether they are static or dynamic.
23 template <typename Sink, index_t Min, index_t Extent, index_t Stride>
24 void AbslStringify(Sink& sink, const dim<Min, Extent, Stride>& d) {
25  if (internal::is_resolved(d.stride())) {
26  absl::Format(&sink, "dim(%v, %v, %v)", d.min(), d.extent(), d.stride());
27  } else {
28  absl::Format(&sink, "dim(%v, %v)", d.min(), d.extent());
29  }
30 }
31 
32 // shape -> string as "shape<`rank`>(dims...).
33 //
34 // Stringifies only the rank and each dim's values, not whether they are static
35 // or dynamic.
36 template <typename Sink, class... Dims>
37 void AbslStringify(Sink& sink, const shape<Dims...>& sh) {
38  absl::Format(&sink, "shape<%d>(%v)", sh.rank(), absl::StrJoin(sh.dims(), ", "));
39 }
40 
41 } // namespace nda
42 
43 #endif // NDARRAY_ABSL_ABSL_H
Main header for array library.
Definition: absl.h:10