aboutsummaryrefslogtreecommitdiff
path: root/examples/vec.ct
blob: dee50ca95dbb63294e1ab0ad4fdd867f8b5a54fd (plain) (blame)
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
type vectorable {
	std;
	/* this would be needed for comparisons like find() or something */
	comparable;
}

type vec {t: vectorable} {
	std_vector: void;
	len: usize;
	buf: 't;
}

pub define vec(t) { struct (vec{t}) }

pub init(v: 'vector)
{
	v.len = 0;
	v.buf = null;
}

pub length(v: *vec => usize) { return v.len; }

pub index(v: *vec, i: usize => '#v.buf)
{
	assert(i < v.len, "index %zu out of bounds\n", i);
	return &v.buf[i];
}

pub index(v: mut *vec, i: const isize => '#v.buf)
{
	if i < 0 {
		assert(-i < v.len, "reverse index %zi out of bounds\n", i);
		return &v.buf[v.len + i];
	}

	assert(i < v.len, "index %zi out of bounds\n", i);
	return &v.buf[i];
}

pub prepend(v: mut 'vec, e: '#v.buf) { insert(v, e, 0); }
pub append(v: mut 'vec, e: '#v.buf) { insert(v, e, v.len); }

pub preplace(v: mut 'vec, e: '#v.buf) { place(v, e, 0); }
pub applace(v: mut 'vec, e: '#v.buf) { place(v, e, v.len - 1); }

pub place(v: mut *vec, e: '#v.buf, i: usize)
{
}

pub insert(v: mut *vec, e^ '#v.buf, i: usize)
{
}

pub deinit(mut v *vec)
{
	for i usize : v {
		deinit(v[i]);
		v[i] = null;
	}

	dealloc(v.buf);
}