Init
This commit is contained in:
74
examples/birth-date-finder/README.md
Normal file
74
examples/birth-date-finder/README.md
Normal file
@@ -0,0 +1,74 @@
|
||||
# Birth Date Finder Example
|
||||
|
||||
This example demonstrates how the Pi Finder Library is used to search for a birth date (01.01.02) in the decimal places of Pi.
|
||||
|
||||
## Execution
|
||||
|
||||
```bash
|
||||
cd birth-date-finder
|
||||
zig build run
|
||||
```
|
||||
|
||||
## Additional Examples
|
||||
|
||||
This project includes two additional examples:
|
||||
|
||||
### 1. Multiple Test Example
|
||||
|
||||
```bash
|
||||
zig build test-multiple
|
||||
```
|
||||
|
||||
This example tests multiple different sequences and measures the performance.
|
||||
|
||||
### 2. Plain Text Format Example
|
||||
|
||||
```bash
|
||||
zig build plain-text
|
||||
```
|
||||
|
||||
This example demonstrates using the `formatSearchResultAsPlainText` function instead of the default JSON format.
|
||||
|
||||
## Features
|
||||
|
||||
- Searches for the sequence "010102" in Pi
|
||||
- Uses '-' as delimiter every 2 digits (01-01-02 format)
|
||||
- Outputs the result as JSON
|
||||
- Shows performance metrics
|
||||
|
||||
## Example Output
|
||||
|
||||
### JSON Output (Default)
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"sequence": "010102",
|
||||
"position": 12345,
|
||||
"context": "...92[01-01-02]94...",
|
||||
"performance": {
|
||||
"file_load_time_ms": 1250.45,
|
||||
"search_time_ms": 23.67,
|
||||
"file_size_mb": 95.37,
|
||||
"load_speed_mbs": 76.28
|
||||
},
|
||||
"context_config": {
|
||||
"size": 50,
|
||||
"position": 25,
|
||||
"delimiter": "-",
|
||||
"delimiter_interval": 2,
|
||||
"prefix": "[",
|
||||
"suffix": "]"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Plain Text Output (Using formatSearchResultAsPlainText)
|
||||
|
||||
```text
|
||||
Search Result:
|
||||
Success: true
|
||||
Sequence: 010102
|
||||
Position: 12345
|
||||
Context: ...92[01-01-02]94...
|
||||
```
|
||||
65
examples/birth-date-finder/build.zig
Normal file
65
examples/birth-date-finder/build.zig
Normal file
@@ -0,0 +1,65 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.Build) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
// Create the pi-finder module locally
|
||||
const pi_finder_module = b.createModule(.{
|
||||
.root_source_file = b.path("../../src/lib.zig"),
|
||||
});
|
||||
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "birth-date-finder",
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
exe.root_module.addImport("pi-finder", pi_finder_module);
|
||||
b.installArtifact(exe);
|
||||
|
||||
const run_cmd = b.addRunArtifact(exe);
|
||||
run_cmd.step.dependOn(b.getInstallStep());
|
||||
|
||||
if (b.args) |args| {
|
||||
run_cmd.addArgs(args);
|
||||
}
|
||||
|
||||
const run_step = b.step("run", "Run the app");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
|
||||
// Add test executable
|
||||
const test_exe = b.addExecutable(.{
|
||||
.name = "test-multiple",
|
||||
.root_source_file = b.path("src/test_multiple.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
test_exe.root_module.addImport("pi-finder", pi_finder_module);
|
||||
b.installArtifact(test_exe);
|
||||
|
||||
const test_run_cmd = b.addRunArtifact(test_exe);
|
||||
test_run_cmd.step.dependOn(b.getInstallStep());
|
||||
|
||||
const test_step = b.step("test-multiple", "Run multiple tests");
|
||||
test_step.dependOn(&test_run_cmd.step);
|
||||
|
||||
// Add plain text example executable
|
||||
const plain_text_exe = b.addExecutable(.{
|
||||
.name = "plain-text-example",
|
||||
.root_source_file = b.path("src/plain_text_example.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
plain_text_exe.root_module.addImport("pi-finder", pi_finder_module);
|
||||
b.installArtifact(plain_text_exe);
|
||||
|
||||
const plain_text_run_cmd = b.addRunArtifact(plain_text_exe);
|
||||
plain_text_run_cmd.step.dependOn(b.getInstallStep());
|
||||
|
||||
const plain_text_step = b.step("plain-text", "Run the plain text format example");
|
||||
plain_text_step.dependOn(&plain_text_run_cmd.step);
|
||||
}
|
||||
48
examples/birth-date-finder/src/main.zig
Normal file
48
examples/birth-date-finder/src/main.zig
Normal file
@@ -0,0 +1,48 @@
|
||||
const std = @import("std");
|
||||
const PiFinder = @import("pi-finder");
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
// Path to the Pi file (relative to the main project)
|
||||
const pi_file_path = "../../data/pi-decimal-10_000_000_000.txt";
|
||||
|
||||
// Initialize Pi Finder (JSON output is standard)
|
||||
var pi_finder = PiFinder.PiFinder.init(allocator, pi_file_path) catch |err| {
|
||||
std.debug.print("Error initializing Pi finder: {}\n", .{err});
|
||||
return;
|
||||
};
|
||||
defer pi_finder.deinit();
|
||||
|
||||
// Configure birth date
|
||||
const birth_date = "010102"; // 01.01.02
|
||||
|
||||
// Context configuration with delimiter '-'
|
||||
const context_config = PiFinder.ContextConfig{
|
||||
.size = 50,
|
||||
.position = 25,
|
||||
.delimiter = "-",
|
||||
.delimiter_interval = 2,
|
||||
.prefix = "[",
|
||||
.suffix = "]",
|
||||
};
|
||||
|
||||
// Perform search
|
||||
const result = try pi_finder.searchSequence(birth_date, context_config, allocator);
|
||||
|
||||
// Output result as JSON
|
||||
const json_output = try PiFinder.formatSearchResultAsJson(allocator, result);
|
||||
defer allocator.free(json_output);
|
||||
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
try stdout.print("{s}\n", .{json_output});
|
||||
|
||||
// Clean up if context was allocated
|
||||
if (result.context) |context| {
|
||||
if (context_config.delimiter != null) {
|
||||
allocator.free(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
50
examples/birth-date-finder/src/plain_text_example.zig
Normal file
50
examples/birth-date-finder/src/plain_text_example.zig
Normal file
@@ -0,0 +1,50 @@
|
||||
const std = @import("std");
|
||||
const PiFinder = @import("pi-finder");
|
||||
|
||||
/// This example demonstrates how to use the formatSearchResultAsPlainText function
|
||||
/// to output search results in plain text format instead of JSON
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
// Path to the Pi file (relative to the main project)
|
||||
const pi_file_path = "../../data/pi-decimal-10_000_000_000.txt";
|
||||
|
||||
// Initialize Pi Finder
|
||||
var pi_finder = PiFinder.PiFinder.init(allocator, pi_file_path) catch |err| {
|
||||
std.debug.print("Error initializing Pi finder: {}\n", .{err});
|
||||
return;
|
||||
};
|
||||
defer pi_finder.deinit();
|
||||
|
||||
// Configure birth date
|
||||
const birth_date = "010102"; // 01.01.02
|
||||
|
||||
// Context configuration with delimiter '-'
|
||||
const context_config = PiFinder.ContextConfig{
|
||||
.size = 50,
|
||||
.position = 25,
|
||||
.delimiter = "-",
|
||||
.delimiter_interval = 2,
|
||||
.prefix = "[",
|
||||
.suffix = "]",
|
||||
};
|
||||
|
||||
// Perform search
|
||||
const result = try pi_finder.searchSequence(birth_date, context_config, allocator);
|
||||
|
||||
// Output result as plain text
|
||||
const plain_text_output = try PiFinder.formatSearchResultAsPlainText(allocator, result);
|
||||
defer allocator.free(plain_text_output);
|
||||
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
try stdout.print("{s}\n", .{plain_text_output});
|
||||
|
||||
// Clean up if context was allocated
|
||||
if (result.context) |context| {
|
||||
if (context_config.delimiter != null) {
|
||||
allocator.free(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
59
examples/birth-date-finder/src/test_multiple.zig
Normal file
59
examples/birth-date-finder/src/test_multiple.zig
Normal file
@@ -0,0 +1,59 @@
|
||||
const std = @import("std");
|
||||
const PiFinder = @import("pi-finder");
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
// Path to the Pi file (relative to the main project)
|
||||
const pi_file_path = "../../data/pi-decimal-10_000_000_000.txt";
|
||||
|
||||
// Initialize Pi Finder (JSON output is standard)
|
||||
var pi_finder = PiFinder.PiFinder.init(allocator, pi_file_path) catch |err| {
|
||||
const result = PiFinder.SearchResult{
|
||||
.success = false,
|
||||
.sequence = "N/A",
|
||||
.position = null,
|
||||
.context = null,
|
||||
.error_message = @errorName(err),
|
||||
};
|
||||
const json_output = try PiFinder.formatSearchResultAsJson(allocator, result);
|
||||
defer allocator.free(json_output);
|
||||
std.debug.print("{s}\n", .{json_output});
|
||||
return;
|
||||
};
|
||||
defer pi_finder.deinit();
|
||||
|
||||
// Test with different birth dates
|
||||
const test_dates = [_][]const u8{ "010102", "123456", "314159" };
|
||||
|
||||
for (test_dates) |birth_date| {
|
||||
// Context configuration with delimiter '-' for date
|
||||
const context_config = PiFinder.ContextConfig{
|
||||
.size = 40,
|
||||
.position = 20,
|
||||
.delimiter = "-",
|
||||
.delimiter_interval = 2,
|
||||
.prefix = "[",
|
||||
.suffix = "]",
|
||||
};
|
||||
|
||||
// Perform search
|
||||
const result = try pi_finder.searchSequence(birth_date, context_config, allocator);
|
||||
|
||||
// Output result as JSON
|
||||
const json_output = try PiFinder.formatSearchResultAsJson(allocator, result);
|
||||
defer allocator.free(json_output);
|
||||
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
try stdout.print("Searching for {s}:\n{s}\n\n", .{ birth_date, json_output });
|
||||
|
||||
// Clean up if context was allocated
|
||||
if (result.context) |context| {
|
||||
if (context_config.delimiter != null) {
|
||||
allocator.free(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user