/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk
49 by Gustav Hartvigsson
Added NamedVector
1
namespace Utils {
2
  [Compact] // Using compact does not compile for some reason.
3
  public class NamedVector<T> {
4
    public T[] values;
5
    public string[] names;
6
7
    public delegate bool ForeachFunc<V> (string name, V val);
8
9
    public NamedVector (string n1, T v1,...) {
10
      // allocate temporary buffer
11
      var T_buf = new T[50];
12
      var N_buf = new string[50];
13
      T_buf[0] = v1;
14
      N_buf[0] = n1;
15
      var list = va_list ();
16
      
17
      var tmp_name = list.arg<string?> ();
18
      var tmp_val = list.arg<T?> ();
19
      size_t i = 1;
20
21
      while (tmp_name != null && tmp_val != null) {
22
        T_buf[i] = tmp_val; N_buf[i] = tmp_name;
23
        i++;
24
        tmp_name = list.arg<string?> ();
25
        tmp_val = list.arg<T?> ();
26
      }
27
      values = new T[i];
28
      names = new string[i];
29
      for (var j = 0; j < i; j++) {
30
        values[j] = T_buf[j];
31
        names[j] = N_buf[j];
32
      }
33
34
      assert (this.names != null);
35
      assert (this.values != null);
36
    }
37
38
    public Pair @get (int i) {
39
      var ret_val = new Pair<string, T> (this.names[i], this.values[i]);
40
      return ret_val;
41
    } 
42
43
    public void @foreach (ForeachFunc<T> funk) {
44
      for (var i = 0; i < values.length; i++) {
45
        if (!funk (names[i], values[i])) {
46
          break;
47
        }
48
      }
49
    }
50
  }
51
}