aboutsummaryrefslogtreecommitdiff
path: root/main.ha
blob: 05c9dabb95b032a830d16f708b9599c0da7f54fa (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
use bufio;
use bytes;
use fmt;
use hash;
use hash::fnv;
use io;
use os;
use sys;
use sort;
use sort::cmp;
use strings;
use types;

type data = struct {
	min: i64,
	max: i64,
	sum: i64,
	count: size
};

fn i64parse(bytes: []u8) (i64, size) = {
	let index = 0z;
	let u: i64 = 0;
	let neg = 1;

	if (bytes[index] == '-') {
		neg = -1;
		index += 1;
	};

	u = (bytes[index] - '0'): i64;
	index += 1;

	if (bytes[index] != '.') {
		u = u * 10 + (bytes[index] - '0'): i64;
		index += 1;
	};

	index += 1;
	u = u * 10 + (bytes[index] - '0'): i64;
	index += 2;

	return (u * neg, index);
};

@test fn parsei64() void = {
    assert(i64parse(strings::toutf8("0.0")).0 == 0);
    assert(i64parse(strings::toutf8("9.2")).0 == 92);
    assert(i64parse(strings::toutf8("-9.2")).0 == -92);
    assert(i64parse(strings::toutf8("98.2")).0 == 982);
    assert(i64parse(strings::toutf8("-98.2")).0 == -982);
};

fn mmap(fd: io::file) []u8 = {
	let sb = sys::st { ... };
	sys::fstat(fd, &sb)!;

	let ptr = sys::mmap(null, sb.sz, sys::PROT_READ, sys::MAP_PRIVATE, fd, 0)!;

	// Hardcoded value for MADV_SEQUENTIAL, from libc-rs it seems like it's
	// 2 on all currently supported platforms (Linux, FreeBSD, NetBSD,
	// OpenBSD, Dragonfly).
	assert(sys::syscall(sys::SYS_madvise, ptr: uintptr: u64, sb.sz: u64, 2) == 0);

	return *(&types::slice {
		data = ptr,
		length = sb.sz,
		capacity = 0
	}: *[]u8);
};

fn free_mmap(map: []u8) void = {
	let map = &map: *types::slice;
	sys::munmap(map.data: *opaque, map.length)!;
};

fn names_cmp(a: *opaque, b: *opaque) int = {
	const a = *(a: *(u64, str));
	const b = *(b: *(u64, str));

	return cmp::strs(&a.1, &b.1);
};

export fn main() void = {
	let map: hashmap = [[]...];
	defer finish(&map);
	if (len(os::args) != 2)
		fmt::fatalf("usage: {} <file>", os::args[0]);

	const handle = os::open(os::args[1])!;
	defer io::close(handle)!;

	const fmap = mmap(handle);
	free_mmap(fmap);

	let buf: *[1024 * 1024]u8 = alloc([0...])!;
	defer free(buf);
	let read_start = 0z;

	let names_buf: *[10000](u64, str) = alloc([(0, "")...])!;
	let names = names_buf[..0];

	let fnv = fnv::fnv64a();
	for (true) {
		const n = match (io::read(handle, buf[read_start..])) {
		case let n: size =>
			yield n;
		case io::EOF =>
			break;
		};

		if (read_start + n == 0) break;

		let chunk = buf[..read_start + n];

		let newline = match(bytes::rindex(chunk, '\n': u8)) {
		case let idx: size =>
			yield idx;
		case void =>
			break;
		};

		let remaining = chunk[newline + 1..];
		chunk = chunk[..newline + 1];

		for (true) {
			let sep_idx = 0z;
			let has_semi = false;
			for (const char .. chunk) {
				if (char == ';': u8) {
					has_semi = true;
					break;
				};

				fnv.v ^= char;
				fnv.v *= 1099511628211;
				sep_idx += 1;
			};

			if (!has_semi) break;

			const hash = fnv::sum64(&fnv);
			hash::reset(&fnv);

			let after = chunk[sep_idx + 1..];
			const name = strings::fromutf8_unsafe(chunk[..sep_idx]);
			const (temp, index) = i64parse(after);

			chunk = after[index..];

			let item = getitem(&map, hash);

			if (item.count == 0) {
				static append(names, (hash, strings::dup(name)!))!;
				item.min = temp;
				item.max = temp;
			} else {
				if (item.min > temp)
					item.min = temp;
				if (item.max < temp)
					item.max = temp;
			};

			item.sum += temp;
			item.count += 1;

			setitem(&map, hash, item);
		};

		read_start = len(remaining);
		buf[..read_start] = remaining[..];
	};

	sort::sort(names, size((u64, str)), &names_cmp)!;

	fmt::print("{")!;
	for (let i = 0z; i < len(names); i += 1) {
		let station = names[i].1;
		let item = getitem(&map, names[i].0);

		if (i > 0)
			fmt::print(", ")!;

		fmt::printf("\"{}\"={:.1f}/{:.1f}/{:.1f}",
			station,
			item.min: f64 / 10.0,
			item.max: f64 / 10.0,
			item.sum: f64 / 10.0 / item.count: f64,
			)!;

		free(names[i].1);
	};
	fmt::println("}")!;

	free(names);
};