/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk

« back to all changes in this revision

Viewing changes to src/libvqdr/variable.vala

  • Committer: Gustav Hartvigsson
  • Date: 2021-09-15 17:10:52 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210915171052-yhdw16iyipyj5a5l
* Fixed Fastnumber's normalisation problem with the getters/setters

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
using Vee;
2
1
namespace VQDR.Expression {
 
2
  
 
3
  
3
4
  /**
4
5
   * Represents a variable.
5
6
   */
6
7
  public struct Variable {
7
 
    public FastNumber min_val;
8
 
    public FastNumber max_val;
9
 
    public FastNumber current_val;
 
8
    public int min_val;
 
9
    public int max_val;
 
10
    public int current_val;
10
11
    
11
 
    public Variable (int32 min = 0, int32 max = 0, uint32 current = 0) {
12
 
      this.max_val.number = max;
13
 
      this.min_val.number = min;
14
 
      this.current_val.number = current;
 
12
    public Variable (int min = 0, int max = 0, int current = 0) {
 
13
      this.max_val = max;
 
14
      this.min_val = min;
 
15
      this.current_val = current;
15
16
    }
16
17
    
17
18
    [CCode (cname = "vqdr_expression_variable_copy")]
18
19
    public Variable.copy (Variable other) {
19
 
      this.max_val.copy (other.max_val);
20
 
      this.min_val.copy (other.min_val);
21
 
      this.current_val.copy (other.current_val);
 
20
      this.max_val = other.max_val;
 
21
      this.min_val = other.min_val;
 
22
      this.current_val = other.current_val;
22
23
    }
23
24
    
24
 
    public int64 compare (Variable other) {
25
 
      if (! this.current_val.equals (other.current_val)) {
26
 
        return this.current_val.compare (other.current_val);
27
 
      } else if (! this.max_val.equals (other.max_val)) {
28
 
        return this.max_val.compare (other.max_val);
29
 
      } else if (! this.min_val.equals (other.min_val)) {
30
 
        return this.min_val.compare (other.min_val);
31
 
      }
32
 
      return 0;
 
25
    public int compare (Variable other) {
 
26
      
 
27
      if (this.current_val > other.current_val) {
 
28
        return -1;
 
29
      } else if (this.current_val < other.current_val) {
 
30
        return 1;
 
31
      } else {
 
32
        if (this.max_val > other.max_val) {
 
33
          return -1;
 
34
        } else if (this.max_val < other.max_val) {
 
35
          return 1;
 
36
        } else {
 
37
          if (this.min_val > other.min_val) {
 
38
            return -1;
 
39
          } else if (this.min_val < other.min_val) {
 
40
            return 1;
 
41
          } // End min_val comp.
 
42
        } // End max_val comp
 
43
      } // End current_val Comp
 
44
      return 0; // They are exacly the same.
33
45
    }
34
46
    
35
47
    /**
38
50
     * This is the same as @c compare. It is an alias to the same C function.
39
51
     */
40
52
    [CCode (cname = "vqdr_expression_variable_equals")]
41
 
    public static extern int32 static_compare (Variable a, Variable b);
 
53
    public static extern int static_compare (Variable a, Variable b);
42
54
    
43
55
    /**
44
56
     * Is this instance equal to the other?
45
57
     */
46
58
    public bool equals (Variable other) {
47
 
      return !(bool) this.compare (other);
 
59
      return equals_values (other.min_val, other.max_val, other.current_val);
48
60
    }
49
61
    
50
 
    public bool equals_values (int32 min, int32 max, int32 current) {
51
 
      return (this.current_val.number == current) &&
52
 
             (this.max_val.number == max) &&
53
 
             (this.min_val.number == min);
 
62
    public bool equals_values (int min, int max, int current) {
 
63
      return (max == this.max_val) &&
 
64
             (min == this.min_val) &&
 
65
             (current == this.current_val);
54
66
    }
55
67
    
56
68
    
58
70
    public string to_string () {
59
71
      StringBuilder s = new StringBuilder ();
60
72
      s.append("(Variable: ");
61
 
      s.append_printf ("(max_val: %s, ", max_val.to_string (true));
62
 
      s.append_printf ("min_val: %s, ", max_val.to_string (true));
63
 
      s.append_printf ("current_val: %s)", current_val.to_string (true));
 
73
      s.append_printf ("(max_val: %i, ", max_val);
 
74
      s.append_printf ("min_val: %i, ", max_val);
 
75
      s.append_printf ("current_val: %i)", current_val);
64
76
      s.append(")");
65
77
      return s.str;
66
78
    }