summaryrefslogtreecommitdiff
path: root/tnslc/vector.tnsl
blob: ecb52e1e8cdda667ad59aec676a1dd2a75e8764a (plain)
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
struct Vector {
    uint
        el_size,
        num_el,
        dat_size,
    ~uint8 dat
}

/; method Vector

    /; resize (uint num_el)
        self.dat_size = num_el
        self.dat = _realloc(self.dat, num_el * self.el_size)
    ;/

    /; get (uint i) [~uint8]
        /; if (i !< self.num_el)
            return self.dat
        ;/
        return self.dat + (i * self.el_size)
    ;/

    /; set (uint i, ~uint8 data)
        ~uint8 index = self.get(i)
        /; loop (i = 0; i < self.el_size) [i++]
            index` = data`
            index++
            data++
        ;/
    ;/

    /; push (~uint8 data)
        self.num_el++
        self.set(self.num_el - 1, data)
        /; if (self.num_el !< self.dat_size)
            self.resize(2 * self.dat_size)
        ;/
    ;/

    /; pop
        self.num_el--
        /; if (self.num_el !== 0 && self.num_el < self.dat_size / 4)
            self.resize(self.dat_size / 2)
        ;/
    ;/

    /; remove (int index)
        index++
        /; loop (index < self.num_el) [index++]
            self.set(index - 1, self.get(index))
        ;/
        self.pop()
    ;/

    /; start (int el_size)
        self.num_el = 0
        self.el_size = el_size
        self.dat_size = 1
        self.dat = _alloc(self.el_size)
    ;/

    /; clean
        self.num_el = 0
        self.el_size = 0
        self.dat_size = 0
        _delete(self.dat)
        self.dat = 0
    ;/
;/