This repository has been archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
clibart.zig
201 lines (180 loc) · 6.46 KB
/
clibart.zig
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
// zig test src/clibart.zig --c-source libartc/src/art.c -I/usr/include/ -I/usr/include/x86_64-linux-gnu/ -lc -I libartc/src -I. -DLANG="'z'"
const std = @import("std");
const artc = @cImport({
@cInclude("art.h");
});
const clibart = @cImport({
@cInclude("src/clibart.c");
});
extern var show_debug: c_int;
const art = @import("art.zig");
const Art = art.Art;
const testing = std.testing;
const a = std.heap.c_allocator;
const Lang = enum { c, z, both };
const lang = switch (clibart.LANG) {
'c' => .c,
'z' => .z,
'b' => .both,
else => unreachable,
};
const UTree = Art(usize);
test "compare node keys" {
var t: artc.art_tree = undefined;
_ = artc.art_tree_init(&t);
defer _ = artc.art_tree_destroy(&t);
var ta = Art(usize).init(a);
defer ta.deinit();
const f = try std.fs.cwd().openFile("./testdata/words.txt", .{ .mode = .read_only });
defer f.close();
var linei: usize = 1;
const stream = &f.inStream();
var buf: [512:0]u8 = undefined;
const stop_line = 200;
var i: usize = 0;
while (try stream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
defer i += 1;
// if (i > stop_line) break;
buf[line.len] = 0;
if (lang == .c or lang == .both) {
// this prevents all inserted values from pointing to the same value
// TODO fix leak
const temp = try a.create(usize);
temp.* = linei;
const result = artc.art_insert(&t, line.ptr, @intCast(c_int, line.len), temp);
} else if (lang == .z or lang == .both) {
const result = try ta.insert(buf[0..line.len :0], linei);
}
linei += 1;
}
if (lang == .c or lang == .both) {
show_debug = 1;
artc.art_print(&t);
}
if (lang == .z or lang == .both) {
try ta.print();
}
}
// this is used to compare against output from the original c version of libart
test "compare tree after delete" {
var t: artc.art_tree = undefined;
_ = artc.art_tree_init(&t);
defer _ = artc.art_tree_destroy(&t);
var ta = Art(usize).init(a);
defer ta.deinit();
const f = try std.fs.cwd().openFile("./testdata/words.txt", .{ .mode = .read_only });
defer f.close();
var linei: usize = 1;
const stream = &f.inStream();
var buf: [512:0]u8 = undefined;
const stop_line = 197141;
while (try stream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
buf[line.len] = 0;
if (lang == .c or lang == .both) {
// this prevents all inserted values from pointing to the same value
// TODO fix leak
const temp = try a.create(usize);
temp.* = linei;
const result = artc.art_insert(&t, line.ptr, @intCast(c_int, line.len), temp);
} else if (lang == .z or lang == .both) {
const result = try ta.insert(buf[0..line.len :0], linei);
}
// if (linei == stop_line) break;
linei += 1;
}
_ = try f.seekTo(0);
linei = 1;
while (try stream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
buf[line.len] = 0;
if (linei == stop_line) {
if (lang == .c or lang == .both) {
show_debug = 1;
artc.art_print(&t);
show_debug = 0;
}
if (lang == .z or lang == .both) {
// var list = std.ArrayList(u8).init(a);
// try ta.printToStream(&std.io.getStdOut().outStream());
// try ta.printToStream(&list.outStream());
try ta.print();
}
}
if (lang == .c or lang == .both) {
const result = artc.art_delete(&t, line.ptr, @intCast(c_int, line.len));
testing.expect(result != null);
} else if (lang == .z or lang == .both) {
const result = try ta.delete(buf[0..line.len :0]);
if (result != .found) {
std.debug.warn("\nfailed on line {}:{}\n", .{ linei, line });
}
testing.expect(result == .found);
}
if (linei == stop_line) break;
linei += 1;
}
if (lang == .c or lang == .both) {
show_debug = 1;
artc.art_print(&t);
show_debug = 0;
}
if (lang == .z or lang == .both) {
// var list = std.ArrayList(u8).init(a);
// try ta.printToStream(&std.io.getStdOut().outStream());
// try ta.printToStream(&list.outStream());
}
}
// zig test src/clibart.zig --c-source libartc/src/art.c -I/usr/include/ -I/usr/include/x86_64-linux-gnu/ -lc -I libartc/src -I. -DLANG="'c'" --test-filter bench --release-fast
test "bench against libart" {
var t: artc.art_tree = undefined;
_ = artc.art_tree_init(&t);
defer _ = artc.art_tree_destroy(&t);
var ta = Art(usize).init(a);
defer ta.deinit();
const filename = "./testdata/words.txt";
const f = try std.fs.cwd().openFile(filename, .{ .mode = .read_only });
defer f.close();
var linei: usize = 1;
const stream = &f.inStream();
var buf: [512:0]u8 = undefined;
var timer = try std.time.Timer.start();
while (try stream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
buf[line.len] = 0;
if (lang == .z or lang == .both)
_ = try ta.insert(buf[0..line.len :0], linei);
if (lang == .c or lang == .both) {
var tmp: usize = 0;
const result = artc.art_insert(&t, line.ptr, @intCast(c_int, line.len), &tmp);
testing.expect(result == null);
}
}
const t1 = timer.read();
timer.reset();
try f.seekTo(0);
while (try stream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
buf[line.len] = 0;
if (lang == .z or lang == .both)
_ = ta.search(buf[0..line.len :0]);
if (lang == .c or lang == .both) {
_ = artc.art_search(&t, line.ptr, @intCast(c_int, line.len));
}
}
const t2 = timer.read();
timer.reset();
try f.seekTo(0);
while (try stream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
buf[line.len] = 0;
if (lang == .z or lang == .both)
_ = try ta.delete(buf[0..line.len :0]);
if (lang == .c or lang == .both) {
_ = artc.art_delete(&t, line.ptr, @intCast(c_int, line.len));
}
}
const t3 = timer.read();
std.debug.warn("{: <7} insert {}ms, search {}ms, delete {}ms, combined {}ms\n", .{
if (lang == .z) "art.zig" else "art.c",
t1 / 1000000,
t2 / 1000000,
t3 / 1000000,
(t1 + t2 + t3) / 1000000,
});
}