/* open scratch buffer; initialize line queue */ bool init_buffers( void ) { /* Read stdin one character at a time to avoid i/o contention with shell escapes invoked by nonterminal input, e.g., ed - <<EOF !cat hello, world EOF */ setvbuf( stdin, 0, _IONBF, 0 ); if( !open_sbuf() ) return false; link_nodes( &buffer_head, &buffer_head ); link_nodes( &yank_buffer_head, &yank_buffer_head ); return true; }
// Consolidate a queue in amortized O(log n) static void consolidate_queue(priority_queue* q) { unsigned int length = q->length; unsigned int array_size = 2 * log(length) / log(2) + 1; priority_node* tree_by_degree[array_size]; // TODO: We need only 2log n entries unsigned int i; for (i=0; i<array_size; i++) tree_by_degree[i] = NULL; priority_node* n = NULL; while (((n = delete_first(q)))) { priority_node* n1 = NULL; while (((n1 = tree_by_degree[n->degree]))) { tree_by_degree[n->degree] = NULL; n = link_nodes(q, n, n1); } tree_by_degree[n->degree] = n; } // Find minimum value in O(log n) // Only if we shorten the array q->rootlist = NULL; q->min = NULL; for (i=0; i<array_size; i++) { if (tree_by_degree[i] != NULL) { meld_queue(q, tree_by_degree[i], 0); } } q->length = length; }
void nodes::set_nodes(int part_) { if (part_ == 0) { //type 0 = nodo rojo, 1 nodo azul, 2 estacion espacial Node* n1 = add_new_node(2,184,311); Node* n2 = add_new_node(0,167,157,n1); Node* n3 = add_new_node(0,348,53,n2); Node* n4 = add_new_node(0,348,259,n2); Node* n5 = add_new_node(0,450,157,n4); link_nodes(n5,n3); Node* n6 = add_new_node(0,638,107,n5); //Node* n7 = add_new_node(0,885,58,n6); //Node* n8 = add_new_node(0,845,345,n6); //Node* n9 = add_new_node(0,597,542,n8); //Node* n10 = add_new_node(0,807,536,n6); //Node* n11 = add_new_node(0,475,280,n9); Node* n12 = add_new_node(0,426,590,n6); Node* n13 = add_new_node(0,278,439,n12); Node* n14 = add_new_node(0,76,568,n13); //std::vector<CL_Vec2f> res = aStarNodeVersion(n5,n1); spaceships_.push_back(spaceship()); main_station_node_ = n1; spaceships_[0].set_current_pos(main_station_node_); //spaceships_[0].create_path(n9); } }
/* move a range of lines */ bool move_lines( const int first_addr, const int second_addr, const int addr, const bool isglobal ) { line_t *b1, *a1, *b2, *a2; int n = inc_addr( second_addr ); int p = first_addr - 1; disable_interrupts(); if( addr == first_addr - 1 || addr == second_addr ) { a2 = search_line_node( n ); b2 = search_line_node( p ); current_addr_ = second_addr; } else if( !push_undo_atom( UMOV, p, n ) || !push_undo_atom( UMOV, addr, inc_addr( addr ) ) ) { enable_interrupts(); return false; } else { a1 = search_line_node( n ); if( addr < first_addr ) { b1 = search_line_node( p ); b2 = search_line_node( addr ); /* this search_line_node last! */ } else { b2 = search_line_node( addr ); b1 = search_line_node( p ); /* this search_line_node last! */ } a2 = b2->q_forw; link_nodes( b2, b1->q_forw ); link_nodes( a1->q_back, a2 ); link_nodes( b1, a1 ); current_addr_ = addr + ( ( addr < first_addr ) ? second_addr - first_addr + 1 : 0 ); } if( isglobal ) unset_active_nodes( b2->q_forw, a2 ); modified_ = true; enable_interrupts(); return true; }
/** Consolidates heap */ void consolidate() { fibonacci_heap_node *x, *y, *w; int d; for(int i = 0; i < Dn; i++) { A[i] = NULL; } min_root->left->right = NULL; min_root->left = NULL; w = min_root; do { x = w; d = x->rank; w = w->right; while(A[d] != NULL) { y = A[d]; if(y->key < x->key) { fibonacci_heap_node *temp; temp = y; y = x; x = temp; } link_nodes(y, x); A[d] = NULL; d++; } A[d] = x; } while(w != NULL); min_root = NULL; num_trees = 0; for(int i = 0; i < Dn; i++) { if(A[i] != NULL) { A[i]->marked = false; add_to_roots(A[i]); } } }
static void clear_yank_buffer( void ) { line_t * lp = yank_buffer_head.q_forw; disable_interrupts(); while( lp != &yank_buffer_head ) { line_t * const p = lp->q_forw; link_nodes( lp->q_back, lp->q_forw ); free( lp ); lp = p; } enable_interrupts(); }
result_t insert_at_end(list_t *list, data_t data) { node_t *last_node, *new_node; if(NULL_CHECK(list)) { return (ERROR); } last_node = get_last_node(list); new_node = get_node(data); link_nodes(last_node, new_node); return (SUCCESS); }
result_t insert_at_begin(list_t *list, data_t data) { node_t *head, *new_node; if(NULL_CHECK(list)) { return (ERROR); } head = list; new_node = get_node(data); link_nodes(head, new_node); return (SUCCESS); }
/* delete a range of lines */ bool delete_lines( const int from, const int to, const bool isglobal ) { line_t *n, *p; if( !yank_lines( from, to ) ) return false; disable_interrupts(); if( !push_undo_atom( UDEL, from, to ) ) { enable_interrupts(); return false; } n = search_line_node( inc_addr( to ) ); p = search_line_node( from - 1 ); /* this search_line_node last! */ if( isglobal ) unset_active_nodes( p->q_forw, n ); link_nodes( p, n ); last_addr_ -= to - from + 1; current_addr_ = from - 1; modified_ = true; enable_interrupts(); return true; }
int read_file(FILE * file, node_t * array[], int max_nodes) { char line[max_nodes]; int count = 0; while (fgets(line, MAX_LENGTH, file)) { node_t * node = (node_t*)malloc(sizeof(node_t)); //parse node and check that it was properly created if(parse_input_line(line, count, node) == 1){ array[count] = node; count++; } else { perror("Could not produce node from line"); return -1; } } link_nodes(array, count); return count; }
// link two binomial heaps static priority_node* link_nodes(priority_queue* q, priority_node* b1, priority_node* b2) { if (q->compare_function(b2->priority, b1->priority) < 0) return link_nodes(q, b2, b1); b2->parent = b1; priority_node* child = b1->child; b1->child = b2; if (child) { b2->left = child->left; b2->left->right = b2; b2->right = child; b2->right->left = b2; } else { b2->left = b2; b2->right = b2; } b1->degree++; b2->mark = false; return b1; }
result_t insert_before_data(list_t *list, data_t search_key, data_t insert_key) { node_t *target; if(NULL_CHECK(list)) { return (ERROR); } target = search_node(list, search_key); if(target != NULL) { link_nodes(target -> prev, get_node(insert_key)); return (SUCCESS); } else { return (ERROR); } }
/* undo last change to the editor buffer */ bool undo( const bool isglobal ) { int n; const int o_current_addr = current_addr_; const int o_last_addr = last_addr_; const bool o_modified = modified_; if( u_ptr <= 0 || u_current_addr < 0 || u_last_addr < 0 ) { set_error_msg( "Nothing to undo" ); return false; } search_line_node( 0 ); /* reset cached value */ disable_interrupts(); for( n = u_ptr - 1; n >= 0; --n ) { switch( ustack[n].type ) { case UADD: link_nodes( ustack[n].head->q_back, ustack[n].tail->q_forw ); break; case UDEL: link_nodes( ustack[n].head->q_back, ustack[n].head ); link_nodes( ustack[n].tail, ustack[n].tail->q_forw ); break; case UMOV: case VMOV: link_nodes( ustack[n-1].head, ustack[n].head->q_forw ); link_nodes( ustack[n].tail->q_back, ustack[n-1].tail ); link_nodes( ustack[n].head, ustack[n].tail ); --n; break; } ustack[n].type ^= 1; } /* reverse undo stack order */ for( n = 0; 2 * n < u_ptr - 1; ++n ) { undo_t tmp = ustack[n]; ustack[n] = ustack[u_ptr-1-n]; ustack[u_ptr-1-n] = tmp; } if( isglobal ) clear_active_list(); current_addr_ = u_current_addr; u_current_addr = o_current_addr; last_addr_ = u_last_addr; u_last_addr = o_last_addr; modified_ = u_modified; u_modified = o_modified; enable_interrupts(); return true; }
/* insert line node into circular queue after previous */ static void insert_node( line_t * const lp, line_t * const prev ) { link_nodes( lp, prev->q_forw ); link_nodes( prev, lp ); }
t_pos *read_file(char *file) { int size, x, y, ry, p, inctile; char line[MAX_LINE_SIZE]; t_pos *ret = (t_pos *) malloc(sizeof(t_pos)); t_hex *hex = &ret->hex; t_tiles *tiles = &ret->tiles; t_done *done = &ret->done; FILE *input; tiles_init(tiles); input = fopen(file, "rb"); if (input == NULL) { free(ret); return NULL; } fgets(line, MAX_LINE_SIZE, input); sscanf(line, "%d", &size); hex_init(hex, size); done_init(done, hex->count); for (y = 0; y < size; ++y) { if (fgets(line, MAX_LINE_SIZE, input) == NULL) { printf("ERROR READING LINE %d!\n", y); free(ret); return NULL; } p = size - 1 - y; for (x = 0; x < size + y; ++x) { char *tile = line + p; p += 2; inctile = 0; if (tile[1] == '.') { inctile = 7; } else { inctile = tile[1] - '0'; } if (tile[0] == '+') { printf("Adding locked tile: %d at pos %d, %d, id=%d\n", inctile, x, y, get_by_pos(hex, make_point(x, y))->id); add_done(done, get_by_pos(hex, make_point(x, y))->id, inctile); } else { (*tiles)[inctile] += 1; } } } for (y = 1; y < size; ++y) { ry = size - 1 + y; if (fgets(line, MAX_LINE_SIZE, input) == NULL) { printf("ERROR READING LINE %d!\n", ry); free(ret); return NULL; } p = y; for (x = y; x < 2 * size - 1; ++x) { char *tile = line + p; p += 2; inctile = 0; if (tile[1] == '.') { inctile = 7; } else { inctile = tile[1] - '0'; } if (tile[0] == '+') { printf("Adding locked tile: %d at pos %d, %d, id=%d\n", inctile, x, ry, get_by_pos(hex, make_point(x, ry))->id); add_done(done, get_by_pos(hex, make_point(x, ry))->id, inctile); } else { (*tiles)[inctile] += 1; } } } fclose(input); pos_init(ret); link_nodes(hex); return ret; }