Example #1
0
/*  Initialise le board dont le pointeur est en parametre   */
board *init_board(board*b){
  /* Creation du tableau */
  char i;
  int j;
  /* On initialise toutes les cases à ' ' si elle n'existe pas, '.' sinon */
  for(i='A'; i <= 'I';i++){
    for(j=1; j <= 9; j++){
      if(j < min_col(i) || j > max_col(i))
	b->tab[c_to_key(i)][i_to_key(j)] = '0';
      else
	b->tab[c_to_key(i)][i_to_key(j)] = '.';
    }
  }

  /* On pose les pieces
   * Piece B */
  for(i='A'; i <='B'; i++){ /* Les lignes A et B sont totalement à remplir */
    for(j=min_col(i); j <= max_col(i); j++){
      b->tab[c_to_key(i)][i_to_key(j)] = 'B';
    }
  }
  b->tab[c_to_key('C')][i_to_key(3)] = 'B';
  b->tab[c_to_key('C')][i_to_key(4)] = 'B';
  b->tab[c_to_key('C')][i_to_key(5)] = 'B';
  /* Piece N */
  for(i='H'; i <='I'; i++){ /* Les lignes H et I sont totalement à remplir */
    for(j=min_col(i); j <= max_col(i); j++){
      b->tab[c_to_key(i)][i_to_key(j)] = 'N';
    }
  }
  b->tab[c_to_key('G')][i_to_key(5)] = 'N';
  b->tab[c_to_key('G')][i_to_key(6)] = 'N';
  b->tab[c_to_key('G')][i_to_key(7)] = 'N';
  return b;
}
double *gauss_sequential (double **a, double*b)
{
    double *x, sum, l[n * n];
    int i, j, k, r;

    x = (double *) malloc(n * sizeof(double));
    for (k = 0; k < n-1; k++) { /* Forward elimination */
        r = max_col(a, k);
        if (k != r) exchange_row(a, b, r, k);
        for (i = k+1; i < n; i++) {
            l[i] = a[i][k] / a[k][k];
            for (j = k+1; j < n; j++) {
                a[i][j] = a[i][j] - l[i] * a[k][j];
            }
            b[i] = b[i] -l[i] * b[k];
        }
    }

    for (k = n-1; k >= 0; k--) { /* Backward substition */
        sum = 0.0;
        for (j = k+1; j < n; j++) {
            sum = sum + a[k][j] * x[j];
        }
        x[k] = 1 / a[k][k] * (b[k] - sum);
    }
    return x;
}
Example #3
0
static void	display_args(char **args)
{
	int		lin;
	int		col;
	int		scnt;

	lin = max_line();
	col = max_col();
	scnt = 1;
	while (args[scnt] != NULL)
	{
		ft_putendl(args[scnt]);
		ft_putchar('\n');
		scnt++;
	}
}
Example #4
0
 /** Run alignment algorithm.
 \param first_last Last aligned position in first sequence (output)
 \param second_last Last aligned position in second sequence (output)
 */
 void align(int& first_last, int& second_last) const {
     adjust_matrix_size();
     limit_range();
     make_frame();
     ASSERT_TRUE(!local() || max_errors() == -1);
     int& r_row = first_last;
     int& r_col = second_last;
     r_row = r_col = -1;
     for (int row = 0; row <= max_row(); row++) {
         int start_col = min_col(row);
         int stop_col = max_col(row);
         int min_score_col = start_col;
         for (int col = start_col; col <= stop_col; col++) {
             ASSERT_TRUE(col >= 0 && col < side());
             ASSERT_TRUE(in(row, col));
             int match = at(row - 1, col - 1) +
                         substitution(row, col);
             int gap1 = at(row, col - 1) + gap_penalty();
             int gap2 = at(row - 1, col) + gap_penalty();
             int score = std::min(match, std::min(gap1, gap2));
             if (local()) {
                 score = std::min(score, 0);
             }
             at(row, col) = score;
             if (score < at(row, min_score_col)) {
                 min_score_col = col;
             }
             track(row, col) = (score == match) ? MATCH :
                               (score == gap1) ? COL_INC :
                               ROW_INC;
         }
         if (max_errors() != -1 &&
                 at(row, min_score_col) > max_errors()) {
             break;
         }
         r_row = row;
         r_col = min_score_col;
     }
     if (max_errors() == -1) {
         // col -> max_col in this row
         ASSERT_TRUE(in(max_row(), max_col(max_row())));
         ASSERT_EQ(r_row, max_row());
         r_col = max_col(max_row());
         // if stopped earlier because of gap range
         int last_row = contents().first_size() - 1;
         int last_col = contents().second_size() - 1;
         if (r_row == last_row) {
             while (r_col < last_col) {
                 r_col += 1;
                 track(r_row, r_col) = COL_INC;
             }
         } else if (r_col == last_col) {
             while (r_row < last_row) {
                 r_row += 1;
                 track(r_row, r_col) = ROW_INC;
             }
         } else {
             throw Exception("row and column are not last");
         }
     }
 }
Example #5
0
/* This is only complicated because it must deal with overlap, and it wants
   to be smart about copying empty space. . .
 */
void
move_region (struct rng *fm, struct rng *to)
{
  /* Delta {row,col} */
  int dr, dc;
  int nr, nc;
  int ov, dn;
  struct rng del_to_1, del_to_2;
  int do_2, dirs[2];
  int maxr, maxc;
  CELLREF cmax, rmax;
  int cdmax, rdmax;
  int must_repaint = 0;		/* If this move changes cell widths/heights */

  switch (set_to_region (fm, to))
    {
    case 0:
      return;

    case 1:
      io_error_msg ("Can't move source to multiple targets");
      return;

    case 2:
      del_to_1 = *to;

      do_2 = 0;
      dirs[0] = 1;
      dirs[1] = 1;

      /* del_fm_1= *fm; */
      break;

    default:
      /* They overlap.  There are eight ways that
		   they can overlap.  */
      if (to->lc == fm->lc && to->lr < fm->lr)
	{
	  /* State 1:  'from' on bottom */
	  del_to_1.lr = to->lr;
	  del_to_1.lc = to->lc;
	  del_to_1.hr = fm->lr - 1;
	  del_to_1.hc = to->hc;

	  do_2 = 0;
	  dirs[0] = 1;
	  dirs[1] = 1;

	  /* del_fm_1.lr=to->hr+1;	del_fm_1.lc=fm->lc;
			del_fm_1.hr=fm->hr;	del_fm_1.hc=fm->hc; */
	}
      else if (to->lc == fm->lc)
	{
	  /* State 2: 'from' on top */
	  del_to_1.lr = fm->hr + 1;
	  del_to_1.lc = to->lc;
	  del_to_1.hr = to->hr;
	  del_to_1.hc = to->hc;

	  do_2 = 0;
	  dirs[0] = -1;
	  dirs[1] = 1;

	  /* del_fm_1.lr=fm->lr;	del_fm_1.lc=fm->lc;
			del_fm_1.hr=to->lr-1;	del_fm_1.hc=fm->hc; */
	}
      else if (to->lr == fm->lr && to->lc < fm->lc)
	{
	  /* State 3: 'from' on right */
	  del_to_1.lr = to->lr;
	  del_to_1.lc = to->lc;
	  del_to_1.hr = to->hr;
	  del_to_1.hc = fm->lc - 1;

	  do_2 = 0;
	  dirs[0] = 1;
	  dirs[1] = 1;

	  /* del_fm_1.lr=fm->lr;	del_fm_1.lc=to->hc+1;
			del_fm_1.hr=fm->hr;	del_fm_1.hc=fm->hc; */
	}
      else if (to->lr == fm->lr)
	{
	  /* State 4: 'from' on left */
	  del_to_1.lr = to->lr;
	  del_to_1.lc = fm->hc + 1;
	  del_to_1.hr = to->hr;
	  del_to_1.hc = to->hc;

	  do_2 = 0;
	  dirs[0] = 1;
	  dirs[1] = -1;

	  /* del_fm_1.lr=fm->lr;	del_fm_1.lc=fm->lc;
			del_fm_1.hr=fm->hr;	del_fm_1.hc=to->lc-1; */
	}
      else if (fm->lr < to->lr && fm->lc < to->lc)
	{
	  /* State 5: From on topleft */

	  del_to_1.lr = to->lr;
	  del_to_1.lc = fm->hc + 1;
	  del_to_1.hr = fm->hr;
	  del_to_1.hc = to->hc;

	  del_to_2.lr = fm->hr + 1;
	  del_to_2.lc = to->lc;
	  del_to_2.hr = to->hr;
	  del_to_2.hc = to->hc;

	  do_2 = 1;
	  dirs[0] = -1;
	  dirs[1] = -1;

	  /* del_fm_1.lr=fm->lr;	del_fm_1.lc=fm->lc;
			del_fm_1.hr=to->lr-1;	del_fm_1.hc=fm->hc;

			del_fm_2.lr=to->lr;	del_fm_2.lc=fm->lc;
			del_fm_2.hr=fm->hr;	del_fm_2.hc=to->lc-1; */
	}
      else if (fm->lr < to->lr)
	{
	  /* State 6: 'from' on topright */
	  del_to_1.lr = to->lr;
	  del_to_1.lc = to->lc;
	  del_to_1.hr = fm->hr;
	  del_to_1.hc = fm->lc - 1;

	  del_to_2.lr = fm->hr + 1;
	  del_to_2.lc = to->lc;
	  del_to_2.hr = to->hr;
	  del_to_2.hc = to->hc;

	  do_2 = 1;
	  dirs[0] = -1;
	  dirs[1] = 1;

	  /* del_fm_1.lr=fm->lr;	del_fm_1.lc=fm->lc;
			del_fm_1.hr=to->lr-1;	del_fm_1.hc=fm->hc;

			del_fm_2.lr=to->lr;	del_fm_2.lc=to->hc+1;
			del_fm_2.hr=fm->hr;	del_fm_2.hc=fm->hc; */
	}
      else if (fm->lc < to->lc)
	{
	  /* State 7: 'from on bottomleft */
	  del_to_1.lr = to->lr;
	  del_to_1.lc = to->lc;
	  del_to_1.hr = fm->lr - 1;
	  del_to_1.hc = to->hc;

	  del_to_2.lr = fm->lr;
	  del_to_2.lc = fm->hc;
	  del_to_2.hr = to->hr;
	  del_to_2.hc = to->hc;

	  do_2 = 1;
	  dirs[0] = 1;
	  dirs[1] = -1;

	  /* del_fm_1.lr=fm->lr;	del_fm_1.lc=fm->lc;
			del_fm_1.hr=to->hr;	del_fm_1.hc=to->lc-1;

			del_fm_2.lr=to->hr+1;	del_fm_2.lc=fm->lc;
			del_fm_2.hr=to->hr+1;	del_fm_2.hc=to->lc-1; */
	}
      else
	{
	  /* State 8: 'from' on bottomright */
	  del_to_1.lr = to->lr;
	  del_to_1.lc = to->lc;
	  del_to_1.hr = fm->lr - 1;
	  del_to_1.hc = to->hc;

	  del_to_2.lr = fm->lr;
	  del_to_2.lc = to->lc;
	  del_to_2.hr = to->hr;
	  del_to_2.hc = fm->lc - 1;

	  do_2 = 1;
	  dirs[0] = 1;
	  dirs[1] = 1;

	  /* del_fm_1.lr=fm->lr;	del_fm_1.lc=to->hc+1;
			del_fm_1.hr=to->hr;	del_fm_1.hc=fm->hc;

			del_fm_2.lr=to->hr+1;	del_fm_2.lc=fm->lc;
			del_fm_2.hr=fm->hr;	del_fm_2.hc=fm->hc; */
	}
    }
  dn = to->hr - fm->hr;
  ov = to->hc - fm->hc;

  dr = fm->hr - fm->lr;
  dc = fm->hc - fm->lc;

  delete_region (&del_to_1);
  if (do_2)
    delete_region (&del_to_2);

  if (to->lr == MIN_ROW && to->hr == MAX_ROW)
    {
      shift_widths (ov, fm->lc, fm->hc);
      must_repaint = 1;
    }

  if (to->lc == MIN_COL && to->hc == MAX_COL)
    {
      shift_heights (dn, fm->lr, fm->hr);
      must_repaint = 1;
    }

  shift_outside (fm, dn, ov);

  rmax = highest_row ();
  if (rmax < fm->lr)
    rdmax = -1;
  else if (rmax > fm->hr)
    rdmax = dr;
  else
    rdmax = rmax - fm->lr;
  nr = (dirs[0] > 0) ? 0 : rdmax;
  maxr = (dirs[0] > 0) ? rdmax + 1 : -1;
  for (; nr != maxr; nr += dirs[0])
    {
      cmax = max_col (fm->lr + nr);
      if (cmax < fm->lc)
	cdmax = -1;
      else if (cmax > fm->hc)
	cdmax = dc;
      else
	{
	  cdmax = cmax - fm->lc;
	}
      nc = (dirs[1] > 0) ? 0 : cdmax;
      maxc = (dirs[1] > 0) ? cdmax + 1 : -1;
      for (; nc != maxc; nc += dirs[1])
	{
	  CELLREF rf, cf, rt, ct;
	  CELL *cpf;

	  rf = fm->lr + nr;
	  cf = fm->lc + nc;
	  rt = to->lr + nr;
	  ct = to->lc + nc;

	  cpf = find_cell (rf, cf);
	  cur_row = rt;
	  cur_col = ct;
	  my_cell = find_cell (cur_row, cur_col);
	  if ((!cpf || (!cpf->cell_font &&
		((cpf->cell_flags.cell_format == 0)
		&& (cpf->cell_flags.cell_precision == 0)
		&& (cpf->cell_flags.cell_justify == 0)
		&& (cpf->cell_flags.cell_type == 0))
		&& (cpf->cell_flags.cell_lock == 0)
		&& !cpf->cell_formula))
	      && !my_cell)
	    continue;

	  if (!cpf)
	    {
	      bzero(&(my_cell->cell_flags), sizeof(my_cell->cell_flags));
	      my_cell->cell_font = 0;
	      my_cell->cell_refs_to = 0;
	      my_cell->cell_formula = 0;
	      my_cell->cell_cycle = 0;
	      my_cell = 0;
	      continue;
	    }
	  if (!my_cell)
	    {
	      my_cell = find_or_make_cell (cur_row, cur_col);
	      cpf = find_cell (rf, cf);
	    }
	  else
	    flush_old_value ();

	  my_cell->cell_flags = cpf->cell_flags;
	  my_cell->cell_font = cpf->cell_font;
	  my_cell->cell_refs_to = cpf->cell_refs_to;
	  my_cell->cell_formula = cpf->cell_formula;
	  my_cell->cell_cycle = cpf->cell_cycle;
	  my_cell->c_z = cpf->c_z;

	  bzero(&(cpf->cell_flags), sizeof(cpf->cell_flags));
	  cpf->cell_font = 0;
	  cpf->cell_refs_to = 0;
	  cpf->cell_formula = 0;
	  cpf->cell_cycle = 0;

	  push_cell (cur_row, cur_col);

 	  if (!must_repaint)
	    {
	      if (cpf)
		io_pr_cell (rf, cf, cpf);
	      if (my_cell)
		io_pr_cell (rt, ct, my_cell);
	    }
	  my_cell = 0;
	}
    }
  if (must_repaint)
    io_repaint ();
/* Perpetration of an interface change here.  If we really
 * need to get back to the old region, we can do it by
 * wrapping the move-region command in macros that set
 * and return to a hardy mark.  We might, however, want
 * to jump the moved text again, or reformat it in some
 * way, so the mark should travel with us.
 * 
 * to->lr and to->lc give the lowest column and row (northwest
 * corner) in the destination region, to->hr and to->hc five the
 * highest, or southeast corner.  The absolute value if their
 * difference tells us how far to move over and down from
 * northwest to mark the region just moved.  This way the new
 * region can be operated on as a chunk immediately
 *
 * --FB 1997.12.17
 */
  if (mkrow != NON_ROW) {
      mkrow = to->lr + abs(to->lr - to->hr);
      mkcol = to->lc + abs(to->lc - to->hc);
  }
  goto_region (to);
  return;
}
/** execute_db_operator takes as input the db_operator and executes the query.
 * It should return the result (currently as a char*, although I'm not clear
 * on what the return type should be, maybe a result struct, and then have
 * a serialization into a string message).
 **/
char* execute_db_operator(db_operator* query) {
    status s;

    if (query->type == INSERT) {
        table* tbl1 = query->tables[0];
        for(size_t i = 0; i < tbl1->col_count; i++) {
            s = col_insert(query->columns[i], query->value1[i]);
            if (s.code != OK) {
                return s.error_message;
            }
        }
        return "Rows successfully inserted.";
    } else if (query->type == SELECT) {
        result* r = malloc(sizeof(struct result));
        if (query->columns) {
            s = select_data(query, &r);
        } else if (query->result1 && query->result2) {
            s = vec_scan(query, &r);
        } else {
            return "Cannot perform select\n";
        }

        if (s.code != OK) {
            return s.error_message;
        }
        int idx = catalogs[0]->var_count;
        catalogs[0]->names[idx] = query->name1;
        catalogs[0]->results[idx] = r;
        catalogs[0]->var_count++;
    } else if (query->type == PROJECT) {
        result* r = malloc(sizeof(struct result));
        status s = fetch(*(query->columns), query->result1->payload, query->result1->num_tuples, &r);
        if (s.code != OK) {
            return s.error_message;
        }
        int idx = catalogs[0]->var_count;
        catalogs[0]->names[idx] = query->name1;
        catalogs[0]->results[idx] = r;
        catalogs[0]->var_count++;
    } else if (query->type == ADD) {
        result* r = malloc(sizeof(struct result));
        s = add_col((int*)query->result1->payload, (int*)query->result2->payload, query->result1->num_tuples, &r);
        if (s.code != OK) {
            return s.error_message;
        }
        int idx = catalogs[0]->var_count;
        catalogs[0]->names[idx] = query->name1;
        catalogs[0]->results[idx] = r;
        // for (size_t i = 0; i < r->num_tuples; ++i)
        // {
        //     printf("%ld\n", ((long*)r->payload)[i]);
        // }
        catalogs[0]->var_count++;
    } else if (query->type == SUB) {
        result* r = malloc(sizeof(struct result));
        s = sub_col((int*)query->result1->payload, (int*)query->result2->payload, query->result1->num_tuples, &r);
        if (s.code != OK) {
            return s.error_message;
        }
        int idx = catalogs[0]->var_count;
        catalogs[0]->names[idx] = query->name1;
        catalogs[0]->results[idx] = r;
        catalogs[0]->var_count++;
    } else if (query->type == AGGREGATE) {
        result* r = malloc(sizeof(struct result));
        if (query->agg == MIN) {
            s = min_col(query->result1, &r);
        } else if (query->agg == MAX) {
            s = max_col(query->result1, &r);
        } else if (query->agg == AVG) {
            s = avg_col(query->result1, &r);
        } else if (query->agg == CNT) {
            s = count_col(query->result1->num_tuples, &r);
        } else {
            return "Failed aggregation";
        }

        if (s.code != OK) {
            return s.error_message;
        }

        int idx = catalogs[0]->var_count;
        catalogs[0]->names[idx] = query->name1;
        catalogs[0]->results[idx] = r;
        catalogs[0]->var_count++;
    }

    return "Success";
}