Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lalinsky committed Dec 1, 2024
1 parent fb5a9bc commit 117cd42
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 57 deletions.
3 changes: 1 addition & 2 deletions src/FileSegment.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/MemorySegment.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
48 changes: 0 additions & 48 deletions src/common.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions src/filefmt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
48 changes: 48 additions & 0 deletions src/segment.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
6 changes: 2 additions & 4 deletions src/segment_merger.zig
Original file line number Diff line number Diff line change
@@ -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 = .{},
Expand Down

0 comments on commit 117cd42

Please sign in to comment.