1
Fork 0
turbonss/src/bdz.zig

29 lines
1.2 KiB
Zig
Raw Normal View History

2022-02-23 15:25:55 +02:00
const std = @import("std");
2022-03-02 11:05:20 +02:00
extern fn bdz_search_packed(packed_mphf: [*]const u8, key: [*]const u8, len: c_uint) u32;
2022-02-23 15:25:55 +02:00
2022-03-02 11:05:20 +02:00
pub fn search(packed_mphf: []const u8, key: []const u8) u32 {
const len = std.math.cast(c_uint, key.len) catch unreachable;
return @as(u32, bdz_search_packed(packed_mphf.ptr, key.ptr, len));
}
pub fn search_u32(packed_mphf: []const u8, key: u32) u32 {
return search(packed_mphf, unzero(key)[0..]);
}
// encode a u32 to 5 bytes so no bytes is a '\0'.
//
// TODO(motiejus) figure out how to use cmph_io_byte_vector_adapter, so cmph
// packing would accept zero bytes. For now we will be doing a dance of not
// passing zero bytes.
pub fn unzero(x: u32) [5]u8 {
const one: u8 = 0b10000000;
var buf: [5]u8 = undefined;
buf[0] = @truncate(u8, (x & 0b11111110_00000000_00000000_00000000) >> 25) | one;
buf[1] = @truncate(u8, (x & 0b00000001_11111100_00000000_00000000) >> 18) | one;
buf[2] = @truncate(u8, (x & 0b00000000_00000011_11110000_00000000) >> 12) | one;
buf[3] = @truncate(u8, (x & 0b00000000_00000000_00001111_11000000) >> 6) | one;
buf[4] = @truncate(u8, (x & 0b00000000_00000000_00000000_00111111) >> 0) | one;
return buf;
2022-02-23 15:25:55 +02:00
}