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.

ExpressionProgram.cpp 4.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* -------------------------------------------------------------------------- *
  2. * Lepton *
  3. * -------------------------------------------------------------------------- *
  4. * This is part of the Lepton expression parser originating from *
  5. * Simbios, the NIH National Center for Physics-Based Simulation of *
  6. * Biological Structures at Stanford, funded under the NIH Roadmap for *
  7. * Medical Research, grant U54 GM072970. See https://simtk.org. *
  8. * *
  9. * Portions copyright (c) 2009-2013 Stanford University and the Authors. *
  10. * Authors: Peter Eastman *
  11. * Contributors: *
  12. * *
  13. * Permission is hereby granted, free of charge, to any person obtaining a *
  14. * copy of this software and associated documentation files (the "Software"), *
  15. * to deal in the Software without restriction, including without limitation *
  16. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  17. * and/or sell copies of the Software, and to permit persons to whom the *
  18. * Software is furnished to do so, subject to the following conditions: *
  19. * *
  20. * The above copyright notice and this permission notice shall be included in *
  21. * all copies or substantial portions of the Software. *
  22. * *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  26. * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  27. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  28. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
  29. * USE OR OTHER DEALINGS IN THE SOFTWARE. *
  30. * -------------------------------------------------------------------------- */
  31. #include "ExpressionProgram.h"
  32. #include "Operation.h"
  33. #include "ParsedExpression.h"
  34. using namespace Lepton;
  35. ExpressionProgram::ExpressionProgram() : maxArgs(0), stackSize(0) {}
  36. ExpressionProgram::ExpressionProgram(const ParsedExpression& expression) : maxArgs(0), stackSize(0)
  37. {
  38. buildProgram(expression.getRootNode());
  39. int currentStackSize = 0;
  40. for (size_t i = 0; i < operations.size(); ++i)
  41. {
  42. int args = operations[i]->getNumArguments();
  43. if (args > maxArgs) { maxArgs = args; }
  44. currentStackSize += 1 - args;
  45. if (currentStackSize > stackSize) { stackSize = currentStackSize; }
  46. }
  47. }
  48. ExpressionProgram::~ExpressionProgram() { for (size_t i = 0; i < operations.size(); ++i) { delete operations[i]; } }
  49. ExpressionProgram::ExpressionProgram(const ExpressionProgram& program) { *this = program; }
  50. ExpressionProgram& ExpressionProgram::operator=(const ExpressionProgram& program)
  51. {
  52. maxArgs = program.maxArgs;
  53. stackSize = program.stackSize;
  54. operations.resize(program.operations.size());
  55. for (size_t i = 0; i < operations.size(); ++i) { operations[i] = program.operations[i]->clone(); }
  56. return *this;
  57. }
  58. void ExpressionProgram::buildProgram(const ExpressionTreeNode& node)
  59. {
  60. for (size_t i = node.getChildren().size() - 1; i >= 0; i--) { buildProgram(node.getChildren()[i]); }
  61. operations.push_back(node.getOperation().clone());
  62. }
  63. int ExpressionProgram::getNumOperations() const { return int(operations.size()); }
  64. const Operation& ExpressionProgram::getOperation(int index) const { return *operations[index]; }
  65. int ExpressionProgram::getStackSize() const { return stackSize; }
  66. double ExpressionProgram::evaluate() const { return evaluate(std::map<std::string, double>()); }
  67. double ExpressionProgram::evaluate(const std::map<std::string, double>& variables) const
  68. {
  69. std::vector<double> stack(stackSize + 1);
  70. int stackPointer = stackSize;
  71. for (size_t i = 0; i < operations.size(); ++i)
  72. {
  73. int numArgs = operations[i]->getNumArguments();
  74. double result = operations[i]->evaluate(&stack[stackPointer], variables);
  75. stackPointer += numArgs - 1;
  76. stack[stackPointer] = result;
  77. }
  78. return stack[stackSize - 1];
  79. }