mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-09 13:45:35 +08:00
upgrade zig codes to 0.11.0-dev.3379+629f0d23b (#563)
* upgrade zig codes to 0.11.0-dev.3379+629f0d23b * upgrade zig codes to 0.11.0-dev.3379+629f0d23b
This commit is contained in:
@@ -4,460 +4,261 @@
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
// Zig Version: 0.10.1
|
||||
// Zig Version: 0.11.0-dev.3379+629f0d23b
|
||||
// Build Command: zig build
|
||||
pub fn build(b: *std.build.Builder) void {
|
||||
pub fn build(b: *std.Build) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const mode = b.standardReleaseOptions();
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
// Section: "Time Complexity"
|
||||
const group_name_path = .{
|
||||
// Source File: "chapter_computational_complexity/time_complexity.zig"
|
||||
// Run Command: zig build run_time_complexity
|
||||
const exe_time_complexity = b.addExecutable("time_complexity", "chapter_computational_complexity/time_complexity.zig");
|
||||
exe_time_complexity.addPackagePath("include", "include/include.zig");
|
||||
exe_time_complexity.setTarget(target);
|
||||
exe_time_complexity.setBuildMode(mode);
|
||||
exe_time_complexity.install();
|
||||
const run_cmd_time_complexity = exe_time_complexity.run();
|
||||
run_cmd_time_complexity.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_time_complexity.addArgs(args);
|
||||
const run_step_time_complexity = b.step("run_time_complexity", "Run time_complexity");
|
||||
run_step_time_complexity.dependOn(&run_cmd_time_complexity.step);
|
||||
.{
|
||||
.name = "time_complexity",
|
||||
.path = "chapter_computational_complexity/time_complexity.zig"
|
||||
},
|
||||
|
||||
// Source File: "chapter_computational_complexity/worst_best_time_complexity.zig"
|
||||
// Run Command: zig build run_worst_best_time_complexity
|
||||
const exe_worst_best_time_complexity = b.addExecutable("worst_best_time_complexity", "chapter_computational_complexity/worst_best_time_complexity.zig");
|
||||
exe_worst_best_time_complexity.addPackagePath("include", "include/include.zig");
|
||||
exe_worst_best_time_complexity.setTarget(target);
|
||||
exe_worst_best_time_complexity.setBuildMode(mode);
|
||||
exe_worst_best_time_complexity.install();
|
||||
const run_cmd_worst_best_time_complexity = exe_worst_best_time_complexity.run();
|
||||
run_cmd_worst_best_time_complexity.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_worst_best_time_complexity.addArgs(args);
|
||||
const run_step_worst_best_time_complexity = b.step("run_worst_best_time_complexity", "Run worst_best_time_complexity");
|
||||
run_step_worst_best_time_complexity.dependOn(&run_cmd_worst_best_time_complexity.step);
|
||||
.{
|
||||
.name = "worst_best_time_complexity",
|
||||
.path = "chapter_computational_complexity/worst_best_time_complexity.zig"
|
||||
},
|
||||
|
||||
// Section: "Space Complexity"
|
||||
// Source File: "chapter_computational_complexity/space_complexity.zig"
|
||||
// Run Command: zig build run_space_complexity
|
||||
const exe_space_complexity = b.addExecutable("space_complexity", "chapter_computational_complexity/space_complexity.zig");
|
||||
exe_space_complexity.addPackagePath("include", "include/include.zig");
|
||||
exe_space_complexity.setTarget(target);
|
||||
exe_space_complexity.setBuildMode(mode);
|
||||
exe_space_complexity.install();
|
||||
const run_cmd_space_complexity = exe_space_complexity.run();
|
||||
run_cmd_space_complexity.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_space_complexity.addArgs(args);
|
||||
const run_step_space_complexity = b.step("run_space_complexity", "Run space_complexity");
|
||||
run_step_space_complexity.dependOn(&run_cmd_space_complexity.step);
|
||||
.{
|
||||
.name = "space_complexity",
|
||||
.path = "chapter_computational_complexity/space_complexity.zig"
|
||||
},
|
||||
|
||||
// Section: "Space Time Tradeoff"
|
||||
// Source File: "chapter_computational_complexity/two_sum.zig"
|
||||
// Run Command: zig build run_two_sum
|
||||
const exe_two_sum = b.addExecutable("two_sum", "chapter_computational_complexity/two_sum.zig");
|
||||
exe_two_sum.addPackagePath("include", "include/include.zig");
|
||||
exe_two_sum.setTarget(target);
|
||||
exe_two_sum.setBuildMode(mode);
|
||||
exe_two_sum.install();
|
||||
const run_cmd_two_sum = exe_two_sum.run();
|
||||
run_cmd_two_sum.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_two_sum.addArgs(args);
|
||||
const run_step_two_sum = b.step("run_two_sum", "Run two_sum");
|
||||
run_step_two_sum.dependOn(&run_cmd_two_sum.step);
|
||||
|
||||
// Section: "Array"
|
||||
// Source File: "chapter_array_and_linkedlist/array.zig"
|
||||
// Run Command: zig build run_array
|
||||
const exe_array = b.addExecutable("array", "chapter_array_and_linkedlist/array.zig");
|
||||
exe_array.addPackagePath("include", "include/include.zig");
|
||||
exe_array.setTarget(target);
|
||||
exe_array.setBuildMode(mode);
|
||||
exe_array.install();
|
||||
const run_cmd_array = exe_array.run();
|
||||
run_cmd_array.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_array.addArgs(args);
|
||||
const run_step_array = b.step("run_array", "Run array");
|
||||
run_step_array.dependOn(&run_cmd_array.step);
|
||||
.{
|
||||
.name = "array",
|
||||
.path = "chapter_array_and_linkedlist/array.zig"
|
||||
},
|
||||
|
||||
// Section: "LinkedList"
|
||||
// Source File: "chapter_array_and_linkedlist/linked_list.zig"
|
||||
// Run Command: zig build run_linked_list
|
||||
const exe_linked_list = b.addExecutable("linked_list", "chapter_array_and_linkedlist/linked_list.zig");
|
||||
exe_linked_list.addPackagePath("include", "include/include.zig");
|
||||
exe_linked_list.setTarget(target);
|
||||
exe_linked_list.setBuildMode(mode);
|
||||
exe_linked_list.install();
|
||||
const run_cmd_linked_list = exe_linked_list.run();
|
||||
run_cmd_linked_list.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_linked_list.addArgs(args);
|
||||
const run_step_linked_list = b.step("run_linked_list", "Run linked_list");
|
||||
run_step_linked_list.dependOn(&run_cmd_linked_list.step);
|
||||
.{
|
||||
.name = "linked_list",
|
||||
.path = "chapter_array_and_linkedlist/linked_list.zig"
|
||||
},
|
||||
|
||||
// Section: "List"
|
||||
// Source File: "chapter_array_and_linkedlist/list.zig"
|
||||
// Run Command: zig build run_list
|
||||
const exe_list = b.addExecutable("list", "chapter_array_and_linkedlist/list.zig");
|
||||
exe_list.addPackagePath("include", "include/include.zig");
|
||||
exe_list.setTarget(target);
|
||||
exe_list.setBuildMode(mode);
|
||||
exe_list.install();
|
||||
const run_cmd_list = exe_list.run();
|
||||
run_cmd_list.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_list.addArgs(args);
|
||||
const run_step_list = b.step("run_list", "Run list");
|
||||
run_step_list.dependOn(&run_cmd_list.step);
|
||||
.{
|
||||
.name = "list",
|
||||
.path = "chapter_array_and_linkedlist/list.zig"
|
||||
},
|
||||
|
||||
// Source File: "chapter_array_and_linkedlist/my_list.zig"
|
||||
// Run Command: zig build run_my_list
|
||||
const exe_my_list = b.addExecutable("my_list", "chapter_array_and_linkedlist/my_list.zig");
|
||||
exe_my_list.addPackagePath("include", "include/include.zig");
|
||||
exe_my_list.setTarget(target);
|
||||
exe_my_list.setBuildMode(mode);
|
||||
exe_my_list.install();
|
||||
const run_cmd_my_list = exe_my_list.run();
|
||||
run_cmd_my_list.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_my_list.addArgs(args);
|
||||
const run_step_my_list = b.step("run_my_list", "Run my_list");
|
||||
run_step_my_list.dependOn(&run_cmd_my_list.step);
|
||||
.{
|
||||
.name = "my_list",
|
||||
.path = "chapter_array_and_linkedlist/my_list.zig"
|
||||
},
|
||||
|
||||
// Section: "Stack"
|
||||
// Source File: "chapter_stack_and_queue/stack.zig"
|
||||
// Run Command: zig build run_stack
|
||||
const exe_stack = b.addExecutable("stack", "chapter_stack_and_queue/stack.zig");
|
||||
exe_stack.addPackagePath("include", "include/include.zig");
|
||||
exe_stack.setTarget(target);
|
||||
exe_stack.setBuildMode(mode);
|
||||
exe_stack.install();
|
||||
const run_cmd_stack = exe_stack.run();
|
||||
run_cmd_stack.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_stack.addArgs(args);
|
||||
const run_step_stack = b.step("run_stack", "Run stack");
|
||||
run_step_stack.dependOn(&run_cmd_stack.step);
|
||||
.{
|
||||
.name = "stack",
|
||||
.path = "chapter_stack_and_queue/stack.zig"
|
||||
},
|
||||
|
||||
// Source File: "chapter_stack_and_queue/linkedlist_stack.zig"
|
||||
// Run Command: zig build run_linkedlist_stack
|
||||
const exe_linkedlist_stack = b.addExecutable("linkedlist_stack", "chapter_stack_and_queue/linkedlist_stack.zig");
|
||||
exe_linkedlist_stack.addPackagePath("include", "include/include.zig");
|
||||
exe_linkedlist_stack.setTarget(target);
|
||||
exe_linkedlist_stack.setBuildMode(mode);
|
||||
exe_linkedlist_stack.install();
|
||||
const run_cmd_linkedlist_stack = exe_linkedlist_stack.run();
|
||||
run_cmd_linkedlist_stack.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_linkedlist_stack.addArgs(args);
|
||||
const run_step_linkedlist_stack = b.step("run_linkedlist_stack", "Run linkedlist_stack");
|
||||
run_step_linkedlist_stack.dependOn(&run_cmd_linkedlist_stack.step);
|
||||
.{
|
||||
.name = "linkedlist_stack",
|
||||
.path = "chapter_stack_and_queue/linkedlist_stack.zig"
|
||||
},
|
||||
|
||||
// Source File: "chapter_stack_and_queue/array_stack.zig"
|
||||
// Run Command: zig build run_array_stack
|
||||
const exe_array_stack = b.addExecutable("array_stack", "chapter_stack_and_queue/array_stack.zig");
|
||||
exe_array_stack.addPackagePath("include", "include/include.zig");
|
||||
exe_array_stack.setTarget(target);
|
||||
exe_array_stack.setBuildMode(mode);
|
||||
exe_array_stack.install();
|
||||
const run_cmd_array_stack = exe_array_stack.run();
|
||||
run_cmd_array_stack.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_array_stack.addArgs(args);
|
||||
const run_step_array_stack = b.step("run_array_stack", "Run array_stack");
|
||||
run_step_array_stack.dependOn(&run_cmd_array_stack.step);
|
||||
.{
|
||||
.name = "array_stack",
|
||||
.path = "chapter_stack_and_queue/array_stack.zig"
|
||||
},
|
||||
|
||||
// Section: "Queue"
|
||||
// Source File: "chapter_stack_and_queue/queue.zig"
|
||||
// Run Command: zig build run_queue
|
||||
const exe_queue = b.addExecutable("queue", "chapter_stack_and_queue/queue.zig");
|
||||
exe_queue.addPackagePath("include", "include/include.zig");
|
||||
exe_queue.setTarget(target);
|
||||
exe_queue.setBuildMode(mode);
|
||||
exe_queue.install();
|
||||
const run_cmd_queue = exe_queue.run();
|
||||
run_cmd_queue.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_queue.addArgs(args);
|
||||
const run_step_queue = b.step("run_queue", "Run queue");
|
||||
run_step_queue.dependOn(&run_cmd_queue.step);
|
||||
.{
|
||||
.name = "queue",
|
||||
.path = "chapter_stack_and_queue/queue.zig"
|
||||
},
|
||||
|
||||
// Source File: "chapter_stack_and_queue/array_queue.zig"
|
||||
// Run Command: zig build run_array_queue
|
||||
const exe_array_queue = b.addExecutable("array_queue", "chapter_stack_and_queue/array_queue.zig");
|
||||
exe_array_queue.addPackagePath("include", "include/include.zig");
|
||||
exe_array_queue.setTarget(target);
|
||||
exe_array_queue.setBuildMode(mode);
|
||||
exe_array_queue.install();
|
||||
const run_cmd_array_queue = exe_array_queue.run();
|
||||
run_cmd_array_queue.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_array_queue.addArgs(args);
|
||||
const run_step_array_queue = b.step("run_array_queue", "Run array_queue");
|
||||
run_step_array_queue.dependOn(&run_cmd_array_queue.step);
|
||||
.{
|
||||
.name = "array_queue",
|
||||
.path = "chapter_stack_and_queue/array_queue.zig"
|
||||
},
|
||||
|
||||
// Source File: "chapter_stack_and_queue/linkedlist_queue.zig"
|
||||
// Run Command: zig build run_linkedlist_queue
|
||||
const exe_linkedlist_queue = b.addExecutable("linkedlist_queue", "chapter_stack_and_queue/linkedlist_queue.zig");
|
||||
exe_linkedlist_queue.addPackagePath("include", "include/include.zig");
|
||||
exe_linkedlist_queue.setTarget(target);
|
||||
exe_linkedlist_queue.setBuildMode(mode);
|
||||
exe_linkedlist_queue.install();
|
||||
const run_cmd_linkedlist_queue = exe_linkedlist_queue.run();
|
||||
run_cmd_linkedlist_queue.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_linkedlist_queue.addArgs(args);
|
||||
const run_step_linkedlist_queue = b.step("run_linkedlist_queue", "Run linkedlist_queue");
|
||||
run_step_linkedlist_queue.dependOn(&run_cmd_linkedlist_queue.step);
|
||||
.{
|
||||
.name = "linkedlist_queue",
|
||||
.path = "chapter_stack_and_queue/linkedlist_queue.zig"
|
||||
},
|
||||
|
||||
// Section: "Deque"
|
||||
// Source File: "chapter_stack_and_queue/deque.zig"
|
||||
// Run Command: zig build run_deque
|
||||
const exe_deque = b.addExecutable("deque", "chapter_stack_and_queue/deque.zig");
|
||||
exe_deque.addPackagePath("include", "include/include.zig");
|
||||
exe_deque.setTarget(target);
|
||||
exe_deque.setBuildMode(mode);
|
||||
exe_deque.install();
|
||||
const run_cmd_deque = exe_deque.run();
|
||||
run_cmd_deque.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_deque.addArgs(args);
|
||||
const run_step_deque = b.step("run_deque", "Run deque");
|
||||
run_step_deque.dependOn(&run_cmd_deque.step);
|
||||
.{
|
||||
.name = "deque",
|
||||
.path = "chapter_stack_and_queue/deque.zig"
|
||||
},
|
||||
|
||||
// Source File: "chapter_stack_and_queue/linkedlist_deque.zig"
|
||||
// Run Command: zig build run_linkedlist_deque
|
||||
const exe_linkedlist_deque = b.addExecutable("linkedlist_deque", "chapter_stack_and_queue/linkedlist_deque.zig");
|
||||
exe_linkedlist_deque.addPackagePath("include", "include/include.zig");
|
||||
exe_linkedlist_deque.setTarget(target);
|
||||
exe_linkedlist_deque.setBuildMode(mode);
|
||||
exe_linkedlist_deque.install();
|
||||
const run_cmd_linkedlist_deque = exe_linkedlist_deque.run();
|
||||
run_cmd_linkedlist_deque.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_linkedlist_deque.addArgs(args);
|
||||
const run_step_linkedlist_deque = b.step("run_linkedlist_deque", "Run linkedlist_deque");
|
||||
run_step_linkedlist_deque.dependOn(&run_cmd_linkedlist_deque.step);
|
||||
|
||||
// Section: "Hash Map"
|
||||
.{
|
||||
.name = "linkedlist_deque",
|
||||
.path = "chapter_stack_and_queue/linkedlist_deque.zig"
|
||||
},
|
||||
|
||||
// Source File: "chapter_hashing/hash_map.zig"
|
||||
// Run Command: zig build run_hash_map
|
||||
const exe_hash_map = b.addExecutable("hash_map", "chapter_hashing/hash_map.zig");
|
||||
exe_hash_map.addPackagePath("include", "include/include.zig");
|
||||
exe_hash_map.setTarget(target);
|
||||
exe_hash_map.setBuildMode(mode);
|
||||
exe_hash_map.install();
|
||||
const run_cmd_hash_map = exe_hash_map.run();
|
||||
run_cmd_hash_map.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_hash_map.addArgs(args);
|
||||
const run_step_hash_map= b.step("run_hash_map", "Run hash_map");
|
||||
run_step_hash_map.dependOn(&run_cmd_hash_map.step);
|
||||
.{
|
||||
.name = "hash_map",
|
||||
.path = "chapter_hashing/hash_map.zig"
|
||||
},
|
||||
|
||||
// Source File: "chapter_hashing/array_hash_map.zig"
|
||||
// Run Command: zig build run_array_hash_map
|
||||
const exe_array_hash_map = b.addExecutable("array_hash_map", "chapter_hashing/array_hash_map.zig");
|
||||
exe_array_hash_map.addPackagePath("include", "include/include.zig");
|
||||
exe_array_hash_map.setTarget(target);
|
||||
exe_array_hash_map.setBuildMode(mode);
|
||||
exe_array_hash_map.install();
|
||||
const run_cmd_array_hash_map = exe_array_hash_map.run();
|
||||
run_cmd_array_hash_map.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_array_hash_map.addArgs(args);
|
||||
const run_step_array_hash_map= b.step("run_array_hash_map", "Run array_hash_map");
|
||||
run_step_array_hash_map.dependOn(&run_cmd_array_hash_map.step);
|
||||
.{
|
||||
.name = "array_hash_map",
|
||||
.path = "chapter_hashing/array_hash_map.zig"
|
||||
},
|
||||
|
||||
// Section: "Binary Tree"
|
||||
// Source File: "chapter_tree/binary_tree.zig"
|
||||
// Run Command: zig build run_binary_tree
|
||||
const exe_binary_tree = b.addExecutable("hash_map", "chapter_tree/binary_tree.zig");
|
||||
exe_binary_tree.addPackagePath("include", "include/include.zig");
|
||||
exe_binary_tree.setTarget(target);
|
||||
exe_binary_tree.setBuildMode(mode);
|
||||
exe_binary_tree.install();
|
||||
const run_cmd_binary_tree = exe_binary_tree.run();
|
||||
run_cmd_binary_tree.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_binary_tree.addArgs(args);
|
||||
const run_step_binary_tree= b.step("run_binary_tree", "Run binary_tree");
|
||||
run_step_binary_tree.dependOn(&run_cmd_binary_tree.step);
|
||||
.{
|
||||
.name = "binary_tree",
|
||||
.path = "chapter_tree/binary_tree.zig"
|
||||
},
|
||||
|
||||
// Source File: "chapter_tree/binary_tree_bfs.zig"
|
||||
// Run Command: zig build run_binary_tree_bfs
|
||||
const exe_binary_tree_bfs = b.addExecutable("binary_tree_bfs", "chapter_tree/binary_tree_bfs.zig");
|
||||
exe_binary_tree_bfs.addPackagePath("include", "include/include.zig");
|
||||
exe_binary_tree_bfs.setTarget(target);
|
||||
exe_binary_tree_bfs.setBuildMode(mode);
|
||||
exe_binary_tree_bfs.install();
|
||||
const run_cmd_binary_tree_bfs = exe_binary_tree_bfs.run();
|
||||
run_cmd_binary_tree_bfs.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_binary_tree_bfs.addArgs(args);
|
||||
const run_step_binary_tree_bfs = b.step("run_binary_tree_bfs", "Run binary_tree_bfs");
|
||||
run_step_binary_tree_bfs.dependOn(&run_cmd_binary_tree_bfs.step);
|
||||
.{
|
||||
.name = "binary_tree_bfs",
|
||||
.path = "chapter_tree/binary_tree_bfs.zig"
|
||||
},
|
||||
|
||||
// Source File: "chapter_tree/binary_tree_dfs.zig"
|
||||
// Run Command: zig build run_binary_tree_dfs
|
||||
const exe_binary_tree_dfs = b.addExecutable("binary_tree_dfs", "chapter_tree/binary_tree_dfs.zig");
|
||||
exe_binary_tree_dfs.addPackagePath("include", "include/include.zig");
|
||||
exe_binary_tree_dfs.setTarget(target);
|
||||
exe_binary_tree_dfs.setBuildMode(mode);
|
||||
exe_binary_tree_dfs.install();
|
||||
const run_cmd_binary_tree_dfs = exe_binary_tree_dfs.run();
|
||||
run_cmd_binary_tree_dfs.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_binary_tree_dfs.addArgs(args);
|
||||
const run_step_binary_tree_dfs = b.step("run_binary_tree_dfs", "Run binary_tree_dfs");
|
||||
run_step_binary_tree_dfs.dependOn(&run_cmd_binary_tree_dfs.step);
|
||||
.{
|
||||
.name = "binary_tree_dfs",
|
||||
.path = "chapter_tree/binary_tree_dfs.zig"
|
||||
},
|
||||
|
||||
// Section: "Binary Search Tree"
|
||||
// Source File: "chapter_tree/binary_search_tree.zig"
|
||||
// Run Command: zig build run_binary_search_tree
|
||||
const exe_binary_search_tree = b.addExecutable("binary_search_tree", "chapter_tree/binary_search_tree.zig");
|
||||
exe_binary_search_tree.addPackagePath("include", "include/include.zig");
|
||||
exe_binary_search_tree.setTarget(target);
|
||||
exe_binary_search_tree.setBuildMode(mode);
|
||||
exe_binary_search_tree.install();
|
||||
const run_cmd_binary_search_tree = exe_binary_search_tree.run();
|
||||
run_cmd_binary_search_tree.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_binary_search_tree.addArgs(args);
|
||||
const run_step_binary_search_tree = b.step("run_binary_search_tree", "Run binary_search_tree");
|
||||
run_step_binary_search_tree.dependOn(&run_cmd_binary_search_tree.step);
|
||||
.{
|
||||
.name = "binary_search_tree",
|
||||
.path = "chapter_tree/binary_search_tree.zig"
|
||||
},
|
||||
|
||||
// Section: "AVL Tree"
|
||||
// Source File: "chapter_tree/avl_tree.zig"
|
||||
// Run Command: zig build run_avl_tree
|
||||
const exe_avl_tree = b.addExecutable("avl_tree", "chapter_tree/avl_tree.zig");
|
||||
exe_avl_tree.addPackagePath("include", "include/include.zig");
|
||||
exe_avl_tree.setTarget(target);
|
||||
exe_avl_tree.setBuildMode(mode);
|
||||
exe_avl_tree.install();
|
||||
const run_cmd_avl_tree = exe_avl_tree.run();
|
||||
run_cmd_avl_tree.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_avl_tree.addArgs(args);
|
||||
const run_step_avl_tree = b.step("run_avl_tree", "Run avl_tree");
|
||||
run_step_avl_tree.dependOn(&run_cmd_avl_tree.step);
|
||||
|
||||
// Section: "Heap"
|
||||
.{
|
||||
.name = "avl_tree",
|
||||
.path = "chapter_tree/avl_tree.zig"
|
||||
},
|
||||
|
||||
// Source File: "chapter_heap/heap.zig"
|
||||
// Run Command: zig build run_heap
|
||||
const exe_heap = b.addExecutable("heap", "chapter_heap/heap.zig");
|
||||
exe_heap.addPackagePath("include", "include/include.zig");
|
||||
exe_heap.setTarget(target);
|
||||
exe_heap.setBuildMode(mode);
|
||||
exe_heap.install();
|
||||
const run_cmd_heap = exe_heap.run();
|
||||
run_cmd_heap.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_heap.addArgs(args);
|
||||
const run_step_heap = b.step("run_heap", "Run heap");
|
||||
run_step_heap.dependOn(&run_cmd_heap.step);
|
||||
.{
|
||||
.name = "heap",
|
||||
.path = "chapter_heap/heap.zig"
|
||||
},
|
||||
|
||||
// Source File: "chapter_heap/my_heap.zig"
|
||||
// Run Command: zig build run_my_heap
|
||||
const exe_my_heap = b.addExecutable("my_heap", "chapter_heap/my_heap.zig");
|
||||
exe_my_heap.addPackagePath("include", "include/include.zig");
|
||||
exe_my_heap.setTarget(target);
|
||||
exe_my_heap.setBuildMode(mode);
|
||||
exe_my_heap.install();
|
||||
const run_cmd_my_heap = exe_my_heap.run();
|
||||
run_cmd_my_heap.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_my_heap.addArgs(args);
|
||||
const run_step_my_heap = b.step("run_my_heap", "Run my_heap");
|
||||
run_step_my_heap.dependOn(&run_cmd_my_heap.step);
|
||||
|
||||
// Section: "Linear Search"
|
||||
.{
|
||||
.name = "my_heap",
|
||||
.path = "chapter_heap/my_heap.zig"
|
||||
},
|
||||
|
||||
// Source File: "chapter_searching/linear_search.zig"
|
||||
// Run Command: zig build run_linear_search
|
||||
const exe_linear_search = b.addExecutable("linear_search", "chapter_searching/linear_search.zig");
|
||||
exe_linear_search.addPackagePath("include", "include/include.zig");
|
||||
exe_linear_search.setTarget(target);
|
||||
exe_linear_search.setBuildMode(mode);
|
||||
exe_linear_search.install();
|
||||
const run_cmd_linear_search = exe_linear_search.run();
|
||||
run_cmd_linear_search.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_linear_search.addArgs(args);
|
||||
const run_step_linear_search= b.step("run_linear_search", "Run linear_search");
|
||||
run_step_linear_search.dependOn(&run_cmd_linear_search.step);
|
||||
.{
|
||||
.name = "linear_search",
|
||||
.path = "chapter_searching/linear_search.zig"
|
||||
},
|
||||
|
||||
// Section: "Binary Search"
|
||||
// Source File: "chapter_searching/binary_search.zig"
|
||||
// Run Command: zig build run_binary_search
|
||||
const exe_binary_search = b.addExecutable("binary_search", "chapter_searching/binary_search.zig");
|
||||
exe_binary_search.addPackagePath("include", "include/include.zig");
|
||||
exe_binary_search.setTarget(target);
|
||||
exe_binary_search.setBuildMode(mode);
|
||||
exe_binary_search.install();
|
||||
const run_cmd_binary_search = exe_binary_search.run();
|
||||
run_cmd_binary_search.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_binary_search.addArgs(args);
|
||||
const run_step_binary_search= b.step("run_binary_search", "Run binary_search");
|
||||
run_step_binary_search.dependOn(&run_cmd_binary_search.step);
|
||||
.{
|
||||
.name = "binary_search",
|
||||
.path = "chapter_searching/binary_search.zig"
|
||||
},
|
||||
|
||||
// Section: "Hash Search"
|
||||
// Source File: "chapter_searching/hashing_search.zig"
|
||||
// Run Command: zig build run_hashing_search
|
||||
const exe_hashing_search = b.addExecutable("hashing_search", "chapter_searching/hashing_search.zig");
|
||||
exe_hashing_search.addPackagePath("include", "include/include.zig");
|
||||
exe_hashing_search.setTarget(target);
|
||||
exe_hashing_search.setBuildMode(mode);
|
||||
exe_hashing_search.install();
|
||||
const run_cmd_hashing_search = exe_hashing_search.run();
|
||||
run_cmd_hashing_search.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_hashing_search.addArgs(args);
|
||||
const run_step_hashing_search= b.step("run_hashing_search", "Run hashing_search");
|
||||
run_step_hashing_search.dependOn(&run_cmd_hashing_search.step);
|
||||
|
||||
// Section: "Bubble Sort"
|
||||
.{
|
||||
.name = "hashing_search",
|
||||
.path = "chapter_searching/hashing_search.zig"
|
||||
},
|
||||
|
||||
// Source File: "chapter_searching/two_sum.zig"
|
||||
// Run Command: zig build run_two_sum
|
||||
.{
|
||||
.name = "two_sum",
|
||||
.path = "chapter_searching/two_sum.zig"
|
||||
},
|
||||
|
||||
// Source File: "chapter_sorting/bubble_sort.zig"
|
||||
// Run Command: zig build run_bubble_sort
|
||||
const exe_bubble_sort = b.addExecutable("bubble_sort", "chapter_sorting/bubble_sort.zig");
|
||||
exe_bubble_sort.addPackagePath("include", "include/include.zig");
|
||||
exe_bubble_sort.setTarget(target);
|
||||
exe_bubble_sort.setBuildMode(mode);
|
||||
exe_bubble_sort.install();
|
||||
const run_cmd_bubble_sort = exe_bubble_sort.run();
|
||||
run_cmd_bubble_sort.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_bubble_sort.addArgs(args);
|
||||
const run_step_bubble_sort = b.step("run_bubble_sort", "Run bubble_sort");
|
||||
run_step_bubble_sort.dependOn(&run_cmd_bubble_sort.step);
|
||||
.{
|
||||
.name = "bubble_sort",
|
||||
.path = "chapter_sorting/bubble_sort.zig"
|
||||
},
|
||||
|
||||
// Section: "Insertion Sort"
|
||||
// Source File: "chapter_sorting/insertion_sort.zig"
|
||||
// Run Command: zig build run_insertion_sort
|
||||
const exe_insertion_sort = b.addExecutable("insertion_sort", "chapter_sorting/insertion_sort.zig");
|
||||
exe_insertion_sort.addPackagePath("include", "include/include.zig");
|
||||
exe_insertion_sort.setTarget(target);
|
||||
exe_insertion_sort.setBuildMode(mode);
|
||||
exe_insertion_sort.install();
|
||||
const run_cmd_insertion_sort = exe_insertion_sort.run();
|
||||
run_cmd_insertion_sort.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_insertion_sort.addArgs(args);
|
||||
const run_step_insertion_sort = b.step("run_insertion_sort", "Run insertion_sort");
|
||||
run_step_insertion_sort.dependOn(&run_cmd_insertion_sort.step);
|
||||
.{
|
||||
.name = "insertion_sort",
|
||||
.path = "chapter_sorting/insertion_sort.zig"
|
||||
},
|
||||
|
||||
// Section: "Quick Sort"
|
||||
// Source File: "chapter_sorting/quick_sort.zig"
|
||||
// Run Command: zig build run_quick_sort
|
||||
const exe_quick_sort = b.addExecutable("quick_sort", "chapter_sorting/quick_sort.zig");
|
||||
exe_quick_sort.addPackagePath("include", "include/include.zig");
|
||||
exe_quick_sort.setTarget(target);
|
||||
exe_quick_sort.setBuildMode(mode);
|
||||
exe_quick_sort.install();
|
||||
const run_cmd_quick_sort = exe_quick_sort.run();
|
||||
run_cmd_quick_sort.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_quick_sort.addArgs(args);
|
||||
const run_step_quick_sort = b.step("run_quick_sort", "Run quick_sort");
|
||||
run_step_quick_sort.dependOn(&run_cmd_quick_sort.step);
|
||||
.{
|
||||
.name = "quick_sort",
|
||||
.path = "chapter_sorting/quick_sort.zig"
|
||||
},
|
||||
|
||||
// Section: "Merge Sort"
|
||||
// Source File: "chapter_sorting/merge_sort.zig"
|
||||
// Run Command: zig build run_merge_sort
|
||||
const exe_merge_sort = b.addExecutable("merge_sort", "chapter_sorting/merge_sort.zig");
|
||||
exe_merge_sort.addPackagePath("include", "include/include.zig");
|
||||
exe_merge_sort.setTarget(target);
|
||||
exe_merge_sort.setBuildMode(mode);
|
||||
exe_merge_sort.install();
|
||||
const run_cmd_merge_sort = exe_merge_sort.run();
|
||||
run_cmd_merge_sort.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_merge_sort.addArgs(args);
|
||||
const run_step_merge_sort = b.step("run_merge_sort", "Run merge_sort");
|
||||
run_step_merge_sort.dependOn(&run_cmd_merge_sort.step);
|
||||
.{
|
||||
.name = "merge_sort",
|
||||
.path = "chapter_sorting/merge_sort.zig"
|
||||
},
|
||||
|
||||
// Section: "Radix Sort"
|
||||
// Source File: "chapter_sorting/radix_sort.zig"
|
||||
// Run Command: zig build run_radix_sort
|
||||
const exe_radix_sort = b.addExecutable("radix_sort", "chapter_sorting/radix_sort.zig");
|
||||
exe_radix_sort.addPackagePath("include", "include/include.zig");
|
||||
exe_radix_sort.setTarget(target);
|
||||
exe_radix_sort.setBuildMode(mode);
|
||||
exe_radix_sort.install();
|
||||
const run_cmd_radix_sort = exe_radix_sort.run();
|
||||
run_cmd_radix_sort.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_radix_sort.addArgs(args);
|
||||
const run_step_radix_sort = b.step("run_radix_sort", "Run radix_sort");
|
||||
run_step_radix_sort.dependOn(&run_cmd_radix_sort.step);
|
||||
.{
|
||||
.name = "radix_sort",
|
||||
.path = "chapter_sorting/radix_sort.zig"
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
inline for (group_name_path) |name_path| {
|
||||
const exe = b.addExecutable(.{
|
||||
.name = name_path.name,
|
||||
.root_source_file = .{ .path = name_path.path },
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
exe.addModule("include", b.addModule("", .{
|
||||
.source_file = .{ .path = "include/include.zig" },
|
||||
}));
|
||||
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_" ++ name_path.name, "Run the app");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ pub fn randomAccess(nums: []i32) i32 {
|
||||
pub fn extend(mem_allocator: std.mem.Allocator, nums: []i32, enlarge: usize) ![]i32 {
|
||||
// 初始化一个扩展长度后的数组
|
||||
var res = try mem_allocator.alloc(i32, nums.len + enlarge);
|
||||
std.mem.set(i32, res, 0);
|
||||
@memset(res, 0);
|
||||
// 将原数组中的所有元素复制到新数组
|
||||
std.mem.copy(i32, res, nums);
|
||||
// 返回扩展后的新数组
|
||||
@@ -62,7 +62,7 @@ pub fn traverse(nums: []i32) void {
|
||||
|
||||
// 在数组中查找指定元素
|
||||
pub fn find(nums: []i32, target: i32) i32 {
|
||||
for (nums) |num, i| {
|
||||
for (nums, 0..) |num, i| {
|
||||
if (num == target) return @intCast(i32, i);
|
||||
}
|
||||
return -1;
|
||||
|
||||
@@ -70,7 +70,7 @@ pub fn main() !void {
|
||||
inc.PrintUtil.printList(i32, list);
|
||||
|
||||
// 排序列表
|
||||
std.sort.sort(i32, list.items, {}, comptime std.sort.asc(i32));
|
||||
std.mem.sort(i32, list.items, {}, comptime std.sort.asc(i32));
|
||||
std.debug.print("\n排序列表后 list = ", .{});
|
||||
inc.PrintUtil.printList(i32, list);
|
||||
|
||||
|
||||
@@ -11,23 +11,23 @@ pub fn MyList(comptime T: type) type {
|
||||
const Self = @This();
|
||||
|
||||
nums: []T = undefined, // 数组(存储列表元素)
|
||||
nums_capacity: usize = 10, // 列表容量
|
||||
num_size: usize = 0, // 列表长度(即当前元素数量)
|
||||
extend_ratio: usize = 2, // 每次列表扩容的倍数
|
||||
numsCapacity: usize = 10, // 列表容量
|
||||
numSize: usize = 0, // 列表长度(即当前元素数量)
|
||||
extendRatio: usize = 2, // 每次列表扩容的倍数
|
||||
mem_arena: ?std.heap.ArenaAllocator = null,
|
||||
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
|
||||
|
||||
// 构造方法(分配内存+初始化列表)
|
||||
// 构造函数(分配内存+初始化列表)
|
||||
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
||||
if (self.mem_arena == null) {
|
||||
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
|
||||
self.mem_allocator = self.mem_arena.?.allocator();
|
||||
}
|
||||
self.nums = try self.mem_allocator.alloc(T, self.nums_capacity);
|
||||
std.mem.set(T, self.nums, @as(T, 0));
|
||||
self.nums = try self.mem_allocator.alloc(T, self.numsCapacity);
|
||||
@memset(self.nums, @as(T, 0));
|
||||
}
|
||||
|
||||
// 析构方法(释放内存)
|
||||
// 析构函数(释放内存)
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.mem_arena == null) return;
|
||||
self.mem_arena.?.deinit();
|
||||
@@ -35,12 +35,12 @@ pub fn MyList(comptime T: type) type {
|
||||
|
||||
// 获取列表长度(即当前元素数量)
|
||||
pub fn size(self: *Self) usize {
|
||||
return self.num_size;
|
||||
return self.numSize;
|
||||
}
|
||||
|
||||
// 获取列表容量
|
||||
pub fn capacity(self: *Self) usize {
|
||||
return self.nums_capacity;
|
||||
return self.numsCapacity;
|
||||
}
|
||||
|
||||
// 访问元素
|
||||
@@ -63,7 +63,7 @@ pub fn MyList(comptime T: type) type {
|
||||
if (self.size() == self.capacity()) try self.extendCapacity();
|
||||
self.nums[self.size()] = num;
|
||||
// 更新元素数量
|
||||
self.num_size += 1;
|
||||
self.numSize += 1;
|
||||
}
|
||||
|
||||
// 中间插入元素
|
||||
@@ -78,7 +78,7 @@ pub fn MyList(comptime T: type) type {
|
||||
}
|
||||
self.nums[index] = num;
|
||||
// 更新元素数量
|
||||
self.num_size += 1;
|
||||
self.numSize += 1;
|
||||
}
|
||||
|
||||
// 删除元素
|
||||
@@ -91,30 +91,30 @@ pub fn MyList(comptime T: type) type {
|
||||
self.nums[j] = self.nums[j + 1];
|
||||
}
|
||||
// 更新元素数量
|
||||
self.num_size -= 1;
|
||||
self.numSize -= 1;
|
||||
// 返回被删除元素
|
||||
return num;
|
||||
}
|
||||
|
||||
// 列表扩容
|
||||
pub fn extendCapacity(self: *Self) !void {
|
||||
// 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组拷贝到新数组
|
||||
var newCapacity = self.capacity() * self.extend_ratio;
|
||||
// 新建一个长度为 size * extendRatio 的数组,并将原数组拷贝到新数组
|
||||
var newCapacity = self.capacity() * self.extendRatio;
|
||||
var extend = try self.mem_allocator.alloc(T, newCapacity);
|
||||
std.mem.set(T, extend, @as(T, 0));
|
||||
@memset(extend, @as(T, 0));
|
||||
// 将原数组中的所有元素复制到新数组
|
||||
std.mem.copy(T, extend, self.nums);
|
||||
self.nums = extend;
|
||||
// 更新列表容量
|
||||
self.nums_capacity = newCapacity;
|
||||
self.numsCapacity = newCapacity;
|
||||
}
|
||||
|
||||
// 将列表转换为数组
|
||||
pub fn toArray(self: *Self) ![]T {
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
var nums = try self.mem_allocator.alloc(T, self.size());
|
||||
std.mem.set(T, nums, @as(T, 0));
|
||||
for (nums) |*num, i| {
|
||||
@memset(nums, @as(T, 0));
|
||||
for (nums, 0..) |*num, i| {
|
||||
num.* = self.get(i);
|
||||
}
|
||||
return nums;
|
||||
|
||||
@@ -153,7 +153,7 @@ pub fn main() !void {
|
||||
|
||||
count = quadratic(n);
|
||||
std.debug.print("平方阶的计算操作数量 = {}\n", .{count});
|
||||
for (nums) |*num, i| {
|
||||
for (&nums, 0..) |*num, i| {
|
||||
num.* = n - @intCast(i32, i); // [n,n-1,...,2,1]
|
||||
}
|
||||
count = bubbleSort(&nums);
|
||||
@@ -174,5 +174,7 @@ pub fn main() !void {
|
||||
|
||||
count = factorialRecur(n);
|
||||
std.debug.print("阶乘阶(递归实现)的计算操作数量 = {}\n", .{count});
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ const inc = @import("include");
|
||||
pub fn randomNumbers(comptime n: usize) [n]i32 {
|
||||
var nums: [n]i32 = undefined;
|
||||
// 生成数组 nums = { 1, 2, 3, ..., n }
|
||||
for (nums) |*num, i| {
|
||||
for (&nums, 0..) |*num, i| {
|
||||
num.* = @intCast(i32, i) + 1;
|
||||
}
|
||||
// 随机打乱数组元素
|
||||
@@ -20,7 +20,7 @@ pub fn randomNumbers(comptime n: usize) [n]i32 {
|
||||
|
||||
// 查找数组 nums 中数字 1 所在索引
|
||||
pub fn findOne(nums: []i32) i32 {
|
||||
for (nums) |num, i| {
|
||||
for (nums, 0..) |num, i| {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if (num == 1) return @intCast(i32, i);
|
||||
@@ -39,5 +39,7 @@ pub fn main() !void {
|
||||
inc.PrintUtil.printArray(i32, &nums);
|
||||
std.debug.print("数字 1 的索引为 {}\n", .{index});
|
||||
}
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ const Pair = struct {
|
||||
key: usize = undefined,
|
||||
val: []const u8 = undefined,
|
||||
|
||||
pub fn init(key: usize, val: []const u8) Pair {
|
||||
pub fn init(key: usize, val: []const u8) Pair {
|
||||
return Pair {
|
||||
.key = key,
|
||||
.val = val,
|
||||
@@ -21,25 +21,25 @@ const Pair = struct {
|
||||
// 基于数组简易实现的哈希表
|
||||
pub fn ArrayHashMap(comptime T: type) type {
|
||||
return struct {
|
||||
buckets: ?std.ArrayList(?T) = null,
|
||||
bucket: ?std.ArrayList(?T) = null,
|
||||
mem_allocator: std.mem.Allocator = undefined,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
// 构造方法
|
||||
// 构造函数
|
||||
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
||||
self.mem_allocator = allocator;
|
||||
// 初始化数组,包含 100 个桶
|
||||
self.buckets = std.ArrayList(?T).init(self.mem_allocator);
|
||||
// 初始化一个长度为 100 的桶(数组)
|
||||
self.bucket = std.ArrayList(?T).init(self.mem_allocator);
|
||||
var i: i32 = 0;
|
||||
while (i < 100) : (i += 1) {
|
||||
try self.buckets.?.append(null);
|
||||
try self.bucket.?.append(null);
|
||||
}
|
||||
}
|
||||
|
||||
// 析构方法
|
||||
// 析构函数
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.buckets != null) self.buckets.?.deinit();
|
||||
if (self.bucket != null) self.bucket.?.deinit();
|
||||
}
|
||||
|
||||
// 哈希函数
|
||||
@@ -51,7 +51,7 @@ pub fn ArrayHashMap(comptime T: type) type {
|
||||
// 查询操作
|
||||
pub fn get(self: *Self, key: usize) []const u8 {
|
||||
var index = hashFunc(key);
|
||||
var pair = self.buckets.?.items[index];
|
||||
var pair = self.bucket.?.items[index];
|
||||
return pair.?.val;
|
||||
}
|
||||
|
||||
@@ -59,44 +59,44 @@ pub fn ArrayHashMap(comptime T: type) type {
|
||||
pub fn put(self: *Self, key: usize, val: []const u8) !void {
|
||||
var pair = Pair.init(key, val);
|
||||
var index = hashFunc(key);
|
||||
self.buckets.?.items[index] = pair;
|
||||
self.bucket.?.items[index] = pair;
|
||||
}
|
||||
|
||||
// 删除操作
|
||||
pub fn remove(self: *Self, key: usize) !void {
|
||||
var index = hashFunc(key);
|
||||
// 置为 null ,代表删除
|
||||
self.buckets.?.items[index] = null;
|
||||
self.bucket.?.items[index] = null;
|
||||
}
|
||||
|
||||
// 获取所有键值对
|
||||
pub fn pairSet(self: *Self) !*std.ArrayList(T) {
|
||||
pub fn pairSet(self: *Self) !std.ArrayList(T) {
|
||||
var entry_set = std.ArrayList(T).init(self.mem_allocator);
|
||||
for (self.buckets.?.items) |item| {
|
||||
for (self.bucket.?.items) |item| {
|
||||
if (item == null) continue;
|
||||
try entry_set.append(item.?);
|
||||
}
|
||||
return &entry_set;
|
||||
return entry_set;
|
||||
}
|
||||
|
||||
// 获取所有键
|
||||
pub fn keySet(self: *Self) !*std.ArrayList(usize) {
|
||||
pub fn keySet(self: *Self) !std.ArrayList(usize) {
|
||||
var key_set = std.ArrayList(usize).init(self.mem_allocator);
|
||||
for (self.buckets.?.items) |item| {
|
||||
for (self.bucket.?.items) |item| {
|
||||
if (item == null) continue;
|
||||
try key_set.append(item.?.key);
|
||||
}
|
||||
return &key_set;
|
||||
return key_set;
|
||||
}
|
||||
|
||||
// 获取所有值
|
||||
pub fn valueSet(self: *Self) !*std.ArrayList([]const u8) {
|
||||
pub fn valueSet(self: *Self) !std.ArrayList([]const u8) {
|
||||
var value_set = std.ArrayList([]const u8).init(self.mem_allocator);
|
||||
for (self.buckets.?.items) |item| {
|
||||
for (self.bucket.?.items) |item| {
|
||||
if (item == null) continue;
|
||||
try value_set.append(item.?.val);
|
||||
}
|
||||
return &value_set;
|
||||
return value_set;
|
||||
}
|
||||
|
||||
// 打印哈希表
|
||||
@@ -144,19 +144,19 @@ pub fn main() !void {
|
||||
for (entry_set.items) |kv| {
|
||||
std.debug.print("{} -> {s}\n", .{kv.key, kv.val});
|
||||
}
|
||||
entry_set.deinit();
|
||||
defer entry_set.deinit();
|
||||
std.debug.print("\n单独遍历键 Key\n", .{});
|
||||
var key_set = try map.keySet();
|
||||
for (key_set.items) |key| {
|
||||
std.debug.print("{}\n", .{key});
|
||||
}
|
||||
key_set.deinit();
|
||||
defer key_set.deinit();
|
||||
std.debug.print("\n单独遍历值 value\n", .{});
|
||||
var value_set = try map.valueSet();
|
||||
for (value_set.items) |val| {
|
||||
std.debug.print("{s}\n", .{val});
|
||||
}
|
||||
value_set.deinit();
|
||||
defer value_set.deinit();
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
}
|
||||
@@ -61,4 +61,4 @@ pub fn main() !void {
|
||||
std.debug.print("目标元素 6 的索引 = {}\n", .{index});
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ pub fn main() !void {
|
||||
// 初始化哈希表
|
||||
var map = std.AutoHashMap(i32, i32).init(std.heap.page_allocator);
|
||||
defer map.deinit();
|
||||
for (nums) |num, i| {
|
||||
for (nums, 0..) |num, i| {
|
||||
try map.put(num, @intCast(i32, i)); // key: 元素,value: 索引
|
||||
}
|
||||
var index = hashingSearchArray(i32, map, target);
|
||||
|
||||
@@ -8,7 +8,7 @@ const inc = @import("include");
|
||||
// 线性查找(数组)
|
||||
fn linearSearchArray(comptime T: type, nums: std.ArrayList(T), target: T) T {
|
||||
// 遍历数组
|
||||
for (nums.items) |num, i| {
|
||||
for (nums.items, 0..) |num, i| {
|
||||
// 找到目标元素, 返回其索引
|
||||
if (num == target) {
|
||||
return @intCast(T, i);
|
||||
@@ -51,5 +51,4 @@ pub fn main() !void {
|
||||
try inc.PrintUtil.printLinkedList(i32, node);
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -53,4 +53,6 @@ pub fn main() !void {
|
||||
res = (try twoSumHashTable(&nums, target)).?;
|
||||
std.debug.print("\n方法二 res = ", .{});
|
||||
inc.PrintUtil.printArray(i32, &res);
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
}
|
||||
|
||||
@@ -28,5 +28,4 @@ pub fn main() !void {
|
||||
inc.PrintUtil.printArray(i32, &nums);
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -160,4 +160,4 @@ pub fn main() !void {
|
||||
inc.PrintUtil.printArray(i32, &nums2);
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ fn countingSortDigit(nums: []i32, exp: i32) !void {
|
||||
// defer mem_arena.deinit();
|
||||
const mem_allocator = mem_arena.allocator();
|
||||
var counter = try mem_allocator.alloc(usize, 10);
|
||||
std.mem.set(usize, counter, 0);
|
||||
@memset(counter, 0);
|
||||
var n = nums.len;
|
||||
// 统计 0~9 各数字的出现次数
|
||||
for (nums) |num| {
|
||||
|
||||
@@ -13,11 +13,11 @@ pub fn ArrayQueue(comptime T: type) type {
|
||||
nums: []T = undefined, // 用于存储队列元素的数组
|
||||
cap: usize = 0, // 队列容量
|
||||
front: usize = 0, // 队首指针,指向队首元素
|
||||
que_size: usize = 0, // 尾指针,指向队尾 + 1
|
||||
queSize: usize = 0, // 尾指针,指向队尾 + 1
|
||||
mem_arena: ?std.heap.ArenaAllocator = null,
|
||||
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
|
||||
|
||||
// 构造方法(分配内存+初始化数组)
|
||||
// 构造函数(分配内存+初始化数组)
|
||||
pub fn init(self: *Self, allocator: std.mem.Allocator, cap: usize) !void {
|
||||
if (self.mem_arena == null) {
|
||||
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
|
||||
@@ -25,10 +25,10 @@ pub fn ArrayQueue(comptime T: type) type {
|
||||
}
|
||||
self.cap = cap;
|
||||
self.nums = try self.mem_allocator.alloc(T, self.cap);
|
||||
std.mem.set(T, self.nums, @as(T, 0));
|
||||
@memset(self.nums, @as(T, 0));
|
||||
}
|
||||
|
||||
// 析构方法(释放内存)
|
||||
// 析构函数(释放内存)
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.mem_arena == null) return;
|
||||
self.mem_arena.?.deinit();
|
||||
@@ -41,12 +41,12 @@ pub fn ArrayQueue(comptime T: type) type {
|
||||
|
||||
// 获取队列的长度
|
||||
pub fn size(self: *Self) usize {
|
||||
return self.que_size;
|
||||
return self.queSize;
|
||||
}
|
||||
|
||||
// 判断队列是否为空
|
||||
pub fn isEmpty(self: *Self) bool {
|
||||
return self.que_size == 0;
|
||||
return self.queSize == 0;
|
||||
}
|
||||
|
||||
// 入队
|
||||
@@ -57,10 +57,10 @@ pub fn ArrayQueue(comptime T: type) type {
|
||||
}
|
||||
// 计算尾指针,指向队尾索引 + 1
|
||||
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
||||
var rear = (self.front + self.que_size) % self.capacity();
|
||||
// 将 num 添加至队尾
|
||||
var rear = (self.front + self.queSize) % self.capacity();
|
||||
// 尾节点后添加 num
|
||||
self.nums[rear] = num;
|
||||
self.que_size += 1;
|
||||
self.queSize += 1;
|
||||
}
|
||||
|
||||
// 出队
|
||||
@@ -68,7 +68,7 @@ pub fn ArrayQueue(comptime T: type) type {
|
||||
var num = self.peek();
|
||||
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
||||
self.front = (self.front + 1) % self.capacity();
|
||||
self.que_size -= 1;
|
||||
self.queSize -= 1;
|
||||
return num;
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ pub fn ArrayQueue(comptime T: type) type {
|
||||
pub fn toArray(self: *Self) ![]T {
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
var res = try self.mem_allocator.alloc(T, self.size());
|
||||
std.mem.set(T, res, @as(T, 0));
|
||||
@memset(res, @as(T, 0));
|
||||
var i: usize = 0;
|
||||
var j: usize = self.front;
|
||||
while (i < self.size()) : ({ i += 1; j += 1; }) {
|
||||
|
||||
@@ -34,7 +34,7 @@ pub fn LinkedListDeque(comptime T: type) type {
|
||||
mem_arena: ?std.heap.ArenaAllocator = null,
|
||||
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
|
||||
|
||||
// 构造方法(分配内存+初始化队列)
|
||||
// 构造函数(分配内存+初始化队列)
|
||||
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
||||
if (self.mem_arena == null) {
|
||||
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
|
||||
@@ -45,7 +45,7 @@ pub fn LinkedListDeque(comptime T: type) type {
|
||||
self.que_size = 0;
|
||||
}
|
||||
|
||||
// 析构方法(释放内存)
|
||||
// 析构函数(释放内存)
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.mem_arena == null) return;
|
||||
self.mem_arena.?.deinit();
|
||||
@@ -150,7 +150,7 @@ pub fn LinkedListDeque(comptime T: type) type {
|
||||
pub fn toArray(self: *Self) ![]T {
|
||||
var node = self.front;
|
||||
var res = try self.mem_allocator.alloc(T, self.size());
|
||||
std.mem.set(T, res, @as(T, 0));
|
||||
@memset(res, @as(T, 0));
|
||||
var i: usize = 0;
|
||||
while (i < res.len) : (i += 1) {
|
||||
res[i] = node.?.val;
|
||||
|
||||
@@ -12,11 +12,11 @@ pub fn LinkedListQueue(comptime T: type) type {
|
||||
|
||||
front: ?*inc.ListNode(T) = null, // 头节点 front
|
||||
rear: ?*inc.ListNode(T) = null, // 尾节点 rear
|
||||
que_size: usize = 0, // 队列的长度
|
||||
que_size: usize = 0, // 队列的长度
|
||||
mem_arena: ?std.heap.ArenaAllocator = null,
|
||||
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
|
||||
|
||||
// 构造方法(分配内存+初始化队列)
|
||||
// 构造函数(分配内存+初始化队列)
|
||||
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
||||
if (self.mem_arena == null) {
|
||||
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
|
||||
@@ -27,7 +27,7 @@ pub fn LinkedListQueue(comptime T: type) type {
|
||||
self.que_size = 0;
|
||||
}
|
||||
|
||||
// 析构方法(释放内存)
|
||||
// 析构函数(释放内存)
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.mem_arena == null) return;
|
||||
self.mem_arena.?.deinit();
|
||||
@@ -79,7 +79,7 @@ pub fn LinkedListQueue(comptime T: type) type {
|
||||
pub fn toArray(self: *Self) ![]T {
|
||||
var node = self.front;
|
||||
var res = try self.mem_allocator.alloc(T, self.size());
|
||||
std.mem.set(T, res, @as(T, 0));
|
||||
@memset(res, @as(T, 0));
|
||||
var i: usize = 0;
|
||||
while (i < res.len) : (i += 1) {
|
||||
res[i] = node.?.val;
|
||||
|
||||
@@ -13,9 +13,9 @@ pub fn LinkedListStack(comptime T: type) type {
|
||||
stack_top: ?*inc.ListNode(T) = null, // 将头节点作为栈顶
|
||||
stk_size: usize = 0, // 栈的长度
|
||||
mem_arena: ?std.heap.ArenaAllocator = null,
|
||||
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
|
||||
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
|
||||
|
||||
// 构造方法(分配内存+初始化栈)
|
||||
// 构造函数(分配内存+初始化栈)
|
||||
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
||||
if (self.mem_arena == null) {
|
||||
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
|
||||
@@ -25,7 +25,7 @@ pub fn LinkedListStack(comptime T: type) type {
|
||||
self.stk_size = 0;
|
||||
}
|
||||
|
||||
// 析构方法(释放内存)
|
||||
// 析构函数(释放内存)
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.mem_arena == null) return;
|
||||
self.mem_arena.?.deinit();
|
||||
@@ -68,7 +68,7 @@ pub fn LinkedListStack(comptime T: type) type {
|
||||
pub fn toArray(self: *Self) ![]T {
|
||||
var node = self.stack_top;
|
||||
var res = try self.mem_allocator.alloc(T, self.size());
|
||||
std.mem.set(T, res, @as(T, 0));
|
||||
@memset(res, @as(T, 0));
|
||||
var i: usize = 0;
|
||||
while (i < res.len) : (i += 1) {
|
||||
res[res.len - i - 1] = node.?.val;
|
||||
|
||||
@@ -108,8 +108,8 @@ pub fn AVLTree(comptime T: type) type {
|
||||
}
|
||||
|
||||
// 插入节点
|
||||
fn insert(self: *Self, val: T) void {
|
||||
self.root = try self.insertHelper(self.root, val);
|
||||
fn insert(self: *Self, val: T) !void {
|
||||
self.root = (try self.insertHelper(self.root, val)).?;
|
||||
}
|
||||
|
||||
// 递归插入节点(辅助方法)
|
||||
@@ -137,7 +137,7 @@ pub fn AVLTree(comptime T: type) type {
|
||||
|
||||
// 删除节点
|
||||
fn remove(self: *Self, val: T) void {
|
||||
self.root = self.removeHelper(self.root, val);
|
||||
self.root = self.removeHelper(self.root, val).?;
|
||||
}
|
||||
|
||||
// 递归删除节点(辅助方法)
|
||||
@@ -200,14 +200,14 @@ pub fn AVLTree(comptime T: type) type {
|
||||
|
||||
pub fn testInsert(comptime T: type, tree_: *AVLTree(T), val: T) !void {
|
||||
var tree = tree_;
|
||||
_ = try tree.insert(val);
|
||||
try tree.insert(val);
|
||||
std.debug.print("\n插入节点 {} 后,AVL 树为\n", .{val});
|
||||
try inc.PrintUtil.printTree(tree.root, null, false);
|
||||
}
|
||||
|
||||
pub fn testRemove(comptime T: type, tree_: *AVLTree(T), val: T) void {
|
||||
var tree = tree_;
|
||||
_ = tree.remove(val);
|
||||
tree.remove(val);
|
||||
std.debug.print("\n删除节点 {} 后,AVL 树为\n", .{val});
|
||||
try inc.PrintUtil.printTree(tree.root, null, false);
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ pub fn BinarySearchTree(comptime T: type) type {
|
||||
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
|
||||
self.mem_allocator = self.mem_arena.?.allocator();
|
||||
}
|
||||
std.sort.sort(T, nums, {}, comptime std.sort.asc(T)); // 排序数组
|
||||
std.mem.sort(T, nums, {}, comptime std.sort.asc(T)); // 排序数组
|
||||
self.root = try self.buildTree(nums, 0, nums.len - 1); // 构建二叉搜索树
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ pub fn BinarySearchTree(comptime T: type) type {
|
||||
}
|
||||
|
||||
// 删除节点
|
||||
fn remove(self: *Self, num: T) !void {
|
||||
fn remove(self: *Self, num: T) void {
|
||||
// 若树为空,直接提前返回
|
||||
if (self.root == null) return;
|
||||
var cur = self.root;
|
||||
@@ -135,11 +135,11 @@ pub fn BinarySearchTree(comptime T: type) type {
|
||||
while (tmp.?.left != null) {
|
||||
tmp = tmp.?.left;
|
||||
}
|
||||
var tmpVal = tmp.?.val;
|
||||
var tmp_val = tmp.?.val;
|
||||
// 递归删除节点 tmp
|
||||
_ = self.remove(tmp.?.val);
|
||||
self.remove(tmp.?.val);
|
||||
// 用 tmp 覆盖 cur
|
||||
cur.?.val = tmpVal;
|
||||
cur.?.val = tmp_val;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -160,18 +160,18 @@ pub fn main() !void {
|
||||
std.debug.print("\n查找到的节点对象为 {any},节点值 = {}\n", .{node, node.?.val});
|
||||
|
||||
// 插入节点
|
||||
node = try bst.insert(16);
|
||||
try bst.insert(16);
|
||||
std.debug.print("\n插入节点 16 后,二叉树为\n", .{});
|
||||
try inc.PrintUtil.printTree(bst.getRoot(), null, false);
|
||||
|
||||
// 删除节点
|
||||
_ = bst.remove(1);
|
||||
bst.remove(1);
|
||||
std.debug.print("\n删除节点 1 后,二叉树为\n", .{});
|
||||
try inc.PrintUtil.printTree(bst.getRoot(), null, false);
|
||||
_ = bst.remove(2);
|
||||
bst.remove(2);
|
||||
std.debug.print("\n删除节点 2 后,二叉树为\n", .{});
|
||||
try inc.PrintUtil.printTree(bst.getRoot(), null, false);
|
||||
_ = bst.remove(4);
|
||||
bst.remove(4);
|
||||
std.debug.print("\n删除节点 4 后,二叉树为\n", .{});
|
||||
try inc.PrintUtil.printTree(bst.getRoot(), null, false);
|
||||
|
||||
|
||||
@@ -67,4 +67,4 @@ pub fn main() !void {
|
||||
inc.PrintUtil.printList(i32, list);
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ pub const TreeNode = TreeUtil.TreeNode;
|
||||
pub fn printArray(comptime T: type, nums: []T) void {
|
||||
std.debug.print("[", .{});
|
||||
if (nums.len > 0) {
|
||||
for (nums) |num, j| {
|
||||
for (nums, 0..) |num, j| {
|
||||
std.debug.print("{}{s}", .{num, if (j == nums.len-1) "]" else ", " });
|
||||
}
|
||||
} else {
|
||||
@@ -24,26 +24,25 @@ pub fn printArray(comptime T: type, nums: []T) void {
|
||||
pub fn printList(comptime T: type, list: std.ArrayList(T)) void {
|
||||
std.debug.print("[", .{});
|
||||
if (list.items.len > 0) {
|
||||
for (list.items) |value, i| {
|
||||
for (list.items, 0..) |value, i| {
|
||||
std.debug.print("{}{s}", .{value, if (i == list.items.len-1) "]" else ", " });
|
||||
}
|
||||
} else {
|
||||
std.debug.print("]", .{});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Print a linked list
|
||||
pub fn printLinkedList(comptime T: type, node: ?*ListNode(T)) !void {
|
||||
if (node == null) return;
|
||||
var list = std.ArrayList(i32).init(std.heap.page_allocator);
|
||||
var list = std.ArrayList(T).init(std.heap.page_allocator);
|
||||
defer list.deinit();
|
||||
var head = node;
|
||||
while (head != null) {
|
||||
try list.append(head.?.val);
|
||||
head = head.?.next;
|
||||
}
|
||||
for (list.items) |value, i| {
|
||||
for (list.items, 0..) |value, i| {
|
||||
std.debug.print("{}{s}", .{value, if (i == list.items.len-1) "\n" else "->" });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user