Skip to content

Commit 1d6a1e1

Browse files
committed
upgrade zig codes to 0.11.0-dev.3379+629f0d23b
1 parent 3f42172 commit 1d6a1e1

File tree

5 files changed

+25
-26
lines changed

5 files changed

+25
-26
lines changed

zig/chapter_hashing/array_hash_map.zig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
const std = @import("std");
66
const inc = @import("include");
77

8-
// 键值对 int->String
9-
const Entry = struct {
8+
// 键值对
9+
const Pair = struct {
1010
key: usize = undefined,
1111
val: []const u8 = undefined,
1212

13-
pub fn init(key: usize, val: []const u8) Entry {
14-
return Entry {
13+
pub fn init(key: usize, val: []const u8) Pair {
14+
return Pair {
1515
.key = key,
1616
.val = val,
1717
};
@@ -57,7 +57,7 @@ pub fn ArrayHashMap(comptime T: type) type {
5757

5858
// 添加操作
5959
pub fn put(self: *Self, key: usize, val: []const u8) !void {
60-
var pair = Entry.init(key, val);
60+
var pair = Pair.init(key, val);
6161
var index = hashFunc(key);
6262
self.bucket.?.items[index] = pair;
6363
}
@@ -70,7 +70,7 @@ pub fn ArrayHashMap(comptime T: type) type {
7070
}
7171

7272
// 获取所有键值对
73-
pub fn entrySet(self: *Self) !std.ArrayList(T) {
73+
pub fn pairSet(self: *Self) !std.ArrayList(T) {
7474
var entry_set = std.ArrayList(T).init(self.mem_allocator);
7575
for (self.bucket.?.items) |item| {
7676
if (item == null) continue;
@@ -101,7 +101,7 @@ pub fn ArrayHashMap(comptime T: type) type {
101101

102102
// 打印哈希表
103103
pub fn print(self: *Self) !void {
104-
var entry_set = try self.entrySet();
104+
var entry_set = try self.pairSet();
105105
defer entry_set.deinit();
106106
for (entry_set.items) |item| {
107107
std.debug.print("{} -> {s}\n", .{item.key, item.val});
@@ -113,7 +113,7 @@ pub fn ArrayHashMap(comptime T: type) type {
113113
// Driver Code
114114
pub fn main() !void {
115115
// 初始化哈希表
116-
var map = ArrayHashMap(Entry){};
116+
var map = ArrayHashMap(Pair){};
117117
try map.init(std.heap.page_allocator);
118118
defer map.deinit();
119119

@@ -140,7 +140,7 @@ pub fn main() !void {
140140

141141
// 遍历哈希表
142142
std.debug.print("\n遍历键值对 Key->Value\n", .{});
143-
var entry_set = try map.entrySet();
143+
var entry_set = try map.pairSet();
144144
for (entry_set.items) |kv| {
145145
std.debug.print("{} -> {s}\n", .{kv.key, kv.val});
146146
}

zig/chapter_searching/linear_search.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const std = @import("std");
66
const inc = @import("include");
77

88
// 线性查找(数组)
9-
fn linearSearchList(comptime T: type, nums: std.ArrayList(T), target: T) T {
9+
fn linearSearchArray(comptime T: type, nums: std.ArrayList(T), target: T) T {
1010
// 遍历数组
1111
for (nums.items, 0..) |num, i| {
1212
// 找到目标元素, 返回其索引
@@ -38,7 +38,7 @@ pub fn main() !void {
3838
var nums = std.ArrayList(i32).init(std.heap.page_allocator);
3939
defer nums.deinit();
4040
try nums.appendSlice(&[_]i32{ 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 });
41-
var index = linearSearchList(i32, nums, target);
41+
var index = linearSearchArray(i32, nums, target);
4242
std.debug.print("目标元素 3 的索引 = {}\n", .{index});
4343

4444
// 在链表中执行线性查找

zig/chapter_sorting/radix_sort.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,27 @@ fn countingSortDigit(nums: []i32, exp: i32) !void {
1717
var mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
1818
// defer mem_arena.deinit();
1919
const mem_allocator = mem_arena.allocator();
20-
var bucket = try mem_allocator.alloc(usize, 10);
21-
@memset(bucket, 0);
20+
var counter = try mem_allocator.alloc(usize, 10);
21+
@memset(counter, 0);
2222
var n = nums.len;
2323
// 借助桶来统计 0~9 各数字的出现次数
2424
for (nums) |num| {
2525
var d = @bitCast(u32, digit(num, exp)); // 获取 nums[i] 第 k 位,记为 d
26-
bucket[d] += 1; // 统计数字 d 的出现次数
26+
counter[d] += 1; // 统计数字 d 的出现次数
2727
}
2828
// 求前缀和,将“出现个数”转换为“数组索引”
2929
var i: usize = 1;
3030
while (i < 10) : (i += 1) {
31-
bucket[i] += bucket[i - 1];
31+
counter[i] += counter[i - 1];
3232
}
3333
// 倒序遍历,根据桶内统计结果,将各元素填入 res
3434
var res = try mem_allocator.alloc(i32, n);
3535
i = n - 1;
3636
while (i >= 0) : (i -= 1) {
3737
var d = @bitCast(u32, digit(nums[i], exp));
38-
var j = bucket[d] - 1; // 获取 d 在数组中的索引 j
38+
var j = counter[d] - 1; // 获取 d 在数组中的索引 j
3939
res[j] = nums[i]; // 将当前元素填入索引 j
40-
bucket[d] -= 1; // 将 d 的数量减 1
40+
counter[d] -= 1; // 将 d 的数量减 1
4141
if (i == 0) break;
4242
}
4343
// 使用结果覆盖原数组 nums

zig/chapter_stack_and_queue/array_queue.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn ArrayQueue(comptime T: type) type {
5050
}
5151

5252
// 入队
53-
pub fn offer(self: *Self, num: T) !void {
53+
pub fn push(self: *Self, num: T) !void {
5454
if (self.size() == self.capacity()) {
5555
std.debug.print("队列已满\n", .{});
5656
return;
@@ -102,11 +102,11 @@ pub fn main() !void {
102102
defer queue.deinit();
103103

104104
// 元素入队
105-
try queue.offer(1);
106-
try queue.offer(3);
107-
try queue.offer(2);
108-
try queue.offer(5);
109-
try queue.offer(4);
105+
try queue.push(1);
106+
try queue.push(3);
107+
try queue.push(2);
108+
try queue.push(5);
109+
try queue.push(4);
110110
std.debug.print("队列 queue = ", .{});
111111
inc.PrintUtil.printArray(i32, try queue.toArray());
112112

@@ -130,7 +130,7 @@ pub fn main() !void {
130130
// 测试环形数组
131131
var i: i32 = 0;
132132
while (i < 10) : (i += 1) {
133-
try queue.offer(i);
133+
try queue.push(i);
134134
_ = queue.pop();
135135
std.debug.print("\n第 {} 轮入队 + 出队后 queue = ", .{i});
136136
inc.PrintUtil.printArray(i32, try queue.toArray());

zig/include/PrintUtil.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,4 @@ pub fn printTree(root: ?*TreeNode(i32), prev: ?*Trunk, isLeft: bool) !void {
130130
trunk.str = " |";
131131

132132
try printTree(root.?.left, &trunk, false);
133-
}
134-
133+
}

0 commit comments

Comments
 (0)