Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 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-2016 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 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 33 : /* -------------------------------------------------------------------------- * 34 : * lepton * 35 : * -------------------------------------------------------------------------- * 36 : * This is part of the lepton expression parser originating from * 37 : * Simbios, the NIH National Center for Physics-Based Simulation of * 38 : * Biological Structures at Stanford, funded under the NIH Roadmap for * 39 : * Medical Research, grant U54 GM072970. See https://simtk.org. * 40 : * * 41 : * Portions copyright (c) 2009-2015 Stanford University and the Authors. * 42 : * Authors: Peter Eastman * 43 : * Contributors: * 44 : * * 45 : * Permission is hereby granted, free of charge, to any person obtaining a * 46 : * copy of this software and associated documentation files (the "Software"), * 47 : * to deal in the Software without restriction, including without limitation * 48 : * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 49 : * and/or sell copies of the Software, and to permit persons to whom the * 50 : * Software is furnished to do so, subject to the following conditions: * 51 : * * 52 : * The above copyright notice and this permission notice shall be included in * 53 : * all copies or substantial portions of the Software. * 54 : * * 55 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 56 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 57 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 58 : * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 59 : * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 60 : * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 61 : * USE OR OTHER DEALINGS IN THE SOFTWARE. * 62 : * -------------------------------------------------------------------------- */ 63 : 64 : #include "ExpressionTreeNode.h" 65 : #include "Exception.h" 66 : #include "Operation.h" 67 : 68 : namespace PLMD { 69 : using namespace lepton; 70 : using namespace std; 71 : 72 99429 : ExpressionTreeNode::ExpressionTreeNode(Operation* operation, const vector<ExpressionTreeNode>& children) : operation(operation), children(children) { 73 198858 : if (operation->getNumArguments() != children.size()) 74 0 : throw Exception("wrong number of arguments to function: "+operation->getName()); 75 99429 : } 76 : 77 11951 : ExpressionTreeNode::ExpressionTreeNode(Operation* operation, const ExpressionTreeNode& child1, const ExpressionTreeNode& child2) : operation(operation) { 78 11951 : children.push_back(child1); 79 11951 : children.push_back(child2); 80 23902 : if (operation->getNumArguments() != children.size()) 81 0 : throw Exception("wrong number of arguments to function: "+operation->getName()); 82 11951 : } 83 : 84 5670 : ExpressionTreeNode::ExpressionTreeNode(Operation* operation, const ExpressionTreeNode& child) : operation(operation) { 85 5670 : children.push_back(child); 86 11340 : if (operation->getNumArguments() != children.size()) 87 0 : throw Exception("wrong number of arguments to function: "+operation->getName()); 88 5670 : } 89 : 90 22083 : ExpressionTreeNode::ExpressionTreeNode(Operation* operation) : operation(operation) { 91 44166 : if (operation->getNumArguments() != children.size()) 92 0 : throw Exception("wrong number of arguments to function: "+operation->getName()); 93 22083 : } 94 : 95 1324688 : ExpressionTreeNode::ExpressionTreeNode(const ExpressionTreeNode& node) : operation(node.operation == NULL ? NULL : node.operation->clone()), children(node.getChildren()) { 96 1324688 : } 97 : 98 118939 : ExpressionTreeNode::ExpressionTreeNode() : operation(NULL) { 99 118939 : } 100 : 101 3165520 : ExpressionTreeNode::~ExpressionTreeNode() { 102 1582760 : if (operation != NULL) 103 1582760 : delete operation; 104 1582760 : } 105 : 106 105835 : bool ExpressionTreeNode::operator!=(const ExpressionTreeNode& node) const { 107 105835 : if (node.getOperation() != getOperation()) 108 : return true; 109 65118 : if (getOperation().isSymmetric() && getChildren().size() == 2) { 110 38945 : if (getChildren()[0] == node.getChildren()[0] && getChildren()[1] == node.getChildren()[1]) 111 : return false; 112 8175 : if (getChildren()[0] == node.getChildren()[1] && getChildren()[1] == node.getChildren()[0]) 113 : return false; 114 : return true; 115 : } 116 181863 : for (int i = 0; i < (int) getChildren().size(); i++) 117 92526 : if (getChildren()[i] != node.getChildren()[i]) 118 : return true; 119 : return false; 120 : } 121 : 122 74993 : bool ExpressionTreeNode::operator==(const ExpressionTreeNode& node) const { 123 74993 : return !(*this != node); 124 : } 125 : 126 168065 : ExpressionTreeNode& ExpressionTreeNode::operator=(const ExpressionTreeNode& node) { 127 168065 : if (operation != NULL) 128 49126 : delete operation; 129 168065 : operation = node.getOperation().clone(); 130 168065 : children = node.getChildren(); 131 168065 : return *this; 132 : } 133 : 134 866539 : const Operation& ExpressionTreeNode::getOperation() const { 135 866539 : return *operation; 136 : } 137 : 138 1982617 : const vector<ExpressionTreeNode>& ExpressionTreeNode::getChildren() const { 139 1982617 : return children; 140 : } 141 : }