You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CompiledExpression.h 4.6KB

3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #pragma once
  2. /* -------------------------------------------------------------------------- *
  3. * Lepton *
  4. * -------------------------------------------------------------------------- *
  5. * This is part of the Lepton expression parser originating from *
  6. * Simbios, the NIH National Center for Physics-Based Simulation of *
  7. * Biological Structures at Stanford, funded under the NIH Roadmap for *
  8. * Medical Research, grant U54 GM072970. See https://simtk.org. *
  9. * *
  10. * Portions copyright (c) 2013 Stanford University and the Authors. *
  11. * Authors: Peter Eastman *
  12. * Contributors: *
  13. * *
  14. * Permission is hereby granted, free of charge, to any person obtaining a *
  15. * copy of this software and associated documentation files (the "Software"), *
  16. * to deal in the Software without restriction, including without limitation *
  17. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  18. * and/or sell copies of the Software, and to permit persons to whom the *
  19. * Software is furnished to do so, subject to the following conditions: *
  20. * *
  21. * The above copyright notice and this permission notice shall be included in *
  22. * all copies or substantial portions of the Software. *
  23. * *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  27. * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  28. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  29. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
  30. * USE OR OTHER DEALINGS IN THE SOFTWARE. *
  31. * -------------------------------------------------------------------------- */
  32. #include "ExpressionTreeNode.h"
  33. #include "windowsIncludes.h"
  34. #include <map>
  35. #include <set>
  36. #include <string>
  37. #include <vector>
  38. namespace Lepton
  39. {
  40. class Operation;
  41. class ParsedExpression;
  42. /**
  43. * A CompiledExpression is a highly optimized representation of an expression for cases when you want to evaluate
  44. * it many times as quickly as possible. You should treat it as an opaque object; none of the internal representation
  45. * is visible.
  46. *
  47. * A CompiledExpression is created by calling createCompiledExpression() on a ParsedExpression.
  48. *
  49. * WARNING: CompiledExpression is NOT thread safe. You should never access a CompiledExpression from two threads at
  50. * the same time.
  51. */
  52. class LEPTON_EXPORT CompiledExpression
  53. {
  54. public:
  55. CompiledExpression() {}
  56. CompiledExpression(const CompiledExpression& expression) { *this = expression; }
  57. ~CompiledExpression() { for (size_t i = 0; i < operation.size(); ++i) if (operation[i] != nullptr) delete operation[i]; }
  58. CompiledExpression& operator=(const CompiledExpression& expression);
  59. /**
  60. * Get the names of all variables used by this expression.
  61. */
  62. const std::set<std::string>& getVariables() const { return variableNames; }
  63. /**
  64. * Get a reference to the memory location where the value of a particular variable is stored. This can be used
  65. * to set the value of the variable before calling evaluate().
  66. */
  67. double& getVariableReference(const std::string& name);
  68. /**
  69. * Evaluate the expression. The values of all variables should have been set before calling this.
  70. */
  71. double evaluate() const;
  72. private:
  73. friend class ParsedExpression;
  74. CompiledExpression(const ParsedExpression& expression);
  75. void compileExpression(const ExpressionTreeNode& node, std::vector<std::pair<ExpressionTreeNode, int>>& temps);
  76. int findTempIndex(const ExpressionTreeNode& node, std::vector<std::pair<ExpressionTreeNode, int>>& temps);
  77. std::vector<std::vector<int>> arguments;
  78. std::vector<int> target;
  79. std::vector<Operation*> operation;
  80. std::map<std::string, int> variableIndices;
  81. std::set<std::string> variableNames;
  82. mutable std::vector<double> workspace;
  83. mutable std::vector<double> argValues;
  84. std::map<std::string, double> dummyVariables;
  85. };
  86. } // namespace Lepton