aboutsummaryrefslogtreecommitdiff
path: root/src/NXP/MathExecutor.php
diff options
context:
space:
mode:
authorNeonXP <frei@neonxp.info>2013-09-06 08:19:02 +0400
committerNeonXP <frei@neonxp.info>2013-09-06 08:19:02 +0400
commit9cef8dbc799343f6fc0fca926fbef4917b94f335 (patch)
tree915397133271cdc9e2e86a800be02ec695184fdc /src/NXP/MathExecutor.php
parent4b08ec4b4dbf83dd242d7882e852b8d933ef0560 (diff)
+ Added cache, which speeds up the repetitive calculations
+ Returned variables, because they need for cached expressions
Diffstat (limited to 'src/NXP/MathExecutor.php')
-rw-r--r--src/NXP/MathExecutor.php93
1 files changed, 88 insertions, 5 deletions
diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php
index a9bf8a9..7eaa914 100644
--- a/src/NXP/MathExecutor.php
+++ b/src/NXP/MathExecutor.php
@@ -23,11 +23,23 @@ use NXP\Classes\TokenFactory;
class MathExecutor
{
/**
+ * Available variables
+ *
+ * @var array
+ */
+ private $variables = array();
+
+ /**
* @var TokenFactory
*/
private $tokenFactory;
/**
+ * @var array
+ */
+ private $cache = array();
+
+ /**
* Base math operators
*/
public function __construct()
@@ -63,6 +75,72 @@ class MathExecutor
$this->tokenFactory->addFunction('max', 'max', 2);
$this->tokenFactory->addFunction('avg', function($arg1, $arg2) { return ($arg1 + $arg2) / 2; }, 2);
+ $this->setVars(array(
+ 'pi' => 3.14159265359,
+ 'e' => 2.71828182846
+ ));
+ }
+
+ /**
+ * Add variable to executor
+ *
+ * @param string $variable
+ * @param integer|float $value
+ * @throws \Exception
+ * @return MathExecutor
+ */
+ public function setVar($variable, $value)
+ {
+ if (!is_numeric($value)) {
+ throw new \Exception("Variable value must be a number");
+ }
+
+ $this->variables[$variable] = $value;
+
+ return $this;
+ }
+
+ /**
+ * Add variables to executor
+ *
+ * @param array $variables
+ * @param bool $clear Clear previous variables
+ * @return MathExecutor
+ */
+ public function setVars(array $variables, $clear = true)
+ {
+ if ($clear) {
+ $this->removeVars();
+ }
+
+ foreach ($variables as $name => $value) {
+ $this->setVar($name, $value);
+ }
+
+ return $this;
+ }
+
+ /**
+ * Remove variable from executor
+ *
+ * @param string $variable
+ * @return MathExecutor
+ */
+ public function removeVar($variable)
+ {
+ unset ($this->variables[$variable]);
+
+ return $this;
+ }
+
+ /**
+ * Remove all variables
+ */
+ public function removeVars()
+ {
+ $this->variables = array();
+
+ return $this;
}
/**
@@ -97,15 +175,20 @@ class MathExecutor
* Execute expression
*
* @param $expression
- * @return int|float
+ * @return number
*/
public function execute($expression)
{
- $lexer = new Lexer($this->tokenFactory);
- $tokensStream = $lexer->stringToTokensStream($expression);
- $tokens = $lexer->buildReversePolishNotation($tokensStream);
+ if (!array_key_exists($expression, $this->cache)) {
+ $lexer = new Lexer($this->tokenFactory);
+ $tokensStream = $lexer->stringToTokensStream($expression);
+ $tokens = $lexer->buildReversePolishNotation($tokensStream);
+ $this->cache[$expression] = $tokens;
+ } else {
+ $tokens = $this->cache[$expression];
+ }
$calculator = new Calculator();
- $result = $calculator->calculate($tokens);
+ $result = $calculator->calculate($tokens, $this->variables);
return $result;
}