-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.h
More file actions
78 lines (59 loc) · 1.77 KB
/
index.h
File metadata and controls
78 lines (59 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// index.h
// Int and Slice indexing
#ifndef TINYTENSOR_INDEX_H_
#define TINYTENSOR_INDEX_H_
#include <tt/exception.h>
#include <tt/export.h>
#include <limits>
#include <optional>
#include <string>
#include <utility>
#include <variant>
#include <vector>
namespace tinytensor::indexing {
constexpr std::nullopt_t None = std::nullopt;
// A range slice (start, end, stride)
class TINYTENSOR_EXPORT Slice {
public:
using SliceIdx = std::optional<int>;
Slice(SliceIdx start_idx = None, SliceIdx end_idx = None, SliceIdx step = None);
[[nodiscard]] constexpr auto start() const -> int {
return start_.value_or(0);
}
[[nodiscard]] constexpr auto end() const -> int {
return end_.value_or(std::numeric_limits<int>::max());
}
[[nodiscard]] constexpr auto end(int dim) const -> int {
return end_.value_or(dim - 1);
}
[[nodiscard]] constexpr auto stride() const -> int {
return stride_.value_or(1);
}
[[nodiscard]] auto to_string() const -> std::string;
[[nodiscard]] auto to_size(int dim) const -> int;
private:
SliceIdx start_;
SliceIdx end_;
SliceIdx stride_;
};
// Index can be either an int or Slice
class TINYTENSOR_EXPORT Index {
public:
using IndexT = std::variant<int, Slice>;
Index(int index)
: index_(index) {}
Index(Slice slice)
: index_(std::move(slice)) {}
[[nodiscard]] constexpr auto get_index() const -> const IndexT & {
return index_;
}
private:
IndexT index_;
};
using IndexList = std::vector<Index>;
/**
* Utility function to convert a list of indices to a pretty-print string
*/
TINYTENSOR_EXPORT auto index_list_to_string(const IndexList &indices) -> std::string;
} // namespace tinytensor::indexing
#endif // TINYTENSOR_INDEX_H_