void BankRBTree::existRecommend(const string& oid, RecommendId& id_container) { id_container.clear(); id_container.reserve(10); vector< pair<int, string> > score_id; int score; string id; //construct traverser rb_traverser bank_traverser; rb_t_init(&bank_traverser, rb_tree); //traverse all rb_tree DataNode* res = (DataNode*)rb_t_first(&bank_traverser, rb_tree); while(res != NULL){ id = (*(res->first)); score = computeScoring(oid, id); score_id.push_back(make_pair(score, id)); res = (DataNode*)rb_t_next(&bank_traverser); } int i = 0, j, selected_index; while (i < 10 && i < score_id.size()) { score = score_id[i].first; id = score_id[i].second; selected_index = i; for (j = i; j < score_id.size(); ++j) { if (score_id[j].first < score) { score = score_id[j].first; id = score_id[j].second; selected_index = j; } else if (score_id[j].first == score && id.compare(score_id[j].second) > 0) { score = score_id[j].first; id = score_id[j].second; selected_index = j; } } std::swap(score_id[i], score_id[selected_index]); id_container.push_back(score_id[i].second); i++; } }
void BankRBTree::findAccount(const string& reg_exp){ RecommendId vec; vec.clear(); //construct traverser rb_traverser bank_traverser; rb_t_init(&bank_traverser, rb_tree); //traverse all rb_tree DataNode* res = (DataNode*)rb_t_first(&bank_traverser, rb_tree); while(res != NULL){ if (wildcmp(reg_exp.c_str(), (*(res->first)).c_str()) && (*(res->first)) != current_login_user){ vec.push_back(*(res->first)); cout << (*(res->first)) << "\n"; } res = (DataNode*)rb_t_next(&bank_traverser); } if (vec.size() > 0) { sort(vec.begin(), vec.end()); cout << vec[0]; for (int i = 1; i < vec.size(); ++i) cout << "," << vec[i]; } cout << "\n"; }
/* Checks that |tree| is well-formed and verifies that the values in |array[]| are actually in |tree|. There must be |n| elements in |array[]| and |tree|. Returns nonzero only if no errors detected. */ static int verify_tree (struct rb_table *tree, int array[], size_t n) { int okay = 1; /* Check |tree|'s bst_count against that supplied. */ if (rb_count (tree) != n) { printf (" Tree count is %lu, but should be %lu.\n", (unsigned long) rb_count (tree), (unsigned long) n); okay = 0; } if (okay) { if (tree->rb_root != NULL && tree->rb_root->rb_color != RB_BLACK) { printf (" Tree's root is not black.\n"); okay = 0; } } if (okay) { /* Recursively verify tree structure. */ size_t count; int bh; recurse_verify_tree (tree->rb_root, &okay, &count, 0, INT_MAX, &bh); if (count != n) { printf (" Tree has %lu nodes, but should have %lu.\n", (unsigned long) count, (unsigned long) n); okay = 0; } } if (okay) { /* Check that all the values in |array[]| are in |tree|. */ size_t i; for (i = 0; i < n; i++) if (rb_find (tree, &array[i]) == NULL) { printf (" Tree does not contain expected value %d.\n", array[i]); okay = 0; } } if (okay) { /* Check that |rb_t_first()| and |rb_t_next()| work properly. */ struct rb_traverser trav; size_t i; int prev = -1; int *item; for (i = 0, item = rb_t_first (&trav, tree); i < 2 * n && item != NULL; i++, item = rb_t_next (&trav)) { if (*item <= prev) { printf (" Tree out of order: %d follows %d in traversal\n", *item, prev); okay = 0; } prev = *item; } if (i != n) { printf (" Tree should have %lu items, but has %lu in traversal\n", (unsigned long) n, (unsigned long) i); okay = 0; } } if (okay) { /* Check that |rb_t_last()| and |rb_t_prev()| work properly. */ struct rb_traverser trav; size_t i; int next = INT_MAX; int *item; for (i = 0, item = rb_t_last (&trav, tree); i < 2 * n && item != NULL; i++, item = rb_t_prev (&trav)) { if (*item >= next) { printf (" Tree out of order: %d precedes %d in traversal\n", *item, next); okay = 0; } next = *item; } if (i != n) { printf (" Tree should have %lu items, but has %lu in reverse\n", (unsigned long) n, (unsigned long) i); okay = 0; } } if (okay) { /* Check that |rb_t_init()| works properly. */ struct rb_traverser init, first, last; int *cur, *prev, *next; rb_t_init (&init, tree); rb_t_first (&first, tree); rb_t_last (&last, tree); cur = rb_t_cur (&init); if (cur != NULL) { printf (" Inited traverser should be null, but is actually %d.\n", *cur); okay = 0; } next = rb_t_next (&init); if (next != rb_t_cur (&first)) { printf (" Next after null should be %d, but is actually %d.\n", *(int *) rb_t_cur (&first), *next); okay = 0; } rb_t_prev (&init); prev = rb_t_prev (&init); if (prev != rb_t_cur (&last)) { printf (" Previous before null should be %d, but is actually %d.\n", *(int *) rb_t_cur (&last), *prev); okay = 0; } rb_t_next (&init); } return okay; }