// maps:put/3 [21] term_t cbif_put3(proc_t *proc, term_t *regs) { term_t Key = regs[0]; term_t Value = regs[1]; term_t Map = regs[2]; if (!is_boxed_map(Map)) badarg(Map); t_map_t *m0 = (t_map_t *)peel_boxed(Map); int index = map_key_index(Key, m0->keys); if (index >= 0) { // same as update/3 int size = map_size(m0); int needed = WSIZE(t_map_t) +size; uint32_t *p = heap_alloc(&proc->hp, needed); t_map_t *m1 = (t_map_t *)p; box_map(p, size, m0->keys); heap_set_top(&proc->hp, p); memcpy(m1->values, m0->values, size *sizeof(term_t)); m1->values[index] = Value; return tag_boxed(m1); } else { uint32_t *q = peel_tuple(m0->keys); int size = *q++; term_t *ks = q; term_t kvs[] = {Key,Value}; int needed = 1 +size+1 +2 +size+1; uint32_t *p = heap_alloc(&proc->hp, needed); term_t keys = tag_tuple(p); *p++ = size+1; term_t *ks1 = p; p += size+1; term_t out = tag_boxed(p); term_t *vs1 = p +WSIZE(t_map_t); box_map(p, size+1, keys); heap_set_top(&proc->hp, p); int size1 = map_merge(ks, m0->values, size, kvs, 1, ks1, vs1); assert(size1 == size+1); return out; } }
static void loop_mergeonly(TinBot* tinbot) { static const long iterations = 10000; long i = 0; hal_time time = hal_get_time(); (void)tinbot; do { map_merge(map_get_accumulated(), 4, 2, map_get_proximity()); } while (++i < iterations); time = hal_get_time() - time; sprintf(hal_get_printbuf(), "merge_only: avg over %ld iter: %.3f us/iter", iterations, time * 1000.0 /* 1000 us/ms */ / iterations); hal_print(hal_get_printbuf()); }
int main() { static const int iterations = 1000 * 1000; Map* accu; Map* prox; int i; clock_t time; accu = map_get_accumulated(); prox = map_get_proximity(); map_clear(accu); map_clear(prox); while (1) { time = clock(); for (i = 0; i < iterations; ++i) { map_merge(accu, 4, 2, prox); } time = clock() - time; printf("%.3f us/iter\n", time * 1000000.0 / (iterations * CLOCKS_PER_SEC)); } }
int word_seg(SenType sen, wdseg_full_type *pwdseg, wstack_type *pwstack) { int i,ic,it; char cached_found = 0; WordType tempwd; wdseg_type wdseg_cache[20]; wdseg_type temp_seg, currt_seg; int reach_sen_end = 0; char *pcurrt = NULL; int cachlen = 0; int currt_pos = 0; /* current postion in sentence */ char debug_wd[MAX_WORD_LEN*2+1]; /* init datas*/ clear_map(&temp_seg); clear_map(&currt_seg); for (it = 0; it < 20; it++) clear_map(&wdseg_cache[it]); printf("wordseg for sentence(len %d):%s \n", sen.len, sen.pdata); // show_map(&currt_seg); /* set start point of segmnt map */ pcurrt = sen.pdata; AddPoint2Map(&currt_seg, currt_pos, 0); // printf(" current_seg: \n"); // show_map(&currt_seg); for (;;) { /* step 1. scan.*/ printf("\n....... step 1 ........ \n"); /* check through cache first */ #ifdef CACHE_SUPPORT cached_found = 0; for (ic = 0; ic < cachlen; ic++) { if (point_in_map(currt_pos, &wdseg_cache[ic])) { printf("cached found!\n"); cached_found++; map_merge(&currt_seg, &wdseg_cache[ic], currt_pos); wdseg_cache[cachlen - 1 + cached_found] = currt_seg; show_map(&currt_seg); } } if (cached_found > 0) cachlen += cached_found; #endif if (0 == cached_found) { /* sen end check . */ reach_sen_end = currt_pos + MAX_WORD_LEN - sen.len/CCLEN; if ( reach_sen_end > 0 )/* reach sentence end. */ { printf("reach sen end."); i = reach_sen_end; } for (i = MAX_WORD_LEN; i > 0; i--) { /* make a word*/ tempwd.pdata = pcurrt; tempwd.len = i*CCLEN; /* to check the exist of a word in dictionary */ if (check_dict_word(&dicts[0], &tempwd, NULL)) { memcpy(debug_wd, tempwd.pdata, tempwd.len); debug_wd[tempwd.len]='\0'; printf("find in dict: len %d ,[%s] \n", tempwd.len, debug_wd); /* add new point to segmnt map */ temp_seg = currt_seg; AddPoint2Map(&temp_seg, currt_pos, tempwd.len/CCLEN); //show_map(&temp_seg); /* check sentence end, if success then cache it. */ if (segmap2len(temp_seg) == sen.len) { printf(" got a sucess: \n"); //show_map(&temp_seg); wdseg_cache[cachlen++] = temp_seg; } else { printf(" put a agenda : \n"); show_map(&temp_seg); put_agenda((wstack_item_type*)&temp_seg, pwstack); } } } } printf("\n........ step 2 ........\n"); /* step 2. get next agenda, if none, then exit. */ if (get_agenda(&currt_seg, pwstack) == -1) { printf(" got none from stack ! \n"); break; /* the end */ } printf("reset currt_seg: \n"); show_map(&currt_seg); /* sync the current data pointer and map postion*/ currt_pos = segmap2len(currt_seg)/CCLEN; pcurrt = sen.pdata + currt_pos*CCLEN; printf("currt_pos:%d, pcurrt:%s \n", currt_pos, pcurrt); } pwdseg->psegmnt = wdseg_cache; pwdseg->num = cachlen; if (cachlen > 0) return 0; /* success */ else return -1; }