From 117cd4284f541c261d4215df4c0b01cae1ecca17 Mon Sep 17 00:00:00 2001 From: Lukas Lalinsky Date: Sun, 1 Dec 2024 08:04:16 +0100 Subject: [PATCH] Code cleanup --- src/FileSegment.zig | 3 +-- src/MemorySegment.zig | 2 +- src/common.zig | 48 ------------------------------------------ src/filefmt.zig | 3 +-- src/segment.zig | 48 ++++++++++++++++++++++++++++++++++++++++++ src/segment_merger.zig | 6 ++---- 6 files changed, 53 insertions(+), 57 deletions(-) diff --git a/src/FileSegment.zig b/src/FileSegment.zig index d8a82a4..fd1a357 100644 --- a/src/FileSegment.zig +++ b/src/FileSegment.zig @@ -3,11 +3,10 @@ const log = std.log.scoped(.segment); const assert = std.debug.assert; const common = @import("common.zig"); -const Item = common.Item; const SearchResults = common.SearchResults; -const SegmentId = common.SegmentId; const KeepOrDelete = common.KeepOrDelete; +const Item = @import("segment.zig").Item; const SegmentInfo = @import("segment.zig").SegmentInfo; const filefmt = @import("filefmt.zig"); diff --git a/src/MemorySegment.zig b/src/MemorySegment.zig index 971811d..41f1d26 100644 --- a/src/MemorySegment.zig +++ b/src/MemorySegment.zig @@ -2,10 +2,10 @@ const std = @import("std"); const log = std.log; const common = @import("common.zig"); -const Item = common.Item; const SearchResults = common.SearchResults; const KeepOrDelete = common.KeepOrDelete; const SegmentInfo = @import("segment.zig").SegmentInfo; +const Item = @import("segment.zig").Item; const Change = @import("change.zig").Change; diff --git a/src/common.zig b/src/common.zig index f8a7916..b702aed 100644 --- a/src/common.zig +++ b/src/common.zig @@ -9,54 +9,6 @@ pub const KeepOrDelete = enum { delete, }; -pub const Item = packed struct(u64) { - id: u32, - hash: u32, - - pub fn cmp(_: void, a: Item, b: Item) bool { - const xa: u64 = @bitCast(a); - const xb: u64 = @bitCast(b); - return xa < xb; - } - - pub fn cmpByHash(_: void, a: Item, b: Item) bool { - return a.hash < b.hash; - } -}; - -test "Item binary" { - try testing.expectEqual(8, @sizeOf(Item)); - try testing.expectEqual(64, @bitSizeOf(Item)); - try testing.expectEqual(0, @bitOffsetOf(Item, "id")); - try testing.expectEqual(32, @bitOffsetOf(Item, "hash")); - - const item1 = Item{ .hash = 1, .id = 2 }; - const item2 = Item{ .hash = 2, .id = 1 }; - - const x1: u64 = @bitCast(item1); - const x2: u64 = @bitCast(item2); - - try testing.expectEqual(0x0000000100000002, x1); - try testing.expectEqual(0x0000000200000001, x2); -} - -test "Item array sort" { - var items = try testing.allocator.alloc(Item, 3); - defer testing.allocator.free(items); - - items[0] = Item{ .hash = 2, .id = 200 }; - items[1] = Item{ .hash = 2, .id = 100 }; - items[2] = Item{ .hash = 1, .id = 300 }; - - std.sort.insertion(Item, items, {}, Item.cmp); - - try testing.expectEqualSlices(Item, &[_]Item{ - Item{ .hash = 1, .id = 300 }, - Item{ .hash = 2, .id = 100 }, - Item{ .hash = 2, .id = 200 }, - }, items); -} - pub const SearchResult = struct { id: u32, score: u32, diff --git a/src/filefmt.zig b/src/filefmt.zig index 756384f..7eda8cf 100644 --- a/src/filefmt.zig +++ b/src/filefmt.zig @@ -8,8 +8,7 @@ const log = std.log.scoped(.filefmt); const msgpack = @import("msgpack"); -const common = @import("common.zig"); -const Item = common.Item; +const Item = @import("segment.zig").Item; const SegmentInfo = @import("segment.zig").SegmentInfo; const MemorySegment = @import("MemorySegment.zig"); const FileSegment = @import("FileSegment.zig"); diff --git a/src/segment.zig b/src/segment.zig index 73693a3..2181d55 100644 --- a/src/segment.zig +++ b/src/segment.zig @@ -49,3 +49,51 @@ test "SegmentInfo.contains" { try std.testing.expect(c.contains(b)); try std.testing.expect(c.contains(c)); } + +pub const Item = packed struct(u64) { + id: u32, + hash: u32, + + pub fn cmp(_: void, a: Item, b: Item) bool { + const xa: u64 = @bitCast(a); + const xb: u64 = @bitCast(b); + return xa < xb; + } + + pub fn cmpByHash(_: void, a: Item, b: Item) bool { + return a.hash < b.hash; + } +}; + +test "Item binary" { + try std.testing.expectEqual(8, @sizeOf(Item)); + try std.testing.expectEqual(64, @bitSizeOf(Item)); + try std.testing.expectEqual(0, @bitOffsetOf(Item, "id")); + try std.testing.expectEqual(32, @bitOffsetOf(Item, "hash")); + + const item1 = Item{ .hash = 1, .id = 2 }; + const item2 = Item{ .hash = 2, .id = 1 }; + + const x1: u64 = @bitCast(item1); + const x2: u64 = @bitCast(item2); + + try std.testing.expectEqual(0x0000000100000002, x1); + try std.testing.expectEqual(0x0000000200000001, x2); +} + +test "Item array sort" { + var items = try std.testing.allocator.alloc(Item, 3); + defer std.testing.allocator.free(items); + + items[0] = Item{ .hash = 2, .id = 200 }; + items[1] = Item{ .hash = 2, .id = 100 }; + items[2] = Item{ .hash = 1, .id = 300 }; + + std.sort.insertion(Item, items, {}, Item.cmp); + + try std.testing.expectEqualSlices(Item, &[_]Item{ + Item{ .hash = 1, .id = 300 }, + Item{ .hash = 2, .id = 100 }, + Item{ .hash = 2, .id = 200 }, + }, items); +} diff --git a/src/segment_merger.zig b/src/segment_merger.zig index f079430..c9e4c54 100644 --- a/src/segment_merger.zig +++ b/src/segment_merger.zig @@ -1,11 +1,9 @@ const std = @import("std"); -const common = @import("common.zig"); -const Item = common.Item; +const Item = @import("segment.zig").Item; const SegmentInfo = @import("segment.zig").SegmentInfo; - -const SharedPtr = @import("utils/shared_ptr.zig").SharedPtr; const SegmentList = @import("segment_list.zig").SegmentList; +const SharedPtr = @import("utils/shared_ptr.zig").SharedPtr; pub const MergedSegmentInfo = struct { info: SegmentInfo = .{},