int main(void) { // char filename[] = "sudo2.csv"; // int matrix[VSIZE][HSIZE]; // read_csv(filename, matrix); int matrix[VSIZE*HSIZE] = { // sums: 8, 9, 8,11, 7,10, 8, 5, 5 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0 }; list sparse_matrix = create_sparse(VSIZE, HSIZE, matrix); list column; DEBUG_PRINT(1, "Check choosing column with min data.\n"); column = choose_column_with_min_data(sparse_matrix, VSIZE + 1); // VSIZE + 1 is an arbitrary number >= min // print_sparse_matrix_transpose(sparse_matrix, VSIZE); // printf("----------\n"); assert(get_data(column)->data == 5); // should have sum for 2nd-last column DEBUG_PRINT(1, "Check covering first column.\n"); column = get_right(sparse_matrix); cover_column(column); // new column sums: 5,1,5,5,5,2,1,2 // print_sparse_matrix_transpose(sparse_matrix, VSIZE); // printf("----------\n"); assert(get_data(get_right(sparse_matrix))->data == 5); // sum for first column assert(get_data(get_left(sparse_matrix))->data == 2); // sum for last column assert(get_data(get_right(get_right(sparse_matrix)))->data == 1); // sum for second column assert(get_data(get_left(get_left(sparse_matrix)))->data == 1); // sum for 2nd-last column DEBUG_PRINT(1, "Check uncovering first column.\n"); uncover_column(column); // print_sparse_matrix_transpose(sparse_matrix, VSIZE); assert(get_data(get_right(sparse_matrix))->data == 8); // sum for first column assert(get_data(get_left(sparse_matrix))->data == 5); // sum for last column assert(get_data(get_right(get_right(sparse_matrix)))->data == 9); // sum for second column assert(get_data(get_left(get_left(sparse_matrix)))->data == 5); // sum for 2nd-last column // printf("----------\n"); // translate_into_constraint_matrix(matrix, constraints); // translate_into_sparse_constraints(constraints, sparse); // print_array(VSIZE, HSIZE, matrix); destroy_entire_grid(sparse_matrix); DEBUG_PRINT(0, "Passed all tests!\n"); return 0; }
int get_hmiddle() const { return (get_left() + get_right()) / 2; }
void html_table::emit_col (int n) { cols *c = columns; cols *b = columns; int width = 0; // must be a different row if (last_col != NULL && n <= last_col->no) emit_new_row(); while (c != NULL && c->no < n) c = c->next; // can we find column, n? if (c != NULL && c->no == n) { // shutdown previous column if (last_col != NULL) out->put_string("</td>").nl(); // find previous column if (last_col == NULL) b = columns; else b = last_col; // have we a gap? if (last_col != NULL) { emit_td(is_gap(b), "></td>"); b = b->next; } // move across to column n while (b != c) { // we compute the difference after converting positions // to avoid rounding errors width = (get_right(b)*100 + get_effective_linelength()/2) / get_effective_linelength() - (b->left*100 + get_effective_linelength()/2) /get_effective_linelength(); emit_td(width, "></td>"); // have we a gap? emit_td(is_gap(b), "></td>"); b = b->next; } width = (get_right(b)*100 + get_effective_linelength()/2) / get_effective_linelength() - (b->left*100 + get_effective_linelength()/2) /get_effective_linelength(); switch (b->alignment) { case 'C': emit_td(width, " align=center>"); break; case 'R': emit_td(width, " align=right>"); break; default: emit_td(width); } // remember column, b last_col = b; } }
void bstree_remove(struct bstree_node *node, struct bstree *tree) { struct bstree_node *left, *right, *next; struct bstree_node fake_parent, *parent; int is_left; do_lookup(node, tree, &parent, &is_left); if (!parent) { INIT_NODE(&fake_parent); parent = &fake_parent; is_left = 0; } left = get_left(node); right = get_right(node); if (!left && !right) { if (is_left) set_prev(get_prev(node), parent); else set_next(get_next(node), parent); next = parent; goto update_first_last; } if (!left) { next = get_first(right); set_prev(get_prev(node), next); set_child(right, parent, is_left); goto update_first_last; } if (!right) { next = get_last(left); set_next(get_next(node), next); set_child(left, parent, is_left); goto update_first_last; } next = get_first(right); if (next != right) { /* 'm' is the parent of 'next' */ struct bstree_node *m = get_next(get_last(next)); if (get_right(next)) set_left(get_right(next), m); else set_prev(next, m); set_right(right, next); } set_child(next, parent, is_left); set_left(left, next); set_next(next, get_last(left)); out: if (parent == &fake_parent) tree->root = get_right(parent); return; update_first_last: if (node == tree->first) tree->first = next; if (node == tree->last) tree->last = next; goto out; }
static int do_splay(const struct splaytree_node *key, struct splaytree *tree) { struct splaytree_node subroots = NODE_INIT; struct splaytree_node *subleft = &subroots, *subright = &subroots; struct splaytree_node *root = tree->root; splaytree_cmp_fn_t cmp = tree->cmp_fn; int rv; for (;;) { rv = cmp(key, root); if (rv == 0) break; if (rv < 0) { struct splaytree_node *left; left = get_left(root); if (!left) break; if ((rv = cmp(key, left)) < 0) { rotate_right(root); root = left; left = get_left(root); if (!left) break; } /* link left */ set_left(root, subright); subright = root; root = left; } else { struct splaytree_node *right; right = get_right(root); if (!right) break; if ((rv = cmp(key, right)) > 0) { rotate_left(root); root = right; right = get_right(root); if (!right) break; } /* link right */ set_right(root, subleft); subleft = root; root = right; } } /* assemble */ if (get_left(root)) set_right(get_left(root), subleft); else set_next(root, subleft); if (get_right(root)) set_left(get_right(root), subright); else set_prev(root, subright); set_left(get_right(&subroots), root); set_right(get_left(&subroots), root); tree->root = root; return rv; }
/* * Remove node 'x' from the tree. This function shall not be called if * node 'x' is not part of the tree. */ static void remove_node(br_ssl_session_cache_lru *cc, uint32_t x) { uint32_t alx, y, aly; /* * Removal algorithm: * ------------------ * * - If we remove the root, then the tree becomes empty. * * - If the removed node has no child, then we can simply remove * it, with nothing else to do. * * - Otherwise, the removed node must be replaced by either its * rightmost left-descendent, or its leftmost right-descendent. * The replacement node itself must be removed from its current * place. By definition, that replacement node has either no * child, or at most a single child that will replace it in the * tree. */ /* * Find node back and its ancestor link. If the node was the * root, then alx is set to ADDR_NULL. */ find_node(cc, cc->store + x + SESSION_ID_OFF, &alx); /* * Find replacement node 'y', and 'aly' is set to the address of * the link to that replacement node. If the removed node has no * child, then both 'y' and 'aly' are set to ADDR_NULL. */ y = find_replacement_node(cc, x, &aly); if (y != ADDR_NULL) { uint32_t z; /* * The unlinked replacement node may have one child (but * not two) that takes its place. */ z = get_left(cc, y); if (z == ADDR_NULL) { z = get_right(cc, y); } set_link(cc, aly, z); /* * Link the replacement node in its new place, overwriting * the current link to the node 'x' (which removes 'x'). */ set_link(cc, alx, y); /* * The replacement node adopts the left and right children * of the removed node. Note that this also works even if * the replacement node was a direct descendent of the * removed node, since we unlinked it previously. */ set_left(cc, y, get_left(cc, x)); set_right(cc, y, get_right(cc, x)); } else { /* * No replacement, we simply unlink the node 'x'. */ set_link(cc, alx, ADDR_NULL); } }
//--------------------------------------------------------------------------------------- void GmoBox::draw_border(Drawer* pDrawer, RenderOptions& opt) { ImoStyle* pStyle = get_style(); if (pStyle && (pStyle->border_width_top() > 0.0f || pStyle->border_width_bottom() > 0.0f || pStyle->border_width_left() > 0.0f || pStyle->border_width_right() > 0.0f) ) { double xLeft = double(m_origin.x + pStyle->margin_left()); double yTop = double(m_origin.y + pStyle->margin_top()); double xRight = double(get_right() - pStyle->margin_right()); double yBottom = double(get_bottom() - pStyle->margin_bottom()); pDrawer->begin_path(); pDrawer->fill( pStyle->background_color() ); pDrawer->stroke( Color(0,0,0) ); //TODO: border color pDrawer->move_to(xLeft, yTop); //top border if (pStyle->border_width_top() > 0.0f) { pDrawer->stroke_width( pStyle->border_width_top() ); pDrawer->hline_to(xRight); } else pDrawer->move_to(xRight, yTop); //right border if (pStyle->border_width_right() > 0.0f) { pDrawer->stroke_width( pStyle->border_width_right() ); pDrawer->vline_to(yBottom); } else pDrawer->move_to(xRight, yBottom); //bottom border if (pStyle->border_width_bottom() > 0.0f) { pDrawer->stroke_width( pStyle->border_width_bottom() ); pDrawer->hline_to(xLeft); } else pDrawer->move_to(xLeft, yBottom); //left border if (pStyle->border_width_left() > 0.0f) { pDrawer->stroke_width( pStyle->border_width_left() ); pDrawer->vline_to(yTop); } pDrawer->end_path(); } if (this->is_item_main_box() && opt.draw_focus_lines_on_boxes_flag == true) { double xorg = m_origin.x; double yorg = m_origin.y; Color color = opt.unfocussed_box_color; draw_box_bounds(pDrawer, xorg, yorg, color); } else if (must_draw_bounds(opt)) { double xorg = m_origin.x; double yorg = m_origin.y; Color color = get_box_color(); draw_box_bounds(pDrawer, xorg, yorg, color); } }
int sum_neighbors(int x, int y) { //std::cout << "sn " << get_top(x, y) << " " << get_right(x,y) << " " << get_left(x,y) << " " << get_bottom(x,y) << std::endl; return get_top(x, y) + get_right(x, y) + get_left(x, y) + get_bottom(x, y); }
bool any_neighbor(int x, int y) { return get_top(x, y) > 0 || get_right(x, y) > 0 || get_left(x, y) > 0 || get_bottom(x, y) > 0; }
void MummyDragon::move(Map *map) { Monster::move(map); switch(m_action) { case Still: set_action(Move); break; case Move: if (m_is_flying) { set_speed(0, m_fly_speed); if (m_fly_timer.check(m_fly_time) || m_y < m_top || m_y > m_bottom) { m_fly_timer.reset(); m_is_flying = false; } } else { if (get_left() < m_left || get_right() > m_right) { swap_move_dir(); if (m_turn_counter.expired(get_attribute("num_turns"))) { m_fly_time = 600; if (m_y < m_top + 100) { m_fly_speed = get_attribute("fly_speed"); } else { m_fly_speed = -get_attribute("fly_speed"); } m_is_flying = true; } else { m_attack_timer.enable(true); } } if (m_dir == Right) { set_speed(get_move_speed(map), 0); } else { set_speed(-get_move_speed(map), 0); } } animate_move(); if (m_attack_timer.expired(get_attribute("attack_timer"))) { m_attack_timer.enable(false); set_attack(); } break; case Attack: fire(); break; case Hit: m_fly_time = get_attribute("fly_time_after_hit"); m_fly_speed = -get_attribute("fly_speed"); m_is_flying = true; break; default: break; } unsigned n = m_bullets.size(); for (unsigned i = 0; i < n; i++) { m_bullets[i]->move(map); } }
//The main function of the sweep algorithm. void findIntersection(float pt[][2]) { int i,j; //"ordHead" doesn't save any data //ordHead->next is the first point in ordered list POrder *ordHead; POrder *ordTemp; ordHead = (POrder *)malloc(LEN_P); ordHead->next=NULL; //Initialize the original points and ordered list. initPt(ordHead,pt); //Draw the original points and lines. glPointSize(3.0); glBegin(GL_POINTS); glColor3f(0.0,0.0,0.0); for(i=0;i<NUM_PT;i+=2){ glVertex2f(pt[i][0],pt[i][1]); glVertex2f(pt[i+1][0],pt[i+1][1]); } glEnd(); glColor3f(0.5,0.5,0.5); glBegin(GL_LINES); for(i=0;i<NUM_PT;i++) glVertex2f(pt[i][0],pt[i][1]); glEnd(); //Scan the ordered list and check intersection, //"top" is always the root node of the scan tree tree *top; top=(struct tree *)malloc(LEN_S); top->id=ordHead->next; top->left=NULL; top->right=NULL; top->father=NULL; _inteCount=0; _maxPtOrd=NUM_PT; tree *left_s; tree *right_s; tree *left_l; tree *right_l; tree *scan_temp; ordTemp = ordHead->next->next; i=1; float x_scan; float y_scan; while(i<_maxPtOrd) { if(ordTemp==NULL) break; tree *scan; scan=(struct tree *)malloc(LEN_S); scan->id=ordTemp; x_scan=scan->id->x; y_scan=scan->id->y; if(scan->id->type==ORD_UPPER) { //For an upper point: //Add it into scan tree. if(top==NULL) { top=scan; scan->id=ordTemp; scan->father=NULL; scan->left=NULL; scan->right=NULL; }else addIntoScan(top,scan,scan->id->y); //Get left and right neighbors in the scan tree. left_s=get_left(scan); while(left_s!=NULL && left_s->id->type!=ORD_UPPER) left_s=get_left(left_s); right_s=get_right(scan); while(right_s!=NULL && right_s->id->type!=ORD_UPPER) right_s=get_right(right_s); //Check intersection points. if(left_s!=NULL) checkInte(ordHead->next,left_s->id,scan->id,top,x_scan,y_scan); if(right_s!=NULL) checkInte(ordHead->next,scan->id,right_s->id,top,x_scan,y_scan); }else if(scan->id->type==ORD_LOWER){ //For a lower point: //Find its upper point in scan tree. scan_temp=searchInScan(top,scan->id->l,NO_LINE,SCAN_L); //Get left and right neighbors of the upper point in scan tree. left_s=get_left(scan_temp); while(left_s!=NULL && (left_s->id->type!=ORD_UPPER || left_s==scan)) left_s=get_left(left_s); right_s=get_right(scan_temp); while(right_s!=NULL && (right_s->id->type!=ORD_UPPER || right_s==scan)) right_s=get_right(right_s); //Check intersection between left and right neighbors of the deleted line. if(left_s!=NULL && right_s!=NULL) checkInte(ordHead->next,left_s->id,right_s->id,top,x_scan,y_scan); //Delete the upper point. top=dltFromScan(top,scan_temp); //Find inte-points of the line, and mark them. //If both of two inte-lines have been deleted, //their inte-points have to be deleted in the scan tree. for(j=0;j<_inteCount;j++) { if(found[j][0]==scan->id->l || found[j][1]==scan->id->l) { if(found[j][2]==FND_2) found[j][2]=FND_1; else if(found[j][2]==FND_1){ scan_temp=searchInScan(top,found[j][0],found[j][1],SCAN_L_L2); if(scan_temp!=NULL) top=dltFromScan(top,scan_temp); found[j][2]=FND_D; } } } }else if(scan->id->type==ORD_INTE){ //For an intersection point: //Find its node in the scan tree //Because the current "scan" node is not in the scan tree, //only its "id" pointer is evaluated. scan=searchInScan(top,scan->id->l,scan->id->l2,SCAN_L_L2); //Check for inte-points between its left line and its right neighbor, //and between its right line and its left neighbor. left_l=searchInScan(top,scan->id->l,NO_LINE,SCAN_L); right_l=searchInScan(top,NO_LINE,scan->id->l2,SCAN_L2); left_s=get_left(left_l); while(left_s!=NULL && (left_s->id->type!=ORD_UPPER || left_s==left_l || left_s==right_l)) left_s=get_left(left_s); right_s=get_right(right_l); while(right_s!=NULL && (right_s->id->type!=ORD_UPPER || right_s==left_l || right_s==right_l)) right_s=get_right(right_s); switchScan(left_l,right_l); if(left_s!=NULL) checkInte(ordHead->next,left_s->id,left_l->id,top,x_scan,y_scan); if(right_s!=NULL) checkInte(ordHead->next,right_l->id,right_s->id,top,x_scan,y_scan); //Delete the inte-point node in the scan tree. top=dltFromScan(top,scan); } //Get next point in the ordered list ordTemp=ordTemp->next; i++; } //Draw all the intersection points. glPointSize(4.0); glBegin(GL_POINTS); glColor3f(0.0,0.0,1.0); for(i=0;i<_inteCount;i++) glVertex2f(inte[i][0],inte[i][1]); glEnd(); }