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.

ExpressionTreeNode.cpp 5.0KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 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 "ExpressionTreeNode.h"
  32. #include "Exception.h"
  33. #include "Operation.h"
  34. using namespace Lepton;
  35. ExpressionTreeNode::ExpressionTreeNode(Operation* operation, const std::vector<ExpressionTreeNode>& children) : operation(operation), children(children)
  36. {
  37. if (size_t(operation->getNumArguments()) != children.size())
  38. {
  39. throw Exception("Parse error: wrong number of arguments to function: " + operation->getName());
  40. }
  41. }
  42. ExpressionTreeNode::ExpressionTreeNode(Operation* operation, const ExpressionTreeNode& child1, const ExpressionTreeNode& child2) : operation(operation)
  43. {
  44. children.push_back(child1);
  45. children.push_back(child2);
  46. if (size_t(operation->getNumArguments()) != children.size())
  47. {
  48. throw Exception("Parse error: wrong number of arguments to function: " + operation->getName());
  49. }
  50. }
  51. ExpressionTreeNode::ExpressionTreeNode(Operation* operation, const ExpressionTreeNode& child) : operation(operation)
  52. {
  53. children.push_back(child);
  54. if (size_t(operation->getNumArguments()) != children.size())
  55. {
  56. throw Exception("Parse error: wrong number of arguments to function: " + operation->getName());
  57. }
  58. }
  59. ExpressionTreeNode::ExpressionTreeNode(Operation* operation) : operation(operation)
  60. {
  61. if (size_t(operation->getNumArguments()) != children.size())
  62. {
  63. throw Exception("Parse error: wrong number of arguments to function: " + operation->getName());
  64. }
  65. }
  66. ExpressionTreeNode::ExpressionTreeNode(const ExpressionTreeNode& node) : operation(node.getOperation().clone()), children(node.getChildren()) {}
  67. ExpressionTreeNode::ExpressionTreeNode() {}
  68. ExpressionTreeNode::~ExpressionTreeNode() { delete operation; }
  69. bool ExpressionTreeNode::operator!=(const ExpressionTreeNode& node) const
  70. {
  71. if (node.getOperation() != getOperation()) { return true; }
  72. if (getOperation().isSymmetric() && getChildren().size() == 2)
  73. {
  74. if (getChildren()[0] == node.getChildren()[0] && getChildren()[1] == node.getChildren()[1]) { return false; }
  75. if (getChildren()[0] == node.getChildren()[1] && getChildren()[1] == node.getChildren()[0]) { return false; }
  76. return true;
  77. }
  78. for (size_t i = 0; i < getChildren().size(); ++i) { if (getChildren()[i] != node.getChildren()[i]) { return true; } }
  79. return false;
  80. }
  81. bool ExpressionTreeNode::operator==(const ExpressionTreeNode& node) const { return !(*this != node); }
  82. ExpressionTreeNode& ExpressionTreeNode::operator=(const ExpressionTreeNode& node)
  83. {
  84. delete operation;
  85. operation = node.getOperation().clone();
  86. children = node.getChildren();
  87. return *this;
  88. }
  89. const Operation& ExpressionTreeNode::getOperation() const { return *operation; }
  90. const std::vector<ExpressionTreeNode>& ExpressionTreeNode::getChildren() const { return children; }