/* * find the element in the tree */ BStree *find_tree(BStree *tree, item_type element) { if(tree == NULL) return NULL; else if(element == tree->element) return tree; else if(element < tree->element) return find_tree(tree->left, element); else return find_tree(tree->right, element); }
/** * find_tree * @t: tree to search in * @val: value to find * * Search for given value in the tree. I relies on fact that sorted tree is populated * by &f_val structures (that can be compared by val_compare()). In each node of tree, * either single value (then t->from==t->to) or range is present. * * Both set matching and |switch() { }| construction is implemented using this function, * thus both are as fast as they can be. */ struct f_tree * find_tree(struct f_tree *t, struct f_val val) { if (!t) return NULL; if ((val_compare(t->from, val) != 1) && (val_compare(t->to, val) != -1)) return t; if (val_compare(t->from, val) == -1) return find_tree(t->right, val); else return find_tree(t->left, val); }
int main (int argc, char *argv[]) { FILE *fpi; long pos; char *keyp; int keylen; int version, features; static unsigned char buffer[1024]; if (argc < 2) { fprintf (stderr, "missing argument\n"); exit (1); } fpi = fopen (argv[1], "r"); if (fpi == NULL) { fprintf (stderr, "Unable to open file \"%s\"\n", argv[1]); return 1; } { char *buf = malloc (4096); setbuffer (fpi, buf, 4096); } pos = trie_hdr_read (fpi, &version, &features); keyp = (argc >= 3 ? argv[2] : ""); keylen = strlen (keyp); find_tree (fpi, features, pos, buffer, buffer, keyp, keylen); return 0; }
void find_tree (FILE *fpi, int features, uint64_t pos, unsigned char *buffer, unsigned char *bp, char *keyp, int keylen) { uint64_t son, refpos, cnt; int len, end, tlen, cmp; if (!pos) return; refpos = pos; for (;;) { trie_node_read (fpi, features, &refpos, &pos, &cnt, &len, &end, &son, bp); if (!len) break; /* pos = ftell (fpi); */ tlen = (keylen < len ? keylen : len); cmp = memcmp (bp, keyp, tlen); if (cmp > 0) break; if (cmp == 0) { if (end) { fwrite (buffer, bp - buffer + len, 1, stdout); fputc ('\n', stdout); } find_tree (fpi, features, son, buffer, bp + len, keyp + tlen, keylen - tlen); } } }
static cb_ret_t tree_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data) { WDialog *h = DIALOG (w); switch (msg) { case MSG_RESIZE: { Widget *bar; /* simply call dlg_set_size() with new size */ dlg_set_size (h, LINES - 9, COLS - 20); bar = WIDGET (find_buttonbar (h)); bar->x = 0; bar->y = LINES - 1; return MSG_HANDLED; } case MSG_ACTION: return send_message (find_tree (h), NULL, MSG_ACTION, parm, NULL); default: return dlg_default_callback (w, sender, msg, parm, data); } }
/* * === FUNCTION ====================================================================== * Name: main * Description: * ===================================================================================== */ int main ( int argc, char *argv[] ) { BStree *root = NULL; printf("insert the data 1 7 4 8 3 6 5\n"); root = insert_tree(root, NULL, 2); delete_tree(root, 2); root = NULL; root = insert_tree(root,NULL, 2); insert_tree(root, NULL, 1); insert_tree(root, NULL, 7); insert_tree(root, NULL, 4); insert_tree(root, NULL, 8); insert_tree(root, NULL, 3); insert_tree(root, NULL, 6); insert_tree(root, NULL, 5); print_tree(root); printf("delete 7\n"); delete_tree(root, 7); print_tree(root); printf("delete 2\n"); delete_tree(root, 2); print_tree(root); printf("find 8\n"); printf("%d\n", find_tree(root, 8)->element); clear(root); return EXIT_SUCCESS; } /* ---------- end of function main ---------- */
/** * Get the carrier id from multiparam_t structure. * * @param mp carrier id as integer, pseudo-variable or AVP name of carrier * @param _msg SIP message * @return carrier id on success, -1 otherwise * */ int mp2carrier_id(struct sip_msg * _msg, struct multiparam_t *mp) { int carrier_id; struct usr_avp *avp; int_str avp_val; str tmp; /* TODO combine the redundant parts of the logic */ switch (mp->type) { case MP_INT: return mp->u.n; break; case MP_AVP: avp = search_first_avp(mp->u.a.flags, mp->u.a.name, &avp_val, 0); if (!avp) { LM_ERR("cannot find AVP '%d'\n", mp->u.a.name); return -1; } if ((avp->flags&AVP_VAL_STR)==0) { return avp_val.n; } else { carrier_id = find_tree(avp_val.s); if (carrier_id < 0) { LM_WARN("could not find carrier tree '%.*s'\n", avp_val.s.len, avp_val.s.s); /* might be using fallback later... */ } return carrier_id; } break; case MP_PVE: /* retrieve carrier name from parameter */ if (pv_printf_s(_msg, mp->u.p, &tmp)<0) { LM_ERR("cannot print the carrier\n"); return -1; } carrier_id = find_tree(tmp); if (carrier_id < 0) { LM_WARN("could not find carrier tree '%.*s'\n", tmp.len, tmp.s); /* might be using fallback later... */ } return carrier_id; default: LM_ERR("invalid carrier type\n"); return -1; } }
/** * fixes the module functions' parameters if it is a carrier. * supports name string, pseudo-variables and AVPs. * * @param param the parameter * * @return 0 on success, -1 on failure */ static int carrier_fixup(void ** param) { pv_spec_t avp_spec; struct multiparam_t *mp; str s; mp = (struct multiparam_t *)pkg_malloc(sizeof(struct multiparam_t)); if (mp == NULL) { LM_ERR("no more memory\n"); return -1; } memset(mp, 0, sizeof(struct multiparam_t)); s.s = (char *)(*param); s.len = strlen(s.s); if (s.s[0]!='$') { /* This is a name string */ mp->type=MP_INT; /* get carrier id */ if ((mp->u.n = find_tree(s)) < 0) { LM_ERR("could not find carrier tree '%s'\n", (char *)(*param)); pkg_free(mp); return -1; } LM_INFO("carrier tree %s has id %i\n", (char *)*param, mp->u.n); pkg_free(*param); *param = (void *)mp; } else { /* This is a pseudo-variable */ if (pv_parse_spec(&s, &avp_spec)==0) { LM_ERR("pv_parse_spec failed for '%s'\n", (char *)(*param)); pkg_free(mp); return -1; } if (avp_spec.type==PVT_AVP) { /* This is an AVP - could be an id or name */ mp->type=MP_AVP; if(pv_get_avp_name(0, &(avp_spec.pvp), &(mp->u.a.name), &(mp->u.a.flags))!=0) { LM_ERR("Invalid AVP definition <%s>\n", (char *)(*param)); pkg_free(mp); return -1; } } else { mp->type=MP_PVE; if(pv_parse_format(&s, &(mp->u.p))<0) { LM_ERR("pv_parse_format failed for '%s'\n", (char *)(*param)); pkg_free(mp); return -1; } } } *param = (void*)mp; return 0; }
void add_tree(deque<string> tag, const stu & u, const vector<int> & no, Tree & t){ for (int i = 0; i != no.size(); ++i) if (!count(t.pid[no[i]].begin(), t.pid[no[i]].end(), u.ID)) t.pid[no[i]].push_back(u.ID); if (!tag.empty()){ string v = tag.front(); tag.pop_front(); add_tree(tag, u, no, t.next[find_tree(v, t)]); } }
/** * rewrites the request URI of msg by calculating a rule, using * crc32 for hashing. The request URI is used to determine tree node * the given _tree is the used routing tree * * @param msg the current SIP message * @param _tree the routing tree to be used (string or pseudo-variable * @param _domain the requested routing domain * * @return 1 on success, -1 on failure */ int tree_route_uri(struct sip_msg * msg, char * _tree, char * _domain) { struct rewrite_data * rd = NULL; pv_elem_t *model; str carrier_name; int index; str ruser; str ruri; if (!_tree) { LM_ERR("bad parameters\n"); return -1; } if (parse_sip_msg_uri(msg) < 0) { return -1; } /* Retrieve carrier name from parameter */ model = (pv_elem_t*)_tree; if (pv_printf_s(msg, model, &carrier_name)<0) { LM_ERR("cannot print the format\n"); return -1; } if ((index = find_tree(carrier_name)) < 0) LM_WARN("could not find carrier %.*s\n", carrier_name.len, carrier_name.s); else LM_DBG("tree %.*s has id %i\n", carrier_name.len, carrier_name.s, index); ruser.s = msg->parsed_uri.user.s; ruser.len = msg->parsed_uri.user.len; ruri.s = msg->parsed_uri.user.s; ruri.len = msg->parsed_uri.user.len; do { rd = get_data(); } while (rd == NULL); if (index < 0) { if (fallback_default) { LM_NOTICE("invalid tree id %i specified, use default tree\n", index); index = rd->default_carrier_index; } else { LM_ERR("invalid tree id %i specified and fallback deactivated\n", index); release_data(rd); return -1; } } release_data(rd); return carrier_rewrite_msg(index, (int)(long)_domain, &ruri, msg, &ruser, shs_call_id, alg_crc32); }
int as_path_match_set(struct adata *path, struct f_tree *set) { u8 *p = path->data; u8 *q = p+path->length; int i, n; while (p<q) { n = p[1]; p += 2; for (i=0; i<n; i++) { struct f_val v = {T_INT, .val.i = get_as(p)}; if (find_tree(set, v)) return 1; p += BS; } }
void Analysis::link_tree() { pAssemblerTree nowtree = root; while (nowtree) { ud_t ud = nowtree->asmpiece.back(); if (ud.operand[0].type == UD_OP_JIMM && ud.mnemonic != UD_Icall) { long addr = ud.operand[0].size == 8 ? ((signed char)ud.operand[0].lval.sbyte + ud.pc) : (ud.operand[0].lval.sdword + ud.pc); pAssemblerTree tree_R= find_tree(addr); if (tree_R) { nowtree->RightChild = tree_R; nowtree->jcc_addr = addr; } } nowtree = nowtree->LeftChild; } }
static cb_ret_t tree_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data) { switch (msg) { case DLG_POST_KEY: /* The enter key will be processed by the tree widget */ if (parm == '\n') { h->ret_value = B_ENTER; dlg_stop (h); } return MSG_HANDLED; case DLG_ACTION: /* command from buttonbar */ return send_message ((Widget *) find_tree (h), WIDGET_COMMAND, parm); default: return default_dlg_callback (h, sender, msg, parm, data); } }
void find_tree (const Filename& fn_where, const std::string& what, DoStr dostr, void* from) { std::string where = fn_where.get_rootful(); // Nach Verzeichnissen suchen al_ffblk info; if (where[where.size()-1] != '/') where += '/'; std::string search_for = where + '*'; if (al_findfirst(search_for.c_str(), &info, FA_RDONLY | FA_HIDDEN | FA_LABEL | FA_DIREC | FA_ARCH) == 0) { do { // Dies nur f�r jedes Verzeichnis au�er . und .. ausf�hren: // Neue Suche mit gleichem Vektor im gefundenen Verzeichnis if ((info.attrib & FA_DIREC) == FA_DIREC && info.name[0] != '.') { Filename fn_recurs(where + info.name + '/'); find_tree(fn_recurs, what, dostr, from); } } while (al_findnext(&info) == 0); al_findclose(&info); } // Nach Dateien suchen, die dem Suchkriterium entsprechen Filename fn_where_with_slash(where); find_files(fn_where_with_slash, what, dostr, from); }
redo_trees(); return (dobj); } } } VOID open_tree(WORD obj) { WORD kind, new, where; GRECT o; LONG tree; WORD themenu, menu; tree = ad_view; select_tree(tree, trunpan_f(obj)); where = find_tree(obj - 1); kind = get_kind(where); switch (kind) { case UNKN: type_tree(obj); dselct_tree(tree, trunpan_f(obj)); return; case PANL: new = PANL_STATE; break; case DIAL: new = DIAL_STATE; break; case ALRT:
int main() { Tree *tree = createTree(); Hashtable *table5, *table50, *table500; FILE *fp50, *fp500, *fp5mil; clock_t tstart, tend; time_t t; srand((unsigned) time(&t)); double favg; int data = 0, i = 0, search, value, found; int array50k[50000], *array500k, *array5mil; array500k = malloc(500001 * sizeof(int)); array5mil = malloc(5000001 * sizeof(int)); if(tree == NULL) printf("Memory allocation failed..."); fp50 = fopen("50tus.txt", "r"); fp500 = fopen("500tus.txt", "r"); fp5mil = fopen("5mil.txt", "r"); if(fp50 == NULL || fp500 == NULL|| fp5mil == NULL) { fprintf(stderr, "error\n"); return 1; } system("color 3"); table5 = create_table(50000000); table50 = create_table(500000); table500 = create_table(5000000); /*50k*/ printf("\n50k insert, balance and search!\n"); printf("Insert: 50k "); tstart = clock(); // start for(i = 0; i < 50000; ++i) { fscanf(fp50,"%d\n", &data); insert(tree, data); } tend = clock(); // end favg = ((double)(tend - tstart))/CLOCKS_PER_SEC; printf("Avg. execution time: %g sec\n",favg); tree_to_arr(tree); printf("Searching 100x in 50k "); tstart = clock(); // start find_tree(tree); tend = clock(); // end favg = ((double)(tend - tstart))/CLOCKS_PER_SEC; printf("Avg. execution time: %g sec\n",favg); clear_tree(tree); /*500k*/ printf("\n500k insert, balance and search!\n"); printf("Insert: 500k "); tstart = clock(); // start for(i = 0; i < 500000; ++i) { fscanf(fp500,"%d\n", &data); insert(tree, data); } tend = clock(); // end favg = ((double)(tend - tstart))/CLOCKS_PER_SEC; printf("Avg. execution time: %g sec\n",favg); tree_to_arr(tree); printf("Searching 100x in 500k "); tstart = clock(); // start find_tree(tree); tend = clock(); // end favg = ((double)(tend - tstart))/CLOCKS_PER_SEC; printf("Avg. execution time: %g sec\n",favg); clear_tree(tree); /*5mille*/ printf("\n5 million insert, balance and search!\n"); printf("Insert: 5 million "); tstart = clock(); // start for(i = 0; i < 5000000; ++i) { fscanf(fp5mil,"%d\n", &data); insert(tree, data); } tend = clock(); // end favg = ((double)(tend - tstart))/CLOCKS_PER_SEC; printf("Avg. execution time: %g sec\n",favg); tree_to_arr(tree); printf("Searching 100x in 5 million "); tstart = clock(); // start find_tree(tree); tend = clock(); // end favg = ((double)(tend - tstart))/CLOCKS_PER_SEC; printf("Avg. execution time: %g sec\n",favg); clear_tree(tree); /*50k sekvens*/ printf("\nSekvential insert 50k!\n"); tstart = clock(); // start for(i = 0; i<50000; i++) { fscanf(fp50, "%d\n", &array50k[i]); } tend = clock(); // end favg = ((double)(tend - tstart))/CLOCKS_PER_SEC; printf("Avg. execution time: %g sec\n",favg); printf("Sekvential search 50k, 100x!\n"); tstart = clock(); // start for(i = 0; i<100; i++) { search = randomGen(); sekvential_search(array50k, search, 50000); } tend = clock(); // end favg = ((double)(tend - tstart))/CLOCKS_PER_SEC; printf("Avg. execution time: %g sec\n",favg); printf("Sekvential search 50k, 100x(o)!\n"); tstart = clock(); // start for(i = 0; i<100; i++) { search = randomGen(); sekvent_search(array50k, search, 50000); } tend = clock(); // end favg = ((double)(tend - tstart))/CLOCKS_PER_SEC; printf("Avg. execution time: %g sec\n",favg); /*500k sekvens*/ printf("\nSekvential insert 500k!\n"); tstart = clock(); // start for(i = 0; i<500000; i++) { fscanf(fp500, "%d\n", &array500k[i]); } tend = clock(); // end favg = ((double)(tend - tstart))/CLOCKS_PER_SEC; printf("Avg. execution time: %g sec\n",favg); printf("Sekvential search 500k, 100x!\n"); tstart = clock(); // start for(i = 0; i<100; i++) { search = randomGen(); sekvential_search(array500k, search, 500000); } tend = clock(); // end favg = ((double)(tend - tstart))/CLOCKS_PER_SEC; printf("Avg. execution time: %g sec\n",favg); printf("Sekvential search 500k, 100x(o)!\n"); tstart = clock(); // start for(i = 0; i<100; i++) { search = randomGen(); sekvent_search(array500k, search, 500000); } tend = clock(); // end favg = ((double)(tend - tstart))/CLOCKS_PER_SEC; printf("Avg. execution time: %g sec\n",favg); /*5mille seq*/ printf("\nSekvential insert 5 million!\n"); tstart = clock(); // start for(i = 0; i<5000000; i++) { fscanf(fp5mil, "%d\n", &array5mil[i]); } tend = clock(); // end favg = ((double)(tend - tstart))/CLOCKS_PER_SEC; printf("Avg. execution time: %g sec\n",favg); printf("Sekvential search 5 million, 100x!\n"); tstart = clock(); // start for(i = 0; i<100; i++) { search = randomGen(); sekvential_search(array5mil, search, 5000000); } tend = clock(); // end favg = ((double)(tend - tstart))/CLOCKS_PER_SEC; printf("Avg. execution time: %g sec\n",favg); printf("Sekvential search 5 million, 100x(o)!\n"); tstart = clock(); // start for(i = 0; i<100; i++) { search = randomGen(); sekvent_search(array5mil, search, 5000000); } tend = clock(); // end favg = ((double)(tend - tstart))/CLOCKS_PER_SEC; printf("Avg. execution time: %g sec\n",favg); /*Hash 500k*/ printf("\nHash insert 50k!\n"); tstart = clock(); // start for(i = 0; i<50000; i++){ fscanf(fp500, "%d\n", &value); insert_hash(table50, value); } tend = clock(); // end favg = ((double)(tend - tstart))/CLOCKS_PER_SEC; printf("Avg. execution time: %g sec\n",favg); /*Hash 500k*/ printf("\nHash insert 500k!\n"); tstart = clock(); // start for(i = 0; i<500000; i++){ fscanf(fp500, "%d\n", &value); insert_hash(table500, value); } tend = clock(); // end favg = ((double)(tend - tstart))/CLOCKS_PER_SEC; printf("Avg. execution time: %g sec\n",favg); /*Hash 5 mille*/ printf("\nHash insert 5 million!\n"); tstart = clock(); // start for(i = 0; i<5000000; i++){ fscanf(fp5mil, "%d\n", &value); insert_hash(table5, value); } tend = clock(); // end favg = ((double)(tend - tstart))/CLOCKS_PER_SEC; printf("Avg. execution time: %g sec\n",favg); destroy_table(table5); destroy_table(table50); destroy_table(table500); destroyTree(tree); return 0; }
int main(int argc, char *argv[]){ element_type test_array[20]; int i; BTREE T = init_tree(); TREE_NODE p; int height; for(i=0; i<20; i++){ test_array[i]= random(200); printf("%d, ", test_array[i]); } printf("\n"); T = creat_tree(test_array, sizeof(test_array)/sizeof(test_array[0]), T); height = height_recursive(T); printf("height is %d\n", height); printf("preorder:\n"); preorder_recursive(T); printf("\n"); printf("inoder:\n"); inorder_recursive(T); printf("\n"); printf("postorder:\n"); postorder_recursive(T); printf("\n"); printf("preorder nonrec:\n"); preorder_norecursive(T); printf("\n"); printf("inorder nonrec:\n"); inorder_norecursive(T); printf("\n"); printf("postorder nonrec:\n"); postorder_norcursive(T); printf("\n"); printf("levelorder:\n"); levelorder(T); printf("\n"); printf("print leaf recursive:\n"); output_leaf_recursive(T); printf("\n"); printf("print leaf norecursive\n"); output_leaf_norecursive(T); printf("\n"); printf("find an element\n"); if(find_tree(177, T) == NULL) printf("no such element\n"); else preorder_recursive(find_tree(177, T)); printf("\n"); printf("min node\n"); visit(find_min(T)->element); printf("\n"); printf("max node\n"); visit(find_max(T)->element); printf("\n"); printf("delet:\n"); tree_delete(115, T); preorder_recursive(T); printf("\n"); printf("exchange tree:\n"); exchange_recursive(T); preorder_recursive(T); printf("\n"); printf("exchange tree norecursive:\n"); exchange_norecursive(T); preorder_recursive(T); printf("\n"); free_tree(T); return 0; }
VOID view_trees(VOID) { WORD nobj, iobj; WORD kind, where, nh; LONG tree, iconad; tree = ad_pbx; rcs_work[0].ob_next = NIL; /* construct tree root */ rcs_work[0].ob_type = G_BOX; rcs_work[0].ob_flags = NONE; rcs_work[0].ob_state = NORMAL; #if TURBO_C rcs_work[0].ob_spec.index = 0x00000007L; #else rcs_work[0].ob_spec = 0x00000007L; #endif rcs_work[0].ob_x = view.g_x; rcs_work[0].ob_y = view.g_y; rcs_work[0].ob_width = view.g_w; rcs_work[0].ob_height = view.g_h; if (!LWGET(RSH_NTREE(head))) { rcs_work[0].ob_head = NIL; rcs_work[0].ob_tail = NIL; } else { nobj = min(VIEWSIZE, LWGET(RSH_NTREE(head)) - rcs_trpan); nobj = min(nobj, fit_vtrees() * (nh = fit_htrees())); rcs_work[0].ob_head = 1; /* root pointers */ rcs_work[0].ob_tail = nobj; for (iobj = 0; iobj++ < nobj; ) { if (iobj < nobj) rcs_work[iobj].ob_next = iobj + 1; else rcs_work[iobj].ob_next = ROOT; rcs_work[iobj].ob_head = NIL; rcs_work[iobj].ob_tail = NIL; rcs_work[iobj].ob_flags = NONE; rcs_work[iobj].ob_state = NORMAL; rcs_work[iobj].ob_type = G_ICON; #if TURBO_C rcs_work[iobj].ob_spec.iconblk = (ICONBLK *)ADDR(&rcs_icons[iobj]); #else rcs_work[iobj].ob_spec = ADDR(&rcs_icons[iobj]); #endif rcs_work[iobj].ob_width = ICON_W; rcs_work[iobj].ob_height = ICON_H + gl_hschar; rcs_work[iobj].ob_x = ((iobj - 1) % nh) * (ICON_W + MIN_WINT); rcs_work[iobj].ob_y = ((iobj - 1) / nh) * (ICON_H + MIN_HINT); where = find_tree(iobj + rcs_trpan - 1); kind = get_kind(where); iconad = GET_SPEC(tree, rcs_typ2icn[kind]); LBCOPY(ADDR(&rcs_icons[iobj]), iconad, sizeof(ICONBLK)); rcs_icons[iobj].ib_ptext = /* address of tree */ (char *)ADDR( get_name(where) ); rcs_icons[iobj].ib_wtext = ch_width(IBM) * (WORD)LSTRLEN(rcs_icons[iobj].ib_ptext); rcs_icons[iobj].ib_ytext = ICON_H; rcs_icons[iobj].ib_xtext = (rcs_icons[iobj].ib_wicon - rcs_icons[iobj].ib_wtext) / 2; } } }