Line data Source code
1 : // SPDX-License-Identifier: BSD-3-Clause 2 : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp) 3 : /// @file 4 : /// @author Simon Heybrock 5 : #pragma once 6 : #include <functional> 7 : #include <unordered_map> 8 : 9 : #include "scipp-core_export.h" 10 : #include "scipp/common/span.h" 11 : #include "scipp/core/time_point.h" 12 : 13 : namespace scipp::core { 14 : 15 : struct SCIPP_CORE_EXPORT DType { 16 : int32_t index; 17 14956409 : constexpr bool operator==(const DType &t) const noexcept { 18 14956409 : return index == t.index; 19 : } 20 12489697 : constexpr bool operator!=(const DType &t) const noexcept { 21 12489697 : return index != t.index; 22 : } 23 223193028 : constexpr bool operator<(const DType &t) const noexcept { 24 223193028 : return index < t.index; 25 : } 26 : }; 27 : 28 : // Note that previously we where using std::type_info to obtain a unique ID, 29 : // however this was causing trouble across library boundaries on certain systems 30 : // (macOS). This mechanism based on hand-code IDs has the risk of clashing dtype 31 : // for two different types T1 and T2, but we currently do not have a better 32 : // solution. 33 : // force compiler error if not specialized 34 : template <class T> constexpr DType dtype{T::missing_specialization_of_dtype}; 35 : // basics types start at 0 36 : template <> inline constexpr DType dtype<void>{0}; 37 : template <> inline constexpr DType dtype<double>{1}; 38 : template <> inline constexpr DType dtype<float>{2}; 39 : template <> inline constexpr DType dtype<int64_t>{3}; 40 : template <> inline constexpr DType dtype<int32_t>{4}; 41 : template <> inline constexpr DType dtype<bool>{5}; 42 : template <> inline constexpr DType dtype<std::string>{6}; 43 : template <> inline constexpr DType dtype<time_point>{7}; 44 : class SubbinSizes; 45 : template <> inline constexpr DType dtype<SubbinSizes>{10}; 46 : // span<T> start at 100 47 : template <> inline constexpr DType dtype<scipp::span<const double>>{100}; 48 : template <> inline constexpr DType dtype<scipp::span<const float>>{101}; 49 : template <> inline constexpr DType dtype<scipp::span<const int64_t>>{102}; 50 : template <> inline constexpr DType dtype<scipp::span<const int32_t>>{103}; 51 : template <> inline constexpr DType dtype<scipp::span<const bool>>{104}; 52 : template <> inline constexpr DType dtype<scipp::span<const std::string>>{105}; 53 : template <> inline constexpr DType dtype<scipp::span<const time_point>>{106}; 54 : // span<inline const T> start at 200 55 : template <> inline constexpr DType dtype<scipp::span<double>>{200}; 56 : template <> inline constexpr DType dtype<scipp::span<float>>{201}; 57 : template <> inline constexpr DType dtype<scipp::span<int64_t>>{202}; 58 : template <> inline constexpr DType dtype<scipp::span<int32_t>>{203}; 59 : template <> inline constexpr DType dtype<scipp::span<bool>>{204}; 60 : template <> inline constexpr DType dtype<scipp::span<std::string>>{205}; 61 : template <> inline constexpr DType dtype<scipp::span<time_point>>{206}; 62 : // std containers start at 300 63 : template <> inline constexpr DType dtype<std::pair<int32_t, int32_t>>{300}; 64 : template <> inline constexpr DType dtype<std::pair<int64_t, int64_t>>{301}; 65 : template <> 66 : inline constexpr DType dtype<std::unordered_map<double, int64_t>>{302}; 67 : template <> 68 : inline constexpr DType dtype<std::unordered_map<double, int32_t>>{303}; 69 : template <> 70 : inline constexpr DType dtype<std::unordered_map<float, int64_t>>{304}; 71 : template <> 72 : inline constexpr DType dtype<std::unordered_map<float, int32_t>>{305}; 73 : template <> 74 : inline constexpr DType dtype<std::unordered_map<int64_t, int64_t>>{306}; 75 : template <> 76 : inline constexpr DType dtype<std::unordered_map<int64_t, int32_t>>{307}; 77 : template <> 78 : inline constexpr DType dtype<std::unordered_map<int32_t, int64_t>>{308}; 79 : template <> 80 : inline constexpr DType dtype<std::unordered_map<int32_t, int32_t>>{309}; 81 : template <> 82 : inline constexpr DType dtype<std::unordered_map<bool, int64_t>>{310}; 83 : template <> 84 : inline constexpr DType dtype<std::unordered_map<bool, int32_t>>{311}; 85 : template <> 86 : inline constexpr DType dtype<std::unordered_map<std::string, int64_t>>{312}; 87 : template <> 88 : inline constexpr DType dtype<std::unordered_map<std::string, int32_t>>{313}; 89 : template <> 90 : inline constexpr DType dtype<std::unordered_map<core::time_point, int64_t>>{ 91 : 314}; 92 : template <> 93 : inline constexpr DType dtype<std::unordered_map<core::time_point, int32_t>>{ 94 : 315}; 95 : // scipp::variable types start at 1000 96 : // scipp::dataset types start at 2000 97 : // scipp::python types start at 3000 98 : // Spatial transform (Eigen) types start at 4000 99 : // User types should start at 10000 100 : 101 : SCIPP_CORE_EXPORT bool is_int(DType tp); 102 : SCIPP_CORE_EXPORT bool is_float(DType tp); 103 : SCIPP_CORE_EXPORT bool is_fundamental(DType tp); 104 : SCIPP_CORE_EXPORT bool is_total_orderable(DType tp); 105 : SCIPP_CORE_EXPORT bool is_span(DType tp); 106 : 107 573424 : template <class T> constexpr bool canHaveVariances() noexcept { 108 : using U = std::remove_const_t<T>; 109 : return std::is_same_v<U, double> || std::is_same_v<U, float> || 110 : std::is_same_v<U, scipp::span<const double>> || 111 : std::is_same_v<U, scipp::span<const float>> || 112 : std::is_same_v<U, scipp::span<double>> || 113 573424 : std::is_same_v<U, scipp::span<float>>; 114 : } 115 : 116 : SCIPP_CORE_EXPORT std::ostream &operator<<(std::ostream &os, 117 : const DType &dtype); 118 : 119 : [[nodiscard]] SCIPP_CORE_EXPORT DType common_type(const DType &a, 120 : const DType &b); 121 : 122 : } // namespace scipp::core 123 : 124 : namespace scipp { 125 : using core::DType; 126 : using core::dtype; 127 : } // namespace scipp 128 : 129 : namespace std { 130 : template <> struct hash<scipp::DType> { 131 : std::size_t operator()(const scipp::DType &dt) const noexcept { 132 : return std::hash<decltype(dt.index)>{}(dt.index); 133 : } 134 : }; 135 : } // namespace std