bool HashMapRemove(HashMap* self, void* key) { HashMapData* data = self->data; /* Calculate the slot index. */ unsigned hash = data->func_hash_(key); hash = hash % data->num_slot_; /* Search the slot list for the deletion target. */ HashMapCompare func_cmp = data->func_cmp_; SlotNode* pred = NULL; SlotNode** arr_slot = data->arr_slot_; SlotNode* curr = arr_slot[hash]; while (curr) { if (func_cmp(key, curr->pair_.key) == 0) { if (data->func_clean_key_) data->func_clean_key_(curr->pair_.key); if (data->func_clean_val_) data->func_clean_val_(curr->pair_.value); if (!pred) arr_slot[hash] = curr->next_; else pred->next_ = curr->next_; free(curr); --(data->size_); return true; } pred = curr; curr = curr->next_; } return false; }
void func_add_args_collect(func_t *f, func_is_t *fis) { int i,j; func_t *g=NULL; if(!func_is_add(f)){ FUNC_ERROR_ARG1("func_add_args_collect",f); } // convert args to split list for(i=0; i<func_asize(f); i++){ g=func_mul_split_list(FR(func_aget(f,i)),fis); func_aset(f,i,FR(g)); g=func_del(g); } // add a arg to same arg for(i=0; i<func_asize(f); i++){ for(j=i+1; func_aget(f,i)!=NULL && !func_is_one(func_aget(func_aget(f,i),1)) && j<func_asize(f); j++){ if(func_aget(f,j)!=NULL && func_cmp(func_aget(func_aget(f,i),1),func_aget(func_aget(f,j),1))==0){ g=func_list(2); func_aset(g,0,func_add(FR(func_aget(func_aget(f,i),0)),FR(func_aget(func_aget(f,j),0)))); func_aset(g,1,FR(func_aget(func_aget(f,i),1))); func_aset(f,i,FR(g)); func_adel(f,j); g=func_del(g); } } } // convert list to mul for(i=0; i<func_asize(f); i++){ if(func_aget(f,i)!=NULL){ g=func_mul(FR(func_aget(func_aget(f,i),0)),FR(func_aget(func_aget(f,i),1))); func_aset(f,i,FR(g)); g=func_del(g); } } func_a_rm_null(f); }
// g in f ? int func_args_have(func_t *f, func_t *g) { int i; for(i=0; i<func_asize(f); i++){ if(func_cmp(f->a[i],g)==0){ return 1; } } return 0; }
bool HashMapPut(HashMap* self, void* key, void* value) { /* Check the loading factor for rehashing. */ HashMapData* data = self->data; if (data->size_ >= data->curr_limit_) _HashMapReHash(data); /* Calculate the slot index. */ unsigned hash = data->func_hash_(key); hash = hash % data->num_slot_; /* Check if the pair conflicts with a certain one stored in the map. If yes, replace that one. */ HashMapCompare func_cmp = data->func_cmp_; SlotNode** arr_slot = data->arr_slot_; SlotNode* curr = arr_slot[hash]; while (curr) { if (func_cmp(key, curr->pair_.key) == 0) { if (data->func_clean_key_) data->func_clean_key_(curr->pair_.key); if (data->func_clean_val_) data->func_clean_val_(curr->pair_.value); curr->pair_.key = key; curr->pair_.value = value; return true; } curr = curr->next_; } /* Insert the new pair into the slot list. */ SlotNode* node = (SlotNode*)malloc(sizeof(SlotNode)); if (unlikely(!node)) return false; node->pair_.key = key; node->pair_.value = value; if (!(arr_slot[hash])) { node->next_ = NULL; arr_slot[hash] = node; } else { node->next_ = arr_slot[hash]; arr_slot[hash] = node; } ++(data->size_); return true; }
bool HashMapContain(HashMap* self, void* key) { HashMapData* data = self->data; /* Calculate the slot index. */ unsigned hash = data->func_hash_(key); hash = hash % data->num_slot_; /* Search the slot list to check if there is a pair having the same key with the designated one. */ HashMapCompare func_cmp = data->func_cmp_; SlotNode* curr = data->arr_slot_[hash]; while (curr) { if (func_cmp(key, curr->pair_.key) == 0) return true; curr = curr->next_; } return false; }