Use ht_* in mixed.c

This commit is contained in:
alvarofe 2017-02-12 17:36:26 +01:00
parent ed8d668247
commit 8695dbf3b5
2 changed files with 63 additions and 46 deletions

View File

@ -1,14 +1,14 @@
#ifndef R_MIXED_H
#define R_MIXED_H
#include <r_list.h>
#include "ht.h"
#include <sdb.h>
#define RMIXED_MAXKEYS 256
typedef struct r_mixed_data_t {
int size;
union {
RHashTable *ht;
RHashTable64 *ht64;
SdbHash *ht;
SdbHash *ht64;
} hash;
} RMixedData;

View File

@ -2,9 +2,14 @@
#include <r_util.h>
#define STR64OFF(x) sdb_fmt (2, "%"PFMT64x, x)
#define STR32OFF(x) sdb_fmt (3, "%"PFMT32x, x)
R_API RMixed *r_mixed_new () {
RMixed *m = R_NEW (RMixed);
if (!m) return NULL;
if (!m) {
return NULL;
}
memset (m->keys, 0, sizeof (m->keys));
m->list = r_list_new ();
return m;
@ -16,9 +21,10 @@ R_API void r_mixed_free (RMixed *m) {
if (m->keys[i]) {
switch (m->keys[i]->size) {
case 1: case 2: case 4:
r_hashtable_free (m->keys[i]->hash.ht);
ht_free (m->keys[i]->hash.ht);
break;
case 8: r_hashtable64_free (m->keys[i]->hash.ht64);
case 8:
ht_free (m->keys[i]->hash.ht64);
break;
}
free (m->keys[i]);
@ -44,14 +50,15 @@ R_API int r_mixed_key(RMixed *m, int key, int size) {
m->keys[key]->size = size;
} else {
m->keys[key] = R_NEW (RMixedData);
if (!m->keys[key])
if (!m->keys[key]) {
return false;
}
m->keys[key]->size = size;
switch (size) {
case 1: case 2: case 4:
m->keys[key]->hash.ht = r_hashtable_new ();
m->keys[key]->hash.ht = ht_new (NULL, NULL, NULL);
return true;
case 8: m->keys[key]->hash.ht64 = r_hashtable64_new ();
case 8: m->keys[key]->hash.ht64 = ht_new (NULL, NULL, NULL);
return true;
}
}
@ -71,12 +78,17 @@ R_API ut64 r_mixed_get_value(int key, int sz, const void *p) {
}
R_API RList *r_mixed_get (RMixed *m, int key, ut64 value) {
if (key>=0 && key<RMIXED_MAXKEYS)
if (m->keys[key])
if (key >= 0 && key < RMIXED_MAXKEYS) {
if (m->keys[key]) {
switch (m->keys[key]->size) {
case 1: case 2: case 4:
return r_hashtable_lookup (m->keys[key]->hash.ht, (ut32)value);
case 8: return r_hashtable64_lookup (m->keys[key]->hash.ht64, value);
case 1:
case 2:
case 4:
return ht_find (m->keys[key]->hash.ht, STR32OFF (value), NULL);
case 8:
return ht_find (m->keys[key]->hash.ht64, STR64OFF (value), NULL);
}
}
}
return NULL;
}
@ -85,41 +97,44 @@ R_API void *r_mixed_get0 (RMixed *m, int key, ut64 value) {
RList *list = r_mixed_get (m, key, value);
if (list && !r_list_empty (list)) {
RListIter *head = r_list_head (list);
if (head) return head->data;
if (head) {
return head->data;
}
}
return NULL;
}
R_API int r_mixed_add(RMixed *m, void *p) {
RHashTable *ht;
RHashTable64 *ht64;
SdbHash *ht;
SdbHash *ht64;
RList *list = NULL;
ut64 value;
int i, size, ret = false;;
r_list_append (m->list, p);
for (i = 0; i < RMIXED_MAXKEYS; i++) {
if (!m->keys[i])
if (!m->keys[i]) {
continue;
}
size = m->keys[i]->size;
value = r_mixed_get_value (i, size, p);
switch (size) {
case 1: case 2: case 4:
ht = m->keys[i]->hash.ht;
list = r_hashtable_lookup (ht, (ut32)value);
list = ht_find (ht, STR32OFF (value), NULL);
if (!list) {
list = r_list_new ();
r_hashtable_insert (ht, (ut32)value, list);
ht_insert (ht, STR32OFF (value), list);
}
r_list_append (list, p);
ret = true;
break;
case 8:
ht64 = m->keys[i]->hash.ht64;
list = r_hashtable64_lookup (ht64, value);
list = ht_find (ht64, STR64OFF (value), NULL);
if (!list) {
list = r_list_new ();
r_hashtable64_insert (ht64, value, list);
ht_insert (ht64, STR64OFF (value), list);
}
r_list_append (list, p);
ret = true;
@ -137,9 +152,10 @@ R_API int r_mixed_del (RMixed *m, void *p) {
if (!m->keys[i]) continue;
switch (m->keys[i]->size) {
case 1: case 2: case 4:
r_hashtable_remove (m->keys[i]->hash.ht, (ut32)value);
ht_delete (m->keys[i]->hash.ht, STR32OFF (value));
break;
case 8: r_hashtable64_remove (m->keys[i]->hash.ht64, value);
case 8:
ht_delete (m->keys[i]->hash.ht64, STR64OFF (value));
break;
}
}
@ -160,8 +176,8 @@ R_API bool r_mixed_change_end(RMixed *m, void *p) {
void *q;
for (i = 0; i < RMIXED_MAXKEYS; i++) {
if (m->keys[i]) {
RHashTable *ht = m->keys[i]->hash.ht;
RHashTable64 *ht64 = m->keys[i]->hash.ht64;
SdbHash *ht = m->keys[i]->hash.ht;
SdbHash *ht64 = m->keys[i]->hash.ht64;
ut64 newstate = r_mixed_get_value (i, m->keys[i]->size, p);
if (newstate != m->state[i]) {
// rehash this pointer
@ -183,26 +199,27 @@ R_API bool r_mixed_change_end(RMixed *m, void *p) {
r_list_free (list);
switch (m->keys[i]->size) {
case 1: case 2: case 4:
r_hashtable_remove (ht, m->state[i]);
ht_delete (ht, STR32OFF (m->state[i]));
break;
case 8: r_hashtable64_remove (ht64, m->state[i]);
case 8:
ht_delete (ht64, STR64OFF (m->state[i]));
break;
}
}
switch (m->keys[i]->size) {
case 1: case 2: case 4:
list = r_hashtable_lookup (ht, (ut32)newstate);
list = ht_find (ht, STR32OFF (newstate), NULL);
if (!list) {
list = r_list_new ();
r_hashtable_insert (ht, (ut32)newstate, list);
ht_insert (ht, STR32OFF (newstate), list);
}
r_list_append (list, p);
break;
case 8:
list = r_hashtable64_lookup (ht64, newstate);
list = ht_find (ht64, STR64OFF (newstate), NULL);
if (!list) {
list = r_list_new ();
r_hashtable64_insert (ht64, newstate, list);
ht_insert (ht64, STR64OFF (newstate), list);
}
r_list_append (list, p);
break;