int buzzvstig_create(buzzvm_t vm) { buzzvm_lnum_assert(vm, 1); /* Get vstig id */ buzzvm_lload(vm, 1); buzzvm_type_assert(vm, 1, BUZZTYPE_INT); uint16_t id = buzzvm_stack_at(vm, 1)->i.value; buzzvm_pop(vm); /* Look for virtual stigmergy */ const buzzvstig_t* vs = buzzdict_get(vm->vstigs, &id, buzzvstig_t); if(vs) { /* Found, destroy it */ buzzdict_remove(vm->vstigs, &id); } /* Create a new virtual stigmergy */ buzzvstig_t nvs = buzzvstig_new(); buzzdict_set(vm->vstigs, &id, &nvs); /* Create a table */ buzzvm_pusht(vm); /* Add data and methods */ buzzvm_dup(vm); buzzvm_pushs(vm, buzzvm_string_register(vm, "id", 1)); buzzvm_pushi(vm, id); buzzvm_tput(vm); function_register(foreach); function_register(size); function_register(put); function_register(get); function_register(onconflict); function_register(onconflictlost); /* Return the table */ return buzzvm_ret1(vm); }
int main() { buzzdict_t di = buzzdict_new(4, sizeof(int16_t), sizeof(float), di_hash, di_cmp, NULL); int16_t k; float d; di_print(di); int i; for(i = 0; i < 10; ++i) { k = i; d = (100.0f + k); fprintf(stdout, "adding (%d, %f)\n", k, d); buzzdict_set(di, &k, &d); di_print(di); } k = 5; d = (200.0f + k); fprintf(stdout, "assigning (%d, %f)\n", k, d); buzzdict_set(di, &k, &d); di_print(di); k = 10; d = (200.0f + k); fprintf(stdout, "assigning (%d, %f)\n", k, d); buzzdict_set(di, &k, &d); di_print(di); k = 4; fprintf(stdout, "removing (%d)\n", k); buzzdict_remove(di, &k); di_print(di); k = 10; fprintf(stdout, "removing (%d)\n", k); buzzdict_remove(di, &k); di_print(di); buzzdict_destroy(&di); return 0; }
int buzzneighbors_ignore(buzzvm_t vm) { buzzvm_lnum_assert(vm, 1); /* Get value id argument */ buzzvm_lload(vm, 1); buzzvm_type_assert(vm, 1, BUZZTYPE_STRING); /* Remove listener */ buzzdict_remove( vm->listeners, &(buzzvm_stack_at(vm, 1)->s.value.sid)); return buzzvm_ret0(vm); }
int buzzvm_vstig_create(buzzvm_t vm) { buzzvm_lnum_assert(vm, 1); /* Get vstig id */ buzzvm_lload(vm, 1); buzzvm_type_assert(vm, 1, BUZZTYPE_INT); uint16_t id = buzzvm_stack_at(vm, 1)->i.value; buzzvm_pop(vm); /* Look for virtual stigmergy */ buzzvstig_t* vs = buzzdict_get(vm->vstigs, &id, buzzvstig_t); if(vs) /* Found, destroy it */ buzzdict_remove(vm->vstigs, &id); /* Create a new virtual stigmergy */ buzzvstig_t nvs = buzzvstig_new(); buzzdict_set(vm->vstigs, &id, &nvs); /* Create a table and add data and methods */ buzzobj_t t = buzzheap_newobj(vm->heap, BUZZTYPE_TABLE); buzzvm_push(vm, t); buzzvm_pushs(vm, buzzvm_string_register(vm, "size")); buzzvm_pushcc(vm, buzzvm_function_register(vm, buzzvm_vstig_size)); buzzvm_tput(vm); buzzvm_push(vm, t); buzzvm_pushs(vm, buzzvm_string_register(vm, "put")); buzzvm_pushcc(vm, buzzvm_function_register(vm, buzzvm_vstig_put)); buzzvm_tput(vm); buzzvm_push(vm, t); buzzvm_pushs(vm, buzzvm_string_register(vm, "get")); buzzvm_pushcc(vm, buzzvm_function_register(vm, buzzvm_vstig_get)); buzzvm_tput(vm); buzzvm_push(vm, t); buzzvm_pushs(vm, buzzvm_string_register(vm, "onconflict")); buzzvm_pushcc(vm, buzzvm_function_register(vm, buzzvm_vstig_setonconflict)); buzzvm_tput(vm); buzzvm_push(vm, t); buzzvm_pushs(vm, buzzvm_string_register(vm, "onconflictlost")); buzzvm_pushcc(vm, buzzvm_function_register(vm, buzzvm_vstig_setonconflictlost)); buzzvm_tput(vm); buzzvm_push(vm, t); buzzvm_pushs(vm, buzzvm_string_register(vm, "id")); buzzvm_pushi(vm, id); buzzvm_tput(vm); /* Push the table on the stack */ buzzvm_push(vm, t); /* Return */ return buzzvm_ret1(vm); }
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 */ }
buzzvm_state buzzvm_tput(buzzvm_t vm) { buzzvm_stack_assert(vm, 3); buzzvm_type_assert(vm, 3, BUZZTYPE_TABLE); buzzobj_t v = buzzvm_stack_at(vm, 1); buzzobj_t k = buzzvm_stack_at(vm, 2); buzzobj_t t = buzzvm_stack_at(vm, 3); buzzvm_pop(vm); buzzvm_pop(vm); buzzvm_pop(vm); if(k->o.type != BUZZTYPE_INT && k->o.type != BUZZTYPE_FLOAT && k->o.type != BUZZTYPE_STRING) { buzzvm_seterror(vm, BUZZVM_ERROR_TYPE, "a %s value can't be used as table key", buzztype_desc[k->o.type]); return vm->state; } if(v->o.type == BUZZTYPE_NIL) { /* Nil, erase entry */ buzzdict_remove(t->t.value, &k); } else if(v->o.type == BUZZTYPE_CLOSURE) { /* Method call */ int i; buzzobj_t o = buzzheap_newobj(vm, BUZZTYPE_CLOSURE); o->c.value.isnative = v->c.value.isnative; o->c.value.ref = v->c.value.ref; buzzdarray_push(o->c.value.actrec, &t); for(i = 1; i < buzzdarray_size(v->c.value.actrec); ++i) buzzdarray_push(o->c.value.actrec, &buzzdarray_get(v->c.value.actrec, i, buzzobj_t)); buzzdict_set(t->t.value, &k, &o); } else { buzzdict_set(t->t.value, &k, &v); } return BUZZVM_STATE_READY; }