Merge pull request #68 from haberman/bugfixes

A small bugfix to upb_table and simplified some code.
pull/13171/head
Joshua Haberman 8 years ago committed by GitHub
commit 0787643281
  1. 29
      upb/table.c

@ -219,38 +219,33 @@ static bool rm(upb_table *t, lookupkey_t key, upb_value *val,
if (eql(chain->key, key)) { if (eql(chain->key, key)) {
/* Element to remove is at the head of its chain. */ /* Element to remove is at the head of its chain. */
t->count--; t->count--;
if (val) { if (val) _upb_value_setval(val, chain->val.val, t->ctype);
_upb_value_setval(val, chain->val.val, t->ctype); if (removed) *removed = chain->key;
}
if (chain->next) { if (chain->next) {
upb_tabent *move = (upb_tabent*)chain->next; upb_tabent *move = (upb_tabent*)chain->next;
*chain = *move; *chain = *move;
if (removed) *removed = move->key;
move->key = 0; /* Make the slot empty. */ move->key = 0; /* Make the slot empty. */
} else { } else {
if (removed) *removed = chain->key;
chain->key = 0; /* Make the slot empty. */ chain->key = 0; /* Make the slot empty. */
} }
return true; return true;
} else { } else {
/* Element to remove is either in a non-head position or not in the /* Element to remove is either in a non-head position or not in the
* table. */ * table. */
while (chain->next && !eql(chain->next->key, key)) while (chain->next && !eql(chain->next->key, key)) {
chain = (upb_tabent*)chain->next; chain = (upb_tabent*)chain->next;
}
if (chain->next) { if (chain->next) {
/* Found element to remove. */ /* Found element to remove. */
upb_tabent *rm; upb_tabent *rm = (upb_tabent*)chain->next;
t->count--;
if (val) { if (val) _upb_value_setval(val, chain->next->val.val, t->ctype);
_upb_value_setval(val, chain->next->val.val, t->ctype);
}
rm = (upb_tabent*)chain->next;
if (removed) *removed = rm->key; if (removed) *removed = rm->key;
rm->key = 0; rm->key = 0; /* Make the slot empty. */
chain->next = rm->next; chain->next = rm->next;
t->count--;
return true; return true;
} else { } else {
/* Element to remove is not in the table. */
return false; return false;
} }
} }
@ -359,7 +354,7 @@ bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
bool upb_strtable_remove3(upb_strtable *t, const char *key, size_t len, bool upb_strtable_remove3(upb_strtable *t, const char *key, size_t len,
upb_value *val, upb_alloc *alloc) { upb_value *val, upb_alloc *alloc) {
uint32_t hash = MurmurHash2(key, strlen(key), 0); uint32_t hash = MurmurHash2(key, len, 0);
upb_tabkey tabkey; upb_tabkey tabkey;
if (rm(&t->t, strkey2(key, len), val, &tabkey, hash, &streql)) { if (rm(&t->t, strkey2(key, len), val, &tabkey, hash, &streql)) {
upb_free(alloc, (void*)tabkey); upb_free(alloc, (void*)tabkey);
@ -569,9 +564,7 @@ bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val) {
success = false; success = false;
} }
} else { } else {
upb_tabkey removed; success = rm(&t->t, intkey(key), val, NULL, upb_inthash(key), &inteql);
uint32_t hash = upb_inthash(key);
success = rm(&t->t, intkey(key), val, &removed, hash, &inteql);
} }
check(t); check(t);
return success; return success;

Loading…
Cancel
Save