void hashmap_remove(hashmap_p m, char* key){ int n = strlen(key); size_t h = hash_func(key) % m->num_buckets; item_t *itm = m->buckets[h]; item_t *last = NULL; int keyind; while(itm!=NULL){ if(strcmp(key, itm->key)==0) break; last = itm; itm = itm->next; } if(itm != NULL){ if(last==NULL) m->buckets[h] = NULL; else last->next = itm->next; free(itm->key); m->destructor(itm->val); free(itm); keyind = vector_index(m->keys, key, n); vector_remove(m->keys, keyind); m->size--; } }
void hashset_add(Hashset *self, const char *key) { assert(self); assert(key); u64 hash = _hashset_djb2(key); u32 index = _hashset_index(self, key); Vector *bucket = NULL; if (!self->buckets[index]) { self->buckets[index] = vector_new(8, free); } bucket = self->buckets[index]; for (u32 i = 0; i < bucket->size; ++i) { void *pkvp = vector_index(bucket, i); HashsetNode *kvp = (HashsetNode *)pkvp; // key already exists in hashset, set it to true and return success if (kvp->key == hash) { kvp->value = true; return; } } HashsetNode *kvp = (HashsetNode *)calloc(1, sizeof(HashsetNode)); kvp->key = hash; kvp->value = true; vector_add(bucket, kvp); }
bool EigenTypekitPlugin::loadOperators() { RTT::types::OperatorRepository::Instance()->add( newBinaryOperator( "[]", vector_index() ) ); //RTT::types::OperatorRepository::Instance()->add( newDotOperator( "size", get_size() ) ); //RTT::types::OperatorRepository::Instance()->add( newTernaryOperator( "[,]", matrix_index() ) ); return true; }
static void init_array( int rnk_n, const INT *n, const INT *local_n, const INT *local_start, unsigned arraytype, void *data ) { INT ln_tot; INT *kvec_loc, *kvec_glob, *kvec_glob_mirrored; kvec_loc = PX(malloc_INT)(rnk_n); kvec_glob = PX(malloc_INT)(rnk_n); kvec_glob_mirrored = PX(malloc_INT)(rnk_n); ln_tot = PX(prod_INT)(rnk_n, local_n); for(INT k=0; k<ln_tot; k++){ vector_index(rnk_n, local_n, k, kvec_loc); PX(vadd_INT)(rnk_n, kvec_loc, local_start, kvec_glob); PX(vsub_INT)(rnk_n, n, kvec_glob, kvec_glob_mirrored); C d1 = init_scalar_periodic(rnk_n, n, kvec_glob); C d2 = init_scalar_periodic(rnk_n, n, kvec_glob_mirrored); switch (arraytype){ case PFFTI_ARRAYTYPE_REAL: /* set padding element to zero */ ((R*)data)[k] = (kvec_glob[rnk_n-1] < n[rnk_n-1]) ? (R) d1 : 0.0; break; case PFFTI_ARRAYTYPE_COMPLEX: ((C*)data)[k] = d1; break; case PFFTI_ARRAYTYPE_HERMITIAN_COMPLEX: ((C*)data)[k] = 0.5 * (d1 + conj(d2)); break; } } free(kvec_loc); free(kvec_glob); free(kvec_glob_mirrored); }
int call_msg_is_retrans(sip_msg_t *msg) { sip_msg_t *prev = NULL; vector_iter_t it; // Get previous message in call with same origin and destination it = vector_iterator(msg->call->msgs); vector_iterator_set_current(&it, vector_index(msg->call->msgs, msg)); while ((prev = vector_iterator_prev(&it))) { if (!strcmp(SRC(prev), SRC(msg)) && !strcmp(DST(prev), DST(msg))) break; } return (prev && !strcasecmp(msg_get_payload(msg), msg_get_payload(prev))); }
int call_msg_is_retrans(sip_msg_t *msg) { sip_msg_t *prev = NULL; vector_iter_t it; // Get previous message in call with same origin and destination it = vector_iterator(msg->call->msgs); vector_iterator_set_current(&it, vector_index(msg->call->msgs, msg)); while ((prev = vector_iterator_prev(&it))) { if (addressport_equals(prev->packet->src, msg->packet->src) && addressport_equals(prev->packet->dst, msg->packet->dst)) break; } return (prev && !strcasecmp(msg_get_payload(msg), msg_get_payload(prev))); }
void vector_remove(vector_t *vector, void *item) { // Get item position int idx = vector_index(vector, item); // Decrease item counter vector->count--; // Move the rest of the elements one position up memmove(vector->list + idx, vector->list + idx + 1, sizeof(void *) * (vector->count - idx)); // Reset vector last position vector->list[vector->count] = NULL; // Destroy the item if vector has a destroyer if (vector->destroyer) { vector->destroyer(item); } }
static R check_array( int rnk_n, const INT *n, const INT *local_n, const INT *local_start, unsigned arraytype, const void *data, MPI_Comm comm ) { INT ln_tot; INT *kvec_loc, *kvec_glob, *kvec_glob_mirrored; R err, maxerr, globmaxerr; err = maxerr = 0; kvec_loc = PX(malloc_INT)(rnk_n); kvec_glob = PX(malloc_INT)(rnk_n); kvec_glob_mirrored = PX(malloc_INT)(rnk_n); ln_tot = PX(prod_INT)(rnk_n, local_n); for(INT k=0; k<ln_tot; k++){ vector_index(rnk_n, local_n, k, kvec_loc); PX(vadd_INT)(rnk_n, kvec_loc, local_start, kvec_glob); PX(vsub_INT)(rnk_n, n, kvec_glob, kvec_glob_mirrored); C d1 = init_scalar_periodic(rnk_n, n, kvec_glob); C d2 = init_scalar_periodic(rnk_n, n, kvec_glob_mirrored); switch (arraytype){ case PFFTI_ARRAYTYPE_REAL: /* ignore padding elements */ err = (kvec_glob[rnk_n-1] < n[rnk_n-1]) ? cabs( ((R*)data)[k] - (R) d1 ) : 0.0; break; case PFFTI_ARRAYTYPE_COMPLEX: err = cabs( ((C*)data)[k] - d1); break; case PFFTI_ARRAYTYPE_HERMITIAN_COMPLEX: err = cabs( ((C*)data)[k] - 0.5 * (d1 + conj(d2))); break; } if( err > maxerr ) maxerr = err; } free(kvec_loc); free(kvec_glob); free(kvec_glob_mirrored); MPI_Allreduce(&maxerr, &globmaxerr, 1, PFFT_MPI_REAL_TYPE, MPI_MAX, comm); return globmaxerr; }
void hashset_remove(Hashset *self, const char *key) { assert(self); assert(key); u64 hash = _hashset_djb2(key); u32 index = _hashset_index(self, key); Vector *bucket = self->buckets[index]; if (!bucket) { return; } for (u32 i = 0; i < bucket->size; ++i) { void *pkvp = vector_index(bucket, i); HashsetNode *kvp = (HashsetNode *)pkvp; if (kvp->key == hash) { kvp->value = false; } } }
// create a new array object with data copied from array a array copy(array a){ const int datasz = productdims(a->rank,a->dims); array z=malloc(sizeof *z + (2*a->rank+datasz)*sizeof(int)); z->type = normal; z->rank = a->rank; z->cons = 0; z->translate = 0; z->dims = (int*)(z+1); memmove(z->dims,a->dims,z->rank*sizeof(int)); z->weight = z->dims + z->rank; calcweight(z->rank, z->dims, z->weight); z->data = z->weight + z->rank; int scratch[a->rank]; for (int i=0; i < datasz; ++i){ z->data[i] = *elema(a, vector_index(i,a->dims,a->rank,scratch)); } return z; }
void su_vector_index(su_state *s, int idx) { s->stack[s->stack_top - 1] = vector_index(s, STK(TOP(idx))->obj.vec, (int)STK(-1)->obj.num); }
static void vm_loop(su_state *s, function_t *func) { value_t tmpv, tmpv2; instruction_t inst; int tmp, narg, i, j, k; const char *tmpcs; su_debug_data dbg; s->frame = FRAME(); s->prot = func->prot; #define ARITH_OP(op) \ su_check_type(s, -2, SU_NUMBER); \ su_check_type(s, -1, SU_NUMBER); \ STK(-2)->obj.num = STK(-2)->obj.num op STK(-1)->obj.num; \ su_pop(s, 1); \ break; #define LOG_OP(op) \ su_check_type(s, -2, SU_NUMBER); \ su_check_type(s, -1, SU_NUMBER); \ STK(-2)->type = SU_BOOLEAN; \ STK(-2)->obj.b = STK(-2)->obj.num op STK(-1)->obj.num; \ su_pop(s, 1); \ break; for (s->pc = 0; s->pc < s->prot->num_inst; s->pc++) { tmp = s->interrupt | atomic_get(&s->msi->interrupt); if (tmp) { if ((tmp & ISCOLLECT) == ISCOLLECT) { su_thread_indisposable(s); su_thread_disposable(s); } if ((tmp & IGC) == IGC) { unmask_thread_interrupt(s, IGC); gc_trace(s); } if ((tmp & IBREAK) == IBREAK) { unmask_thread_interrupt(s, IBREAK); dbg.file = s->prot->name->str; dbg.line = s->prot->lineinf[s->pc]; s->debug_cb(s, &dbg, s->debug_cb_data); } } inst = s->prot->inst[s->pc]; switch (inst.id) { case OP_PUSH: push_value(s, &func->constants[inst.a]); break; case OP_POP: su_pop(s, inst.a); break; case OP_ADD: ARITH_OP(+) case OP_SUB: ARITH_OP(-) case OP_MUL: ARITH_OP(*) case OP_DIV: su_check_type(s, -2, SU_NUMBER); su_check_type(s, -1, SU_NUMBER); su_assert(s, STK(-1)->obj.num != 0.0, "Division by zero!"); STK(-2)->obj.num = STK(-2)->obj.num / STK(-1)->obj.num; su_pop(s, 1); break; case OP_MOD: su_check_type(s, -2, SU_NUMBER); su_check_type(s, -1, SU_NUMBER); STK(-2)->obj.num = (double)((int)STK(-2)->obj.num % (int)STK(-1)->obj.num); su_pop(s, 1); break; case OP_POW: su_check_type(s, -2, SU_NUMBER); su_check_type(s, -1, SU_NUMBER); STK(-2)->obj.num = pow(STK(-2)->obj.num, STK(-1)->obj.num); su_pop(s, 1); break; case OP_UNM: su_check_type(s, -1, SU_NUMBER); STK(-1)->obj.num = -STK(-1)->obj.num; break; case OP_EQ: STK(-2)->obj.b = value_eq(STK(-2), STK(-1)); STK(-2)->type = SU_BOOLEAN; su_pop(s, 1); break; case OP_LESS: LOG_OP(<); case OP_LEQUAL: LOG_OP(<=); case OP_NOT: if (STK(-1)->type == SU_BOOLEAN) { STK(-1)->obj.b = !STK(-1)->obj.b; } else { STK(-1)->obj.b = (STK(-1)->type == SU_NIL) ? 1 : 0; STK(-1)->type = SU_BOOLEAN; } break; case OP_AND: tmp = STK(-2)->type != SU_NIL && (STK(-2)->type != SU_BOOLEAN || STK(-2)->obj.b); if (tmp && STK(-1)->type != SU_NIL && (STK(-1)->type != SU_BOOLEAN || STK(-1)->obj.b)) { s->stack[s->stack_top - 2] = *STK(-1); } else { STK(-2)->obj.b = 0; STK(-2)->type = SU_BOOLEAN; } su_pop(s, 1); break; case OP_OR: if (STK(-2)->type != SU_NIL && (STK(-2)->type != SU_BOOLEAN || STK(-2)->obj.b)) { /* return -2 */ } else if (STK(-1)->type != SU_NIL && (STK(-1)->type != SU_BOOLEAN || STK(-1)->obj.b)) { s->stack[s->stack_top - 2] = *STK(-1); } else { STK(-2)->obj.b = 0; STK(-2)->type = SU_BOOLEAN; } su_pop(s, 1); break; case OP_TEST: if (STK(-1)->type != SU_NIL && (STK(-1)->type != SU_BOOLEAN || STK(-1)->obj.b)) s->pc = inst.b - 1; su_pop(s, 1); break; case OP_FOR: if (STK(-2)->type == SU_NIL) { su_swap(s, -2, -1); s->stack_top--; s->pc = inst.b - 1; } else { s->stack_top--; su_check_type(s, -1, SU_SEQ); su_rest(s, -1); su_swap(s, -2, -1); su_first(s, -1); su_swap(s, -2, -1); s->stack_top--; } break; case OP_JMP: s->pc = inst.b - 1; break; case OP_RETURN: s->pc = s->frame->ret_addr - 1; s->prot = s->frame->func->prot; func = s->frame->func; s->stack[s->frame->stack_top] = *STK(-1); s->stack_top = s->frame->stack_top + 1; s->frame_top--; s->frame = FRAME(); break; case OP_TCALL: s->pc = s->frame->ret_addr - 1; s->prot = s->frame->func->prot; func = s->frame->func; memmove(&s->stack[s->frame->stack_top], &s->stack[s->stack_top - (inst.a + 1)], sizeof(value_t) * (inst.a + 1)); s->stack_top = s->frame->stack_top + inst.a + 1; s->frame_top--; s->frame = FRAME(); /* Do a normal call. */ case OP_CALL: tmp = s->stack_top - inst.a - 1; switch (s->stack[tmp].type) { case SU_FUNCTION: s->frame = &s->frames[s->frame_top++]; assert(s->frame_top <= MAX_CALLS); s->frame->ret_addr = s->pc + 1; s->frame->func = func; s->frame->stack_top = tmp; func = s->stack[tmp].obj.func; if (func->narg < 0) su_vector(s, inst.a); else if (func->narg != inst.a) su_error(s, "Bad number of arguments to function! Expected %i, but got %i.", (int)func->narg, (int)inst.a); s->prot = func->prot; s->pc = -1; break; case SU_NATIVEFUNC: narg = s->narg; s->narg = inst.a; if (s->stack[tmp].obj.nfunc(s, inst.a)) { s->stack[tmp] = *STK(-1); } else { s->stack[tmp].type = SU_NIL; } s->stack_top = tmp + 1; s->narg = narg; break; case SU_VECTOR: if (inst.a == 1) { su_check_type(s, -1, SU_NUMBER); tmpv = vector_index(s, s->stack[tmp].obj.vec, su_tointeger(s, -1)); su_pop(s, 2); push_value(s, &tmpv); } else { for (i = -inst.a, j = 0; i; i++, j++) { su_check_type(s, i - j, SU_NUMBER); tmpv = vector_index(s, s->stack[tmp].obj.vec, su_tointeger(s, i - j)); push_value(s, &tmpv); } su_vector(s, inst.a); s->stack[tmp] = s->stack[s->stack_top - 1]; s->stack_top -= inst.a + 1; } break; case SU_MAP: if (inst.a == 1) { tmpv2 = *STK(-1); tmpv = map_get(s, s->stack[tmp].obj.m, &tmpv2, hash_value(&tmpv2)); su_assert(s, tmpv.type != SU_INV, "No value with key: %s", stringify(s, &tmpv2)); su_pop(s, 2); push_value(s, &tmpv); } else { for (i = -inst.a, j = 0; i; i++, j += 2) { tmpv2 = *STK(i - j); push_value(s, &tmpv2); tmpv = map_get(s, s->stack[tmp].obj.m, &tmpv2, hash_value(&tmpv2)); su_assert(s, tmpv.type != SU_INV, "No value with key: %s", stringify(s, &tmpv2)); push_value(s, &tmpv); } su_map(s, inst.a); s->stack[tmp] = s->stack[s->stack_top - 1]; s->stack_top -= inst.a + 1; } break; case SU_STRING: if (inst.a == 1) { su_check_type(s, -1, SU_NUMBER); j = su_tointeger(s, -1); su_assert(s, j < s->stack[tmp].obj.str->size, "Out of range!"); s->scratch_pad[0] = s->stack[tmp].obj.str->str[j]; su_pop(s, 2); su_pushbytes(s, s->scratch_pad, 1); } else { k = 0; for (i = -inst.a; i; i++) { su_check_type(s, i, SU_NUMBER); j = su_tointeger(s, i); su_assert(s, j < s->stack[tmp].obj.str->size, "Out of range!"); s->scratch_pad[k++] = s->stack[tmp].obj.str->str[j]; assert(k < SU_SCRATCHPAD_SIZE); } su_pushbytes(s, s->scratch_pad, k); s->stack[tmp] = s->stack[s->stack_top - 1]; s->stack_top -= inst.a + 1; } break; case SU_NATIVEDATA: tmpv = s->stack[tmp]; if (tmpv.obj.data->vt && tmpv.obj.data->vt->call) { narg = s->narg; s->narg = inst.a; if (tmpv.obj.data->vt->call(s, (void*)tmpv.obj.data->data, inst.a)) s->stack[tmp] = *STK(-1); else s->stack[tmp].type = SU_NIL; s->stack_top = tmp + 1; s->narg = narg; break; } default: if (inst.a == 1 && isseq(s, &s->stack[tmp])) { su_check_type(s, -1, SU_STRING); tmpcs = su_tostring(s, -1, NULL); if (!strcmp(tmpcs, "first")) { s->stack[(--s->stack_top) - 1] = seq_first(s, STK(-1)->obj.q); break; } else if (!strcmp(tmpcs, "rest")) { s->stack[(--s->stack_top) - 1] = seq_rest(s, STK(-1)->obj.q); break; } } su_error(s, "Can't apply '%s'.", type_name(s->stack[tmp].type)); } break; case OP_LAMBDA: assert(inst.a < s->prot->num_prot); lambda(s, &s->prot->prot[inst.a], inst.b); break; case OP_GETGLOBAL: tmpv = func->constants[inst.a]; su_assert(s, tmpv.type == SU_STRING, "Global key must be a string!"); tmpv = map_get(s, unref_local(s, s->stack[SU_GLOBAL_INDEX].obj.loc).obj.m, &tmpv, hash_value(&tmpv)); if (tmpv.type == SU_INV) global_error(s, "Undefined global variable", &func->constants[inst.a]); push_value(s, &tmpv); break; case OP_SETGLOBAL: tmpv = func->constants[inst.a]; su_assert(s, tmpv.type == SU_STRING, "Global key must be a string!"); i = hash_value(&tmpv); tmpv2 = unref_local(s, s->stack[SU_GLOBAL_INDEX].obj.loc); tmpv = map_insert(s, tmpv2.obj.m, &tmpv, i, STK(-1)); set_local(s, s->stack[SU_GLOBAL_INDEX].obj.loc, &tmpv); break; case OP_SHIFT: s->stack[s->stack_top - (inst.a + 1)] = *STK(-1); s->stack_top -= inst.a; break; case OP_LOAD: assert(FRAME()->stack_top + inst.a < s->stack_top); push_value(s, &s->stack[FRAME()->stack_top + inst.a]); break; case OP_LUP: assert(inst.a < func->num_ups); push_value(s, &func->upvalues[inst.a]); break; case OP_LCL: assert(inst.b < s->msi->num_c_lambdas); push_value(s, &s->msi->c_lambdas[inst.b]); break; default: assert(0); } #undef ARITH_OP #undef LOG_OP } }
int call_group_color(sip_call_group_t *group, sip_call_t *call) { return (vector_index(group->calls, call) % 7) + 1; }
int call_group_exists(sip_call_group_t *group, sip_call_t *call) { return (vector_index(group->calls, call) >= 0) ? 1 : 0; }
bool sip_call_is_active(sip_call_t *call) { return vector_index(calls.active, call) != -1; }
void call_list_draw_list(PANEL *panel) { WINDOW *win; int height, width, cline = 0; struct sip_call *call; int i, collen; char coltext[256]; int colid; int colpos; int color; // Get panel info call_list_info_t *info = call_list_info(panel); // Get window of call list panel win = info->list_win; getmaxyx(win, height, width); // If autoscroll is enabled, select the last dialog if (info->autoscroll) { call_list_handle_key(panel, key_action_key(ACTION_END)); } // If no active call, use the fist one (if exists) if (info->first_call == -1 && vector_iterator_count(&info->calls)) { vector_iterator_reset(&info->calls); call = vector_iterator_next(&info->calls); info->cur_call = info->first_call = vector_index(vector_iterator_vector(&info->calls), call); info->cur_line = info->first_line = 1; } // Clear call list before redrawing werase(win); // Set the iterator position to the first call vector_iterator_set_current(&info->calls, info->first_call - 1 ); // Fill the call list while ((call = vector_iterator_next(&info->calls))) { // Stop if we have reached the bottom of the list if (cline == height) break; // We only print calls with messages (In fact, all call should have msgs) if (!call_msg_count(call)) continue; // Show bold selected rows if (call_group_exists(info->group, call)) wattron(win, A_BOLD | COLOR_PAIR(CP_DEFAULT)); // Highlight active call if (call->index == info->cur_call + 1) { wattron(win, COLOR_PAIR(CP_WHITE_ON_BLUE)); // Reverse colors on monochrome terminals if (!has_colors()) wattron(win, A_REVERSE); } // Set current line background clear_line(win, cline); // Set current line selection box mvwprintw(win, cline, 2, call_group_exists(info->group, call) ? "[*]" : "[ ]"); // Print requested columns colpos = 6; for (i = 0; i < info->columncnt; i++) { // Get current column id colid = info->columns[i].id; // Get current column width collen = info->columns[i].width; // Check if next column fits on window width if (colpos + collen >= width) break; // Initialize column text memset(coltext, 0, sizeof(coltext)); // Get call attribute for current column if (!call_get_attribute(call, colid, coltext)) { colpos += collen + 1; continue; } // Enable attribute color (if not current one) color = 0; if (call->index != info->cur_call + 1 && (color = sip_attr_get_color(colid, coltext)) > 0) wattron(win, color); // Add the column text to the existing columns mvwprintw(win, cline, colpos, "%.*s", collen, coltext); colpos += collen + 1; // Disable attribute color if (color > 0) wattroff(win, color); } cline++; wattroff(win, COLOR_PAIR(CP_DEFAULT)); wattroff(win, COLOR_PAIR(CP_DEF_ON_BLUE)); wattroff(win, A_BOLD | A_REVERSE); } // Draw scrollbar to the right draw_vscrollbar(win, info->first_line, info->dispcallcnt, 1); wnoutrefresh(info->list_win); }
int main(int argc, char **argv) { vector *v; string *s; string **tmp; string *sup; int i; v = vector_init(stcmp); sup = string_init_cstring("6"); while(--argc) { s = string_init_cstring(argv[argc]); vector_append(v, s); } printf("size: %d\n", v->len); printf("---\n"); /* Print. */ for(i = 0; i < v->len; ++i) string_println(vector_get(v, i)); printf("---\n"); /* Sort and print again. */ vector_sort(v); for(i = 0; i < v->len; ++i) string_println(vector_get(v, i)); printf("---\n"); /* Search for sup. */ if(vector_index(v, sup) != -1) string_print(sup), printf(" found.\n"); else string_print(sup), printf(" not found.\n"); printf("---\n"); /* Using bsearch. */ tmp = vector_search(v, sup); if(tmp) { string_print(*tmp); printf(" found using bsearch.\n"); } else { string_print(sup); printf(" bsearch failed.\n"); } printf("---\n"); /* Shuffle and print again. */ vector_shuffle(v); for(i = 0; i < v->len; ++i) string_println(vector_get(v, i)); printf("---\n"); for(i = 0; i < v->len; ++i) string_free(vector_get(v, i)); string_free(sup); vector_free(v); return 0; }