summaryrefslogtreecommitdiff
path: root/tnslc/vector.tnsl
blob: c8450acf2c63243496cdd515737f57e80c0b0af4 (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
struct Vector {
	~void data,
	uint
		count,
		size,
		_elsz
}

uint VECTOR_MIN_ELEMENTS = 4
uint VECTOR_MAX_GROW = 256
~void NULL = 0

~uint8 NUM_STR = "Num %d\n\0"

/; method Vector
	
	/; init (uint elsz)
		self._elsz = elsz
		self.size = VECTOR_MIN_ELEMENTS
		self.count = 0
		self.data = _alloc(self.size * self._elsz)
	;/

	/; _grow (uint i)
		self.size = self.size + i
		self.data = _realloc(self.data, self.size * self._elsz)
	;/

	/; get (uint index) [~void]
		/; if (index !< self.count)
			return NULL
		;/

		return self.data + index * self._elsz
	;/

	/; push (~void el)
		/; if (self.size == self.count)
			/; if (self.size < VECTOR_MAX_GROW)
				self._grow(self.size)
			;; else
				self._grow(VECTOR_MAX_GROW)
			;/
		;/

		~void start = self.data + self.count * self._elsz
		/; loop (int i = 0; i < self._elsz) [i++]
			~void to = start + i
			~void from = el + i
			to` = from`
		;/
		self.count++
	;/

	/; push_char (uint8 ch)
		self.push(~ch)
	;/

	/; push_cstr(~uint8 ch)
		/; loop (ch` !== 0) [ch++]
			self.push(ch)
		;/
		self.push(ch)
	;/

	/; end
		self.count = 0
		self.size = 0
		self._elsz = 0
		_delete(self.data)
	;/
;/