[gnome-calculator/60-split-out-a-backend-library] gcalc: variables store are handled per MathEquation



commit 68019fa8acb95acbd9f7a205858a2a0e25be530f
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date:   Sat Jan 5 13:22:02 2019 -0600

    gcalc: variables store are handled per MathEquation

 gcalc/gcalc-expression-hash-map.vala    | 36 +++++++++++++++++++++++++++++++++
 gcalc/gcalc-gfunction.vala              |  6 +++++-
 gcalc/gcalc-gmath-equation-manager.vala |  2 --
 gcalc/gcalc-gmath-equation.vala         |  2 ++
 gcalc/gcalc-gparser.vala                |  4 ++--
 gcalc/gcalc-gvariable.vala              |  6 +++++-
 gcalc/gcalc-hashable.vala               | 24 ++++++++++++++++++++++
 gcalc/gcalc-math-equation-manager.vala  |  1 -
 gcalc/gcalc-math-equation.vala          |  4 +++-
 gcalc/meson.build                       |  2 ++
 10 files changed, 79 insertions(+), 8 deletions(-)
---
diff --git a/gcalc/gcalc-expression-hash-map.vala b/gcalc/gcalc-expression-hash-map.vala
new file mode 100644
index 00000000..06e8a793
--- /dev/null
+++ b/gcalc/gcalc-expression-hash-map.vala
@@ -0,0 +1,36 @@
+/* gcalc-expression-hash-table.vala
+ *
+ * Copyright (C) 2019  Daniel Espinosa <esodan gmail com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *      Daniel Espinosa <esodan gmail com>
+ */
+public class GCalc.ExpressionHashMap : Gee.HashMap<uint,Expression> {
+  public weak Expression parent { get; set; }
+  public void add (Expression exp)
+    requires (exp is Hashable)
+  {
+    (this as Gee.HashMap<uint,Expression>).set (((Hashable) exp).hash (), exp);
+    exp.parent = parent;
+  }
+  public void remove (Expression exp) {
+    (this as Gee.HashMap<uint,Expression>).unset (((Hashable) exp).hash ());
+  }
+  public Expression find_named (string name) {
+    return (this as Gee.HashMap<uint,Expression>).@get (name.hash ());
+  }
+}
+
diff --git a/gcalc/gcalc-gfunction.vala b/gcalc/gcalc-gfunction.vala
index 49b40f27..bfb29479 100644
--- a/gcalc/gcalc-gfunction.vala
+++ b/gcalc/gcalc-gfunction.vala
@@ -18,7 +18,7 @@
  * Authors:
  *      Daniel Espinosa <esodan gmail com>
  */
-public class GCalc.GFunction : GExpression, Function {
+public class GCalc.GFunction : GExpression, Function, Hashable {
   ExpressionContainer _param_types = new ExpressionContainer ();
 
   public ExpressionContainer param_types { get { return _param_types; } }
@@ -52,5 +52,9 @@ public class GCalc.GFunction : GExpression, Function {
   public new virtual Expression evaluate () throws GLib.Error {
     return new GErrorExpression ();
   }
+  // Hashable
+  public uint hash () {
+    return name.hash ();
+  }
 }
 
diff --git a/gcalc/gcalc-gmath-equation-manager.vala b/gcalc/gcalc-gmath-equation-manager.vala
index 77cc5518..c0733d35 100644
--- a/gcalc/gcalc-gmath-equation-manager.vala
+++ b/gcalc/gcalc-gmath-equation-manager.vala
@@ -21,10 +21,8 @@
 public class GCalc.GMathEquationManager : Object, MathEquationManager {
   ExpressionContainer _equations = new ExpressionContainer ();
   ExpressionContainer _functions = new ExpressionContainer ();
-  ExpressionContainer _variables = new ExpressionContainer ();
   public ExpressionContainer equations { get { return _equations; } }
   public ExpressionContainer functions { get { return _functions; } }
-  public ExpressionContainer variables { get { return _variables; } }
 
   construct {
     // Initialize default Functions
diff --git a/gcalc/gcalc-gmath-equation.vala b/gcalc/gcalc-gmath-equation.vala
index 96b865ff..3f03734d 100644
--- a/gcalc/gcalc-gmath-equation.vala
+++ b/gcalc/gcalc-gmath-equation.vala
@@ -19,6 +19,8 @@
  *      Daniel Espinosa <esodan gmail com>
  */
 public class GCalc.GMathEquation : GExpression, MathEquation {
+  ExpressionHashMap _variables = new ExpressionHashMap ();
+  public ExpressionHashMap variables { get { return _variables; } }
   public override Result solve () {
     if (expressions.get_n_items () == 0) {
       var err = new GErrorResult ("No expressions found in equation");
diff --git a/gcalc/gcalc-gparser.vala b/gcalc/gcalc-gparser.vala
index d0046917..abe807ba 100644
--- a/gcalc/gcalc-gparser.vala
+++ b/gcalc/gcalc-gparser.vala
@@ -158,8 +158,8 @@ public class GCalc.GParser : Object {
             throw new ParserError.INVALID_TOKEN_ERROR ("Found an unexpected function definition expression");
           } else {
             var v = new GVariable (n) as Expression;
-            if (eqman.variables.find_named (n) == null) {
-              eqman.variables.add (v);
+            if (eq.variables.find_named (n) == null) {
+              eq.variables.add (v);
             }
             if (current == null) {
               current = v;
diff --git a/gcalc/gcalc-gvariable.vala b/gcalc/gcalc-gvariable.vala
index 7a56c6d3..4257a066 100644
--- a/gcalc/gcalc-gvariable.vala
+++ b/gcalc/gcalc-gvariable.vala
@@ -18,7 +18,7 @@
  * Authors:
  *      Daniel Espinosa <esodan gmail com>
  */
-public class GCalc.GVariable : GExpression, Variable {
+public class GCalc.GVariable : GExpression, Variable, Hashable {
   private GLib.Value _value;
 
   public string name { get; construct set; }
@@ -34,5 +34,9 @@ public class GCalc.GVariable : GExpression, Variable {
   public override string to_string () {
     return name;
   }
+  // Hashable
+  public uint hash () {
+    return name.hash ();
+  }
 }
 
diff --git a/gcalc/gcalc-hashable.vala b/gcalc/gcalc-hashable.vala
new file mode 100644
index 00000000..9d147607
--- /dev/null
+++ b/gcalc/gcalc-hashable.vala
@@ -0,0 +1,24 @@
+/* gcalc-hashable.vala
+ *
+ * Copyright (C) 2018  Daniel Espinosa <esodan gmail com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *      Daniel Espinosa <esodan gmail com>
+ */
+public interface GCalc.Hashable : Object {
+  public abstract uint hash ();
+}
+
diff --git a/gcalc/gcalc-math-equation-manager.vala b/gcalc/gcalc-math-equation-manager.vala
index 49d7cc55..97b085c1 100644
--- a/gcalc/gcalc-math-equation-manager.vala
+++ b/gcalc/gcalc-math-equation-manager.vala
@@ -21,6 +21,5 @@
 public interface GCalc.MathEquationManager : Object {
   public abstract ExpressionContainer equations { get; }
   public abstract ExpressionContainer functions { get; }
-  public abstract ExpressionContainer variables { get; }
 }
 
diff --git a/gcalc/gcalc-math-equation.vala b/gcalc/gcalc-math-equation.vala
index 238c3437..f76a3032 100644
--- a/gcalc/gcalc-math-equation.vala
+++ b/gcalc/gcalc-math-equation.vala
@@ -18,5 +18,7 @@
  * Authors:
  *      Daniel Espinosa <esodan gmail com>
  */
-public interface GCalc.MathEquation : Object, Expression {}
+public interface GCalc.MathEquation : Object, Expression {
+  public abstract ExpressionHashMap variables { get; }
+}
 
diff --git a/gcalc/meson.build b/gcalc/meson.build
index eb487e09..70802e07 100644
--- a/gcalc/meson.build
+++ b/gcalc/meson.build
@@ -46,6 +46,7 @@ sources = files([
        'gcalc-division.vala',
        'gcalc-expression.vala',
        'gcalc-expression-container.vala',
+       'gcalc-expression-hash-map.vala',
        'gcalc-function.vala',
        'gcalc-function-acos.vala',
        'gcalc-function-acosh.vala',
@@ -83,6 +84,7 @@ sources = files([
        'gcalc-gsolver.vala',
        'gcalc-gterm.vala',
        'gcalc-gvariable.vala',
+       'gcalc-hashable.vala',
        'gcalc-math-equation.vala',
        'gcalc-math-equation-manager.vala',
        'gcalc-minus.vala',


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]