/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/utils/named_vector.vala

  • Committer: Gustav Hartvigsson
  • Date: 2020-06-07 18:48:24 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20200607184824-jf14f7a1b1di2i2q
* Initial code - far from done

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * The contects of this file is in the Public Domain.
3
 
 *
4
 
 * Created by Gustav Hartivgsson.
5
 
 */
6
 
[CCode (cname = "V", cprefix = "v_")]
7
 
namespace Utils {
8
 
  [CCode (cname = "VNamedVector", cprefix = "v_named_vector_")]
9
 
  public class NamedVector<T> {
10
 
    public T[] values;
11
 
    public string[] names;
12
 
 
13
 
    public delegate bool ForeachFunc<V> (string name, V val);
14
 
 
15
 
    public NamedVector (string n1, T v1,...) {
16
 
      // allocate temporary buffer
17
 
      var T_buf = new T[50];
18
 
      var N_buf = new string[50];
19
 
      T_buf[0] = v1;
20
 
      N_buf[0] = n1;
21
 
      var list = va_list ();
22
 
      
23
 
      var tmp_name = list.arg<string?> ();
24
 
      var tmp_val = list.arg<T?> ();
25
 
      size_t i = 1;
26
 
 
27
 
      while (tmp_name != null && tmp_val != null) {
28
 
        T_buf[i] = tmp_val; N_buf[i] = tmp_name;
29
 
        i++;
30
 
        tmp_name = list.arg<string?> ();
31
 
        tmp_val = list.arg<T?> ();
32
 
      }
33
 
      values = new T[i];
34
 
      names = new string[i];
35
 
      for (var j = 0; j < i; j++) {
36
 
        values[j] = T_buf[j];
37
 
        names[j] = N_buf[j];
38
 
      }
39
 
 
40
 
      assert (this.names != null);
41
 
      assert (this.values != null);
42
 
    }
43
 
 
44
 
    public Pair @get (int i) {
45
 
      var ret_val = new Pair<string, T> (this.names[i], this.values[i]);
46
 
      return ret_val;
47
 
    } 
48
 
 
49
 
    public void @foreach (ForeachFunc<T> funk) {
50
 
      for (var i = 0; i < values.length; i++) {
51
 
        if (!funk (names[i], values[i])) {
52
 
          break;
53
 
        }
54
 
      }
55
 
    }
56
 
  }
57
 
}