Skip to content

Commit

Permalink
#28 add delta zigzag (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
iiSeymour authored and lemire committed May 8, 2019
1 parent 7e89ebf commit 92ab8a7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/streamvbyte_zigzag.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
Expand Down
15 changes: 15 additions & 0 deletions src/streamvbyte_zigzag.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
12 changes: 12 additions & 0 deletions tests/unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 92ab8a7

Please sign in to comment.