Skip to content

Commit

Permalink
unsigned -> unsigned int. useless typedef removed.
Browse files Browse the repository at this point in the history
function argument type adjusted.
  • Loading branch information
gcerretani committed Oct 8, 2018
1 parent 7b37760 commit dd4f31c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
20 changes: 10 additions & 10 deletions src/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
#include "map.h"

struct map_node_t {
unsigned hash;
unsigned int hash;
void *value;
map_node_t *next;
/* char key[]; */
/* char value[]; */
};


static unsigned map_hash(const char *str) {
unsigned hash = 5381;
static unsigned int map_hash(const char *str) {
unsigned int hash = 5381;
while (*str) {
hash = ((hash << 5) + hash) ^ *str++;
}
Expand All @@ -42,7 +42,7 @@ static map_node_t *map_newnode(const char *key, void *value, int vsize) {
}


static int map_bucketidx(map_base_t *m, unsigned hash) {
static int map_bucketidx(map_base_t *m, unsigned int hash) {
/* If the implementation is changed to allow a non-power-of-2 bucket count,
* the line below should be changed to use mod instead of AND */
return hash & (m->nbuckets - 1);
Expand All @@ -56,12 +56,12 @@ static void map_addnode(map_base_t *m, map_node_t *node) {
}


static int map_resize(map_base_t *m, int nbuckets) {
static int map_resize(map_base_t *m, unsigned int nbuckets) {
map_node_t *nodes, *node, *next;
map_node_t **buckets;
/* Chain all nodes together */
nodes = NULL;
unsigned i = m->nbuckets;
unsigned int i = m->nbuckets;
while (i--) {
node = (m->buckets)[i];
while (node) {
Expand Down Expand Up @@ -93,7 +93,7 @@ static int map_resize(map_base_t *m, int nbuckets) {


static map_node_t **map_getref(map_base_t *m, const char *key) {
unsigned hash = map_hash(key);
unsigned int hash = map_hash(key);
if (m->nbuckets > 0) {
map_node_t **next = &m->buckets[map_bucketidx(m, hash)];
while (*next) {
Expand All @@ -107,7 +107,7 @@ static map_node_t **map_getref(map_base_t *m, const char *key) {
}


int map_init_(map_base_t *m, unsigned initial_nbuckets) {
int map_init_(map_base_t *m, unsigned int initial_nbuckets) {
// Clear the memory.
memset(m, 0, sizeof(*m));
m->initialized = true;
Expand All @@ -125,7 +125,7 @@ void map_deinit_(map_base_t *m) {
return;
if (!m->initialized)
return;
unsigned i = m->nbuckets;
unsigned int i = m->nbuckets;
while (i--) {
map_node_t *node = m->buckets[i];
while (node) {
Expand Down Expand Up @@ -164,7 +164,7 @@ int map_set_(map_base_t *m, const char *key, void *value, int vsize) {
map_node_t *node = map_newnode(key, value, vsize);
if (node == NULL) goto fail;
if (m->nnodes >= m->nbuckets) {
int n = (m->nbuckets > 0) ? (m->nbuckets << 1) : 1;
unsigned int n = (m->nbuckets > 0) ? (m->nbuckets << 1) : 1;
int err = map_resize(m, n);
if (err) goto fail;
}
Expand Down
13 changes: 3 additions & 10 deletions src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ typedef struct map_node_t map_node_t;

typedef struct {
map_node_t **buckets;
unsigned nbuckets, nnodes;
unsigned int nbuckets, nnodes;
bool initialized;
} map_base_t;

typedef struct {
unsigned bucketidx;
unsigned int bucketidx;
map_node_t *node;
} map_iter_t;

Expand Down Expand Up @@ -59,7 +59,7 @@ typedef struct {
( ((m) != NULL) ? map_next_(&(m)->base, iter) : NULL )


int map_init_(map_base_t *m, unsigned initial_nbuckets);
int map_init_(map_base_t *m, unsigned int initial_nbuckets);
void map_deinit_(map_base_t *m);
void *map_get_(map_base_t *m, const char *key);
int map_set_(map_base_t *m, const char *key, void *value, int vsize);
Expand All @@ -68,11 +68,4 @@ map_iter_t map_iter_(void);
const char *map_next_(map_base_t *m, map_iter_t *iter);


typedef map_t(void*) map_void_t;
typedef map_t(char*) map_str_t;
typedef map_t(int) map_int_t;
typedef map_t(char) map_char_t;
typedef map_t(float) map_float_t;
typedef map_t(double) map_double_t;

#endif

0 comments on commit dd4f31c

Please sign in to comment.