expose len to the delta-compressed iterator

This commit is contained in:
Motiejus Jakštys 2022-03-06 09:35:18 +02:00 committed by Motiejus Jakštys
parent 039aeb80dd
commit efd3cfc367

View File

@ -102,14 +102,20 @@ const varintSliceIterator = struct {
arr: []const u8, arr: []const u8,
idx: usize, idx: usize,
pub fn next(it: *varintSliceIterator) error{Overflow}!?u64 { pub fn next(self: *varintSliceIterator) error{Overflow}!?u64 {
if (it.remaining == 0) if (self.remaining == 0)
return null; return null;
const value = try uvarint(it.arr[it.idx..]); const value = try uvarint(self.arr[self.idx..]);
it.idx += value.bytes_read; self.idx += value.bytes_read;
it.remaining -= 1; self.remaining -= 1;
return value.value; return value.value;
} }
// returns the number of remaining items. If called before the first
// next(), returns the length of the slice.
pub fn remaining(self: *const varintSliceIterator) usize {
return self.remaining;
}
}; };
pub fn VarintSliceIterator(arr: []const u8) error{Overflow}!varintSliceIterator { pub fn VarintSliceIterator(arr: []const u8) error{Overflow}!varintSliceIterator {
@ -126,16 +132,22 @@ const deltaDecompressionIterator = struct {
prev: u64, prev: u64,
add_to_prev: u1, add_to_prev: u1,
pub fn next(it: *deltaDecompressionIterator) error{Overflow}!?u64 { pub fn next(self: *deltaDecompressionIterator) error{Overflow}!?u64 {
const current = try it.vit.next(); const current = try self.vit.next();
if (current == null) return null; if (current == null) return null;
const prevExtra = try math.add(u64, it.prev, it.add_to_prev); const prevExtra = try math.add(u64, self.prev, self.add_to_prev);
const result = try math.add(u64, current.?, prevExtra); const result = try math.add(u64, current.?, prevExtra);
it.prev = result; self.prev = result;
it.add_to_prev = 1; self.add_to_prev = 1;
return result; return result;
} }
// returns the number of remaining items. If called before the first
// next(), returns the length of the slice.
pub fn remaining(self: *const deltaDecompressionIterator) usize {
return self.vit.remaining;
}
}; };
pub fn DeltaDecompressionIterator(vit: *varintSliceIterator) deltaDecompressionIterator { pub fn DeltaDecompressionIterator(vit: *varintSliceIterator) deltaDecompressionIterator {
@ -257,10 +269,9 @@ test "delta decompress overflow" {
} }
test "delta decompression with an iterator" { test "delta decompression with an iterator" {
const allocator = testing.allocator; var compressed: [uvarint_tests.len]u64 = undefined;
var compressed = try testing.allocator.dupe(u64, uvarint_tests[0..]); std.mem.copy(u64, compressed[0..], uvarint_tests[0..]);
defer allocator.free(compressed); try deltaCompress(u64, compressed[0..]);
try deltaCompress(u64, compressed);
var buf = ArrayList(u8).init(testing.allocator); var buf = ArrayList(u8).init(testing.allocator);
defer buf.deinit(); defer buf.deinit();
@ -270,6 +281,7 @@ test "delta decompression with an iterator" {
var it = DeltaDecompressionIterator(&try VarintSliceIterator(buf.items)); var it = DeltaDecompressionIterator(&try VarintSliceIterator(buf.items));
var i: usize = 0; var i: usize = 0;
try testing.expectEqual(it.remaining(), uvarint_tests.len);
while (try it.next()) |got| : (i += 1) { while (try it.next()) |got| : (i += 1) {
try testing.expectEqual(uvarint_tests[i], got); try testing.expectEqual(uvarint_tests[i], got);
} }