EXPORT void set_node_doubly_linked_list( INTERFACE *intfc) { NODE **n; n = intfc->nodes; first_node(intfc) = *n; prev_node(*n) = NULL; for (; *(n+1); ++n) { next_node(*n) = *(n+1); prev_node(*(n+1)) = (*n); } last_node(intfc) = *n; next_node(*n) = NULL; } /*end set_node_doubly_linked_list*/
SparseNode crm114__list_search(unsigned int c, SparseNode init, SparseElementList *l) { SparseNode curr = init; if (!l) { if (CRM114__MATR_DEBUG_MODE) { fprintf(stderr, "crm114__list_search: null list.\n"); } return make_null_node(l->compact); } if (crm114__list_is_empty(l)) { return make_null_node(l->compact); } if (c <= node_col(l->head)) { return l->head; } if (c >= node_col(l->tail)) { return l->tail; } while (!null_node(curr) && node_col(curr) < c) { curr = next_node(curr); } while (!null_node(curr) && node_col(curr) > c) { curr = prev_node(curr); } return curr; }
EXPORT void print_linked_node_list( INTERFACE *intfc) { NODE **n, *m; int i, node_count; n = intfc->nodes; if (! n) { (void) printf("NULL node list on intfc\n"); return; } (void) printf("\tnode list - intfc %llu\n",(long long unsigned int)interface_number(intfc)); for (node_count = 0; n && *n; ++n, ++node_count) ; m = first_node(intfc); for (i = 0; i <= node_count + 2; ++i) { if (m != NULL) { (void) printf("prev %llu m %llu next %llu ", (long long unsigned int)node_number(prev_node(m)), (long long unsigned int)node_number(m), (long long unsigned int)node_number(next_node(m))); print_propagation_status(m); } else break; m = next_node(m); } } /*end print_linked_node_list*/
ScmDictEntry *Scm_TreeIterPrev(ScmTreeIter *iter) { if (iter->at_end) return NULL; if (iter->e) { iter->e = (ScmDictEntry*)prev_node((Node*)iter->e); } else { iter->e = Scm_TreeCoreGetBound(iter->t, SCM_TREE_CORE_MAX); } if (iter->e == NULL) iter->at_end = TRUE; return iter->e; }
void crm114__list_remove_elt(SparseElementList *l, SparseNode toremove) { if (!l) { if (CRM114__MATR_DEBUG_MODE) { fprintf(stderr, "crm114__list_remove_elt: null list.\n"); } return; } if (null_node(toremove)) { return; } if (!null_node(prev_node(toremove))) { if (l->compact) { toremove.compact->prev->next = toremove.compact->next; } else { toremove.precise->prev->next = toremove.precise->next; } } else { if (l->compact) { l->head.compact = toremove.compact->next; } else { l->head.precise = toremove.precise->next; } } if (!null_node(next_node(toremove))) { if (l->compact) { toremove.compact->next->prev = toremove.compact->prev; } else { toremove.precise->next->prev = toremove.precise->prev; } } else { if (l->compact) { l->tail.compact = toremove.compact->prev; } else { l->tail.precise = toremove.precise->prev; } } if (l->compact) { if (!(l->last_addr) || (void *)toremove.compact < (void *)l || (void *)toremove.compact >= l->last_addr) { node_free(toremove); } } else { if (!(l->last_addr) || (void *)toremove.precise < (void *)l || (void *)toremove.precise >= l->last_addr) { node_free(toremove); } } }
static Node *delete_node(ScmTreeCore *tc, Node *n) { while (n->left && n->right) { /* N has both children. We swap N and its previous node. It would be easier just to swap key and value, but it could lead to a hard-to-track bug if a pointer to N is retained in somewhere (e.g. iterator). So we actually swap the node. */ swap_node(tc, n, prev_node(n)); } /* we have at most one child */ if (n->left) { delete_node1(tc, n, n->left); } else { delete_node1(tc, n, n->right); /* this covers no child case */ } clear_node(n); return n; }
static int handle_input(int ch) { switch (ch) { case 'q': quit_mode = quit_mode ? 0 : 1; return 1; case 0x1b: quit_mode = 0; print_help = 0; return 1; case 'y': if (quit_mode) exit(0); break; case 'n': if (quit_mode) { quit_mode = 0; return 1; } break; case 'f': fold(); return 1; case 12: case KEY_CLEAR: #ifdef HAVE_REDRAWWIN redrawwin(stdscr); #endif clear(); return 1; case 'c': c_combined_node_list = c_combined_node_list ? 0 : 1; return 1; case 'S': clear(); set_x_unit("s", 1); return 1; case 'M': clear(); set_x_unit("m", 1); return 1; case 'H': clear(); set_x_unit("h", 1); return 1; case 'D': clear(); set_x_unit("d", 1); return 1; case 'R': clear(); set_x_unit("r", 1); return 1; case '?': clear(); print_help = print_help ? 0 : 1; return 1; case 'g': c_graphical_in_list = c_graphical_in_list ? 0 : 1; return 1; case 'd': c_detailed_in_list = c_detailed_in_list ? 0 : 1; return 1; case 'l': c_list_in_list = c_list_in_list ? 0 : 1; return 1; case KEY_LEFT: prev_node(); return 1; case KEY_RIGHT: next_node(); return 1; case KEY_DOWN: if (next_intf() == END_OF_LIST) { if (next_node() != END_OF_LIST) first_intf(); } return 1; case KEY_UP: if (prev_intf() == END_OF_LIST) { if (prev_node() != END_OF_LIST) last_intf(); } return 1; default: if (get_current_intf()) if (handle_bindings(ch, get_current_intf()->i_name)) return 1; break; } return 0; }
/* accessor */ Node *core_ref(ScmTreeCore *tc, intptr_t key, enum TreeOp op, Node **lo, Node **hi) { Node *e = ROOT(tc), *n = NULL; if (e == NULL) { /* Tree is empty */ if (op == TREE_CREATE) { n = new_node(NULL, key); PAINT(n, BLACK); SET_ROOT(tc, n); tc->num_entries++; } if (op == TREE_NEAR) { *lo = *hi = NULL; } return n; } for (;;) { int r = 0; if (tc->cmp) r = tc->cmp(tc, e->key, key); if (tc->cmp? (r == 0) : (e->key == key)) { /* Exact match */ if (op == TREE_DELETE) { n = delete_node(tc, e); tc->num_entries--; return n; } if (op == TREE_NEAR) { *lo = prev_node(e); *hi = next_node(e); } return e; } if (tc->cmp? (r < 0) : (e->key < key)) { /* Key is larger than E */ if (e->right) { e = e->right; } else { if (op == TREE_CREATE) { n = new_node(e, key); e->right = n; balance_tree(tc, n); tc->num_entries++; return n; } if (op == TREE_NEAR) { *lo = e; *hi = next_node(e); } return NULL; } } else { /* Key is smaller than E */ if (e->left) { e = e->left; } else { if (op == TREE_CREATE) { n = new_node(e, key); e->left = n; balance_tree(tc, n); tc->num_entries++; return n; } if (op == TREE_NEAR) { *hi = e; *lo = prev_node(e); } return NULL; } } } }
std::vector<double> Graph::dijkstra(int source) { // stores the distance from source to each node std::vector<double> distances(adjList.size()); // stores which nodes have been looked at std::vector<bool> visited_nodes(adjList.size()); // stores the pervious node of each node in the optimal path std::vector<int> prev_node(adjList.size()); // the compare function for the priority queue, the nodes are // added to the queue along with there distance from the source // and should be sorted by that distance auto compare = [&] (std::pair<int, double> &n1, std::pair<int, double> &n2) { if (n1.second == -1) { return false; } if (n2.second == -1) { return true; } return n1.second < n2.second; }; std::vector< std::pair<int, double> > node_queue(adjList.size()); // initialize distance to every node to infitity (-1 in this case // since negative edge lengths arent used in this graph. add add every // node to the queue of nodes for (unsigned int i = 0; i < adjList.size(); i++) { // the distance to the source item should be 0 if (i == source) { distances[i] = 0; } else { distances[i] = -1; prev_node[i] = -1; } node_queue[i] = (std::make_pair(i, distances[i])); visited_nodes[i] = false; } // std::priority_queue does not have the ability to decrease the priority // of an item, so we will use a simple array for this and just make it a heap std::sort(node_queue.begin(), node_queue.end(), compare); while (node_queue.size() != 0) { // get the item in node_queue with the smallest distance std::pair<int, double> node_pair = node_queue.front(); node_queue.erase(node_queue.begin()); int node1 = node_pair.first; double dist = node_pair.second; // mark this node as visited visited_nodes[node1] = true; // loop through every neighbor of this node for (auto &edge : neighbors(node1)) { int node2 = edge.dest; // only look at this neighbor if it hasnt been looked at yet if (visited_nodes[node2] == false) { double temp_dist = distances[node1] + edge.cost; // if this distance is smaller than our current best guess, update it if (temp_dist < distances[node2] || distances[node2] < 0) { distances[node2] = temp_dist; prev_node[node2] = node1; auto it = std::find_if(node_queue.begin(), node_queue.end(), [&] (const std::pair<int, double> &n) { return n.first == node2; }); // update the priority for this node, this check of < 0 must // be done because -1 is used to represent a priority of infinity if (it->second < 0) { it->second = temp_dist; } else { it->second -= temp_dist; } // make sure the node_queue vector continues to act like a queue std::sort(node_queue.begin(), node_queue.end(), compare); } } } } return distances; }
Self &operator--(int) { Self tmp = *this; node = const_cast<TreeNode *>(prev_node(node)); return tmp; }
Self &operator--() { node = const_cast<TreeNode *>(prev_node(node)); return *this; }
Self operator--(int) { Self tmp = *this; node = prev_node(node); return tmp; }
Self &operator--() { node = prev_node(node); return *this; }
EXPORT NODE *reorder_node_loop( NODE *oldn, NODE *newn) { INTERFACE *intfc; NODE *next, *prev; NODE *next_old_node; if (oldn != NULL) { intfc = oldn->interface; next = next_node(oldn); if (next == NULL) { screen("ERROR in reorder_node_loop(), " "next_node(oldn) == NULL\n"); print_node(oldn); (void) printf("Old interface information\n"); print_linked_node_list(intfc); print_interface(intfc); if (newn != NULL) { intfc = newn->interface; (void) printf("New interface information\n"); print_linked_node_list(intfc); print_interface(intfc); } clean_up(ERROR); } prev = prev_node(oldn); prev_node(next) = prev; if (prev != NULL) next_node(prev) = next; else first_node(intfc) = next; next_node(last_node(intfc)) = oldn; next_node(oldn) = NULL; prev_node(oldn) = last_node(intfc); last_node(intfc) = oldn; next_old_node = next; } else next_old_node = NULL; if (newn != NULL) { intfc = newn->interface; next = next_node(newn); if (next == NULL) { screen("ERROR in reorder_node_loop(), " "next_node(newn) == NULL\n"); print_node(newn); if (oldn != NULL) { (void) printf("Old interface information\n"); print_linked_node_list(intfc); print_interface(intfc); } intfc = newn->interface; (void) printf("New interface information\n"); print_linked_node_list(intfc); print_interface(intfc); clean_up(ERROR); } prev = prev_node(newn); prev_node(next) = prev; if (prev != NULL) next_node(prev) = next; else first_node(intfc) = next; next_node(last_node(intfc)) = newn; next_node(newn) = NULL; prev_node(newn) = last_node(intfc); last_node(intfc) = newn; } return next_old_node; } /*end reorder_node_loop*/
static int handle_input(int ch) { switch (ch) { case 'q': quit_mode = quit_mode ? 0 : 1; return 1; case 0x1b: quit_mode = 0; print_help = 0; return 1; case 'y': if (quit_mode) exit(0); break; case 'a': next_attr(); return 1; case 'n': if (quit_mode) quit_mode = 0; else new_graph(); return 1; case 'x': del_graph(); return 1; case 'f': fold(); return 1; case 12: case KEY_CLEAR: #ifdef HAVE_REDRAWWIN redrawwin(stdscr); #endif clear(); return 1; case 'c': c_combined_node_list = c_combined_node_list ? 0 : 1; return 1; case 'S': clear(); set_graph_unit(X_SEC); return 1; case 'M': clear(); set_graph_unit(X_MIN); return 1; case 'H': clear(); set_graph_unit(X_HOUR); return 1; case 'D': clear(); set_graph_unit(X_DAY); return 1; case 'R': clear(); set_graph_unit(X_READ); return 1; case '?': clear(); print_help = print_help ? 0 : 1; return 1; case 'g': c_graphical_in_list = c_graphical_in_list ? 0 : 1; return 1; case 'd': c_detailed_in_list = c_detailed_in_list ? 0 : 1; return 1; case 'l': c_list_in_list = c_list_in_list ? 0 : 1; return 1; case KEY_PPAGE: if (print_help) help_page = help_page ? 0 : 1; else prev_node(); return 1; case KEY_NPAGE: if (print_help) help_page = help_page ? 0 : 1; else next_node(); return 1; case KEY_DOWN: if (next_item() == END_OF_LIST) { if (next_node() != END_OF_LIST) first_item(); } return 1; case KEY_UP: if (prev_item() == END_OF_LIST) { if (prev_node() != END_OF_LIST) last_item(); } return 1; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': goto_item(ch - 48); return 1; case '>': next_graph(); return 1; case '<': prev_graph(); return 1; default: if (get_current_item()) if (handle_bindings(ch, get_current_item()->i_name)) return 1; break; } return 0; }