summaryrefslogtreecommitdiff
path: root/tnslc/utils/vector.tnsl
blob: d00d698ef4571c74f20a9661ecbce63b65afe2bf (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
struct Vector {
	~void data,
	uint
		count,
		size,
		_elsz
}

uint VECTOR_MIN_ELEMENTS = 4
uint VECTOR_MAX_GROW = 256

~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)
	;/

	/; from_cstr(~uint8 cstr)
		self.init(1)
		self.push_cstr(cstr)
	;/

	/; _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 + 1)
			/; 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++]
			~uint8 to = start + i
			~uint8 from = el + i
			to` = from`
		;/
		self.count++
	;/

	/; _shrink(uint i)
		/; if (i !< self.size)
			self.size = 1
		;; else
			self.size = self.size - i
		;/

		self.data = _realloc(self.data, self.size * self._elsz)
	;/

	/; pop
		self.count--

		/; if (self.count < self.size / 2)
			self._shrink(self.size / 3)
		;/
	;/

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

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

	/; as_cstr [~uint8]
		~uint8 z = self.data + self.count
		z` = 0
		return self.data
	;/

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


# Artifacts

struct Artifact {
	~~uint8 strings,
	uint
		size,
		count
}

/; method Artifact

	/; init
		self.size = VECTOR_MIN_ELEMENTS
		self.count = 0
		self.strings = _alloc(len self.strings * self.size)
	;/

	/; split_cstr (~uint8 str, uint8 split)
		Vector track
		track.init(1)

		/; loop (str` !== 0) [str++]
			/; if (str` == split)
				self.push(track.as_cstr())
				track.init(1)
			;; else
				track.push(str)
			;/
		;/

		self.push(track.as_cstr())
	;/

	/; _grow (uint i)
		self.size = self.size + i
		self.strings = _realloc(self.strings, self.size * len self.strings)
	;/

	/; push (~uint8 str)
		/; if (self.size == self.count + 1)
			/; if (self.size < VECTOR_MAX_GROW)
				self._grow(self.size)
			;; else
				self._grow(VECTOR_MAX_GROW)
			;/
		;/

		self.strings{self.count} = str
		self.count++
	;/

	/; _shrink(uint i)
		/; if (i !< self.size)
			self.size = 1
		;; else
			self.size = self.size - i
		;/

		self.strings = _realloc(self.strings, self.size * len self.strings)
	;/

	/; pop
		_delete(self.get(self.count - 1))
		self.count--

		/; if (self.count < self.size / 2)
			self._shrink(self.size / 3)
		;/
	;/

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

		return self.strings{index}
	;/
	
	/; to_cstr (uint8 join) [~uint8]
		Vector out
		out.init(1)

		/; loop (int i = 0; i < self.count) [i++]
			out.push_cstr(self.get(i))
			/; if (i < self.count - 1)
				out.push(~join)
			;/
		;/

		return out.as_cstr()
	;/

	/; end
		/; loop (int i = 0; i < self.count) [i++]
			_delete(self.get(i))
		;/
		_delete(self.strings)
	;/
;/