void buzzdict_set(buzzdict_t dt, const void* key, const void* data) { /* Hash the key */ uint32_t h = dt->hashf(key) % dt->num_buckets; /* Is the bucket empty? */ if(!dt->buckets[h]) { /* Create new entry list */ dt->buckets[h] = buzzdarray_new(1, sizeof(struct buzzdict_entry_s), NULL); /* Add entry */ buzzdict_entry_new(dt, e, key, data); buzzdarray_push(dt->buckets[h], &e); /* Increase size */ ++(dt->size); } else { /* Bucket not empty - is the entry present? */ uint32_t i; for(i = 0; i < buzzdarray_size(dt->buckets[h]); ++i) { const struct buzzdict_entry_s* e = &buzzdarray_get(dt->buckets[h], i, struct buzzdict_entry_s); if(dt->keycmpf(key, e->key) == 0) { /* Yes, destroy the entry */ dt->dstryf(e->key, e->data, dt); buzzdarray_remove(dt->buckets[h], i); --(dt->size); break; } } /* Add new entry */ buzzdict_entry_new(dt, e, key, data); buzzdarray_push(dt->buckets[h], &e); /* Increase size */ ++(dt->size); } }
int buzzdict_remove(buzzdict_t dt, const void* key) { /* Hash the key */ uint32_t h = dt->hashf(key) % dt->num_buckets, i; /* Is the bucket empty? */ if(!dt->buckets[h]) return 0; /* Bucket not empty - is the entry present? */ for(i = 0; i < buzzdarray_size(dt->buckets[h]); ++i) { const struct buzzdict_entry_s* e = &buzzdarray_get(dt->buckets[h], i, struct buzzdict_entry_s); if(dt->keycmpf(key, e->key) == 0) { /* Entry found - remove it */ dt->dstryf(e->key, e->data, dt); buzzdarray_remove(dt->buckets[h], i); /* Is the entry list empty? If so, free the memory */ if(buzzdarray_isempty(dt->buckets[h])) buzzdarray_destroy(&(dt->buckets[h])); /* Increase size */ --(dt->size); /* Done */ return 1; } } /* Entry not found, nothing to do */ return 0; }
void buzzswarm_members_leave(buzzswarm_members_t m, uint16_t robot, uint16_t swarm) { /* Is an entry for the passed robot present? */ buzzswarm_elem_t* e = buzzdict_get(m, &robot, buzzswarm_elem_t); if(e) { /* Yes, update it */ (*e)->age = 0; /* Search for the passed id */ uint32_t i; for(i = 0; i < buzzdarray_size((*e)->swarms); ++i) { if(buzzdarray_get((*e)->swarms, i, uint16_t) == swarm) break; } /* If the element was found, remove it */ if(i < buzzdarray_size((*e)->swarms)) buzzdarray_remove((*e)->swarms, i); /* If no swarm id is known for this robot, remove the entry altogether */ if(buzzdarray_isempty((*e)->swarms)) buzzdict_remove(m, &robot); } /* Nothing to do if you get a 'leave' message for someone you don't know */ }