Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
Add st_foreach_map().
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Weaver committed Mar 24, 2011
1 parent 49f7e62 commit ee40b95
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 23 additions & 1 deletion st.c
Expand Up @@ -40,7 +40,6 @@ static struct st_hash_type type_numhash = {
};

/* extern int strcmp(const char *, const char *); */
int strhash(const char *);
static struct st_hash_type type_strhash = {
strcmp,
strhash,
Expand Down Expand Up @@ -471,6 +470,29 @@ st_cleanup_safe(table, never)
table->num_entries = num_entries;
}

void
st_foreach_map(table, map_key_func, map_value_func)
st_table *table;
st_data_t (*map_key_func)();
st_data_t (*map_value_func)();
{
st_table_entry *ptr;
st_data_t new_key;
int i;

for(i = 0; i < table->num_bins; i++) {
for(ptr = table->bins[i]; ptr != 0;) {
new_key = (*map_key_func)(ptr->key);
if(new_key != ptr->key && table->type->compare(new_key, ptr->key)) {
rb_bug("Key replaced with non-equal key");
}
ptr->key = new_key;
ptr->record = (*map_value_func)(ptr->record);
ptr = ptr->next;
}
}
}

int
st_foreach(table, func, arg)
st_table *table;
Expand Down
2 changes: 2 additions & 0 deletions st.h
Expand Up @@ -56,10 +56,12 @@ int st_delete_safe _((st_table *, st_data_t *, st_data_t *, st_data_t));
int st_insert _((st_table *, st_data_t, st_data_t));
int st_lookup _((st_table *, st_data_t, st_data_t *));
int st_foreach _((st_table *, int (*)(ANYARGS), st_data_t));
void st_foreach_map _((st_table*, st_data_t (*)(ANYARGS), st_data_t (*)(ANYARGS)));
void st_add_direct _((st_table *, st_data_t, st_data_t));
void st_free_table _((st_table *));
void st_cleanup_safe _((st_table *, st_data_t));
st_table *st_copy _((st_table *));
int strhash(const char *);

#define ST_NUMCMP ((int (*)()) 0)
#define ST_NUMHASH ((int (*)()) -2)
Expand Down

0 comments on commit ee40b95

Please sign in to comment.