Skip to content

Commit

Permalink
code for access and comparison of bits
Browse files Browse the repository at this point in the history
  • Loading branch information
rieck committed Feb 12, 2015
1 parent 16678f2 commit 776a9ec
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/hconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ int config_check(config_t * cfg)
/* Sanity checks for tokens */
config_lookup_string(cfg, "measures.granularity", &str1);
config_lookup_string(cfg, "measures.token_delim", &str2);

if (!strcasecmp(str1, "tokens") && strlen(str2) == 0) {
error("Delimiters are required if the granularity is tokens.");
return 0;
Expand Down
52 changes: 42 additions & 10 deletions src/hstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,18 @@ static stoptoken_t *stoptokens = NULL;
*/
void hstring_destroy(hstring_t *x)
{
if (x->type == TYPE_BYTE && x->str.c)
free(x->str.c);
if (x->type == TYPE_TOKEN && x->str.s)
free(x->str.s);
switch (x->type) {
case TYPE_BYTE:
case TYPE_BIT:
if (x->str.c)
free(x->str.c);
break;
case TYPE_TOKEN:
if (x->str.s)
free(x->str.s);
break;
}

if (x->src)
free(x->src);

Expand Down Expand Up @@ -77,14 +85,20 @@ int hstring_has_delim()
sym_t hstring_get(hstring_t x, int i)
{
assert(i < x.len);
int b;

if (x.type == TYPE_TOKEN)
switch (x.type) {
case TYPE_TOKEN:
return x.str.s[i];
else if (x.type == TYPE_BYTE)
case TYPE_BYTE:
return x.str.c[i];
else
case TYPE_BIT:
b = x.str.c[i / 8];
return b >> (7 - i % 8) & 1;
default:
error("Unknown string type");
return 0;
return 0;
}
}


Expand All @@ -96,19 +110,25 @@ void hstring_print(hstring_t x)
{
int i;

if (x.type == TYPE_BIT && x.str.c) {
for (i = 0; i < x.len; i++)
printf("%d", (int) hstring_get(x, i));
printf(" (bits)\n");
}

if (x.type == TYPE_BYTE && x.str.c) {
for (i = 0; i < x.len; i++)
if (isprint(x.str.c[i]))
printf("%c", x.str.c[i]);
else
printf("%%%.2x", (char) x.str.c[i]);
printf(" (char)\n");
printf(" (bytes)\n");
}

if (x.type == TYPE_TOKEN && x.str.s) {
for (i = 0; i < x.len; i++)
printf("%" PRIu64 " ", (uint64_t) x.str.s[i]);
printf(" (sym)\n");
printf(" (tokens)\n");
}

printf(" [type: %d, len: %d; src: %s, label: %f]\n",
Expand Down Expand Up @@ -268,6 +288,8 @@ hstring_t hstring_empty(hstring_t x, int t)
*/
uint64_t hstring_hash1(hstring_t x)
{
if (x.type == TYPE_BIT && x.str.c)
return MurmurHash64B(x.str.c, sizeof(char) * x.len / 8, 0xc0ffee);
if (x.type == TYPE_BYTE && x.str.c)
return MurmurHash64B(x.str.c, sizeof(char) * x.len, 0xc0ffee);
if (x.type == TYPE_TOKEN && x.str.s)
Expand All @@ -293,6 +315,11 @@ uint64_t hstring_hash_sub(hstring_t x, int i, int l)
return 0;
}

if (x.type == TYPE_BIT && x.str.c) {
error("Substrings are currently not supported for bits");
return 0;
}

if (x.type == TYPE_BYTE && x.str.c)
return MurmurHash64B(x.str.c + i, sizeof(char) * l, 0xc0ffee);
if (x.type == TYPE_TOKEN && x.str.s)
Expand Down Expand Up @@ -329,6 +356,11 @@ uint64_t hstring_hash2(hstring_t x, hstring_t y)
{
uint64_t a, b;

if (x.type == TYPE_BIT && y.type == TYPE_BIT && x.str.c && y.str.c) {
a = MurmurHash64B(x.str.c, sizeof(char) * x.len / 8, 0xc0ffee);
b = MurmurHash64B(y.str.c, sizeof(char) * y.len / 8, 0xc0ffee);
return swap(a) ^ b;
}
if (x.type == TYPE_BYTE && y.type == TYPE_BYTE && x.str.c && y.str.c) {
a = MurmurHash64B(x.str.c, sizeof(char) * x.len, 0xc0ffee);
b = MurmurHash64B(y.str.c, sizeof(char) * y.len, 0xc0ffee);
Expand Down
15 changes: 11 additions & 4 deletions src/hstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,20 @@ static inline int hstring_compare(hstring_t x, int i, hstring_t y, int j)
{
assert(x.type == y.type);
assert(i < x.len && j < y.len);

if (x.type == TYPE_TOKEN)
int a, b;

switch (x.type) {
case TYPE_BIT:
a = x.str.c[i / 8] >> (7 - i % 8) & 1;
b = y.str.c[j / 8] >> (7 - j % 8) & 1;
return (a - b);
case TYPE_TOKEN:
return (x.str.s[i] - y.str.s[j]);
else if (x.type == TYPE_BYTE)
case TYPE_BYTE:
return (x.str.c[i] - y.str.c[j]);
else
default:
error("Unknown string type");
}
return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ void log_print(long a, long b, long c)
float hround(float f, int p)
{
if (p == 0)
return f;
return round(f * pow(10, p)) / pow(10,p);
return f;
return round(f * pow(10, p)) / pow(10, p);
}

/** @} */

0 comments on commit 776a9ec

Please sign in to comment.