/bitfield/trunk

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

« back to all changes in this revision

Viewing changes to src/bit_map.vala

  • Committer: Gustav Hartvigsson
  • Date: 2020-06-05 21:05:07 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20200605210507-cf47ohzmnet3ohc4
* encountered a bug in Valac:
https://gitlab.gnome.org/GNOME/vala/-/issues/1003

This makes it so I can't continue with this in any sane way.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
namespace BitMap {
2
 
 
3
 
 
 
1
namespace BitField {
 
2
  
 
3
  private static GLib.Tree<string, Type?> list_of_types;
 
4
  
 
5
  private static GLib.Tree<FieldInfo?, uint16> mask_cash;
 
6
  
 
7
  void init () {
 
8
    list_of_types = new GLib.Tree<string, Type?> ((a, b) => {
 
9
      return (GLib.strcmp (a,b));
 
10
    });
 
11
    
 
12
    mask_cash = new GLib.Tree<FieldInfo?, uint16> ((a, b) => {
 
13
       return a.compare (b);
 
14
    });
 
15
  }
 
16
  
 
17
  void deinit () {
 
18
    list_of_types.foreach ((_key, _val) => {
 
19
      list_of_types.remove (_key);
 
20
      
 
21
      return false;
 
22
    });
 
23
  }
 
24
  
 
25
  /**
 
26
   * @Return true on error.
 
27
   */
 
28
  public bool add_type (string name, FieldInfo first_field, ...) {
 
29
    var va = va_list ();
 
30
    
 
31
    GLib.List<FieldInfo?> lst = new GLib.List<FieldInfo?> ();
 
32
    
 
33
    lst.append (first_field);
 
34
    for (FieldInfo? fi = va.arg<FieldInfo> (); fi != null;
 
35
                                               fi = va.arg<FieldInfo> ()) {
 
36
      lst.append (fi);
 
37
    }
 
38
    
 
39
    if (lst.length () >= 16) {
 
40
      return true;
 
41
    }
 
42
    
 
43
    lst.sort ((a,b) => {return a.compare (b);});
 
44
    
 
45
    
 
46
    for (uint8 i = 0; i < lst.length (); i++) {
 
47
      var a = lst.nth_data (i); 
 
48
      // We valitade the items whilst we are at it.
 
49
      if (a.validate ()) {
 
50
        return true;
 
51
      }
 
52
      for (uint8 j = i + 1; i < lst.length (); j++) {
 
53
        var b = lst.nth_data (i);
 
54
        
 
55
        
 
56
        if (a.overlap (b)) {
 
57
          GLib.critical ("Overlappinng fields in %s: (%s) (%s).\n" +
 
58
                         "\t Will not add bitmap type defitions.",
 
59
                         lst.nth_data (i).to_string (),
 
60
                         lst.nth_data (j).to_string ());
 
61
          return true;
 
62
        }
 
63
      }
 
64
    }
 
65
    
 
66
    Type t = Type ();
 
67
    for (uint8 i = 0; i < lst.length (); i++) {
 
68
      t.fields[i] = lst.nth_data (i);
 
69
    }
 
70
    
 
71
    return false;
 
72
  }
 
73
  
 
74
  public void set_8 (ref uint8 field_id, string type_name, uint8 data) {
 
75
    
 
76
    
 
77
    
 
78
  }
 
79
  
 
80
  public struct FieldInfo {
 
81
    uint8 field_id;
 
82
    uint8 start;
 
83
    uint8 end;
 
84
    uint8 length;
 
85
    
 
86
    public int compare (FieldInfo other) {
 
87
      if (this.field_id != other.field_id) {
 
88
        return other.field_id - this.field_id; 
 
89
      } else if (this.start != other.start) {
 
90
        return other.start - this.start;
 
91
      } else if (this.end != other.end) {
 
92
        return other.end - this.end;
 
93
      } else if (this.length != other.length) {
 
94
        return other.length - this.length;
 
95
      }
 
96
      
 
97
      #if 0
 
98
      if (this.start > other.start) {
 
99
        return -1;
 
100
      } else if (this.start < other.start) {
 
101
        return 1;
 
102
      } else {
 
103
        if (this.end > other.end) {
 
104
          return -1;
 
105
        } else if (this.end < other.end) {
 
106
          return 1;
 
107
        } else {
 
108
          if (this.length > other.length) {
 
109
            return -1;
 
110
          } else if (this.length < other.length) {
 
111
            return 1;
 
112
          }
 
113
        }
 
114
      }
 
115
      #endif
 
116
      
 
117
      return 0;
 
118
    }
 
119
    
 
120
    [CCode (cname = "bit_field_field_info_compare")]
 
121
    public static extern int static_campare (FieldInfo a, FieldInfo b);
 
122
    
 
123
    
 
124
    public bool overlap (FieldInfo other) {
 
125
      return (!((this.start < other.end) || (this.end > other.start)));
 
126
    }
 
127
    
 
128
    [CCode (cname = "bit_field_field_info_overlap")]
 
129
    public static extern int static_overlap (FieldInfo a, FieldInfo b);
 
130
    
 
131
    
 
132
    public string to_string () {
 
133
      return "start: %i, end: %i, length: %i".printf (this.start,
 
134
                                                      this.end,
 
135
                                                      this.length);
 
136
    }
 
137
    
 
138
    /**
 
139
     * returns true on error;
 
140
     */
 
141
    public bool validate () {
 
142
      var distance = this.start - this.end;
 
143
      if (distance < 1 || distance != this.length) {
 
144
        GLib.critical ("Validtion if FieldInfo object failed: (%s)",
 
145
                       this.to_string ());
 
146
        return true;
 
147
      }
 
148
      return false;
 
149
    }
 
150
    
 
151
    [CCode (cname = "bit_field_field_info_validate")]
 
152
    public extern static bool static_validate (FieldInfo info);
 
153
    
 
154
    public uint16 generate_mask () {
 
155
      uint16 mask = 0;
 
156
      for (size_t i = 0; i < this.length; i++) {
 
157
        mask += 0x8000; // set the left-most bit in the field
 
158
        mask >> 1; // shit it over to the right one.
 
159
      }
 
160
      
 
161
      mask >> this.start;
 
162
      
 
163
      return mask;
 
164
    }
 
165
    
 
166
    [CCode (cname = "bit_field_field_generate_mask")]
 
167
    public extern static uint16 static_generate_mask (FieldInfo info);
 
168
  }
 
169
  
 
170
  private struct Type {
 
171
    FieldInfo[] fields;
 
172
    
 
173
    Type () {
 
174
        fields = new FieldInfo[16];
 
175
    }
 
176
    
 
177
    public string to_string () {
 
178
      var sb = new GLib.StringBuilder ();
 
179
      
 
180
      sb.append (typeof (Type).name ())
 
181
        .append (": (\n");
 
182
      for (size_t i = 0; i < fields.length; i++) {
 
183
          sb.append ("\t (")
 
184
            .append (fields[i].to_string ())
 
185
            .append (")\n");
 
186
      }
 
187
      
 
188
      sb.append (")\n");
 
189
      return sb.str;
 
190
    }
 
191
  }
 
192
  
 
193
  
 
194
  
 
195
  
4
196
}