diff options
author | Max Horn | 2009-02-16 09:40:25 +0000 |
---|---|---|
committer | Max Horn | 2009-02-16 09:40:25 +0000 |
commit | ee92dbbe41c2789e7e237f524d184de11de47eef (patch) | |
tree | fa55260629bdee3bf531690c3d3cc0967bc20ef3 /engines/sci/engine | |
parent | 42cb3a55d9e556f63c5bccabeff21b8f5ec18c44 (diff) | |
download | scummvm-rg350-ee92dbbe41c2789e7e237f524d184de11de47eef.tar.gz scummvm-rg350-ee92dbbe41c2789e7e237f524d184de11de47eef.tar.bz2 scummvm-rg350-ee92dbbe41c2789e7e237f524d184de11de47eef.zip |
SCI: Committed stuff I forgot in my last commit; changed int_hash_map_t to a C++ class
svn-id: r38360
Diffstat (limited to 'engines/sci/engine')
-rw-r--r-- | engines/sci/engine/int_hashmap.cpp | 98 | ||||
-rw-r--r-- | engines/sci/engine/int_hashmap.h | 86 | ||||
-rw-r--r-- | engines/sci/engine/savegame.cfsml | 12 | ||||
-rw-r--r-- | engines/sci/engine/savegame.cpp | 12 | ||||
-rw-r--r-- | engines/sci/engine/seg_manager.cpp | 23 |
5 files changed, 82 insertions, 149 deletions
diff --git a/engines/sci/engine/int_hashmap.cpp b/engines/sci/engine/int_hashmap.cpp index 487a87aa62..ffd903d5d1 100644 --- a/engines/sci/engine/int_hashmap.cpp +++ b/engines/sci/engine/int_hashmap.cpp @@ -29,82 +29,38 @@ #define HASH_MAX DCS_INT_HASH_MAX -#define COMP(x, y) ((x)-(y)) #define HASH(x) (x & 0xff) -int_hash_map_t * -new_int_hash_map(void) { - int_hash_map_t *map = (int_hash_map_t*)calloc(1, sizeof(int_hash_map_t)); - - return map; +int_hash_map_t::int_hash_map_t() { + base_value = 0; + memset(nodes, 0, sizeof(nodes)); + holes = 0; } - -static void -print_int_nodes(int_hash_map_node_t *node) { - while (node) { - fprintf(stderr, "%p ", (void *)node); - node = node->next; - } -} - -void -print_int_hash_map(int_hash_map_t *map) { - int bucket; - fprintf(stderr, "int map %p: base value=%d\n", (void *)map, - map->base_value); - for (bucket = 0; bucket <= HASH_MAX; bucket++) { - fprintf(stderr, "bucket %d: ", bucket); - print_int_nodes(map->nodes[bucket]); - fprintf(stderr, "\n"); - } - fprintf(stderr, "holes: "); - print_int_nodes(map->holes); - fprintf(stderr, "\n"); -} - -void -apply_to_int_hash_map(int_hash_map_t *map, void *param, void (*note)(void *param, int name, int value)) { - int i; - for (i = 0; i < HASH_MAX; i++) { - int_hash_map_node_t *node = map->nodes[i]; - while (node) { - note(param, node->name, node->value); - node = node->next; - } - } -} - - -static void -free_int_hash_map_node_t_recursive(int_hash_map_node_t *node) { +void int_hash_map_t::free_node_recursive(node_t *node) { if (node) { - free_int_hash_map_node_t_recursive(node->next); + free_node_recursive(node->next); free(node); } } -void -free_int_hash_map(int_hash_map_t *map) { +int_hash_map_t::~int_hash_map_t() { int i; for (i = 0; i <= HASH_MAX; i++) - free_int_hash_map_node_t_recursive(map->nodes[i]); + free_node_recursive(nodes[i]); - free_int_hash_map_node_t_recursive(map->holes); + free_node_recursive(holes); - map->base_value = -42000; /* Trigger problems for people who - ** forget to loose the reference */ - free(map); + // Trigger problems for people who forget to loose the reference + base_value = -42000; } -int -int_hash_map_check_value(int_hash_map_t *map, int value, - char add, char *was_added) { - int_hash_map_node_t **node = &(map->nodes[HASH(value)]); +int int_hash_map_t::check_value(int value, bool add, char *was_added) { + node_t **node = &(nodes[HASH(value)]); - while (*node && COMP(value, (*node)->name)) + while (*node && (value != (*node)->name)) node = &((*node)->next); if (was_added) @@ -121,15 +77,15 @@ int_hash_map_check_value(int_hash_map_t *map, int value, if (was_added) *was_added = 1; - if (map->holes) { /* Re-use old node */ - (*node) = map->holes; - map->holes = (*node)->next; + if (holes) { /* Re-use old node */ + (*node) = holes; + holes = (*node)->next; (*node)->next = NULL; (*node)->name = value; } else { - *node = (int_hash_map_node_t*)malloc(sizeof(int_hash_map_node_t)); + *node = (node_t*)malloc(sizeof(node_t)); (*node)->name = value; - (*node)->value = map->base_value++; + (*node)->value = base_value++; (*node)->next = NULL; } @@ -137,20 +93,20 @@ int_hash_map_check_value(int_hash_map_t *map, int value, } -int -int_hash_map_remove_value(int_hash_map_t *map, int value) { - int_hash_map_node_t **node = &(map->nodes[HASH(value)]); +int int_hash_map_t::remove_value(int value) { + node_t **node = &(nodes[HASH(value)]); - while (*node && COMP(value, (*node)->name)) + while (*node && (value != (*node)->name)) node = &((*node)->next); if (*node) { - int_hash_map_node_t *oldnode = *node; + node_t *oldnode = *node; *node = (*node)->next; - oldnode->next = map->holes; /* Old node is now a 'hole' */ - map->holes = oldnode; + oldnode->next = holes; /* Old node is now a 'hole' */ + holes = oldnode; return oldnode->value; - } else return -1; /* Not found */ + } else + return -1; /* Not found */ } diff --git a/engines/sci/engine/int_hashmap.h b/engines/sci/engine/int_hashmap.h index d87ba78c88..bc522d5de8 100644 --- a/engines/sci/engine/int_hashmap.h +++ b/engines/sci/engine/int_hashmap.h @@ -44,67 +44,45 @@ #define DCS_INT_HASH_MAX 255 -struct int_hash_map_node_t { - int name; - int value; - int_hash_map_node_t *next; -}; - - struct int_hash_map_t { + struct node_t { + int name; + int value; + node_t *next; + }; + int base_value; /* Starts at zero, counts upwards */ - int_hash_map_node_t *nodes[DCS_INT_HASH_MAX+1]; - int_hash_map_node_t *holes; /* List of freed entries to minimize + node_t *nodes[DCS_INT_HASH_MAX+1]; + node_t *holes; /* List of freed entries to minimize ** memory operations and modifications ** to base_value */ -}; -typedef int_hash_map_t *int_hash_map_ptr; + void free_node_recursive(node_t *node); + +public: + int_hash_map_t(); + ~int_hash_map_t(); + + /** + * Checks whether a value is in the map, adds it if neccessary. + * @param value The value to check for/add + * @param add Whether to add the value if it's not in there + * @param was_added Set to non-zero iff the value is new, ignored if NULL. + * @return The new (or old) index, or -1 if add was zero and + * the value couldn't be found + */ + int check_value(int value, bool add, char *was_added = 0); + + /** + * Removes a value from the hash map. + * @param value The value to remove + * @return The ID of the value, or -1 if it wasn't present + */ + int remove_value(int value); +}; -int_hash_map_t * -new_int_hash_map(void); -/* Creates a new hash map for the specified int -** Parameters: (void) -** Returns : (int_hash_map_t *) The newly allocated hash map -*/ - -void -free_int_hash_map(int_hash_map_ptr map); -/* Frees the specified hash map -** Parameters: (int_hash_map_t *) map: The map to free -** Returns : (void) -*/ - -void -apply_to_int_hash_map(int_hash_map_ptr map, void *param, void (*note)(void *param, int name, int value)); -/* Iterates over all entries in the hash map and invokes 'note' -** Parameters: (int_hash_map_t *) map: The map to iterate over -** (void *) param: Some parameter to pass to 'note' -** ((voidptr * int * value) -> void) note: The callback to invoke for each entry -*/ - -int -int_hash_map_check_value(int_hash_map_ptr map, int value, char add, char *was_added); -/* Checks whether a value is in the map, adds it if neccessary -** Parameters: (int_hash_map_t *) map: The map to look in/modify -** (int) value: The value to check for/add -** (char) add: Whether to add the value if it's not in there -** (char *) was_added: Set to non-zero iff the value is new -** Ignored if NULL. -** Returns : (int) The new (or old) index, or -1 if add was zero and -** the value couldn't be found -** If MUST_FREE is defined and add is set but the value was already in -** there, the value is freed. -*/ - -int -int_hash_map_remove_value(int_hash_map_ptr map, int value); -/* Removes a value from the hash map -** Parameters: (int_hash_map_t *) map: The map to remove from -** (int) value: The value to remove -** Returns : (int) The ID of the value, or -1 if it wasn't presen -*/ +typedef int_hash_map_t *int_hash_map_ptr; #endif /* INT_HASHMAP_H */ diff --git a/engines/sci/engine/savegame.cfsml b/engines/sci/engine/savegame.cfsml index 62de040dac..68f3f0bc65 100644 --- a/engines/sci/engine/savegame.cfsml +++ b/engines/sci/engine/savegame.cfsml @@ -163,9 +163,9 @@ int read_songlib_t(FILE *fh, songlib_t *foo, const char *lastval, int *line, int *hiteof); void -write_int_hash_map_node_tp(FILE *fh, int_hash_map_node_t **foo); +write_int_hash_map_node_tp(FILE *fh, int_hash_map_t::node_t **foo); int -read_int_hash_map_node_tp(FILE *fh, int_hash_map_node_t **foo, const char *lastval, int *line, int *hiteof); +read_int_hash_map_node_tp(FILE *fh, int_hash_map_t::node_t **foo, const char *lastval, int *line, int *hiteof); int read_song_tp(FILE *fh, song_t **foo, const char *lastval, int *line, int *hiteof); @@ -185,7 +185,7 @@ TYPE mem_obj_ptr "mem_obj_t *" USING write_mem_obj_tp read_mem_obj_tp; TYPE reg_t "reg_t" USING write_reg_t read_reg_t; TYPE size_t "size_t" LIKE int; TYPE int_hash_map_tp "int_hash_map_t *" USING write_int_hash_map_tp read_int_hash_map_tp; -TYPE int_hash_map_node_tp "int_hash_map_node_t *" USING write_int_hash_map_node_tp read_int_hash_map_node_tp; +TYPE int_hash_map_node_tp "int_hash_map_t::node_t *" USING write_int_hash_map_node_tp read_int_hash_map_node_tp; TYPE songlib_t "songlib_t" USING write_songlib_t read_songlib_t; TYPE song_tp "song_t *" USING write_song_tp read_song_tp; TYPE song_iterator_t "song_iterator_t" USING write_song_iterator_t read_song_iterator_t; @@ -490,7 +490,7 @@ read_int_hash_map_tp(FILE *fh, int_hash_map_t **foo, const char *lastval, int *l } void -write_int_hash_map_node_tp(FILE *fh, int_hash_map_node_t **foo) { +write_int_hash_map_node_tp(FILE *fh, int_hash_map_t::node_t **foo) { if (!(*foo)) { fputs("\\null", fh); } else { @@ -503,13 +503,13 @@ write_int_hash_map_node_tp(FILE *fh, int_hash_map_node_t **foo) { } int -read_int_hash_map_node_tp(FILE *fh, int_hash_map_node_t **foo, const char *lastval, int *line, int *hiteof) { +read_int_hash_map_node_tp(FILE *fh, int_hash_map_t::node_t **foo, const char *lastval, int *line, int *hiteof) { static char buffer[80]; if (lastval[0] == '\\') { *foo = NULL; /* No hash map node */ } else { - *foo = (int_hash_map_node_t*)malloc(sizeof(int_hash_map_node_t)); + *foo = (int_hash_map_t::node_t*)malloc(sizeof(int_hash_map_t::node_t)); if (lastval[0] != '[') { sciprintf("Expected opening bracket in hash_map_node_t on line %d\n", *line); return 1; diff --git a/engines/sci/engine/savegame.cpp b/engines/sci/engine/savegame.cpp index 6578644c3b..b3c368a8f7 100644 --- a/engines/sci/engine/savegame.cpp +++ b/engines/sci/engine/savegame.cpp @@ -163,9 +163,9 @@ int read_songlib_t(FILE *fh, songlib_t *foo, const char *lastval, int *line, int *hiteof); void -write_int_hash_map_node_tp(FILE *fh, int_hash_map_node_t **foo); +write_int_hash_map_node_tp(FILE *fh, int_hash_map_t::node_t **foo); int -read_int_hash_map_node_tp(FILE *fh, int_hash_map_node_t **foo, const char *lastval, int *line, int *hiteof); +read_int_hash_map_node_tp(FILE *fh, int_hash_map_t::node_t **foo, const char *lastval, int *line, int *hiteof); int read_song_tp(FILE *fh, song_t **foo, const char *lastval, int *line, int *hiteof); @@ -4207,7 +4207,7 @@ int mem_obj_string_to_enum(const char *str) { int i; for (i = 0; i <= MEM_OBJ_MAX; i++) { - if (!scumm_stricmp(mem_obj_string_names[i].name, str)) + if (!strcasecmp(mem_obj_string_names[i].name, str)) return i; } @@ -4302,7 +4302,7 @@ read_int_hash_map_tp(FILE *fh, int_hash_map_t **foo, const char *lastval, int *l } void -write_int_hash_map_node_tp(FILE *fh, int_hash_map_node_t **foo) { +write_int_hash_map_node_tp(FILE *fh, int_hash_map_t::node_t **foo) { if (!(*foo)) { fputs("\\null", fh); } else { @@ -4320,13 +4320,13 @@ write_int_hash_map_node_tp(FILE *fh, int_hash_map_node_t **foo) { } int -read_int_hash_map_node_tp(FILE *fh, int_hash_map_node_t **foo, const char *lastval, int *line, int *hiteof) { +read_int_hash_map_node_tp(FILE *fh, int_hash_map_t::node_t **foo, const char *lastval, int *line, int *hiteof) { static char buffer[80]; if (lastval[0] == '\\') { *foo = NULL; /* No hash map node */ } else { - *foo = (int_hash_map_node_t*)malloc(sizeof(int_hash_map_node_t)); + *foo = (int_hash_map_t::node_t*)malloc(sizeof(int_hash_map_t::node_t)); if (lastval[0] != '[') { sciprintf("Expected opening bracket in hash_map_node_t on line %d\n", *line); return 1; diff --git a/engines/sci/engine/seg_manager.cpp b/engines/sci/engine/seg_manager.cpp index 6818f737c5..39af669e36 100644 --- a/engines/sci/engine/seg_manager.cpp +++ b/engines/sci/engine/seg_manager.cpp @@ -94,8 +94,7 @@ find_free_id(seg_manager_t *self, int *id) { int retval = 0; while (!was_added) { - retval = int_hash_map_check_value(self->id_seg_map, self->reserved_id, - 1, &was_added); + retval = self->id_seg_map->check_value(self->reserved_id, true, &was_added); *id = self->reserved_id--; if (self->reserved_id < -1000000) self->reserved_id = -10; @@ -118,9 +117,9 @@ void sm_init(seg_manager_t* self, int sci1_1) { self->mem_allocated = 0; /* Initialise memory count */ - self->id_seg_map = new_int_hash_map(); + self->id_seg_map = new int_hash_map_t(); self->reserved_id = INVALID_SCRIPT_ID; - int_hash_map_check_value(self->id_seg_map, self->reserved_id, 1, NULL); /* reserve 0 for seg_id */ + self->id_seg_map->check_value(self->reserved_id, true); /* reserve 0 for seg_id */ self->reserved_id--; /* reserved_id runs in the reversed direction to make sure no one will use it. */ self->heap_size = DEFAULT_SCRIPTS; @@ -152,7 +151,7 @@ void sm_destroy(seg_manager_t* self) { _sm_deallocate(self, i, 0); } - free_int_hash_map(self->id_seg_map); + delete self->id_seg_map; sci_free(self->heap); self->heap = NULL; @@ -170,7 +169,7 @@ mem_obj_t* sm_allocate_script(seg_manager_t* self, struct _state *s, int script_ char was_added; mem_obj_t* mem; - seg = int_hash_map_check_value(self->id_seg_map, script_nr, 1, &was_added); + seg = self->id_seg_map->check_value(script_nr, true, &was_added); if (!was_added) { *seg_id = seg; return self->heap[*seg_id]; @@ -256,7 +255,7 @@ int sm_initialise_script(mem_obj_t *mem, struct _state *s, int script_nr) { scr->marked_as_deleted = 0; scr->relocated = 0; - scr->obj_indices = new_int_hash_map(); + scr->obj_indices = new int_hash_map_t(); if (s->version >= SCI_VERSION(1, 001, 000)) scr->heap_start = scr->buf + scr->script_size; @@ -272,7 +271,7 @@ _sm_deallocate(seg_manager_t* self, int seg, int recursive) { VERIFY(sm_check(self, seg), "invalid seg id"); mobj = self->heap[seg]; - int_hash_map_remove_value(self->id_seg_map, mobj->segmgr_id); + self->id_seg_map->remove_value(mobj->segmgr_id); switch (mobj->type) { @@ -452,7 +451,7 @@ sm_free_script(mem_obj_t* mem) { mem->data.script.objects_nr = 0; } - free_int_hash_map(mem->data.script.obj_indices); + delete mem->data.script.obj_indices; if (NULL != mem->data.script.code) { sci_free(mem->data.script.code); } @@ -597,7 +596,7 @@ void sm_put_heap(seg_manager_t* self, reg_t reg, gint16 value) { /* return the seg if script_id is valid and in the map, else -1 */ int sm_seg_get(seg_manager_t* self, int script_id) { - return int_hash_map_check_value(self->id_seg_map, script_id, 0, NULL); + return self->id_seg_map->check_value(script_id, false); } /* validate the seg @@ -966,7 +965,7 @@ sm_script_obj_init0(seg_manager_t *self, state_t *s, reg_t obj_pos) { } temp = make_reg(obj_pos.segment, base); - id = int_hash_map_check_value(scr->obj_indices, base, 1, NULL); + id = scr->obj_indices->check_value(base, true); scr->objects_nr++; obj = scr->objects + id; @@ -1041,7 +1040,7 @@ sm_script_obj_init11(seg_manager_t *self, state_t *s, reg_t obj_pos) { } - id = int_hash_map_check_value(scr->obj_indices, obj_pos.offset, 1, NULL); + id = scr->obj_indices->check_value(obj_pos.offset, true); scr->objects_nr++; obj = scr->objects + id; |