Browse Source

* move simdb_bitmap_diffmap() from library to tool

master
Alex 'AdUser' Z 8 years ago
parent
commit
69aaa729b9
  1. 18
      src/bitmap.c
  2. 20
      src/bitmap.h
  3. 70
      src/simdb-query.c

18
src/bitmap.c

@ -45,9 +45,7 @@ static unsigned char dict[256] = {
}; };
int int
simdb_bitmap_compare(const unsigned char *a, simdb_bitmap_compare(const unsigned char *a, const unsigned char *b) {
const unsigned char *b)
{
unsigned char diff = 0; unsigned char diff = 0;
size_t i = 0; size_t i = 0;
size_t cnt = 0; size_t cnt = 0;
@ -60,20 +58,6 @@ simdb_bitmap_compare(const unsigned char *a,
return cnt; return cnt;
} }
size_t
simdb_bitmap_diffmap(const unsigned char *a,
const unsigned char *b,
unsigned char *diff)
{
size_t i = 0;
for (i = 0; i < SIMDB_BITMAP_SIZE; i++, a++, b++, diff++) {
*diff = *a ^ *b;
}
return i;
}
size_t size_t
simdb_bitmap_unpack(const unsigned char *map, char **buf) { simdb_bitmap_unpack(const unsigned char *map, char **buf) {
size_t buf_size = SIMDB_BITMAP_BITS; size_t buf_size = SIMDB_BITMAP_BITS;

20
src/bitmap.h

@ -19,22 +19,7 @@
* @param b Second bitmap to compare * @param b Second bitmap to compare
* @returns Integer showing difference between bitmaps in bits (0-256) * @returns Integer showing difference between bitmaps in bits (0-256)
*/ */
int int simdb_bitmap_compare(const unsigned char *a, const unsigned char *b);
simdb_bitmap_compare(const unsigned char *a,
const unsigned char *b);
/**
* @brief Make difference map of to bitmaps
* @param a First bitmap to compare
* @param b Second bitmap to compare
* @param diff Storage for difference map
* @returns Size of generated map (now is @a BITMAP_SIZE)
*/
size_t
simdb_bitmap_diffmap(const unsigned char *a,
const unsigned char *b,
unsigned char *diff);
/** /**
* @brief Unpack BITmap to BYTEmap * @brief Unpack BITmap to BYTEmap
@ -42,6 +27,5 @@ simdb_bitmap_diffmap(const unsigned char *a,
* @param buf Pointer to store generated bytemap * @param buf Pointer to store generated bytemap
* @returns Size of generated bytemap (now is @a BITMAP_BITS) * @returns Size of generated bytemap (now is @a BITMAP_BITS)
*/ */
size_t size_t simdb_bitmap_unpack(const unsigned char *map, char **buf);
simdb_bitmap_unpack(const unsigned char *map, char **buf);
#endif #endif

70
src/simdb-query.c

@ -171,43 +171,55 @@ int rec_bitmap(simdb_t *db, int num) {
} }
int rec_diff(simdb_t *db, int a, int b, unsigned short int showmap) { int rec_diff(simdb_t *db, int a, int b, unsigned short int showmap) {
unsigned char map[SIMDB_BITMAP_SIZE]; char *map1 = NULL, *map2 = NULL, *dmap = NULL;
simdb_urec_t *one, *two; size_t size = 0, diff = 0;
float diff = 0.0;
int ret; int ret;
assert(db != NULL); assert(db != NULL);
if ((ret = simdb_read(db, a, 1, &one)) < 0) { do {
fprintf(stderr, "read error: %s\n", simdb_error(ret)); if ((ret = simdb_record_bitmap(db, a, &map1, &size)) < 0) {
return 1; if (ret < 0) {
} else if (ret == 0 || (ret > 0 && !one->used)) { fprintf(stderr, "can't get bitmap for record #%d: %s\n", a, simdb_error(ret));
fprintf(stderr, "record diff: first sample not exists\n"); } else {
if (ret > 0) free(one); fprintf(stderr, "record diff: record #%d not exists\n", a);
return 1; }
} ret = 1;
break;
}
if ((ret = simdb_read(db, b, 1, &two)) < 0) { if ((ret = simdb_record_bitmap(db, b, &map2, &size)) <= 0) {
fprintf(stderr, "read error: %s\n", simdb_error(ret)); if (ret < 0) {
return 1; fprintf(stderr, "can't get bitmap for record #%d: %s\n", b, simdb_error(ret));
} else if (ret == 0 || (ret > 0 && !two->used)) { } else {
fprintf(stderr, "record diff: first sample not exists\n"); fprintf(stderr, "record diff: record #%d not exists\n", b);
if (ret > 0) free(two); }
return 1; ret = 1;
} break;
}
if (showmap) { if ((dmap = calloc(size, sizeof(char))) == NULL) {
simdb_bitmap_diffmap(one->bitmap, two->bitmap, map); ret = 1;
bitmap_print(map, SIMDB_BITMAP_SIDE); break;
return 0; }
} char *m1 = map1, *m2 = map2, *dm = dmap;
for (size_t i = 0; i < size; i++, m1++, m2++, dm++) {
*dm = *m1 ^ *m2;
diff += *dm ? 0 : 1;
}
if (showmap) {
bitmap_print(dmap, size);
} else {
printf("%.2f%%\n", ((float) diff / size) * 100);
}
ret = 0;
} while (0);
diff = (float) simdb_bitmap_compare(one->bitmap, two->bitmap); free(map1);
printf("%.2f%%\n", (diff / SIMDB_BITMAP_BITS) * 100); free(map2);
free(one); free(dmap);
free(two);
return 0; return ret;
} }
int main(int argc, char **argv) int main(int argc, char **argv)

Loading…
Cancel
Save