/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-06 21:24:51 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20200606212451-4qiz5gog39p3stb2
* added ignore for build directory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
namespace BitMap {
2
 
 
3
 
 
 
1
/* (c) Gustav Hartvigsson 2020
 
2
 
 
3
                        Cool Licence 1.0
 
4
 
 
5
0) You is granted to copy, redistrubute, modify, redistrubute
 
6
   modified copies of the software, in any shape or form.
 
7
1) You are not obligated to give credit the original author(s) of
 
8
   the sofware, but it would be cool of you if you did.
 
9
2) You are allowed to removed the copyright notice if you want to,
 
10
   it is up to you, but it would be cool if you did not.
 
11
 */
 
12
 
 
13
namespace BitField {
 
14
  
 
15
  private static GLib.Tree<string, Type?> list_of_types;
 
16
  
 
17
  private static GLib.Tree<FieldInfo?, uint16> mask_cache;
 
18
  
 
19
  void init () {
 
20
    list_of_types = new GLib.Tree<string, Type?> ((a, b) => {
 
21
      return (GLib.strcmp (a,b));
 
22
    });
 
23
    
 
24
    mask_cache = new GLib.Tree<FieldInfo?, uint16> ((a, b) => {
 
25
       return a.compare (b);
 
26
    });
 
27
  }
 
28
  
 
29
  void deinit () {
 
30
    list_of_types.foreach ((_key, _val) => {
 
31
      list_of_types.remove (_key);
 
32
      
 
33
      return false;
 
34
    });
 
35
  }
 
36
  
 
37
  static bool add_type_v (string name, FieldInfo first_field, ...) {
 
38
    var va = va_list ();
 
39
    
 
40
    GLib.List<FieldInfo?> lst = new GLib.List<FieldInfo?> ();
 
41
    
 
42
    lst.append (first_field);
 
43
    for (FieldInfo? fi = va.arg<FieldInfo> (); fi != null;
 
44
                                               fi = va.arg<FieldInfo> ()) {
 
45
      lst.append (fi);
 
46
    }
 
47
    
 
48
    FieldInfo[] lst2 = new FieldInfo[lst.length ()];
 
49
    
 
50
    add_type (name, lst2);
 
51
    
 
52
    return false;
 
53
  }
 
54
  
 
55
  /**
 
56
   * @Return true on error.
 
57
   */
 
58
  public bool add_type (string name, FieldInfo[] fields) {
 
59
    GLib.List<FieldInfo?> lst = new GLib.List<FieldInfo?> ();
 
60
    
 
61
    foreach  (FieldInfo fi in fields) {
 
62
      lst.append (fi);
 
63
    }
 
64
    
 
65
    if (lst.length () >= 16) {
 
66
      return true;
 
67
    }
 
68
    
 
69
    lst.sort ((a,b) => {return a.compare (b);});
 
70
    
 
71
    
 
72
    for (uint8 i = 0; i < lst.length (); i++) {
 
73
      var a = lst.nth_data (i); 
 
74
      // We valitade the items whilst we are at it.
 
75
      if (a.validate ()) {
 
76
        GLib.critical ("Validtion of FieldInfo object failed: (%s)",
 
77
                       a.to_string ());
 
78
        return true;
 
79
      }
 
80
      for (uint8 j = i + 1; i < lst.length (); j++) {
 
81
        var b = lst.nth_data (j);
 
82
        if (b == null) {
 
83
          break;
 
84
        }
 
85
        if (a.overlap (b)) {
 
86
          GLib.critical ("Overlappinng fields in \"%s\": (%s) (%s).\n" +
 
87
                         "\t Will not add bitmap type defitions.",
 
88
                         name,
 
89
                         a.to_string (),
 
90
                         b.to_string ());
 
91
          return true;
 
92
        }
 
93
      }
 
94
    }
 
95
    
 
96
    Type t = Type ();
 
97
    for (uint8 i = 0; i < lst.length (); i++) {
 
98
      t.fields[i] = lst.nth_data (i);
 
99
    }
 
100
    
 
101
    list_of_types.insert (name, t);
 
102
    
 
103
    // add the masks to the mask cach, so we don't have to re-calculate them
 
104
    // each time we need them.
 
105
    lst.foreach ((ii) => {
 
106
      mask_cache.insert (ii, ii.generate_mask ());
 
107
    });
 
108
    
 
109
    
 
110
    return false;
 
111
  }
 
112
  
 
113
  public void set (ref uint16 data,
 
114
                   string type_name,
 
115
                   int field_id,
 
116
                   uint16 in_data) {
 
117
    
 
118
    var tt = list_of_types.lookup (type_name);
 
119
    if (tt == null) {
 
120
      GLib.critical ("Name \"%s\" dose not exist among the types valid types.",
 
121
                     type_name);
 
122
      return;
 
123
    }
 
124
    var fi = tt.get_field_info (field_id);
 
125
    uint16 mask = mask_cache.lookup (fi);
 
126
    uint16 invert_mask = ~mask;
 
127
    uint16 tmp = data & invert_mask; // everything exept the field.
 
128
    
 
129
    uint16 tmp_mask = 1;
 
130
    for (uint8 i = 0; i < fi.length; i++) {
 
131
      tmp_mask <<= 1;
 
132
      tmp_mask += 1;
 
133
    }
 
134
    
 
135
    uint16 tmp2 = in_data & tmp_mask;
 
136
    
 
137
    
 
138
    uint16 distance = 15 - fi.end;
 
139
    
 
140
    
 
141
    tmp2 = tmp2 << distance;
 
142
    
 
143
    
 
144
    tmp2 = tmp2 | tmp;
 
145
    
 
146
    data = tmp2;
 
147
  }
 
148
  
 
149
  public uint16 get (uint16 data,
 
150
                     string type_name,
 
151
                     int field_id) {
 
152
    
 
153
    var fi = list_of_types.lookup (type_name).get_field_info (field_id);
 
154
    uint16 mask = mask_cache.lookup (fi);
 
155
    uint16 tmp = data & mask; // only what is in the field.
 
156
    
 
157
    
 
158
    uint16 distance = 15 - fi.end;
 
159
    
 
160
    
 
161
    tmp = tmp >> distance;
 
162
    return tmp;
 
163
  }
 
164
  
 
165
  Type? get_type (string name) {
 
166
    return list_of_types.lookup (name);
 
167
  } 
 
168
  
 
169
  /**
 
170
   * Create a new FieldInfo using the following syntax:
 
171
   * {{{
 
172
   * }}}
 
173
   * 
 
174
   */
 
175
  public struct FieldInfo {
 
176
    int field_id;
 
177
    uint8 start;
 
178
    uint8 end;
 
179
    uint8 length;
 
180
    GLib.pointer padding;
 
181
    
 
182
    public FieldInfo (int field_id, uint8 start, uint8 end, uint8 length) {
 
183
      this.field_id = field_id;
 
184
      this.start = start;
 
185
      this.end = end;
 
186
      this.length = length; 
 
187
      this.padding = null;
 
188
    }
 
189
    
 
190
    public int compare (FieldInfo other) {
 
191
      if (this.field_id != other.field_id) {
 
192
        return  this.field_id - other.field_id; 
 
193
      } else if (this.start != other.start) {
 
194
        return  this.start - other.start;
 
195
      } else if (this.end != other.end) {
 
196
        return this.end - other.end;
 
197
      } else if (this.length != other.length) {
 
198
        return this.length - other.length;
 
199
      }
 
200
      
 
201
      #if 0
 
202
      if (this.start > other.start) {
 
203
        return -1;
 
204
      } else if (this.start < other.start) {
 
205
        return 1;
 
206
      } else {
 
207
        if (this.end > other.end) {
 
208
          return -1;
 
209
        } else if (this.end < other.end) {
 
210
          return 1;
 
211
        } else {
 
212
          if (this.length > other.length) {
 
213
            return -1;
 
214
          } else if (this.length < other.length) {
 
215
            return 1;
 
216
          }
 
217
        }
 
218
      }
 
219
      #endif
 
220
      
 
221
      return 0;
 
222
    }
 
223
    
 
224
    [CCode (cname = "bit_field_field_info_compare")]
 
225
    public static extern int static_campare (FieldInfo a, FieldInfo b);
 
226
    
 
227
    
 
228
    public bool overlap (FieldInfo other) {
 
229
      return (!((this.start < other.end) || (this.end > other.start)));
 
230
    }
 
231
    
 
232
    [CCode (cname = "bit_field_field_info_overlap")]
 
233
    public static extern int static_overlap (FieldInfo a, FieldInfo b);
 
234
    
 
235
    
 
236
    public string to_string () {
 
237
      return "field_id: %i start: %i, end: %i, length: %i".printf (
 
238
                                                      this.field_id,
 
239
                                                      this.start,
 
240
                                                      this.end,
 
241
                                                      this.length);
 
242
    }
 
243
    
 
244
    /**
 
245
     * returns true on error;
 
246
     */
 
247
    public bool validate () {
 
248
      var distance = this.end - this.start + 1;
 
249
      if (distance < 1 || distance != this.length) {
 
250
        return true;
 
251
      }
 
252
      return false;
 
253
    }
 
254
    
 
255
    [CCode (cname = "bit_field_field_info_validate")]
 
256
    public extern static bool static_validate (FieldInfo info);
 
257
    
 
258
    public uint16 generate_mask () {
 
259
      uint16 mask = 0;
 
260
      for (size_t i = 0; i < this.length; i++) {
 
261
        mask >>= 1; // shit it over to the right one.
 
262
        mask += 0x8000; // set the left-most bit in the field
 
263
      }
 
264
      
 
265
      // Shift over the mask to where it should start.
 
266
      mask >>= this.start; 
 
267
      
 
268
      return mask;
 
269
    }
 
270
    
 
271
    [CCode (cname = "bit_field_field_generate_mask")]
 
272
    public extern static uint16 static_generate_mask (FieldInfo info);
 
273
  }
 
274
  
 
275
  public struct Type {
 
276
    FieldInfo[] fields;
 
277
    
 
278
    Type () {
 
279
        fields = new FieldInfo[16];
 
280
        for (uint8 i = 0; i < 16; i++) {
 
281
          fields[i] = {255,255,255,255};
 
282
        }
 
283
    }
 
284
    
 
285
    public string to_string () {
 
286
      var sb = new GLib.StringBuilder ();
 
287
      
 
288
      sb.append (typeof (Type).name ())
 
289
        .append (": (\n");
 
290
      for (size_t i = 0; i < fields.length; i++) {
 
291
          sb.append ("\t (")
 
292
            .append (fields[i].to_string ())
 
293
            .append (")\n");
 
294
      }
 
295
      
 
296
      sb.append (")\n");
 
297
      return sb.str;
 
298
    }
 
299
    
 
300
    public FieldInfo get_field_info (int field_id) {
 
301
      
 
302
      FieldInfo ii = {0};
 
303
      
 
304
      foreach (FieldInfo ij in fields) {
 
305
        if (ij.field_id == field_id) {
 
306
          ii = ij;
 
307
        }
 
308
      }
 
309
      
 
310
      return ii;
 
311
    }
 
312
    
 
313
  }
 
314
  
4
315
}