Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2021-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/univ/detail/LogicEvaluator.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include "corecel/Assert.hh"
0011 #include "corecel/cont/Span.hh"
0012 #include "corecel/data/LdgIterator.hh"
0013 #include "orange/OrangeTypes.hh"
0014 
0015 #include "LogicStack.hh"
0016 
0017 namespace celeritas
0018 {
0019 namespace detail
0020 {
0021 //---------------------------------------------------------------------------//
0022 /*!
0023  * Evaluate a logical expression applied to a vector of senses.
0024  */
0025 class LogicEvaluator
0026 {
0027   public:
0028     //@{
0029     //! \name Type aliases
0030     using SpanConstLogic = LdgSpan<logic_int const>;
0031     using SpanConstSense = Span<Sense const>;
0032     //@}
0033 
0034   public:
0035     // Construct with a view to some logic definition
0036     explicit CELER_FORCEINLINE_FUNCTION LogicEvaluator(SpanConstLogic logic);
0037 
0038     // Evaluate a logical expression, substituting bools from the vector
0039     inline CELER_FUNCTION bool operator()(SpanConstSense values) const;
0040 
0041   private:
0042     //// DATA ////
0043 
0044     SpanConstLogic logic_;
0045 };
0046 
0047 //---------------------------------------------------------------------------//
0048 /*!
0049  * Construct with a view to some logic definition.
0050  */
0051 CELER_FUNCTION LogicEvaluator::LogicEvaluator(SpanConstLogic logic)
0052     : logic_(logic)
0053 {
0054     CELER_EXPECT(!logic_.empty());
0055 }
0056 
0057 //---------------------------------------------------------------------------//
0058 /*!
0059  * Evaluate a logical expression, substituting bools from the sense view.
0060  */
0061 CELER_FUNCTION bool LogicEvaluator::operator()(SpanConstSense values) const
0062 {
0063     LogicStack stack;
0064 
0065     for (logic_int lgc : logic_)
0066     {
0067         if (!logic::is_operator_token(lgc))
0068         {
0069             // Push a boolean from the senses onto the stack
0070             CELER_EXPECT(lgc < values.size());
0071             stack.push(static_cast<bool>(values[lgc]));
0072             continue;
0073         }
0074 
0075         // Apply logic operator
0076         switch (lgc)
0077         {
0078                 // clang-format off
0079             case logic::ltrue: stack.push(true);  break;
0080             case logic::lor:   stack.apply_or();  break;
0081             case logic::land:  stack.apply_and(); break;
0082             case logic::lnot:  stack.apply_not(); break;
0083             default:           CELER_ASSERT_UNREACHABLE();
0084         }
0085         // clang-format on
0086     }
0087     CELER_ENSURE(stack.size() == 1);
0088     return stack.top();
0089 }
0090 
0091 //---------------------------------------------------------------------------//
0092 }  // namespace detail
0093 }  // namespace celeritas