/+junk/vala-bit-field

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/vala-bit-field
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* (c) Gustav Hartvigsson 2020

                        Cool Licence 1.0

0) You is granted to copy, redistrubute, modify, redistrubute
   modified copies of the software, in any shape or form.
1) You are not obligated to give credit the original author(s) of
   the sofware, but it would be cool of you if you did.
2) You are allowed to removed the copyright notice if you want to,
   it is up to you, but it would be cool if you did not.
 */

namespace BitField {
  
  private static GLib.Tree<string, Type?> list_of_types;
  
  private static GLib.Tree<FieldInfo?, uint16> mask_cache;
  
  public void init () {
    list_of_types = new GLib.Tree<string, Type?> ((a, b) => {
      return (GLib.strcmp (a,b));
    });
    
    mask_cache = new GLib.Tree<FieldInfo?, uint16> ((a, b) => {
       return a.compare (b);
    });
  }
  
  public void deinit () {
    list_of_types.foreach ((_key, _val) => {
      list_of_types.remove (_key);
      return false;
    });
  }
  
  public static bool add_type_v (string name, FieldInfo first_field, ...) {
    var va = va_list ();
    
    GLib.List<FieldInfo?> lst = new GLib.List<FieldInfo?> ();
    
    lst.append (first_field);
    for (FieldInfo? fi = va.arg<FieldInfo> (); fi != null;
                                               fi = va.arg<FieldInfo> ()) {
      lst.append (fi);
    }
    
    FieldInfo[] lst2 = new FieldInfo[lst.length ()];
    
    for (uint i = 0; i < lst.length (); i++) {
      lst2[i] = lst.nth_data (i);
    }
   
    return add_type (name, lst2);
  }
  
  /**
   * @Return true on error.
   */
  public bool add_type (string name, FieldInfo[] fields) {
    GLib.List<FieldInfo?> lst = new GLib.List<FieldInfo?> ();
    
    foreach  (FieldInfo fi in fields) {
      lst.append (fi);
    }
    
    if (lst.length () >= 16) {
      return true;
    }
    
    lst.sort ((a,b) => {return a.compare (b);});
    
    
    for (uint8 i = 0; i < lst.length (); i++) {
      var a = lst.nth_data (i); 
      // We valitade the items whilst we are at it.
      if (a.validate ()) {
        GLib.critical ("Validtion of FieldInfo object failed: (%s)",
                       a.to_string ());
        return true;
      }
      for (uint8 j = i + 1; i < lst.length (); j++) {
        var b = lst.nth_data (j);
        if (b == null) {
          break;
        }
        if (a.overlap (b)) {
          GLib.critical ("Overlappinng fields in \"%s\": (%s) (%s).\n" +
                         "\t Will not add bitmap type defitions.",
                         name,
                         a.to_string (),
                         b.to_string ());
          return true;
        }
      }
    }
    
    Type t = Type ();
    for (uint8 i = 0; i < lst.length (); i++) {
      t.fields[i] = lst.nth_data (i);
    }
    
    list_of_types.insert (name, t);
    
    // add the masks to the mask cach, so we don't have to re-calculate them
    // each time we need them.
    lst.foreach ((ii) => {
      mask_cache.insert (ii, ii.generate_mask ());
    });
    
    
    return false;
  }
  
  public void set (ref uint16 data,
                   string type_name,
                   int field_id,
                   uint16 in_data) {
    
    var tt = list_of_types.lookup (type_name);
    if (tt == null) {
      GLib.critical ("Name \"%s\" dose not exist among the types valid types.",
                     type_name);
      return;
    }
    var fi = tt.get_field_info (field_id);
    uint16 mask = mask_cache.lookup (fi);
    uint16 invert_mask = ~mask;
    uint16 tmp = data & invert_mask; // everything exept the field.
    
    uint16 tmp_mask = 1;
    for (uint8 i = 0; i < fi.length; i++) {
      tmp_mask <<= 1;
      tmp_mask += 1;
    }
    
    uint16 tmp2 = in_data & tmp_mask;
    
    
    uint16 distance = 15 - fi.end;
    
    
    tmp2 = tmp2 << distance;
    
    
    tmp2 = tmp2 | tmp;
    
    data = tmp2;
  }
  
  public uint16 get (uint16 data,
                     string type_name,
                     int field_id) {
    
    var fi = list_of_types.lookup (type_name).get_field_info (field_id);
    uint16 mask = mask_cache.lookup (fi);
    uint16 tmp = data & mask; // only what is in the field.
    
    
    uint16 distance = 15 - fi.end;
    
    
    tmp = tmp >> distance;
    return tmp;
  }
  
  Type? get_type (string name) {
    return list_of_types.lookup (name);
  } 
  
  /**
   * Create a new FieldInfo using the following syntax:
   * {{{
   *  enum MyTypeFields {
   *    F1,
   *    F2,
   *    //....
   *  }
   *
   *  BitField.FieldInfo[] my_fields_info = {
   *    BitField.FieldInfo (MyTypeFields.F1, 0, 5, 6),
   *    BitField.FieldInfo (MyTypeFields.F2, 0, 5, 6),
   *    //.....
   *  }
   *  
   *  if (FieldInfo.add_type ("MyType", my_fields_info)) {
   *    stderr.printf ("Something went wrong!");
   *  }
   * }}}
   * 
   */
  [CCode (cprefix="bit_field_info_", cname="BitFieldInfo")]
  public struct FieldInfo {
    int field_id;
    uint8 start;
    uint8 end;
    uint8 length;
    
    public FieldInfo (int field_id, uint8 start, uint8 end, uint8 length) {
      this.field_id = field_id;
      this.start = start;
      this.end = end;
      this.length = length; 
    }
    
    public int compare (FieldInfo other) {
      if (this.field_id != other.field_id) {
        return  this.field_id - other.field_id; 
      } else if (this.start != other.start) {
        return  this.start - other.start;
      } else if (this.end != other.end) {
        return this.end - other.end;
      } else if (this.length != other.length) {
        return this.length - other.length;
      }
      
      return 0;
    }
    
    [CCode (cname = "bit_field_info_compare")]
    public static extern int static_compare (FieldInfo a, FieldInfo b);
    
    
    
    public bool overlap (FieldInfo other) {
      return (!((this.start < other.end) || (this.end > other.start)));
    }
    
    [CCode (cname = "bit_field_info_overlap")]
    public static extern bool static_overlap (FieldInfo a, FieldInfo b);
    
    
    public string to_string () {
      return "field_id: %i start: %i, end: %i, length: %i".printf (
                                                      this.field_id,
                                                      this.start,
                                                      this.end,
                                                      this.length);
    }
    
    /**
     * returns true on error;
     */
    public bool validate () {
      var distance = this.end - this.start + 1;
      if (distance < 1 || distance != this.length) {
        return true;
      }
      return false;
    }
    
    [CCode (cname = "bit_field_info_validate")]
    public extern static bool static_validate (FieldInfo info);
    
    public uint16 generate_mask () {
      uint16 mask = 0;
      for (size_t i = 0; i < this.length; i++) {
        mask >>= 1; // shit it over to the right one.
        mask += 0x8000; // set the left-most bit in the field
      }
      
      // Shift over the mask to where it should start.
      mask >>= this.start; 
      
      return mask;
    }
    
    [CCode (cname = "bit_field_info_generate_mask")]
    public extern static uint16 static_generate_mask (FieldInfo info);
  }
  
  public struct Type {
    FieldInfo[] fields;
    
    Type () {
        fields = new FieldInfo[16];
        for (uint8 i = 0; i < 16; i++) {
          fields[i] = {255,255,255,255};
        }
    }
    
    public string to_string () {
      var sb = new GLib.StringBuilder ();
      
      sb.append (typeof (Type).name ())
        .append (": (\n");
      for (size_t i = 0; i < fields.length; i++) {
          sb.append ("\t (")
            .append (fields[i].to_string ())
            .append (")\n");
      }
      
      sb.append (")\n");
      return sb.str;
    }
    
    public FieldInfo get_field_info (int field_id) {
      
      FieldInfo ii = {0};
      
      foreach (FieldInfo ij in fields) {
        if (ij.field_id == field_id) {
          ii = ij;
        }
      }
      
      return ii;
    }
    
  }
  
}