expose len
to the delta-compressed iterator
This commit is contained in:
parent
039aeb80dd
commit
efd3cfc367
@ -102,14 +102,20 @@ const varintSliceIterator = struct {
|
||||
arr: []const u8,
|
||||
idx: usize,
|
||||
|
||||
pub fn next(it: *varintSliceIterator) error{Overflow}!?u64 {
|
||||
if (it.remaining == 0)
|
||||
pub fn next(self: *varintSliceIterator) error{Overflow}!?u64 {
|
||||
if (self.remaining == 0)
|
||||
return null;
|
||||
const value = try uvarint(it.arr[it.idx..]);
|
||||
it.idx += value.bytes_read;
|
||||
it.remaining -= 1;
|
||||
const value = try uvarint(self.arr[self.idx..]);
|
||||
self.idx += value.bytes_read;
|
||||
self.remaining -= 1;
|
||||
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 {
|
||||
@ -126,16 +132,22 @@ const deltaDecompressionIterator = struct {
|
||||
prev: u64,
|
||||
add_to_prev: u1,
|
||||
|
||||
pub fn next(it: *deltaDecompressionIterator) error{Overflow}!?u64 {
|
||||
const current = try it.vit.next();
|
||||
pub fn next(self: *deltaDecompressionIterator) error{Overflow}!?u64 {
|
||||
const current = try self.vit.next();
|
||||
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);
|
||||
it.prev = result;
|
||||
it.add_to_prev = 1;
|
||||
self.prev = result;
|
||||
self.add_to_prev = 1;
|
||||
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 {
|
||||
@ -257,10 +269,9 @@ test "delta decompress overflow" {
|
||||
}
|
||||
|
||||
test "delta decompression with an iterator" {
|
||||
const allocator = testing.allocator;
|
||||
var compressed = try testing.allocator.dupe(u64, uvarint_tests[0..]);
|
||||
defer allocator.free(compressed);
|
||||
try deltaCompress(u64, compressed);
|
||||
var compressed: [uvarint_tests.len]u64 = undefined;
|
||||
std.mem.copy(u64, compressed[0..], uvarint_tests[0..]);
|
||||
try deltaCompress(u64, compressed[0..]);
|
||||
|
||||
var buf = ArrayList(u8).init(testing.allocator);
|
||||
defer buf.deinit();
|
||||
@ -270,6 +281,7 @@ test "delta decompression with an iterator" {
|
||||
|
||||
var it = DeltaDecompressionIterator(&try VarintSliceIterator(buf.items));
|
||||
var i: usize = 0;
|
||||
try testing.expectEqual(it.remaining(), uvarint_tests.len);
|
||||
while (try it.next()) |got| : (i += 1) {
|
||||
try testing.expectEqual(uvarint_tests[i], got);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user