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

  • Committer: Gustav Hartvigsson
  • Date: 2024-12-21 23:51:45 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20241221235145-4metak6z6u6vf6b0
[General] Major refactoring. Utils -> Vee nenaming.
Todo: Split libvee into a different library??

Also: Fixed spelling mistakes.

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
 
 
7
namespace Vee {
 
8
  [Compact]
 
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;
 
29
        N_buf[i] = tmp_name;
 
30
        i++;
 
31
        tmp_name = list.arg<string?> ();
 
32
        tmp_val = list.arg<T?> ();
 
33
      }
 
34
      values = new T[i];
 
35
      names = new string[i];
 
36
      for (var j = 0; j < i; j++) {
 
37
        values[j] = T_buf[j];
 
38
        names[j] = N_buf[j];
 
39
      }
 
40
 
 
41
      assert (this.names != null);
 
42
      assert (this.values != null);
 
43
    }
 
44
 
 
45
    public Pair @get (int i) {
 
46
      var ret_val = new Pair<string, T> (this.names[i], this.values[i]);
 
47
      return ret_val;
 
48
    } 
 
49
 
 
50
    public void @foreach (ForeachFunc<T> funk) {
 
51
      for (var i = 0; i < values.length; i++) {
 
52
        if (!funk (names[i], values[i])) {
 
53
          break;
 
54
        }
 
55
      }
 
56
    }
 
57
  }
 
58
}