From 92ab8a74c96b9e0f684c45aec9cf7f57405ff8da Mon Sep 17 00:00:00 2001 From: Chris Seymour Date: Wed, 8 May 2019 17:18:38 +0100 Subject: [PATCH] #28 add delta zigzag (#29) --- include/streamvbyte_zigzag.h | 12 ++++++++++++ src/streamvbyte_zigzag.c | 15 +++++++++++++++ tests/unit.c | 12 ++++++++++++ 3 files changed, 39 insertions(+) diff --git a/include/streamvbyte_zigzag.h b/include/streamvbyte_zigzag.h index 53e7f07..4e5b370 100644 --- a/include/streamvbyte_zigzag.h +++ b/include/streamvbyte_zigzag.h @@ -17,12 +17,24 @@ extern "C" { */ void zigzag_encode(const int32_t * in, uint32_t * out, size_t N); +/** + * Convert N signed integers to N unsigned integers, using zigzag + * delta encoding. + */ +void zigzag_delta_encode(const int32_t * in, uint32_t * out, size_t N, int32_t prev); + /** * Convert N unsigned integers to N signed integers, using zigzag * encoding. */ void zigzag_decode(const uint32_t * in, int32_t * out, size_t N); +/** + * Convert N unsigned integers to N signed integers, using zigzag + * delta encoding. + */ +void zigzag_delta_decode(const uint32_t * in, int32_t * out, size_t N, int32_t prev); + #if defined(__cplusplus) }; diff --git a/src/streamvbyte_zigzag.c b/src/streamvbyte_zigzag.c index cf77cca..dffbecb 100644 --- a/src/streamvbyte_zigzag.c +++ b/src/streamvbyte_zigzag.c @@ -10,6 +10,12 @@ void zigzag_encode(const int32_t * in, uint32_t * out, size_t N) { out[i] = _zigzag_encode_32(in[i]); } +void zigzag_delta_encode(const int32_t * in, uint32_t * out, size_t N, int32_t prev) { + for (size_t i = 0; i < N; i++) { + out[i] = _zigzag_encode_32(in[i] - prev); + prev = in[i]; + } +} static inline int32_t _zigzag_decode_32 (uint32_t val) { @@ -21,3 +27,12 @@ void zigzag_decode(const uint32_t * in, int32_t * out, size_t N) { for(size_t i = 0; i < N; i++) out[i] = _zigzag_decode_32(in[i]); } + + +void zigzag_delta_decode(const uint32_t * in, int32_t * out, size_t N, int32_t prev) { + for(size_t i = 0; i < N; i++) { + int32_t val =_zigzag_decode_32(in[i]); + out[i] = val + prev; + prev += val; + } +} diff --git a/tests/unit.c b/tests/unit.c index e8aeccc..e71da8a 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -20,15 +20,27 @@ int zigzagtests() { datain[i] = rand() - rand(); uint32_t *dataout = malloc(N * sizeof(uint32_t)); int32_t *databack = malloc(N * sizeof(int32_t)); + + uint32_t *deltadataout = malloc(N * sizeof(uint32_t)); + int32_t *deltadataback = malloc(N * sizeof(int32_t)); + zigzag_encode(datain, dataout, N); zigzag_decode(dataout, databack, N); + zigzag_delta_encode(datain, deltadataout, N, 0); + zigzag_delta_decode(deltadataout, deltadataback, N, 0); + int isok = 1; for(size_t i = 0; i < N; i++) { if(datain[i] != databack[i]) { printf("bug\n"); isok = -1; } + if(datain[i] != deltadataback[i]) { + printf("bug\n"); + isok = -1; + } } + free(databack); free(dataout); free(datain);