Skip to content

Instantly share code, notes, and snippets.

@voigt
Created April 19, 2022 16:42
Show Gist options
  • Save voigt/1760a2be495523b85cee24c73faeb8ed to your computer and use it in GitHub Desktop.
Save voigt/1760a2be495523b85cee24c73faeb8ed to your computer and use it in GitHub Desktop.
Wordcount
$ wordcounter ./zig-out/bin/wordcounter
Start processing
> No man is an island,
No ('No' added to HashMap)
man ('man' added to HashMap)
is ('is' added to HashMap)
an ('an' added to HashMap)
island, ('island,' added to HashMap)
> Entire of itself,
Entire ('Entire' added to HashMap)
of ('of' added to HashMap)
itself, ('itself,' added to HashMap)
> Every man is a piece of the continent,
Every ('Every' added to HashMap)
man ('man' existed in HashMap, inc(2))
is ('is' existed in HashMap, inc(2))
a ('a' added to HashMap)
piece ('piece' added to HashMap)
of ('of' existed in HashMap, inc(2))
the ('the' added to HashMap)
continent, ('continent,' added to HashMap)
> A part of the main.
A ('A' added to HashMap)
part ('part' added to HashMap)
of ('of' existed in HashMap, inc(3))
the ('the' existed in HashMap, inc(2))
main. ('main.' added to HashMap)
> If a clod be washed away by the sea,
If ('If' added to HashMap)
a ('a' existed in HashMap, inc(2))
clod ('clod' added to HashMap)
be ('be' added to HashMap)
washed ('washed' added to HashMap)
away ('away' added to HashMap)
by ('by' added to HashMap)
the ('the' existed in HashMap, inc(3))
sea, ('sea,' added to HashMap)
> Europe is the less.
Europe ('Europe' added to HashMap)
is ('is' existed in HashMap, inc(3))
the ('the' existed in HashMap, inc(4))
less. ('less.' added to HashMap)
> As well as if a promontory were.
As ('As' added to HashMap)
well ('well' added to HashMap)
as ('as' added to HashMap)
if ('if' added to HashMap)
a ('a' existed in HashMap, inc(3))
promontory ('promontory' added to HashMap)
were. ('were.' added to HashMap)
> As well as if a manor of thy friend’s
As ('As' existed in HashMap, inc(2))
well ('well' existed in HashMap, inc(2))
as ('as' existed in HashMap, inc(2))
if ('if' existed in HashMap, inc(2))
a ('a' existed in HashMap, inc(4))
manor ('manor' added to HashMap)
of ('of' existed in HashMap, inc(4))
thy ('thy' added to HashMap)
friend’s ('friend’s' added to HashMap)
> Or of thine own were:
Or ('Or' added to HashMap)
of ('of' existed in HashMap, inc(5))
thine ('thine' added to HashMap)
own ('own' added to HashMap)
were: ('were:' added to HashMap)
> Any man’s death diminishes me,
Any ('Any' added to HashMap)
man’s ('man’s' added to HashMap)
death ('death' added to HashMap)
diminishes ('diminishes' added to HashMap)
me, ('me,' added to HashMap)
> Because I am involved in mankind,
Because ('Because' added to HashMap)
I ('I' added to HashMap)
am ('am' added to HashMap)
involved ('involved' added to HashMap)
in ('in' added to HashMap)
mankind, ('mankind,' added to HashMap)
> And therefore never send to know for whom the bell tolls;
And ('And' added to HashMap)
therefore ('therefore' added to HashMap)
never ('never' added to HashMap)
send ('send' added to HashMap)
to ('to' added to HashMap)
know ('know' added to HashMap)
for ('for' added to HashMap)
whom ('whom' added to HashMap)
the ('the' existed in HashMap, inc(5))
bell ('bell' added to HashMap)
tolls; ('tolls;' added to HashMap)
> It tolls for thee.
It ('It' added to HashMap)
tolls ('tolls' added to HashMap)
for ('for' existed in HashMap, inc(2))
thee. ('thee.' added to HashMap)
std.hash_map.HashMap([]const u8,u8,std.hash_map.StringContext,80){ .unmanaged = std.hash_map.HashMapUnmanaged([]const u8,u8,std.hash_map.StringContext,80){ .metadata = [*]std.hash_map.HashMapUnmanaged([]const u8,u8,std.hash_map.StringContext,80).Metadata@103976030, .size = 62, .available = 40 }, .allocator = Allocator{ .ptr = anyopaque@7ffeec32f748, .vtable = std.mem.Allocator.VTable{ .alloc = fn(*anyopaque, usize, u29, u29, usize) std.mem.Allocator.Error![]u8@1038cfe90, .resize = fn(*anyopaque, []u8, u29, usize, u29, usize) ?usize@1038cff70, .free = fn(*anyopaque, []u8, u29, usize) void@1038d0030 } }, .ctx = std.hash_map.StringContext{ } }
---------------------------------------
counts.count(): 62
Segmentation fault at address 0x103976028
/Users/cvoigt/go/src/github.com/voigt/ziglings/bin/lib/zig/std/hash_map.zig:955:33: 0x1038d310c in std.hash_map.HashMapUnmanaged([]const u8,u8,std.hash_map.StringContext,80).capacity (wordcounter)
return self.header().capacity;
^
/Users/cvoigt/go/src/github.com/voigt/ziglings/bin/lib/zig/std/hash_map.zig:818:50: 0x1038d11bc in std.hash_map.HashMapUnmanaged([]const u8,u8,std.hash_map.StringContext,80).Iterator.next (wordcounter)
assert(it.index <= it.hm.capacity());
^
/Users/cvoigt/go/src/github.com/voigt/zigwasm/wordcounter/src/main.zig:51:19: 0x1038d0627 in main (wordcounter)
while (it.next()) |word| {
^
/Users/cvoigt/go/src/github.com/voigt/ziglings/bin/lib/zig/std/start.zig:583:37: 0x1038d76b8 in std.start.callMain (wordcounter)
const result = root.main() catch |err| {
^
/Users/cvoigt/go/src/github.com/voigt/ziglings/bin/lib/zig/std/start.zig:517:12: 0x1038d16b7 in std.start.callMainWithArgs (wordcounter)
return @call(.{ .modifier = .always_inline }, callMain, .{});
^
/Users/cvoigt/go/src/github.com/voigt/ziglings/bin/lib/zig/std/start.zig:482:12: 0x1038d15f5 in std.start.main (wordcounter)
return @call(.{ .modifier = .always_inline }, callMainWithArgs, .{ @intCast(usize, c_argc), c_argv, envp });
^
???:?:?: 0x7fff204ddf3c in ??? (???)
???:?:?: 0x0 in ??? (???)
zsh: abort ./zig-out/bin/wordcounter
No man is an island,
Entire of itself,
Every man is a piece of the continent,
A part of the main.
If a clod be washed away by the sea,
Europe is the less.
As well as if a promontory were.
As well as if a manor of thy friend’s
Or of thine own were:
Any man’s death diminishes me,
Because I am involved in mankind,
And therefore never send to know for whom the bell tolls;
It tolls for thee.
const std = @import("std");
fn process(file_input: []const u8) anyerror!std.StringHashMap(u8) {
var file = try std.fs.cwd().openFile(file_input, .{});
defer file.close();
var buf_reader = std.io.bufferedReader(file.reader());
var in_stream = buf_reader.reader();
var buf: [1024]u8 = undefined;
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
var counts = std.StringHashMap(u8).init(allocator);
while (try in_stream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
std.debug.print("> {s}\n", .{ line });
var words = std.mem.split(u8, line, " ");
while (words.next()) |word| {
std.debug.print("{s} ", .{word});
const duped = try allocator.dupe(u8, word);
var v = try counts.getOrPut(duped);
if (!v.found_existing) {
v.value_ptr.* = 1;
std.debug.print("('{s}' added to HashMap)\n", .{duped});
} else {
v.value_ptr.* += 1;
std.debug.print("('{s}' existed in HashMap, inc({}))\n", .{ duped, v.value_ptr.*});
}
}
}
return counts;
}
pub fn main() anyerror!void {
std.debug.print("Start processing\n", .{ });
var counts = try process("text.txt");
std.debug.print("{}\n", .{ counts });
std.debug.print("---------------------------------------\n", .{ });
var fields = counts.count();
std.debug.print("counts.count(): {}\n", .{ fields });
var it = &counts.iterator();
while (it.next()) |word| {
std.debug.print("{}\n", .{ word });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment