Use protozero functions for zigzag encoding and decoding

This commit is contained in:
Eric Fischer
2016-04-27 14:22:44 -07:00
parent f3b9e15267
commit a52733eb07
+4 -3
View File
@@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "protozero/varint.hpp"
#include "serial.hpp" #include "serial.hpp"
size_t fwrite_check(const void *ptr, size_t size, size_t nitems, FILE *stream, const char *fname) { size_t fwrite_check(const void *ptr, size_t size, size_t nitems, FILE *stream, const char *fname) {
@@ -18,7 +19,7 @@ void serialize_int(FILE *out, int n, long long *fpos, const char *fname) {
} }
void serialize_long_long(FILE *out, long long n, long long *fpos, const char *fname) { void serialize_long_long(FILE *out, long long n, long long *fpos, const char *fname) {
unsigned long long zigzag = (n << 1) ^ (n >> 63); unsigned long long zigzag = protozero::encode_zigzag32(n);
while (1) { while (1) {
unsigned char b = zigzag & 0x7F; unsigned char b = zigzag & 0x7F;
@@ -74,7 +75,7 @@ void deserialize_long_long(char **f, long long *n) {
} }
} }
*n = (zigzag >> 1) ^ (-(zigzag & 1)); *n = protozero::decode_zigzag32(zigzag);
} }
void deserialize_uint(char **f, unsigned *n) { void deserialize_uint(char **f, unsigned *n) {
@@ -108,7 +109,7 @@ int deserialize_long_long_io(FILE *f, long long *n, long long *geompos) {
} }
} }
*n = (zigzag >> 1) ^ (-(zigzag & 1)); *n = protozero::decode_zigzag32(zigzag);
return 1; return 1;
} }