Skip to content

Commit 117cd42

Browse files
committed
Code cleanup
1 parent fb5a9bc commit 117cd42

6 files changed

+53
-57
lines changed

src/FileSegment.zig

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ const log = std.log.scoped(.segment);
33
const assert = std.debug.assert;
44

55
const common = @import("common.zig");
6-
const Item = common.Item;
76
const SearchResults = common.SearchResults;
8-
const SegmentId = common.SegmentId;
97
const KeepOrDelete = common.KeepOrDelete;
108

9+
const Item = @import("segment.zig").Item;
1110
const SegmentInfo = @import("segment.zig").SegmentInfo;
1211

1312
const filefmt = @import("filefmt.zig");

src/MemorySegment.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ const std = @import("std");
22
const log = std.log;
33

44
const common = @import("common.zig");
5-
const Item = common.Item;
65
const SearchResults = common.SearchResults;
76
const KeepOrDelete = common.KeepOrDelete;
87
const SegmentInfo = @import("segment.zig").SegmentInfo;
8+
const Item = @import("segment.zig").Item;
99

1010
const Change = @import("change.zig").Change;
1111

src/common.zig

-48
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,6 @@ pub const KeepOrDelete = enum {
99
delete,
1010
};
1111

12-
pub const Item = packed struct(u64) {
13-
id: u32,
14-
hash: u32,
15-
16-
pub fn cmp(_: void, a: Item, b: Item) bool {
17-
const xa: u64 = @bitCast(a);
18-
const xb: u64 = @bitCast(b);
19-
return xa < xb;
20-
}
21-
22-
pub fn cmpByHash(_: void, a: Item, b: Item) bool {
23-
return a.hash < b.hash;
24-
}
25-
};
26-
27-
test "Item binary" {
28-
try testing.expectEqual(8, @sizeOf(Item));
29-
try testing.expectEqual(64, @bitSizeOf(Item));
30-
try testing.expectEqual(0, @bitOffsetOf(Item, "id"));
31-
try testing.expectEqual(32, @bitOffsetOf(Item, "hash"));
32-
33-
const item1 = Item{ .hash = 1, .id = 2 };
34-
const item2 = Item{ .hash = 2, .id = 1 };
35-
36-
const x1: u64 = @bitCast(item1);
37-
const x2: u64 = @bitCast(item2);
38-
39-
try testing.expectEqual(0x0000000100000002, x1);
40-
try testing.expectEqual(0x0000000200000001, x2);
41-
}
42-
43-
test "Item array sort" {
44-
var items = try testing.allocator.alloc(Item, 3);
45-
defer testing.allocator.free(items);
46-
47-
items[0] = Item{ .hash = 2, .id = 200 };
48-
items[1] = Item{ .hash = 2, .id = 100 };
49-
items[2] = Item{ .hash = 1, .id = 300 };
50-
51-
std.sort.insertion(Item, items, {}, Item.cmp);
52-
53-
try testing.expectEqualSlices(Item, &[_]Item{
54-
Item{ .hash = 1, .id = 300 },
55-
Item{ .hash = 2, .id = 100 },
56-
Item{ .hash = 2, .id = 200 },
57-
}, items);
58-
}
59-
6012
pub const SearchResult = struct {
6113
id: u32,
6214
score: u32,

src/filefmt.zig

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ const log = std.log.scoped(.filefmt);
88

99
const msgpack = @import("msgpack");
1010

11-
const common = @import("common.zig");
12-
const Item = common.Item;
11+
const Item = @import("segment.zig").Item;
1312
const SegmentInfo = @import("segment.zig").SegmentInfo;
1413
const MemorySegment = @import("MemorySegment.zig");
1514
const FileSegment = @import("FileSegment.zig");

src/segment.zig

+48
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,51 @@ test "SegmentInfo.contains" {
4949
try std.testing.expect(c.contains(b));
5050
try std.testing.expect(c.contains(c));
5151
}
52+
53+
pub const Item = packed struct(u64) {
54+
id: u32,
55+
hash: u32,
56+
57+
pub fn cmp(_: void, a: Item, b: Item) bool {
58+
const xa: u64 = @bitCast(a);
59+
const xb: u64 = @bitCast(b);
60+
return xa < xb;
61+
}
62+
63+
pub fn cmpByHash(_: void, a: Item, b: Item) bool {
64+
return a.hash < b.hash;
65+
}
66+
};
67+
68+
test "Item binary" {
69+
try std.testing.expectEqual(8, @sizeOf(Item));
70+
try std.testing.expectEqual(64, @bitSizeOf(Item));
71+
try std.testing.expectEqual(0, @bitOffsetOf(Item, "id"));
72+
try std.testing.expectEqual(32, @bitOffsetOf(Item, "hash"));
73+
74+
const item1 = Item{ .hash = 1, .id = 2 };
75+
const item2 = Item{ .hash = 2, .id = 1 };
76+
77+
const x1: u64 = @bitCast(item1);
78+
const x2: u64 = @bitCast(item2);
79+
80+
try std.testing.expectEqual(0x0000000100000002, x1);
81+
try std.testing.expectEqual(0x0000000200000001, x2);
82+
}
83+
84+
test "Item array sort" {
85+
var items = try std.testing.allocator.alloc(Item, 3);
86+
defer std.testing.allocator.free(items);
87+
88+
items[0] = Item{ .hash = 2, .id = 200 };
89+
items[1] = Item{ .hash = 2, .id = 100 };
90+
items[2] = Item{ .hash = 1, .id = 300 };
91+
92+
std.sort.insertion(Item, items, {}, Item.cmp);
93+
94+
try std.testing.expectEqualSlices(Item, &[_]Item{
95+
Item{ .hash = 1, .id = 300 },
96+
Item{ .hash = 2, .id = 100 },
97+
Item{ .hash = 2, .id = 200 },
98+
}, items);
99+
}

src/segment_merger.zig

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
const std = @import("std");
22

3-
const common = @import("common.zig");
4-
const Item = common.Item;
3+
const Item = @import("segment.zig").Item;
54
const SegmentInfo = @import("segment.zig").SegmentInfo;
6-
7-
const SharedPtr = @import("utils/shared_ptr.zig").SharedPtr;
85
const SegmentList = @import("segment_list.zig").SegmentList;
6+
const SharedPtr = @import("utils/shared_ptr.zig").SharedPtr;
97

108
pub const MergedSegmentInfo = struct {
119
info: SegmentInfo = .{},

0 commit comments

Comments
 (0)