Skip to content

Commit

Permalink
added int hashmap and replaced arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
qwe321 committed Aug 30, 2011
1 parent 784fc90 commit cae35aa
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 51 deletions.
6 changes: 5 additions & 1 deletion ai.h
@@ -1,6 +1,10 @@
#ifndef __AI_H__
#define __AI_H__
#include "critter.h"

struct Critter;
typedef struct Critter Critter;

typedef void (*AiFunc)(Critter *c);

void ai_noop(Critter* c);
void ai_run_around(Critter* c);
Expand Down
9 changes: 5 additions & 4 deletions critter.h
Expand Up @@ -3,14 +3,15 @@


#include "commands.h"
#include "ai.h"

#define CRITTER_BASE \
CritterVTable* vtable;

struct CritterVTable;
typedef struct CritterVTable CritterVTable;

typedef struct {
typedef struct Critter {
CRITTER_BASE
} Critter ;

Expand All @@ -24,11 +25,11 @@ struct CritterVTable {
void (*damage)(Critter*,float hp);
float (*get_hp)(Critter*);
float (*get_velocity)(Critter*);
void (*set_ai)(Critter*,void (*think)(Critter*));
void (*set_ai)(Critter*, AiFunc ai);
};

//HACK
#include "array.h"
extern Array *monsters;
#include "hashmap.h"
extern IntMap *critters;

#endif
30 changes: 19 additions & 11 deletions critters/human.c
Expand Up @@ -39,7 +39,7 @@ typedef struct {

int anim;

void (*ai)(Critter* c);
AiFunc ai;
} Human;

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -127,14 +127,21 @@ static void serialize(Critter *c, void **buf, uint32_t *size)
*size = sizeof(HumanCore);
}

//-----------------------------------------------------------------------------
// rebuilds whole struct from struct's core
void human_rebuild(Critter *c)
{
Human *cri = (Human *)c;
cri->face_x = cri->c.move_x - cri->c.x;
cri->face_y = cri->c.move_y - cri->c.y;
}

//-----------------------------------------------------------------------------
static void deserialize(Critter *c, void *buf, uint32_t size)
{
Human *cri = (Human*)c;
memcpy(&cri->c, buf, sizeof(HumanCore));

cri->face_x = cri->c.move_x - cri->c.x;
cri->face_y = cri->c.move_y - cri->c.y;
memcpy(&cri->c, buf, sizeof(HumanCore));
human_rebuild(c);
}

//-----------------------------------------------------------------------------
Expand All @@ -160,7 +167,7 @@ static float get_velocity(Critter * c)
}

//-----------------------------------------------------------------------------
static void set_ai(Critter * c,void (*ai)(Critter*))
static void set_ai(Critter * c, AiFunc ai)
{
Human *cri = (Human *) c;
cri->ai = ai;
Expand Down Expand Up @@ -192,16 +199,17 @@ uint32_t human_pack_size(void)
Critter *new_human(float x, float y,int anim)
{
Human *h = (Human *) malloc(sizeof(Human));
h->vtable = &vtable;
h->vtable = &vtable;
h->c.x = x;
h->c.y = y;
h->c.velocity = 0;
h->face_x = 0;
h->face_y = 1;
h->c.state = CRI_IDLE;
h->c.anim_time = 0;
h->c.hp = 100;
h->c.hp = 100;

h->face_x = 0;
h->face_y = 1;
h->anim = anim;
h->ai = ai_noop;
return (Critter *) h;
return (Critter *)h;
}
125 changes: 125 additions & 0 deletions hashmap.c
Expand Up @@ -146,3 +146,128 @@ void hashmap_resize(HashMap *hm, uint32_t new_size)
free(oldData);
free(oldKeys);
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

uint32_t intmap_h1(uint32_t a)
{
a -= (a<<6);
a ^= (a>>17);
a -= (a<<9);
a ^= (a<<4);
a -= (a<<3);
a ^= (a<<10);
a ^= (a>>15);
return a;
}

//-----------------------------------------------------------------------------
IntMap *new_intmap(void)
{
IntMap *im = malloc(sizeof(IntMap));
im->size = HASHMAP_MIN_SIZE;
im->free = im->size;
im->keys = malloc(im->size * sizeof(uint32_t));
im->data = malloc(im->size * sizeof(void*));
memset(im->keys, 0, im->size * sizeof(uint32_t));
return im;
}

//-----------------------------------------------------------------------------
static uint32_t _intmap_reloc(IntMap *im, uint32_t key, void *elem)
{
uint32_t h, k, i = intmap_h1(key) % im->size;
for (k=0; k<im->size; ++k) {
h = (i+k) % im->size;
if (im->keys[h] == 0) {
im->data[h] = elem;
im->keys[h] = key;
return 1;
}
}
return 0;
}

//-----------------------------------------------------------------------------
static void intmap_resize(IntMap *im, uint32_t new_size)
{
uint32_t i, oldSize = im->size;
void **oldData = im->data;
uint32_t *oldKeys = im->keys;

im->size = new_size;
im->data = malloc(im->size * sizeof(void*));
im->keys = malloc(im->size * sizeof(uint32_t));
im->free += (new_size - oldSize);

memset(im->keys, 0, im->size * sizeof(uint32_t));

for (i=0; i<oldSize; ++i) {
if (oldKeys[i] != 0) {
_intmap_reloc(im, oldKeys[i], oldData[i]);
}
}

free(oldData);
free(oldKeys);
}

//-----------------------------------------------------------------------------
static inline void _intmap_write_elem(IntMap *im, uint32_t h, uint32_t key, void *elem)
{
im->data[h] = elem;
im->keys[h] = key;
im->free--;
if (im->free <= im->size>>2)
intmap_resize(im, im->size << 2);
}

//-----------------------------------------------------------------------------
int intmap_ins(IntMap *im, uint32_t key, void *elem)
{
uint32_t h, k, i = intmap_h1(key) % im->size;
for (k=0; k<im->size; ++k) {
h = (i+k) % im->size;
if (im->keys[h] == 0) {
_intmap_write_elem(im, h, key, elem);
return 1;
}
}
return 0;
}

//-----------------------------------------------------------------------------
void *intmap_find(IntMap *im, uint32_t key)
{
uint32_t h, k, i = intmap_h1(key) % im->size;
for (k=0; k<im->size; ++k) {
h = (i+k) % im->size;
if (im->keys[h] == key) {
return im->data[h];
}
}
return NULL;
}

//-----------------------------------------------------------------------------
void intmap_rem(IntMap *im, uint32_t key)
{
uint32_t h, k, i = intmap_h1(key) % im->size;
for (k=0; k<im->size; ++k) {
h = (i+k) % im->size;
if (im->keys[h] == key) {
im->keys[h] = 0;
im->free++;
if (im->free > 3 * (im->size >> 2) && im->size > HASHMAP_MIN_SIZE) {
intmap_resize(im, im->size >> 1);
}
return;
}
}
}

//-----------------------------------------------------------------------------

13 changes: 13 additions & 0 deletions hashmap.h
Expand Up @@ -22,5 +22,18 @@ void hashmap_rem(HashMap *hm, Str *key);
void hashmap_resize(HashMap *hm, uint32_t new_siz);
void hashmap_print(HashMap *hm);

typedef struct
{
uint32_t size;
uint32_t free;
uint32_t *keys;
void **data;
} IntMap;

IntMap *new_intmap(void);
int intmap_ins(IntMap *im, uint32_t key, void *elem);
void intmap_rem(IntMap *im, uint32_t key);
void *intmap_find(IntMap *im, uint32_t key);


#endif // __HASHMAP_H__

0 comments on commit cae35aa

Please sign in to comment.