summaryrefslogtreecommitdiff
path: root/tnslc/vector.tnsl
blob: 87a49e9c65be18cd91c2ed51a0e69a0daf0cc176 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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)
        ;; else if (self.num_el < 0)
			self.num_el = 0
		;/
    ;/

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

    /; copy (Vector v)
        self.dat_size = v.dat_size
        self.num_el = v.num_el
        self.el_size = v.el_size
        self.dat = _alloc(self.dat_size * self.el_size)
        
        /; loop (int i = 0; i < self.num_el) [i++]
            self.set(i, v.dat + i * self.el_size)
        ;/
    ;/
    
    /; add (Vector v)
        /; loop (int i = 0; i < v._len()) [i++]
            self.push(v.get(i))
        ;/
    ;/

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

    /; _len [uint]
        return self.num_el
    ;/

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