Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:17:11

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2023-2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file orange/orangeinp/CsgTypes.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <functional>
0011 #include <iosfwd>
0012 #include <string>
0013 #include <variant>
0014 #include <vector>
0015 
0016 #include "corecel/OpaqueId.hh"
0017 #include "corecel/math/HashUtils.hh"
0018 #include "orange/OrangeTypes.hh"
0019 
0020 namespace celeritas
0021 {
0022 namespace orangeinp
0023 {
0024 //---------------------------------------------------------------------------//
0025 //! Operator token
0026 using OperatorToken = logic::OperatorToken;
0027 
0028 //! Unique identifier for a node
0029 using NodeId = OpaqueId<struct Node_>;
0030 
0031 inline constexpr OperatorToken op_and = OperatorToken::land;
0032 inline constexpr OperatorToken op_or = OperatorToken::lor;
0033 
0034 //! Node that represents "always true" for simplification
0035 struct True
0036 {
0037 };
0038 
0039 //! Node that represents "always false" for simplification
0040 struct False
0041 {
0042 };
0043 
0044 //! (Internal) stand-in node for a replacement for another node ID
0045 struct Aliased
0046 {
0047     NodeId node;
0048 };
0049 
0050 //! Node that negates the next ID
0051 struct Negated
0052 {
0053     NodeId node;
0054 };
0055 
0056 //! Node that is a single surface
0057 struct Surface
0058 {
0059     LocalSurfaceId id;
0060 };
0061 
0062 //! Internal node applying an operation to multiple leaf nodes
0063 struct Joined
0064 {
0065     OperatorToken op;
0066     std::vector<NodeId> nodes;
0067 };
0068 
0069 //! Generic node
0070 using Node = std::variant<True, False, Aliased, Negated, Surface, Joined>;
0071 
0072 //---------------------------------------------------------------------------//
0073 // Equality operators
0074 //---------------------------------------------------------------------------//
0075 inline constexpr bool operator==(True const&, True const&)
0076 {
0077     return true;
0078 }
0079 inline constexpr bool operator==(False const&, False const&)
0080 {
0081     return true;
0082 }
0083 inline constexpr bool operator==(Aliased const& a, Aliased const& b)
0084 {
0085     return a.node == b.node;
0086 }
0087 inline constexpr bool operator==(Negated const& a, Negated const& b)
0088 {
0089     return a.node == b.node;
0090 }
0091 inline constexpr bool operator==(Surface const& a, Surface const& b)
0092 {
0093     return a.id == b.id;
0094 }
0095 inline constexpr bool operator==(Joined const& a, Joined const& b)
0096 {
0097     return a.op == b.op && a.nodes == b.nodes;
0098 }
0099 
0100 #define CELER_DEFINE_CSG_NE(CLS)                                 \
0101     inline constexpr bool operator!=(CLS const& a, CLS const& b) \
0102     {                                                            \
0103         return !(a == b);                                        \
0104     }
0105 
0106 CELER_DEFINE_CSG_NE(True)
0107 CELER_DEFINE_CSG_NE(False)
0108 CELER_DEFINE_CSG_NE(Aliased)
0109 CELER_DEFINE_CSG_NE(Negated)
0110 CELER_DEFINE_CSG_NE(Surface)
0111 CELER_DEFINE_CSG_NE(Joined)
0112 
0113 //---------------------------------------------------------------------------//
0114 // Stream output
0115 //---------------------------------------------------------------------------//
0116 std::ostream& operator<<(std::ostream& os, True const&);
0117 std::ostream& operator<<(std::ostream& os, False const&);
0118 std::ostream& operator<<(std::ostream& os, Aliased const&);
0119 std::ostream& operator<<(std::ostream& os, Negated const&);
0120 std::ostream& operator<<(std::ostream& os, Surface const&);
0121 std::ostream& operator<<(std::ostream& os, Joined const&);
0122 
0123 // Write a variant node to a stream
0124 std::ostream& operator<<(std::ostream& os, Node const&);
0125 
0126 // Get a string representation of a variant node
0127 std::string to_string(Node const&);
0128 
0129 //---------------------------------------------------------------------------//
0130 // Helper functions
0131 //---------------------------------------------------------------------------//
0132 //! Whether a node is a boolean (True or False instances)
0133 inline constexpr bool is_boolean_node(Node const& n)
0134 {
0135     return std::holds_alternative<True>(n) || std::holds_alternative<False>(n);
0136 }
0137 
0138 //---------------------------------------------------------------------------//
0139 }  // namespace orangeinp
0140 }  // namespace celeritas
0141 
0142 namespace std
0143 {
0144 //---------------------------------------------------------------------------//
0145 // HASH SPECIALIZATIONS
0146 //---------------------------------------------------------------------------//
0147 //! \cond
0148 template<>
0149 struct hash<celeritas::orangeinp::True>
0150 {
0151     using argument_type = celeritas::orangeinp::True;
0152     using result_type = std::size_t;
0153     result_type operator()(argument_type const&) const noexcept
0154     {
0155         return result_type{0};
0156     }
0157 };
0158 
0159 template<>
0160 struct hash<celeritas::orangeinp::False>
0161 {
0162     using argument_type = celeritas::orangeinp::False;
0163     using result_type = std::size_t;
0164     result_type operator()(argument_type const&) const noexcept
0165     {
0166         return result_type{0};
0167     }
0168 };
0169 
0170 template<>
0171 struct hash<celeritas::orangeinp::Aliased>
0172 {
0173     using argument_type = celeritas::orangeinp::Aliased;
0174     using result_type = std::size_t;
0175     result_type operator()(argument_type const& val) const noexcept
0176     {
0177         return std::hash<celeritas::orangeinp::NodeId>{}(val.node);
0178     }
0179 };
0180 
0181 template<>
0182 struct hash<celeritas::orangeinp::Negated>
0183 {
0184     using argument_type = celeritas::orangeinp::Negated;
0185     using result_type = std::size_t;
0186     result_type operator()(argument_type const& val) const noexcept
0187     {
0188         return std::hash<celeritas::orangeinp::NodeId>{}(val.node);
0189     }
0190 };
0191 
0192 template<>
0193 struct hash<celeritas::orangeinp::Surface>
0194 {
0195     using argument_type = celeritas::orangeinp::Surface;
0196     using result_type = std::size_t;
0197     result_type operator()(argument_type const& val) const noexcept
0198     {
0199         return std::hash<celeritas::LocalSurfaceId>{}(val.id);
0200     }
0201 };
0202 
0203 template<>
0204 struct hash<celeritas::orangeinp::Joined>
0205 {
0206     using argument_type = celeritas::orangeinp::Joined;
0207     using result_type = std::size_t;
0208     result_type operator()(argument_type const& val) const noexcept
0209     {
0210         result_type result;
0211         celeritas::Hasher hash{&result};
0212         hash(static_cast<std::size_t>(val.op));
0213         hash(val.nodes.size());
0214         for (auto& v : val.nodes)
0215         {
0216             hash(std::hash<celeritas::orangeinp::NodeId>{}(v));
0217         }
0218         return result;
0219     }
0220 };
0221 //! \endcond
0222 //---------------------------------------------------------------------------//
0223 }  // namespace std