diff options
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 55 |
1 files changed, 40 insertions, 15 deletions
diff --git a/src/main.zig b/src/main.zig index c1d63c0..ac52f8a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4,6 +4,7 @@ pub const gss = @import("gss.zig"); pub const recognizer = @import("recognizer.zig"); pub const argument = @import("argument.zig"); pub const Generator = @import("generator.zig").Generator; +pub const Scheduler = @import("scheduler.zig").Scheduler; const Args = argument.Args; const RecognizeArgs = argument.RecognizeArgs; @@ -83,26 +84,50 @@ fn recognize(args: *RecognizeArgs, allocator: std.mem.Allocator) !void { try bufwriter.flush(); } -fn generate(args: *GenerateArgs, allocator: std.mem.Allocator) !void { - var writer = args.output.file.writer(); - var count: usize = 0; +const RandomGenerator = Generator(struct { + const Self = @This(); - var generator = Generator(struct { - const Self = @This(); + pub fn next(_: *Self, n: usize) usize { + return std.crypto.random.uintLessThan(usize, n); + } +}); - pub fn next(_: *Self, n: usize) usize { - return std.crypto.random.uintLessThan(usize, n); +pub fn generate_word( + args: *GenerateArgs, + generator: *RandomGenerator, + allocator: std.mem.Allocator +) []const u8 { + while (true) { + const text = generator.sentential_from_grammar( + &args.grammar, + 10000, + allocator + ) catch continue; + if (text.len >= args.min_length) { + return text; } - }){}; - while (count < args.count) { - const text = try generator.sentential_from_grammar(&args.grammar, 1000, allocator); - defer allocator.free(text); + allocator.free(text); + } +} - if (args.empty or text.len > 0) { - try writer.print("{s}\n", .{text}); - count += 1; - } +fn generate(args: *GenerateArgs, allocator: std.mem.Allocator) !void { + var writer = args.output.file.writer(); + + var scheduler: Scheduler(generate_word) = undefined; + try scheduler.init(allocator); + var generator = RandomGenerator {}; + + for (0..args.count) |_| { + try scheduler.push_task(.{args, &generator, allocator}); + } + + const results = try scheduler.deinit(); + defer allocator.free(results); + + for (results) |result| { + try writer.print("{s}\n", .{result}); + allocator.free(result); } } |