Browse Source

Added many missing modules and files.

Stan Hebben 6 years ago
parent
commit
ea73cfcf1a

+ 1
- 1
collections/src/HashSet.zs View File

@@ -3,7 +3,7 @@ export class HashSet<T> {
3 3
 		add(value as T) as bool;
4 4
 		remove(value as T) as bool;
5 5
 		
6
-		get size as int;
6
+		get size as usize;
7 7
 		
8 8
 		in(value as T) as bool;
9 9
 		for(x as T);

+ 8
- 13
collections/src/LinkedList.zs View File

@@ -1,9 +1,9 @@
1 1
 export class LinkedList<T> {
2 2
 	var first as Node?;
3 3
 	var last as Node?;
4
-	var size as int : get;
4
+	var size as usize : get;
5 5
 	
6
-	public get empty as bool
6
+	public get isEmpty as bool
7 7
 		=> first == null;
8 8
 	
9 9
 	public add(value as T) as void {
@@ -23,26 +23,21 @@ export class LinkedList<T> {
23 23
 		size = 0;
24 24
 	}
25 25
 	
26
-	public [](index as int) as T {
26
+	[Precondition(ENFORCE, index < size, "Index out of bounds")]
27
+	public [](index as usize) as T {
27 28
 		var node = first;
28
-		while index > 0 {
29
-			if node == null
30
-				return panic<T>("index out of bounds");
31
-			
29
+		while index > 0 && node != null
32 30
 			node = node.next;
33
-		}
34 31
 		
35 32
 		if node == null
36
-			return panic<T>("index out of bounds");
33
+			panic "index out of bounds";
37 34
 		
38 35
 		return node.value;
39 36
 	}
40 37
 	
41 38
 	public implements Queue<T> {
39
+		[Precondition(ENFORCE, first != null, "Cannot poll an empty queue")]
42 40
 		poll() as T {
43
-			if first == null
44
-				return panic<T>("Cannot poll an empty queue");
45
-			
46 41
 			val result = first.value;
47 42
 			first = first.next;
48 43
 			if first == null
@@ -54,7 +49,7 @@ export class LinkedList<T> {
54 49
 		}
55 50
 		
56 51
 		peek() as T? {
57
-			return first.value;
52
+			return first == null ? null : first.value;
58 53
 		}
59 54
 		
60 55
 		offer(value as T) as void

+ 1
- 1
collections/src/Set.zs View File

@@ -2,7 +2,7 @@ export interface Set<T> {
2 2
 	add(value as T) as bool;
3 3
 	remove(value as T) as bool;
4 4
 	
5
-	get size as int;
5
+	get size as usize;
6 6
 	
7 7
 	toArray();
8 8
 	toArray(comparator as function(a as T, b as T) as int);

+ 13
- 16
collections/src/Stack.zs View File

@@ -1,21 +1,18 @@
1
+import stdlib.List;
2
+
1 3
 export class Stack<T> {
2
-	var values as T[] = new T[](8);
3
-	var size as int : get = 0;
4
+	var values as List<T> = new List<T>();
4 5
 	
5
-	public push(value as T) as void {
6
-		if size == values.length
7
-			values = values.copy(values.length * 2);
8
-			
9
-		values[size++] = value;
10
-	}
6
+	public push(value as T) as void
7
+		=> values.add(value);
11 8
 	
12
-	public pop() as T {
13
-		if size == 0
14
-			throw new NoSuchElementException("Stack is empty!");
15
-		
16
-		return values[--size];
17
-	}
9
+	public get size as usize
10
+		=> values.length;
18 11
 	
19
-	public get empty as bool
20
-		=> size == 0;
12
+	[Precondition(ENFORCE, size > 0, "Cannot pop an empty stack")]
13
+	public pop() as T
14
+		=> values.remove(values.length - 1);
15
+	
16
+	public get isEmpty as bool
17
+		=> values.isEmpty;
21 18
 }

+ 6
- 0
compactio/module.json View File

@@ -0,0 +1,6 @@
1
+{
2
+	"package": "compactio",
3
+	"javaPackage": "org.openzen.zencode.stdlib.compactio",
4
+	"host": "universal",
5
+	"dependencies": ["io"]
6
+}

+ 293
- 0
compactio/src/CompactBytesDataInput.zs View File

@@ -0,0 +1,293 @@
1
+export class CompactBytesDataInput {
2
+	private const P6 as uint = 1 << 6;
3
+	private const P7 as uint = 1 << 7;
4
+	private const P13 as uint = 1 << 13;
5
+	private const P14 as uint = 1 << 14;
6
+	private const P20 as uint = 1 << 20;
7
+	private const P21 as uint = 1 << 21;
8
+	private const P27 as uint = 1 << 27;
9
+	private const P28 as uint = 1 << 28;
10
+	private const P34 as ulong = 1UL << 34;
11
+	private const P35 as ulong = 1UL << 35;
12
+	private const P41 as ulong = 1UL << 41;
13
+	private const P42 as ulong = 1UL << 42;
14
+	private const P48 as ulong = 1UL << 48;
15
+	private const P49 as ulong = 1UL << 49;
16
+	private const P55 as ulong = 1UL << 55;
17
+	private const P56 as ulong = 1UL << 56;
18
+	
19
+	val data as const byte[];
20
+	var offset as usize : get;
21
+	
22
+	public this(data as const byte[]) {
23
+		this.data = data;
24
+		this.offset = 0;
25
+	}
26
+	
27
+	public this(data as const byte[], offset as usize) {
28
+		this.data = data;
29
+		this.offset = offset;
30
+	}
31
+	
32
+	~this {}
33
+	
34
+	public implements CompactDataInput {
35
+		readBool() => readByte() != 0;
36
+		
37
+		readByte() => data[offset++];
38
+		
39
+		readSByte() => data[offset++];
40
+		
41
+		readShort() {
42
+			val b0 = data[offset++] as uint;
43
+			val b1 = data[offset++] as uint;
44
+			return ((b0 << 8) | b1) as short;
45
+		}
46
+		
47
+		readUShort() => readShort();
48
+		
49
+		readInt() {
50
+			val b0 = data[offset++] as int;
51
+			val b1 = data[offset++] as int;
52
+			val b2 = data[offset++] as int;
53
+			val b3 = data[offset++] as int;
54
+			return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
55
+    	}
56
+    	
57
+    	readUInt() => readInt();
58
+    	
59
+    	readLong() {
60
+    		val i0 = readUInt() as long;
61
+    		val i1 = readUInt() as long;
62
+    		return (i0 << 32) | i1;
63
+    	}
64
+    	
65
+    	readULong() => readLong();
66
+    	
67
+    	readVarInt() {
68
+    		val value = readVarUInt();
69
+    		return (value & 1) == 0 ? (value >> 1) : -((value >> 1) as int + 1);
70
+		}
71
+		
72
+		readVarUInt() {
73
+			var value = data[offset++] as uint;
74
+			if (value & P7) == 0
75
+				return value;
76
+			
77
+			value = (value & (P7 - 1)) | (data[offset++] as uint) << 7;
78
+			if (value & P14) == 0
79
+				return value;
80
+			
81
+			value = (value & (P14 - 1)) | (data[offset++] as uint) << 14;
82
+			if (value & P21) == 0
83
+				return value;
84
+			
85
+			value = (value & (P21 - 1)) | (data[offset++] as uint) << 21;
86
+			if (value & P28) == 0
87
+				return value;
88
+			
89
+			return (value & (P28 - 1)) | (data[offset++] as uint) << 28;
90
+		}
91
+		
92
+		readVarLong() {
93
+			val value = readVarULong();
94
+			return (value & 1) == 0 ? (value >> 1) : -((value >> 1) as long + 1);
95
+		}
96
+		
97
+		readVarULong() {
98
+			var value = data[offset++] as ulong;
99
+			if (value & P7) == 0
100
+				return value;
101
+			
102
+			value = (value & (P7 - 1)) | (data[offset++] as ulong) << 7;
103
+			if (value & P14) == 0
104
+				return value;
105
+			
106
+			value = (value & (P14 - 1)) | (data[offset++] as ulong) << 14;
107
+			if (value & P21) == 0
108
+				return value;
109
+			
110
+			value = (value & (P21 - 1)) | (data[offset++] as ulong) << 21;
111
+			if (value & P28) == 0
112
+				return value;
113
+			
114
+			value = (value & (P28 - 1)) | (data[offset++] as ulong) << 28;
115
+			if (value & P35) == 0
116
+				return value;
117
+			
118
+			value = (value & (P35 - 1)) | (data[offset++] as ulong) << 35;
119
+			if (value & P42) == 0
120
+				return value;
121
+			
122
+			value = (value & (P42 - 1)) | (data[offset++] as ulong) << 42;
123
+			if (value & P49) == 0
124
+				return value;
125
+			
126
+			value = (value & (P49 - 1)) | (data[offset++] as ulong) << 49;
127
+			if (value & P56) == 0
128
+				return value;
129
+			
130
+			return (value & (P56 - 1)) | (data[offset++] as ulong) << 56;
131
+		}
132
+		
133
+		readFloat() => float.fromBits(readUInt());
134
+		
135
+		readDouble() => double.fromBits(readULong());
136
+	
137
+		readChar() => readVarUInt() as char;
138
+		
139
+		readString() => string.fromUTF8Bytes(readBytes());
140
+		
141
+		readBytes() as byte[] {
142
+			val size = readVarUInt();
143
+			return readRawBytes(size);
144
+		}
145
+		
146
+		readRawBytes(size as usize) {
147
+			val result = data[offset .. offset + size];
148
+			offset += size;
149
+			return result;
150
+		}
151
+		
152
+		readBoolArray() as bool[] {
153
+			val size = readVarUInt() as usize;
154
+			val result = new bool[](size);
155
+			for i in 0 .. ((size + 7) / 8) {
156
+				val bvalue = readByte();
157
+				val remainingBits = result.length - 8 * i;
158
+				
159
+				if (remainingBits > 0)
160
+					result[i * 8 + 0] = (bvalue & 1) > 0;
161
+				if (remainingBits > 1)
162
+					result[i * 8 + 2] = (bvalue & 4) > 0;
163
+				if (remainingBits > 3)
164
+					result[i * 8 + 3] = (bvalue & 8) > 0;
165
+				if (remainingBits > 4)
166
+					result[i * 8 + 4] = (bvalue & 16) > 0;
167
+				if (remainingBits > 5)
168
+					result[i * 8 + 5] = (bvalue & 32) > 0;
169
+				if (remainingBits > 6)
170
+					result[i * 8 + 6] = (bvalue & 64) > 0;
171
+				if (remainingBits > 7)
172
+					result[i * 8 + 7] = (bvalue & 128) > 0;
173
+			}
174
+			
175
+			return result;
176
+		}
177
+		
178
+		readByteArray() => readBytes();
179
+		
180
+		readSByteArray() => readBytes();
181
+		
182
+		readShortArray() => readShortArrayRaw(readVarUInt());
183
+		
184
+		readShortArrayRaw(length) {
185
+			val result = new short[](length);
186
+			for i in 0 .. result.length
187
+				result[i] = readShort();
188
+			return result;
189
+		}
190
+		
191
+		readUShortArray() => readShortArray();
192
+		
193
+		readUShortArrayRaw(length) => readShortArrayRaw(length);
194
+		
195
+		readVarIntArray() => readVarIntArrayRaw(readVarUInt());
196
+		
197
+		readVarIntArrayRaw(length) {
198
+			val result = new int[](length);
199
+			for i in 0 .. result.length
200
+				result[i] = readVarInt();
201
+			return result;
202
+		}
203
+		
204
+		readVarUIntArray() => readVarUIntArrayRaw(readVarUInt());
205
+		
206
+		readVarUIntArrayRaw(length) {
207
+			val result = new uint[](length);
208
+			for i in 0 .. result.length
209
+				result[i] = readVarUInt();
210
+			return result;
211
+		}
212
+		
213
+		readIntArray() => readIntArrayRaw(readVarUInt());
214
+		
215
+		readIntArrayRaw(length) {
216
+			val result = new int[](length);
217
+			for i in 0 .. result.length
218
+				result[i] = readInt();
219
+			return result;
220
+		}
221
+		
222
+		readUIntArray() => readUIntArrayRaw(readVarUInt());
223
+		
224
+		readUIntArrayRaw(length) {
225
+			val result = new int[](length);
226
+			for i in 0 .. result.length
227
+				result[i] = readUInt();
228
+			return result;
229
+		}
230
+		
231
+		readVarLongArray() => readVarLongArrayRaw(readVarUInt());
232
+		
233
+		readVarLongArrayRaw(length) {
234
+			val result = new long[](length);
235
+			for i in 0 .. result.length
236
+				result[i] = readVarLong();
237
+			return result;
238
+		}
239
+		
240
+		readVarULongArray() => readVarULongArrayRaw(readVarUInt());
241
+		
242
+		readVarULongArrayRaw(length) {
243
+			val result = new long[](length);
244
+			for i in 0 .. result.length
245
+				result[i] = readVarULong();
246
+			return result;
247
+		}
248
+		
249
+		readLongArray() => readLongArrayRaw(readVarUInt());
250
+		
251
+		readLongArrayRaw(length) {
252
+			val result = new long[](length);
253
+			for i in 0 .. result.length
254
+				result[i] = readLong();
255
+			return result;
256
+		}
257
+		
258
+		readULongArray() => readLongArray();
259
+		
260
+		readULongArrayRaw(length) => readLongArrayRaw(length);
261
+		
262
+		readFloatArray() => readFloatArrayRaw(readVarUInt());
263
+		
264
+		readFloatArrayRaw(length) {
265
+			val result = new float[](length);
266
+			for i in 0 .. result.length
267
+				result[i] = readFloat();
268
+			return result;
269
+		}
270
+		
271
+		readDoubleArray() => readDoubleArrayRaw(readVarUInt());
272
+		
273
+		readDoubleArrayRaw(length) {
274
+			val result = new double[](length);
275
+			for i in 0 .. length
276
+				result[i] = readDouble();
277
+			return result;
278
+		}
279
+		
280
+		readStringArray() => readStringArrayRaw(readVarUInt());
281
+		
282
+		readStringArrayRaw(length) {
283
+			val result = new string[](length);
284
+			for i in 0 .. result.length
285
+				result[i] = readString();
286
+			return result;
287
+		}
288
+		
289
+		skip(bytes) => offset += bytes;
290
+		
291
+		hasMore() => offset < data.length;
292
+	}
293
+}

+ 367
- 0
compactio/src/CompactBytesDataOutput.zs View File

@@ -0,0 +1,367 @@
1
+export class CompactBytesDataOutput {
2
+	private const P6 as uint = 1 << 6;
3
+	private const P7 as uint = 1 << 7;
4
+	private const P13 as uint = 1 << 13;
5
+	private const P14 as uint = 1 << 14;
6
+	private const P20 as uint = 1 << 20;
7
+	private const P21 as uint = 1 << 21;
8
+	private const P27 as uint = 1 << 27;
9
+	private const P28 as uint = 1 << 28;
10
+	private const P34 as ulong = 1UL << 34;
11
+	private const P35 as ulong = 1UL << 35;
12
+	private const P41 as ulong = 1UL << 41;
13
+	private const P42 as ulong = 1UL << 42;
14
+	private const P48 as ulong = 1UL << 48;
15
+	private const P49 as ulong = 1UL << 49;
16
+	private const P55 as ulong = 1UL << 55;
17
+	private const P56 as ulong = 1UL << 56;
18
+	
19
+	var data = new byte[](16);
20
+	var length as usize = 0;
21
+	
22
+	private reserve(bytes as usize) as void {
23
+		while length + bytes > data.length
24
+			data = data.copy(2 * data.length);
25
+	}
26
+	
27
+	public asByteArray() as byte[] => data.copy(length);
28
+	
29
+	~this {}
30
+	
31
+	public implements CompactDataOutput {
32
+		writeBool(value) => writeByte(value ? 1 : 0);
33
+		
34
+		writeByte(value) {
35
+			reserve(1);
36
+			data[length++] = value;
37
+		}
38
+		
39
+		writeSByte(value) => writeByte(value);
40
+		
41
+		writeShort(value) => writeUShort(value);
42
+		
43
+		writeUShort(value) {
44
+			reserve(2);
45
+			data[length++] = (value >> 8) as byte;
46
+			data[length++] = value as byte;
47
+		}
48
+		
49
+		writeInt(value) => writeUInt(value);
50
+		
51
+		writeUInt(value) {
52
+			reserve(4);
53
+			data[length++] = (value >> 24) as byte;
54
+			data[length++] = (value >> 16) as byte;
55
+			data[length++] = (value >> 8) as byte;
56
+			data[length++] = value as byte;
57
+		}
58
+		
59
+		writeLong(value) => writeULong(value);
60
+		
61
+		writeULong(value) {
62
+			reserve(8);
63
+			data[length++] = (value >> 56) as byte;
64
+			data[length++] = (value >> 48) as byte;
65
+			data[length++] = (value >> 40) as byte;
66
+			data[length++] = (value >> 32) as byte;
67
+			data[length++] = (value >> 24) as byte;
68
+			data[length++] = (value >> 16) as byte;
69
+			data[length++] = (value >> 8) as byte;
70
+			data[length++] = value as byte;
71
+		}
72
+		
73
+		writeVarInt(value) => writeVarUInt(value < 0 ? (((1 - (value as uint)) << 1) + 1) : (value << 1));
74
+		
75
+		writeVarUInt(value) {
76
+			reserve(5);
77
+			
78
+			if value < P7 {
79
+				data[length++] = (value & 0x7F) as byte;
80
+			} else if value < P14 {
81
+				data[length++] = ((value & 0x7F) | 0x80) as byte;
82
+				data[length++] = ((value >> 7) & 0x7F) as byte;
83
+			} else if value < P21 {
84
+				data[length++] = ((value & 0x7F) | 0x80) as byte;
85
+				data[length++] = (((value >> 7) & 0x7F) | 0x80) as byte;
86
+				data[length++] = ((value >> 14) & 0x7F) as byte;
87
+			} else if value < P28 {
88
+				data[length++] = ((value & 0x7F) | 0x80) as byte;
89
+				data[length++] = (((value >> 7) & 0x7F) | 0x80) as byte;
90
+				data[length++] = (((value >> 14) & 0x7F) | 0x80) as byte;
91
+				data[length++] = ((value >> 21) & 0x7F) as byte;
92
+			} else {
93
+				data[length++] = ((value & 0x7F) | 0x80) as byte;
94
+				data[length++] = (((value >> 7) & 0x7F) | 0x80) as byte;
95
+				data[length++] = (((value >> 14) & 0x7F) | 0x80) as byte;
96
+				data[length++] = (((value >> 21) & 0x7F) | 0x80) as byte;
97
+				data[length++] = ((value >> 28) & 0x7F) as byte;
98
+			}
99
+		}
100
+		
101
+		writeVarLong(value) => writeVarULong(value < 0 ? (((1 - (value as ulong)) << 1) + 1) : (value << 1));
102
+		
103
+		writeVarULong(value) {
104
+			reserve(9);
105
+			
106
+			if value < P7 {
107
+				data[length++] = (value & 0x7F) as byte;
108
+			} else if value < P14 {
109
+				data[length++] = ((value & 0x7F) | 0x80) as byte;
110
+				data[length++] = ((value >> 7) & 0x7F) as byte;
111
+			} else if value < P21 {
112
+				data[length++] = ((value & 0x7F) | 0x80) as byte;
113
+				data[length++] = (((value >> 7) & 0x7F) | 0x80) as byte;
114
+				data[length++] = ((value >> 14) & 0x7F) as byte;
115
+			} else if value < P28 {
116
+				data[length++] = ((value & 0x7F) | 0x80) as byte;
117
+				data[length++] = (((value >> 7) & 0x7F) | 0x80) as byte;
118
+				data[length++] = (((value >> 14) & 0x7F) | 0x80) as byte;
119
+				data[length++] = ((value >> 21) & 0x7F) as byte;
120
+			} else if value < P35 {
121
+				data[length++] = ((value & 0x7F) | 0x80) as byte;
122
+				data[length++] = (((value >> 7) & 0x7F) | 0x80) as byte;
123
+				data[length++] = (((value >> 14) & 0x7F) | 0x80) as byte;
124
+				data[length++] = (((value >> 21) & 0x7F) | 0x80) as byte;
125
+				data[length++] = ((value >> 28) & 0x7F) as byte;
126
+			} else if value < P42 {
127
+				data[length++] = ((value & 0x7F) | 0x80) as byte;
128
+				data[length++] = (((value >> 7) & 0x7F) | 0x80) as byte;
129
+				data[length++] = (((value >> 14) & 0x7F) | 0x80) as byte;
130
+				data[length++] = (((value >> 21) & 0x7F) | 0x80) as byte;
131
+				data[length++] = (((value >> 28) & 0x7F) | 0x80) as byte;
132
+				data[length++] = ((value >> 35) & 0x7F) as byte;
133
+			} else if value < P49 {
134
+				data[length++] = ((value & 0x7F) | 0x80) as byte;
135
+				data[length++] = (((value >> 7) & 0x7F) | 0x80) as byte;
136
+				data[length++] = (((value >> 14) & 0x7F) | 0x80) as byte;
137
+				data[length++] = (((value >> 21) & 0x7F) | 0x80) as byte;
138
+				data[length++] = (((value >> 28) & 0x7F) | 0x80) as byte;
139
+				data[length++] = (((value >> 35) & 0x7F) | 0x80) as byte;
140
+				data[length++] = ((value >> 42) & 0x7F) as byte;
141
+			} else if value < P56 {
142
+				data[length++] = ((value & 0x7F) | 0x80) as byte;
143
+				data[length++] = (((value >> 7) & 0x7F) | 0x80) as byte;
144
+				data[length++] = (((value >> 14) & 0x7F) | 0x80) as byte;
145
+				data[length++] = (((value >> 21) & 0x7F) | 0x80) as byte;
146
+				data[length++] = (((value >> 28) & 0x7F) | 0x80) as byte;
147
+				data[length++] = (((value >> 35) & 0x7F) | 0x80) as byte;
148
+				data[length++] = (((value >> 42) & 0x7F) | 0x80) as byte;
149
+				data[length++] = ((value >> 49) & 0x7F) as byte;
150
+			} else {
151
+				data[length++] = ((value & 0x7F) | 0x80) as byte;
152
+				data[length++] = (((value >> 7) & 0x7F) | 0x80) as byte;
153
+				data[length++] = (((value >> 14) & 0x7F) | 0x80) as byte;
154
+				data[length++] = (((value >> 21) & 0x7F) | 0x80) as byte;
155
+				data[length++] = (((value >> 28) & 0x7F) | 0x80) as byte;
156
+				data[length++] = (((value >> 35) & 0x7F) | 0x80) as byte;
157
+				data[length++] = (((value >> 42) & 0x7F) | 0x80) as byte;
158
+				data[length++] = (((value >> 49) & 0x7F) | 0x80) as byte;
159
+				data[length++] = (value >> 56) as byte;
160
+			}
161
+		}
162
+		
163
+		writeFloat(value) => writeUInt(value.bits);
164
+		
165
+		writeDouble(value) => writeULong(value.bits);
166
+		
167
+		writeChar(value) => writeVarUInt(value);
168
+		
169
+		[Precondition(ENFORCE, data.length < uint.MAX_VALUE, "Array length cannot exceed uint limit")]
170
+		writeBytes(data) {
171
+			writeVarUInt(data.length as uint);
172
+			writeRawBytes(data);
173
+		}
174
+		
175
+		[Precondition(ENFORCE, length < uint.MAX_VALUE, "Array length cannot exceed uint limit")]
176
+		writeBytes(data, offset, length) {
177
+			writeVarUInt(length as uint);
178
+			writeRawBytes(data, offset, length);
179
+		}
180
+		
181
+		writeString(str) => writeBytes(str.toUTF8Bytes());
182
+		
183
+		writeRawBytes(value) {
184
+			reserve(value.length);
185
+			value.copyTo(data, 0, length, value.length);
186
+			length += value.length;
187
+		}
188
+		
189
+		writeRawBytes(value, offset, length) {
190
+			reserve(value.length);
191
+			value.copyTo(data, offset, this.length, value.length);
192
+			this.length += length;
193
+		}
194
+		
195
+		[Precondition(ENFORCE, data.length < uint.MAX_VALUE, "Array length cannot exceed uint limit")]
196
+		writeBoolArray(data) {
197
+			writeVarUInt(data.length as uint);
198
+			
199
+			var i = 0 as usize;
200
+			while i < data.length - 7 {
201
+				var bvalue as byte = 0;
202
+				if data[i + 0]
203
+					bvalue |= 1;
204
+				if data[i + 1]
205
+					bvalue |= 2;
206
+				if data[i + 2]
207
+					bvalue += 4;
208
+				if data[i + 3]
209
+					bvalue += 8;
210
+				if data[i + 4]
211
+					bvalue += 16;
212
+				if data[i + 5]
213
+					bvalue += 32;
214
+				if data[i + 6]
215
+					bvalue += 64;
216
+				if data[i + 7]
217
+					bvalue += 128;
218
+				writeByte(bvalue);
219
+				i += 8;
220
+			}
221
+			
222
+			if i < data.length {
223
+				var bvalue as byte = 0;
224
+				for offset in 0 .. (data.length % 7)
225
+					if data[i + offset]
226
+						bvalue += (1 << i) as byte;
227
+				
228
+				writeByte(bvalue);
229
+			}
230
+		}
231
+		
232
+		[Precondition(ENFORCE, data.length < uint.MAX_VALUE, "Array length cannot exceed uint limit")]
233
+		writeByteArray(data) => writeBytes(data);
234
+		
235
+		[Precondition(ENFORCE, data.length < uint.MAX_VALUE, "Array length cannot exceed uint limit")]
236
+		writeSByteArray(data) => writeBytes(data);
237
+		
238
+		[Precondition(ENFORCE, data.length < uint.MAX_VALUE, "Array length cannot exceed uint limit")]
239
+		writeShortArray(data) {
240
+			writeVarUInt(data.length as uint);
241
+			writeShortArrayRaw(data);
242
+		}
243
+		
244
+		writeShortArrayRaw(data) {
245
+			for element in data
246
+				writeShort(element);
247
+		}
248
+		
249
+		[Precondition(ENFORCE, data.length < uint.MAX_VALUE, "Array length cannot exceed uint limit")]
250
+		writeUShortArray(data) => writeShortArray(data);
251
+		
252
+		writeUShortArrayRaw(data) => writeShortArrayRaw(data);
253
+		
254
+		[Precondition(ENFORCE, data.length < uint.MAX_VALUE, "Array length cannot exceed uint limit")]
255
+		writeVarIntArray(data) {
256
+			writeVarUInt(data.length as uint);
257
+			writeVarIntArrayRaw(data);
258
+		}
259
+		
260
+		writeVarIntArrayRaw(data) {
261
+			for element in data
262
+				writeVarInt(element);
263
+		}
264
+		
265
+		[Precondition(ENFORCE, data.length < uint.MAX_VALUE, "Array length cannot exceed uint limit")]
266
+		writeVarUIntArray(data) {
267
+			writeVarUInt(data.length as uint);
268
+			writeVarUIntArrayRaw(data);
269
+		}
270
+		
271
+		writeVarUIntArrayRaw(data) {
272
+			for element in data
273
+				writeVarUInt(element);
274
+		}
275
+		
276
+		[Precondition(ENFORCE, data.length < uint.MAX_VALUE, "Array length cannot exceed uint limit")]
277
+		writeIntArray(data) {
278
+			writeVarUInt(data.length as uint);
279
+			writeIntArrayRaw(data);
280
+		}
281
+		
282
+		writeIntArrayRaw(data) {
283
+			for element in data
284
+				writeInt(element);
285
+		}
286
+		
287
+		[Precondition(ENFORCE, data.length < uint.MAX_VALUE, "Array length cannot exceed uint limit")]
288
+		writeUIntArray(data) => writeIntArray(data);
289
+		
290
+		writeUIntArrayRaw(data) => writeIntArrayRaw(data);
291
+		
292
+		[Precondition(ENFORCE, data.length < uint.MAX_VALUE, "Array length cannot exceed uint limit")]
293
+		writeVarLongArray(data) {
294
+			writeVarUInt(data.length as uint);
295
+			writeVarLongArrayRaw(data);
296
+		}
297
+		
298
+		writeVarLongArrayRaw(data) {
299
+			for element in data
300
+				writeVarLong(element);
301
+		}
302
+		
303
+		[Precondition(ENFORCE, data.length < uint.MAX_VALUE, "Array length cannot exceed uint limit")]
304
+		writeVarULongArray(data) {
305
+			writeVarUInt(data.length as uint);
306
+			writeVarULongArrayRaw(data);
307
+		}
308
+		
309
+		writeVarULongArrayRaw(data) {
310
+			for element in data
311
+				writeVarULong(element);
312
+		}
313
+		
314
+		[Precondition(ENFORCE, data.length < uint.MAX_VALUE, "Array length cannot exceed uint limit")]
315
+		writeLongArray(data) {
316
+			writeVarUInt(data.length as uint);
317
+			writeLongArrayRaw(data);
318
+		}
319
+		
320
+		writeLongArrayRaw(data) {
321
+			for element in data
322
+				writeLong(element);
323
+		}
324
+		
325
+		[Precondition(ENFORCE, data.length < uint.MAX_VALUE, "Array length cannot exceed uint limit")]
326
+		writeULongArray(data) => writeLongArray(data);
327
+		
328
+		writeULongArrayRaw(data) => writeLongArrayRaw(data);
329
+		
330
+		[Precondition(ENFORCE, data.length < uint.MAX_VALUE, "Array length cannot exceed uint limit")]
331
+		writeFloatArray(data) {
332
+			writeVarUInt(data.length as uint);
333
+			writeFloatArrayRaw(data);
334
+		}
335
+		
336
+		writeFloatArrayRaw(data) {
337
+			for element in data
338
+				writeFloat(element);
339
+		}
340
+		
341
+		[Precondition(ENFORCE, data.length < uint.MAX_VALUE, "Array length cannot exceed uint limit")]
342
+		writeDoubleArray(data) {
343
+			writeVarUInt(data.length as uint);
344
+			writeDoubleArrayRaw(data);
345
+		}
346
+		
347
+		writeDoubleArrayRaw(data) {
348
+			for element in data
349
+				writeDouble(element);
350
+		}
351
+		
352
+		[Precondition(ENFORCE, data.length < uint.MAX_VALUE, "Array length cannot exceed uint limit")]
353
+		writeStringArray(data) {
354
+			writeVarUInt(data.length as uint);
355
+			writeStringArrayRaw(data);
356
+		}
357
+		
358
+		writeStringArrayRaw(data) {
359
+			for element in data
360
+				writeString(element);
361
+		}
362
+		
363
+		flush() {
364
+			// nothing to do
365
+		}
366
+	}
367
+}

+ 103
- 0
compactio/src/CompactDataInput.zs View File

@@ -0,0 +1,103 @@
1
+export interface CompactDataInput {
2
+	readBool() as bool;
3
+	
4
+	readByte() as sbyte;
5
+	
6
+	readSByte() as byte;
7
+	
8
+	readShort() as short;
9
+	
10
+	readUShort() as ushort;
11
+	
12
+	readInt() as int;
13
+	
14
+	readUInt() as uint;
15
+	
16
+	readLong() as long;
17
+	
18
+	readULong() as ulong;
19
+	
20
+	readVarInt() as int;
21
+	
22
+	readVarUInt() as uint;
23
+	
24
+	readVarLong() as long;
25
+	
26
+	readVarULong() as ulong;
27
+	
28
+	readFloat() as float;
29
+	
30
+	readDouble() as double;
31
+	
32
+	readChar() as char;
33
+	
34
+	readString() as string;
35
+	
36
+	readBytes() as byte[];
37
+	
38
+	readRawBytes(length as usize) as byte[];
39
+	
40
+	readBoolArray() as bool[];
41
+	
42
+	readByteArray() as byte[];
43
+	
44
+	readSByteArray() as sbyte[];
45
+	
46
+	readShortArray() as short[];
47
+	
48
+	readShortArrayRaw(length as usize) as short[];
49
+	
50
+	readUShortArray() as ushort[];
51
+	
52
+	readUShortArrayRaw(length as usize) as ushort[];
53
+	
54
+	readVarIntArray() as int[];
55
+	
56
+	readVarIntArrayRaw(length as usize) as int[];
57
+	
58
+	readVarUIntArray() as uint[];
59
+	
60
+	readVarUIntArrayRaw(length as usize) as uint[];
61
+	
62
+	readIntArray() as int[];
63
+	
64
+	readIntArrayRaw(length as usize) as int[];
65
+	
66
+	readUIntArray() as uint[];
67
+	
68
+	readUIntArrayRaw(length as usize) as uint[];
69
+	
70
+	readVarLongArray() as long[];
71
+	
72
+	readVarLongArrayRaw(length as usize) as long[];
73
+	
74
+	readVarULongArray() as ulong[];
75
+	
76
+	readVarULongArrayRaw(length as usize) as ulong[];
77
+	
78
+	readLongArray() as long[];
79
+	
80
+	readLongArrayRaw(length as usize) as long[];
81
+	
82
+	readULongArray() as ulong[];
83
+	
84
+	readULongArrayRaw(length as usize) as ulong[];
85
+	
86
+	readFloatArray() as float[];
87
+	
88
+	readFloatArrayRaw(length as usize) as float[];
89
+	
90
+	readDoubleArray() as double[];
91
+	
92
+	readDoubleArrayRaw(length as usize) as double[];
93
+	
94
+	readStringArray() as string[];
95
+	
96
+	readStringArrayRaw(length as usize) as string[];
97
+	
98
+	skip(bytes as usize) as void;
99
+	
100
+	hasMore() as bool;
101
+	
102
+	~this;
103
+}

+ 105
- 0
compactio/src/CompactDataOutput.zs View File

@@ -0,0 +1,105 @@
1
+public interface CompactDataOutput {
2
+	writeBool(value as bool) as void;
3
+	
4
+	writeByte(value as byte) as void;
5
+	
6
+	writeSByte(value as sbyte) as void;
7
+	
8
+	writeShort(value as short) as void;
9
+	
10
+	writeUShort(value as ushort) as void;
11
+	
12
+	writeInt(value as int) as void;
13
+	
14
+	writeUInt(value as uint) as void;
15
+	
16
+	writeLong(value as long) as void;
17
+	
18
+	writeULong(value as ulong) as void;
19
+	
20
+	writeVarInt(value as int) as void;
21
+	
22
+	writeVarUInt(value as uint) as void;
23
+	
24
+	writeVarLong(value as long) as void;
25
+	
26
+	writeVarULong(value as ulong) as void;
27
+	
28
+    writeFloat(value as float) as void;
29
+	
30
+    writeDouble(value as double) as void;
31
+	
32
+	writeChar(value as char) as void;
33
+	
34
+	writeString(value as string) as void;
35
+	
36
+    writeBytes(data as byte[]) as void;
37
+	
38
+	writeBytes(data as byte[], offset as usize, length as usize) as void;
39
+	
40
+	writeRawBytes(value as byte[]) as void;
41
+	
42
+	writeRawBytes(value as byte[], offset as usize, length as usize) as void;
43
+	
44
+	writeBoolArray(data as bool[]) as void;
45
+	
46
+	writeByteArray(data as byte[]) as void;
47
+	
48
+	writeSByteArray(data as sbyte[]) as void;
49
+	
50
+	writeShortArray(data as short[]) as void;
51
+	
52
+	writeShortArrayRaw(data as short[]) as void;
53
+	
54
+	writeUShortArray(data as short[]) as void;
55
+	
56
+	writeUShortArrayRaw(data as short[]) as void;
57
+	
58
+	writeVarIntArray(data as int[]) as void;
59
+	
60
+	writeVarIntArrayRaw(data as int[]) as void;
61
+	
62
+	writeVarUIntArray(data as uint[]) as void;
63
+	
64
+	writeVarUIntArrayRaw(data as uint[]) as void;
65
+	
66
+	writeIntArray(data as int[]) as void;
67
+	
68
+	writeIntArrayRaw(data as int[]) as void;
69
+	
70
+	writeUIntArray(data as uint[]) as void;
71
+	
72
+	writeUIntArrayRaw(data as uint[]) as void;
73
+	
74
+	writeVarLongArray(data as long[]) as void;
75
+	
76
+	writeVarLongArrayRaw(data as long[]) as void;
77
+	
78
+	writeVarULongArray(data as ulong[]) as void;
79
+	
80
+	writeVarULongArrayRaw(data as ulong[]) as void;
81
+	
82
+	writeLongArray(data as long[]) as void;
83
+	
84
+	writeLongArrayRaw(data as long[]) as void;
85
+	
86
+	writeULongArray(data as long[]) as void;
87
+	
88
+	writeULongArrayRaw(data as long[]) as void;
89
+	
90
+	writeFloatArray(data as float[]) as void;
91
+	
92
+	writeFloatArrayRaw(data as float[]) as void;
93
+	
94
+	writeDoubleArray(data as double[]) as void;
95
+	
96
+	writeDoubleArrayRaw(data as double[]) as void;
97
+	
98
+	writeStringArray(data as string[]) as void;
99
+	
100
+	writeStringArrayRaw(data as string[]) as void;
101
+	
102
+	flush() as void;
103
+	
104
+	~this;
105
+}

+ 2
- 1
io/module.json View File

@@ -1,4 +1,5 @@
1 1
 {
2
-	"package": "collections",
2
+	"package": "io",
3
+	"javaPackage": "org.openzen.zencode.stdlib.io",
3 4
 	"host": "universal"
4 5
 }

+ 2
- 0
io/src/IOException.zs View File

@@ -1,4 +1,6 @@
1
+[Native("io::IOException")]
1 2
 public class IOException : Exception {
3
+	[Native("constructor")]
2 4
 	public this(message as string) {
3 5
 		super(message);
4 6
 	}

+ 14
- 0
io/src/InputStream.zs View File

@@ -0,0 +1,14 @@
1
+[Native("io::InputStream")]
2
+export interface InputStream {
3
+	[Native("read")]
4
+	read() as int throws IOException;
5
+	
6
+	[Native("readArray")]
7
+	read(array as byte[]) as usize throws IOException;
8
+	
9
+	[Native("readSlice")]
10
+	read(array as byte[], offset as usize, length as usize) as usize throws IOException;
11
+	
12
+	[Native("destruct")]
13
+	~this;
14
+}

+ 14
- 0
io/src/OutputStream.zs View File

@@ -0,0 +1,14 @@
1
+[Native("io::OutputStream")]
2
+export interface OutputStream {
3
+	[Native("write")]
4
+	write(value as byte) as void throws IOException;
5
+	
6
+	[Native("writeArray")]
7
+	write(value as byte[]) as void throws IOException;
8
+	
9
+	[Native("writeSlice")]
10
+	write(value as byte[], offset as usize, length as usize) as void throws IOException;
11
+	
12
+	[Native("destruct")]
13
+	~this;
14
+}

+ 7
- 2
io/src/Reader.zs View File

@@ -1,10 +1,15 @@
1
+[Native("io::Reader")]
1 2
 export interface Reader {
3
+	[Native("destruct")]
2 4
 	~this;
3 5
 	
6
+	[Native("readCharacter")]
4 7
 	read() as int throws IOException;
5 8
 	
6
-	read(buffer as char[]) as int throws IOException
9
+	[Native("readArray")]
10
+	read(buffer as char[]) as usize throws IOException
7 11
 		=> read(buffer, 0, buffer.length);
8 12
 	
9
-	read(buffer as char[], offset as int, length as int) as int throws IOException;
13
+	[Native("readArraySlice")]
14
+	read(buffer as char[], offset as usize, length as usize) as usize throws IOException;
10 15
 }

+ 15
- 6
io/src/StringReader.zs View File

@@ -1,19 +1,28 @@
1
+[Native("io::StringReader")]
1 2
 export class StringReader {
2 3
 	val data as char[];
3
-	var offset as int;
4
+	var offset as usize;
4 5
 	
6
+	[Native("constructor")]
5 7
 	public this(value as string) {
6 8
 		data = value.characters;
7 9
 	}
8 10
 	
9 11
 	public implements Reader {
12
+		[Native("destructor")]
10 13
 		~this {}
11
-	
12
-		read() as int
14
+		
15
+		[Native("readCharacter")]
16
+		read()
13 17
 			=> offset == data.length ? -1 : data[offset++];
14
-	
15
-		read(buffer as char[], offset as int, length as int) as int {
16
-			length = int.min(data.length - this.offset, length);
18
+		
19
+		[Native("readArray")]
20
+		read(buffer)
21
+			=> read(buffer, 0, buffer.length);
22
+		
23
+		[Native("readSlice")]
24
+		read(buffer, offset, length) {
25
+			length = usize.min(data.length - this.offset, length);
17 26
 			data.copyTo(buffer, this.offset, offset, length);
18 27
 			this.offset += length;
19 28
 			return length;

+ 5
- 0
listeners/module.json View File

@@ -0,0 +1,5 @@
1
+{
2
+	"package": "listeners",
3
+	"javaPackage": "org.openzen.zencode.stdlib.listeners",
4
+	"host": "universal"
5
+}

+ 9
- 0
listeners/src/DummyListenerHandle.zs View File

@@ -0,0 +1,9 @@
1
+export class DummyListenerHandle<T> {
2
+	val listener as T : get;
3
+	
4
+	public this(listener as T) {
5
+		this.listener = listener;
6
+	}
7
+	
8
+	public implements ListenerHandle<T> {}
9
+}

+ 5
- 0
listeners/src/ListenerHandle.zs View File

@@ -0,0 +1,5 @@
1
+public interface ListenerHandle<T> {
2
+	~this;
3
+	
4
+	get listener as T;
5
+}

+ 80
- 0
listeners/src/ListenerList.zs View File

@@ -0,0 +1,80 @@
1
+public class ListenerList<T> {
2
+	public const PRIORITY_HIGH = 100;
3
+	public const PRIORITY_DEFAULT = 0;
4
+	public const PRIORITY_LOW = -100;
5
+	
6
+	var first as EventListenerNode? = null;
7
+	var last as EventListenerNode? = null;
8
+	
9
+	public add(listener as T) as ListenerHandle<T>
10
+		=> add(listener, PRIORITY_DEFAULT);
11
+	
12
+	public add(listener as T, priority as int) as ListenerHandle<T> {
13
+		val node = new EventListenerNode(listener, priority);
14
+		
15
+		if first == null {
16
+			first = last = node;
17
+		} else {
18
+			// prioritized list: where to insert?
19
+			var previousNode = last;
20
+			while previousNode != null && priority > previousNode.priority
21
+				previousNode = previousNode.prev;
22
+			
23
+			if previousNode == null {
24
+				node.next = first;
25
+				first.prev = previousNode;
26
+				first = node;
27
+			} else {
28
+				if previousNode.next == null
29
+					last = node;
30
+				else
31
+					previousNode.next.prev = node;
32
+				
33
+				previousNode.next = node;
34
+				node.prev = previousNode;
35
+			}
36
+		}
37
+		
38
+		return node;
39
+	}
40
+	
41
+	public clear() as void {
42
+		first = last = null;
43
+	}
44
+	
45
+	public accept(consumer as function(listener as T) as void) as void {
46
+		var current = first;
47
+		while current != null {
48
+			consumer(current.listener);
49
+			current = current.next;
50
+		}
51
+	}
52
+	
53
+	public get isEmpty => first == null;
54
+	
55
+	private class EventListenerNode {
56
+		val listener as T : get;
57
+		val priority as int;
58
+		var next as EventListenerNode? = null;
59
+		var prev as EventListenerNode? = null;
60
+		
61
+		public this(listener as T, priority as int) {
62
+			this.listener = listener;
63
+			this.priority = priority;
64
+		}
65
+		
66
+		~this {
67
+			if prev == null
68
+				first = next;
69
+			else
70
+				prev.next = next;
71
+			
72
+			if next == null
73
+				last = prev;
74
+			else
75
+				next.prev = prev;
76
+		}
77
+		
78
+		public implements ListenerHandle<T> {}
79
+	}
80
+}

+ 6
- 0
live/module.json View File

@@ -0,0 +1,6 @@
1
+{
2
+	"package": "live",
3
+	"javaPackage": "org.openzen.zencode.stdlib.live",
4
+	"host": "universal",
5
+	"dependencies": ["listeners"]
6
+}

+ 16
- 0
live/src/ImmutableLiveBool.zs View File

@@ -0,0 +1,16 @@
1
+import listeners.DummyListenerHandle;
2
+
3
+export class ImmutableLiveBool {
4
+	public const TRUE = new ImmutableLiveBool(true);
5
+	public const FALSE = new ImmutableLiveBool(false);
6
+	
7
+	val value as bool : get;
8
+	
9
+	private this(value as bool) {
10
+		this.value = value;
11
+	}
12
+	
13
+	public implements LiveBool {
14
+		addListener(listener) => new DummyListenerHandle<LiveBool.Listener>(listener);
15
+	}
16
+}

+ 13
- 0
live/src/ImmutableLiveObject.zs View File

@@ -0,0 +1,13 @@
1
+import listeners.DummyListenerHandle;
2
+
3
+export class ImmutableLiveObject<T> {
4
+	val value as T : get;
5
+	
6
+	public this(value as T) {
7
+		this.value = value;
8
+	}
9
+	
10
+	public implements LiveObject<T> {
11
+		addListener(listener) => new DummyListenerHandle<LiveObject<T>.Listener<T>>(listener);
12
+	}
13
+}

+ 13
- 0
live/src/ImmutableLiveString.zs View File

@@ -0,0 +1,13 @@
1
+import listeners.DummyListenerHandle;
2
+
3
+export class ImmutableLiveString {
4
+	val value as string : get;
5
+	
6
+	public this(value as string) {
7
+		this.value = value;
8
+	}
9
+	
10
+	public implements LiveString {
11
+		addListener(listener) => new DummyListenerHandle<LiveString.Listener>(listener);
12
+	}
13
+}

+ 14
- 0
live/src/InverseLiveBool.zs View File

@@ -0,0 +1,14 @@
1
+export class InverseLiveBool {
2
+	val source as LiveBool;
3
+	
4
+	public this(source as LiveBool) {
5
+		this.source = source;
6
+	}
7
+	
8
+	public implements LiveBool {
9
+		get value => !source.value;
10
+		
11
+		addListener(listener)
12
+			=> source.addListener((oldVal, newVal) => listener(!oldVal, !newVal));
13
+	}
14
+}

+ 52
- 0
live/src/LiveArrayList.zs View File

@@ -0,0 +1,52 @@
1
+import listeners.ListenerList;
2
+
3
+class LiveArrayList<T> {
4
+	val values = new List<T>();
5
+	val listeners = new ListenerList<LiveList<T>.Listener<T>>();
6
+	
7
+	public implements MutableLiveList<T> {
8
+		add(value) {
9
+			val index = values.length;
10
+			values.add(value);
11
+			listeners.accept(listener => listener.onInserted(index, value));
12
+		}
13
+		
14
+		insert(index, value) {
15
+			values.insert(index, value);
16
+			listeners.accept(listener => listener.onInserted(index, value));
17
+		}
18
+		
19
+		[]=(index, value) {
20
+			val oldValue = values[index];
21
+			values[index] = value;
22
+			listeners.accept(listener => listener.onChanged(index, oldValue, value));
23
+		}
24
+		
25
+		remove(index as usize) {
26
+			val oldValue = values.remove(index);
27
+			listeners.accept(listener => listener.onRemoved(index, oldValue));
28
+		}
29
+		
30
+		remove(value as T) {
31
+			val index = indexOf(value);
32
+			if index == null
33
+				return;
34
+			
35
+			remove(index);
36
+		}
37
+		
38
+		clear() {
39
+			var i = length;
40
+			while i > 0 {
41
+				i--;
42
+				remove(i);
43
+			}
44
+		}
45
+		
46
+		iterate() => values.iterate();
47
+		indexOf(value) => values.indexOf(value);
48
+		get length => values.length;
49
+		[](index) => values[index];
50
+		addListener(listener) => listeners.add(listener);
51
+	}
52
+}

+ 8
- 0
live/src/LiveBool.zs View File

@@ -0,0 +1,8 @@
1
+public interface LiveBool {
2
+	get value as bool;
3
+	
4
+	addListener(listener as Listener) as ListenerHandle;
5
+	
6
+	alias Listener as function(oldValue as bool, newValue as bool) as void;
7
+	alias ListenerHandle as listeners.ListenerHandle<Listener>;
8
+}

+ 9
- 0
live/src/LiveInt.zs View File

@@ -0,0 +1,9 @@
1
+export interface LiveInt {
2
+	get value as int;
3
+	set value as int;
4
+	
5
+	addListener(listener as Listener) as ListenerHandle;
6
+	
7
+	alias Listener as function(oldValue as int, newValue as int) as void;
8
+	alias ListenerHandle as listeners.ListenerHandle<Listener>;
9
+}

+ 21
- 0
live/src/LiveList.zs View File

@@ -0,0 +1,21 @@
1
+export interface LiveList<T> : Iterable<T> {
2
+	~this;
3
+	
4
+	get length as usize;
5
+	
6
+	indexOf(value as T) as usize?;
7
+	
8
+	//[IndexIterator]
9
+	//for (index as int, value as T);
10
+	
11
+	[](index as usize) as T;
12
+	
13
+	addListener(listener as Listener<T>) as ListenerHandle<T>;
14
+	
15
+	public interface Listener<T> {
16
+		onInserted(index as usize, value as T) as void;
17
+		onChanged(index as usize, oldValue as T, newValue as T) as void;
18
+		onRemoved(index as usize, oldValue as T) as void;
19
+	}
20
+	alias ListenerHandle<T> as listeners.ListenerHandle<Listener<T>>;
21
+}

+ 8
- 0
live/src/LiveObject.zs View File

@@ -0,0 +1,8 @@
1
+export interface LiveObject<T> {
2
+	get value as T;
3
+	
4
+	addListener(listener as Listener<T>) as ListenerHandle<T>;
5
+	
6
+	alias Listener<T> as function(oldValue as T, newValue as T) as void;
7
+	alias ListenerHandle<T> as listeners.ListenerHandle<Listener<T>>;
8
+}

+ 8
- 0
live/src/LiveString.zs View File

@@ -0,0 +1,8 @@
1
+export interface LiveString {
2
+	get value as string;
3
+	
4
+	addListener(listener as Listener) as ListenerHandle;
5
+	
6
+	alias Listener as function(oldValue as string, newValue as string) as void;
7
+	alias ListenerHandle as listeners.ListenerHandle<Listener>;
8
+}

+ 13
- 0
live/src/MutableLiveList.zs View File

@@ -0,0 +1,13 @@
1
+export interface MutableLiveList<T> : LiveList<T> {
2
+	add(value as T) as void;
3
+	
4
+	insert(index as usize, value as T) as void;
5
+	
6
+	[]=(index as usize, value as T) as void;
7
+	
8
+	remove(index as usize) as void;
9
+	
10
+	remove(value as T) as void;
11
+	
12
+	clear() as void;
13
+}

+ 3
- 0
stdlib/module.json View File

@@ -7,6 +7,9 @@
7 7
 		"EqualsComparable": {"type": "Definition", "definition": "EqualsComparable"},
8 8
 		"Hashable": {"type": "Definition", "definition": "Hashable"},
9 9
 		"IllegalArgumentException": {"type": "Definition", "definition": "IllegalArgumentException"},
10
+		"Iterable": {"type": "Definition", "definition": "Iterable"},
11
+		"Iterator": {"type": "Definition", "definition": "Iterator"},
12
+		"List": {"type": "Definition", "definition": "List"},
10 13
 		"StringBuilder": {"type": "Definition", "definition": "StringBuilder"},
11 14
 		"StringBuildable": {"type": "Definition", "definition": "StringBuildable"},
12 15
 		"Result": {"type": "Definition", "definition": "Result"}

+ 20
- 13
stdlib/src/Arrays.zs View File

@@ -9,7 +9,7 @@ export expand <T : Comparable<T>> T[] {
9 9
 export expand <T : Hashable<T>> T[] {
10 10
 	public implements Hashable<T[]> {
11 11
 		public extern hashCode() as int;
12
-		public extern == (other as T) as bool;
12
+		public extern == (other as T[]) as bool;
13 13
 	}
14 14
 }
15 15
 
@@ -22,15 +22,15 @@ export expand <T> T[] {
22 22
 	[Native("copy")]
23 23
 	public extern copy() as T[];
24 24
 	[Native("copyResize")]
25
-	public extern copy(newSize as int) as T[];
25
+	public extern copy(newSize as usize) as T[];
26 26
 	[Native("copyTo")]
27
-	public extern copyTo(target as T[], sourceOffset as int, targetOffset as int, length as int) as void;
27
+	public extern copyTo(target as T[], sourceOffset as usize, targetOffset as usize, length as usize) as void;
28 28
 	
29 29
 	public get first as T?
30 30
 		=> this.isEmpty ? null : this[0];
31 31
 	
32 32
 	public get last as T?
33
-		=> this.isEmpty ? null : this[$ - 1];
33
+		=> this.isEmpty ? null : this[$ - (1 as usize)];
34 34
 	
35 35
 	[Native("reverse")]
36 36
 	public reverse() as void {
@@ -53,7 +53,7 @@ export expand <T> T[] {
53 53
 	}
54 54
 	
55 55
 	[Native("mapKeyValues")]
56
-	public map<U>(projection as function(index as int, value as T) as U) as U[] {
56
+	public map<U>(projection as function(index as usize, value as T) as U) as U[] {
57 57
 		return new U[]<T>(this, projection);
58 58
 	}
59 59
 	
@@ -67,7 +67,7 @@ export expand <T> T[] {
67 67
 	}
68 68
 	
69 69
 	[Native("filterKeyValues")]
70
-	public filter(predicate as function(index as int, value as T) as bool) as T[] {
70
+	public filter(predicate as function(index as usize, value as T) as bool) as T[] {
71 71
 		var values = new List<T>();
72 72
 		for i, value in this
73 73
 			if predicate(i, value)
@@ -80,7 +80,7 @@ export expand <T> T[] {
80 80
 			consumer(value);
81 81
 	}
82 82
 	
83
-	public each(consumer as function(index as int, value as T) as void) as void {
83
+	public each(consumer as function(index as usize, value as T) as void) as void {
84 84
 		for i, value in this
85 85
 			consumer(i, value);
86 86
 	}
@@ -93,7 +93,7 @@ export expand <T> T[] {
93 93
 		return false;
94 94
 	}
95 95
 	
96
-	public contains(predicate as function(index as int, value as T) as bool) as bool {
96
+	public contains(predicate as function(index as usize, value as T) as bool) as bool {
97 97
 		for i, value in this
98 98
 			if predicate(i, value)
99 99
 				return true;
@@ -109,7 +109,7 @@ export expand <T> T[] {
109 109
 		return true;
110 110
 	}
111 111
 	
112
-	public all(predicate as function(i as int, value as T) as bool) as bool {
112
+	public all(predicate as function(i as usize, value as T) as bool) as bool {
113 113
 		for i, value in this
114 114
 			if !predicate(i, value)
115 115
 				return false;
@@ -125,7 +125,7 @@ export expand <T> T[] {
125 125
 		return null;
126 126
 	}
127 127
 	
128
-	public first(predicate as function(i as int, value as T) as bool) as T? {
128
+	public first(predicate as function(i as usize, value as T) as bool) as T? {
129 129
 		for i, value in this
130 130
 			if predicate(i, value)
131 131
 				return value;
@@ -144,7 +144,7 @@ export expand <T> T[] {
144 144
 		return null;
145 145
 	}
146 146
 	
147
-	public last(predicate as function(index as int, value as T) as bool) as T? {
147
+	public last(predicate as function(index as usize, value as T) as bool) as T? {
148 148
 		var i = length;
149 149
 		while i > 0 {
150 150
 			i--;
@@ -155,7 +155,7 @@ export expand <T> T[] {
155 155
 		return null;
156 156
 	}
157 157
 	
158
-	public count(predicate as function(value as T) as bool) as int {
158
+	public count(predicate as function(value as T) as bool) as usize {
159 159
 		var result = 0;
160 160
 		for value in this
161 161
 			if predicate(value)
@@ -163,11 +163,18 @@ export expand <T> T[] {
163 163
 		return result;
164 164
 	}
165 165
 	
166
-	public count(predicate as function(index as int, value as T) as bool) as int {
166
+	public count(predicate as function(index as usize, value as T) as bool) as usize {
167 167
 		var result = 0;
168 168
 		for i, value in this
169 169
 			if predicate(i, value)
170 170
 				result++;
171 171
 		return result;
172 172
 	}
173
+	
174
+	public index<K>(key as function(value as T) as K) as T[K] {
175
+		var result = new T[K];
176
+		for value in this
177
+			result[key(value)] = value;
178
+		return result;
179
+	}
173 180
 }

+ 1
- 1
stdlib/src/Chars.zs View File

@@ -1,5 +1,5 @@
1 1
 export expand char {
2
-	public times(number as int) as string {
2
+	public times(number as usize) as string {
3 3
 		return new string(new char[](number, this));
4 4
 	}
5 5
 }

+ 5
- 0
stdlib/src/Iterable.zs View File

@@ -0,0 +1,5 @@
1
+[Native("stdlib::Iterable")]
2
+public interface Iterable<T> {
3
+	[Native("iterate")]
4
+	iterate() as Iterator<T>;
5
+}

+ 8
- 0
stdlib/src/Iterator.zs View File

@@ -0,0 +1,8 @@
1
+[Native("stdlib::Iterator")]
2
+public interface Iterator<T> {
3
+	[Native("hasNext")]
4
+	hasNext() as bool;
5
+	
6
+	[Native("next")]
7
+	next() as T;
8
+}

+ 21
- 4
stdlib/src/List.zs View File

@@ -7,20 +7,37 @@ export class List<T> {
7 7
 	public add(value as T) as void;
8 8
 	
9 9
 	[Native("insert")]
10
-	public insert(index as int, value as T) as void;
10
+	public insert(index as usize, value as T) as void;
11
+	
12
+	[Native("remove")]
13
+	public remove(value as usize) as T;
14
+	
15
+	[Native("indexOf")]
16
+	public indexOf(value as T) as usize;
17
+	
18
+	[Native("lastIndexOf")]
19
+	public lastIndexOf(value as T) as usize;
11 20
 	
12 21
 	[Native("getAtIndex")]
13
-	public [](index as int) as T;
22
+	public [](index as usize) as T;
14 23
 	
15 24
 	[Native("setAtIndex")]
16
-	public []=(index as int, value as T) as T;
25
+	public []=(index as usize, value as T) as T;
26
+	
27
+	[Native("contains")]
28
+	public in(value as T) as bool;
17 29
 	
18 30
 	[Native("toArray")]
19 31
 	public as T[];
20 32
 	
21 33
 	[Native("length")]
22
-	public get length as int;
34
+	public get length as usize;
23 35
 	
24 36
 	[Native("isEmpty")]
25 37
 	public get isEmpty as bool;
38
+	
39
+	public implements Iterable<T> {
40
+		[Native("iterate")]
41
+		iterate();
42
+	}
26 43
 }

+ 4
- 2
stdlib/src/StringBuilder.zs View File

@@ -3,14 +3,14 @@ export class StringBuilder {
3 3
 	[Native("constructor")]
4 4
 	public extern this();
5 5
 	[Native("constructorWithCapacity")]
6
-	public extern this(capacity as int);
6
+	public extern this(capacity as usize);
7 7
 	[Native("constructorWithValue")]
8 8
 	public extern this(value as string);
9 9
 	
10 10
 	[Native("isEmpty")]
11 11
 	public extern get isEmpty as bool;
12 12
 	[Native("length")]
13
-	public extern get length as int;
13
+	public extern get length as usize;
14 14
 	
15 15
 	[Native("appendBool")]
16 16
 	public extern <<(value as bool) as StringBuilder;
@@ -30,6 +30,8 @@ export class StringBuilder {
30 30
 	public extern <<(value as long) as StringBuilder;
31 31
 	[Native("appendULong")]
32 32
 	public extern <<(value as ulong) as StringBuilder;
33
+	[Native("appendUSize")]
34
+	public extern <<(value as usize) as StringBuilder;
33 35
 	[Native("appendFloat")]
34 36
 	public extern <<(value as float) as StringBuilder;
35 37
 	[Native("appendDouble")]

+ 25
- 13
stdlib/src/Strings.zs View File

@@ -1,31 +1,37 @@
1 1
 [Native("stdlib::String")]
2 2
 export expand string {
3
+	[Native("fromAsciiBytes")]
4
+	public static fromAsciiBytes(data as byte[]) as string;
5
+	
6
+	[Native("fromUTF8Bytes")]
7
+	public static fromUTF8Bytes(data as byte[]) as string;
8
+	
3 9
 	[Native("contains")]
4 10
 	public const in(c as char) as bool
5
-		=> indexOf(c) >= 0;
11
+		=> indexOf(c) != null;
6 12
 	
7 13
 	[Native("indexOf")]
8
-	public const indexOf(c as char) as int {
14
+	public const indexOf(c as char) as usize? {
9 15
 		for i in 0 .. length {
10 16
 			if this[i] == c
11 17
 				return i;
12 18
 		}
13 19
 		
14
-		return -1;
20
+		return null;
15 21
 	}
16 22
 	
17 23
 	[Native("indexOfFrom")]
18
-	public const indexOf(c as char, from as int) as int {
24
+	public const indexOf(c as char, from as usize) as usize? {
19 25
 		for i in from .. length {
20 26
 			if this[i] == c
21 27
 				return i;
22 28
 		}
23 29
 		
24
-		return -1;
30
+		return null;
25 31
 	}
26 32
 	
27 33
 	[Native("lastIndexOf")]
28
-	public const lastIndexOf(c as char) as int {
34
+	public const lastIndexOf(c as char) as usize? {
29 35
 		var i = length;
30 36
 		while i > 0 {
31 37
 			i--;
@@ -33,11 +39,11 @@ export expand string {
33 39
 				return i;
34 40
 		}
35 41
 		
36
-		return -1;
42
+		return null;
37 43
 	}
38 44
 	
39 45
 	[Native("lastIndexOfFrom")]
40
-	public const lastIndexOf(c as char, until as int) as int {
46
+	public const lastIndexOf(c as char, until as usize) as usize? {
41 47
 		var i = until;
42 48
 		while i > 0 {
43 49
 			i--;
@@ -45,13 +51,13 @@ export expand string {
45 51
 				return i;
46 52
 		}
47 53
 		
48
-		return -1;
54
+		return null;
49 55
 	}
50 56
 	
51 57
 	[Native("split")]
52 58
 	public const split(delimiter as char) as string[] {
53 59
 		val result = new List<string>();
54
-		var start = 0;
60
+		var start = 0 as usize;
55 61
 		for i in 0 .. this.length {
56 62
 			if this[i] == delimiter {
57 63
 				result.add(this[start .. i]);
@@ -64,7 +70,7 @@ export expand string {
64 70
 	
65 71
 	[Native("trim")]
66 72
 	public const trim() as string {
67
-		var from = 0;
73
+		var from = 0 as usize;
68 74
 		while from < this.length && this[from] in [' ', '\t', '\r', '\n']
69 75
 			from++;
70 76
 		var to = this.length;
@@ -75,10 +81,16 @@ export expand string {
75 81
 	}
76 82
 	
77 83
 	[Native("lpad")]
78
-	public const lpad(length as int, c as char) as string
84
+	public const lpad(length as usize, c as char) as string
79 85
 		=> this.length >= length ? this : c.times(length - this.length) + this;
80 86
 	
81 87
 	[Native("rpad")]
82
-	public const rpad(length as int, c as char) as string
88
+	public const rpad(length as usize, c as char) as string
83 89
 		=> this.length >= length ? this : this + c.times(length - this.length);
90
+		
91
+	[Native("toAsciiBytes")]
92
+	public const toAsciiBytes() as byte[];
93
+	
94
+	[Native("toUTF8Bytes")]
95
+	public const toUTF8Bytes() as byte[];
84 96
 }

+ 13
- 0
stdlib/src/USize.zs View File

@@ -0,0 +1,13 @@
1
+[Native("stdlib::USize")]
2
+expand usize {
3
+	[Native("toHexString")]
4
+	public extern toHexString() as string;
5
+	
6
+	[Native("min")]
7
+	public static min(a as usize, b as usize) as usize
8
+		=> a < b ? a : b;
9
+	
10
+	[Native("max")]
11
+	public static max(a as usize, b as usize) as usize
12
+		=> a > b ? a : b;
13
+}

Loading…
Cancel
Save