示例#1
0
文件: bst.cpp 项目: ramir2rx/cs240
/****************************************************************************
 *     Print nodes according to the mode given                              *
 ****************************************************************************/
void bst::printTree( int mode , Position p ) const
{
    if(p.isValid())
        rec_print(p.v, mode);
    else
        cout << "Starting Position is not Valid..." << endl;
}
示例#2
0
void solve_maze()
{
  int i,j,x,y,bx,by,tx,ty,goal,nx,ny,nbx,nby,cost,dist;

  for(i=0;i<MAXDIM*MAXDIM*MAXDIM*MAXDIM;i++)
    {
      st[i].dist = MAXDIM*MAXDIM*MAXDIM*MAXDIM*MAXDIM*MAXDIM+1;
      st[i].heap = st[i].last = 0;
    }
  heap_init();
  for(i=0;i<r;i++)
    for(j=0;j<c;j++)
      switch(m[i][j])
	{
	case 'S': x = j; y = i; m[i][j] = '.'; break;
	case 'B': bx = j; by = i; m[i][j] = '.'; break;
	case 'T': tx = j; ty = i; m[i][j] = '.'; break;
	}
  heap_push(x,y,bx,by,0,0);
  while(heapsize != 0)
    {
      heap_pop(&x,&y,&bx,&by,&dist);
      for(i=0;i<4;i++)
	{
	  nx = x+dx[i];
	  ny = y+dy[i];
	  cost = 1;
	  if(nx == bx && ny == by)
	    { 
	      nbx = bx+dx[i]; nby = by+dy[i]; 
	      cost += MAXDIM*MAXDIM*MAXDIM*MAXDIM;
	    }
	  else
	    { nbx = bx; nby = by; }
	  if(nx >= 0 && nx < c && ny >= 0 && ny < r &&
	     nbx >= 0 && nbx < c && nby >= 0 && nby < r &&
	     m[ny][nx] == '.' && m[nby][nbx] == '.')
	    heap_push(nx,ny,nbx,nby,dist+cost,cost==1?walk[i]:push[i]);
	}
    }
  x = y = 0;
  for(i=0;i<r;i++)
    for(j=0;j<c;j++)
      if(st[j+i*MAXDIM+tx*MAXDIM*MAXDIM+ty*MAXDIM*MAXDIM*MAXDIM].dist <
	 st[x+y*MAXDIM+tx*MAXDIM*MAXDIM+ty*MAXDIM*MAXDIM*MAXDIM].dist)
	{
	  x = j; y = i;
	}
  goal = x+y*MAXDIM+tx*MAXDIM*MAXDIM+ty*MAXDIM*MAXDIM*MAXDIM;
  if(st[goal].dist > MAXDIM*MAXDIM*MAXDIM*MAXDIM*MAXDIM*MAXDIM)
    printf("Impossible.");
  else
    rec_print(x,y,tx,ty);
  printf("\n\n");
}
示例#3
0
void rec_print(int x, int y, int bx, int by)
{
  int idx,i;

  idx = x + y*MAXDIM + bx*MAXDIM*MAXDIM + by*MAXDIM*MAXDIM*MAXDIM;
  if(st[idx].last)
    {
      for(i=0;i<4;i++)
	if(st[idx].last == walk[i])
	  {
	    rec_print(x-dx[i],y-dy[i],bx,by); break;
	  }
      for(i=0;i<4;i++)
	if(st[idx].last == push[i])
	  {
	    rec_print(x-dx[i],y-dy[i],bx-dx[i],by-dy[i]); break;
	  }
      printf("%c",st[idx].last);
    }
}
示例#4
0
文件: bst.cpp 项目: ramir2rx/cs240
/****************************************************************************
 *     positions() utility function                                         *
 ****************************************************************************/
void     bst::rec_print    ( Node * u , int mode ) const 
{
    if(u->left == NULL && u->right == NULL)
    {
        cout << "_ ";
        return;
    }
    cout << "( ";
    if(mode == PREORDER)
    {
        cout << u->data;
    }
    rec_print(u->left, mode);
    if (mode == INORDER)
    {
        cout << u->data;
    }
    rec_print(u->right, mode);
    if(mode == POSTORDER)
    {
        cout << u->data;
    }
    cout << " )";
}
示例#5
0
/* recursively generate the target string by tracing back through the
   decoding dictionary, appending one character to the previous output
   as the last step in each recursive call */
int
rec_print(decoder_t *D, int strnum) {
	int len;
	if (strnum==0) {
		/* base case for all tracebacks, every string starts with
		   an empty string, and doing nothing will print that... */
		return 0;
	} else {
		/* print the string's prefix by tracing it back */
		len = rec_print(D, D[strnum].copystring);
		/* and then append the last character of the string
		   we were asked for in this call */
		putchar(D[strnum].extension);
		return len+1;
	}
}
示例#6
0
int
main(int argc, char *argv[]) {

	decoder_t *D=NULL;	/* pointer to decoding table */
	int current_size=0;	/* current decode table capacity */
	int nstrings=0;		/* current decode table actual size */
	char extension;		/* one extension character */
	int copystring;		/* index of the string it builds on */
	char dummybyte;		/* reads the newline at end of each line */

	int i_bits=0, o_bytes=0;
				/* count of input bits (estimated) and
				   output bytes (exact) */

	/* create initial decode array */
	D = (decoder_t*)malloc(INITSIZE*sizeof(*D));
	current_size = INITSIZE;
	assert(D);

	/* and insert the first string, the empty string */
	D[0].extension = '\0';
	D[0].copystring = 0;
	nstrings = 1;

	/* now process extension,copystring pairs from the input */
	while ((extension=getchar()) != EOF) {
		/* have got the extension character, so there really
		   should be a matching copystring value and then a 
		   newline */
		assert(scanf("%d%c", &copystring, &dummybyte)==2);
		assert(copystring>=0 && copystring<=nstrings);
#if DEBUG
		printf("DB: extension = %c, copystring = %d\n",
			extension, copystring);
#endif
		/* check if decode array has enough space */
		if (nstrings==current_size) {
			/* and if not, extend it */
			current_size *= MULTIPLY;
			D = (decoder_t*)realloc(D, current_size*sizeof(*D));
			assert(D);
#if DEBUG
			printf("DB: D now %d entries\n", current_size);
#endif
		}

		/* add the new string to the decode array */
		assert(nstrings<current_size);
		D[nstrings].extension = extension;
		D[nstrings].copystring = copystring;
		
		/* and then print out the string it represents,
		   return value indicayes how long it was */
		o_bytes += rec_print(D, nstrings);
		/* and how many bit could the two inputs have fitted in to? */
		i_bits += EXTENSION_BITS + comp_len(nstrings);
#if DEBUG
		printf("\n");
#endif
		/* and finally, now ready to iterate with a new phrase
		   installed in the dictionary */
		nstrings++;
	}

	/* print some final summary statistics to the screen, but first make
	   sure that all of the stdout output has been emitted */
	fflush(stdout);
	fprintf(stderr, "\n");
	fprintf(stderr, "decode: %6d factors processed\n", nstrings-1);
	fprintf(stderr, "decode: %6d bytes output\n", o_bytes);
	fprintf(stderr, "decode: %6d bits = %5d bytes sufficient as input\n",
		i_bits, bits2bytes(i_bits));
	fprintf(stderr, "decode: %6.3f bits/byte achieved by encoder\n",
		1.0*i_bits/o_bytes);

	/* not strictly necessary to do this, but good housekeeping habit */
	free(D);
	D = NULL;
	return 0;
}
示例#7
0
文件: main.c 项目: barosl/b-tree
void process() {
	int i;
	char line[100];
	char cmd, arg[100];
	bool done;
	rec_t rec;

	done = false;
	while (1) {
		printf("> ");
		fflush(stdout);

		if (!fgets(line, sizeof(line), stdin)) break;

		sscanf(line, "%c %[^\n]", &cmd, arg);

		switch (cmd) {
			FILE *fout, *fin;

			case 'i':
				snprintf(rec.name, sizeof(rec.name), "%s", arg);
				rec.x = 0;
				rec.y = 0;
				*rec.addr = '\0';

				if (insert_rec(root_idx, &rec)) {
					fprintf(stderr, "Duplicate found.\n");
				}
				break;

			case 'r':
				snprintf(rec.name, sizeof(rec.name), "%s", arg);
				remove_rec(root_idx, &rec);
				break;

			case 's':
				snprintf(rec.name, sizeof(rec.name), "%s", arg);
				if (!search_rec(root_idx, &rec)) {
					printf("Record found: ");
					rec_print(&rec);
					printf("\n");
				} else {
					fprintf(stderr, "Record not found.\n");
				}
				break;

			case 'p':
				fout = fopen("pout.txt", "w");
				print_node(fout, root_idx);
				fclose(fout);
				break;

			case 'k':
				fin = fopen("../names_200k.txt", "r");
				for (i=0;i<10000;i++) {
					if (!fgets(line, sizeof(line), fin)) break;

					if (*line) {
						char *end = line+strlen(line)-1;
						if (*end == '\n') *end = '\0';
					}

					snprintf(rec.name, sizeof(rec.name), "%s", line);
					remove_rec(root_idx, &rec);
				}
				fclose(fin);

				fout = fopen("pout.txt", "w");
				print_node(fout, root_idx);
				fclose(fout);
				break;

			case 'q':
				done = true;
				break;

			default:
				fprintf(stderr, "Invalid input.\n");
				break;
		}

		if (done) break;
	}

}
示例#8
0
/***********************************************************//**
Delete unmarks a secondary index entry which must be found. It might not be
delete-marked at the moment, but it does not harm to unmark it anyway. We also
need to update the fields of the secondary index record if we updated its
fields but alphabetically they stayed the same, e.g., 'abc' -> 'aBc'.
@return	DB_FAIL or DB_SUCCESS or DB_OUT_OF_FILE_SPACE */
static
ulint
row_undo_mod_del_unmark_sec_and_undo_update(
/*========================================*/
	ulint		mode,	/*!< in: search mode: BTR_MODIFY_LEAF or
				BTR_MODIFY_TREE */
	que_thr_t*	thr,	/*!< in: query thread */
	dict_index_t*	index,	/*!< in: index */
	const dtuple_t*	entry)	/*!< in: index entry */
{
	mem_heap_t*		heap;
	btr_pcur_t		pcur;
	btr_cur_t*		btr_cur;
	upd_t*			update;
	ulint			err		= DB_SUCCESS;
	big_rec_t*		dummy_big_rec;
	mtr_t			mtr;
	trx_t*			trx		= thr_get_trx(thr);
	enum row_search_result	search_result;

	/* Ignore indexes that are being created. */
	if (UNIV_UNLIKELY(*index->name == TEMP_INDEX_PREFIX)) {

		return(DB_SUCCESS);
	}

	log_free_check();
	mtr_start(&mtr);

	ut_ad(mode == BTR_MODIFY_TREE || mode == BTR_MODIFY_LEAF);

	search_result = row_search_index_entry(index, entry, mode,
					       &pcur, &mtr);

	switch (search_result) {
	case ROW_BUFFERED:
	case ROW_NOT_DELETED_REF:
		/* These are invalid outcomes, because the mode passed
		to row_search_index_entry() did not include any of the
		flags BTR_INSERT, BTR_DELETE, or BTR_DELETE_MARK. */
		ut_error;
	case ROW_NOT_FOUND:
		fputs("InnoDB: error in sec index entry del undo in\n"
		      "InnoDB: ", stderr);
		dict_index_name_print(stderr, trx, index);
		fputs("\n"
		      "InnoDB: tuple ", stderr);
		dtuple_print(stderr, entry);
		fputs("\n"
		      "InnoDB: record ", stderr);
		rec_print(stderr, btr_pcur_get_rec(&pcur), index);
		putc('\n', stderr);
		trx_print(stderr, trx, 0);
		fputs("\n"
		      "InnoDB: Submit a detailed bug report"
		      " to http://bugs.mysql.com\n", stderr);
		break;
	case ROW_FOUND:
		btr_cur = btr_pcur_get_btr_cur(&pcur);
		err = btr_cur_del_mark_set_sec_rec(BTR_NO_LOCKING_FLAG,
						   btr_cur, FALSE, thr, &mtr);
		ut_a(err == DB_SUCCESS);
		heap = mem_heap_create(100);

		update = row_upd_build_sec_rec_difference_binary(
			index, entry, btr_cur_get_rec(btr_cur), trx, heap);
		if (upd_get_n_fields(update) == 0) {

			/* Do nothing */

		} else if (mode == BTR_MODIFY_LEAF) {
			/* Try an optimistic updating of the record, keeping
			changes within the page */

			err = btr_cur_optimistic_update(
				BTR_KEEP_SYS_FLAG | BTR_NO_LOCKING_FLAG,
				btr_cur, update, 0, thr, &mtr);
			switch (err) {
			case DB_OVERFLOW:
			case DB_UNDERFLOW:
			case DB_ZIP_OVERFLOW:
				err = DB_FAIL;
			}
		} else {
			ut_a(mode == BTR_MODIFY_TREE);
			err = btr_cur_pessimistic_update(
				BTR_KEEP_SYS_FLAG | BTR_NO_LOCKING_FLAG,
				btr_cur, &heap, &dummy_big_rec,
				update, 0, thr, &mtr);
			ut_a(!dummy_big_rec);
		}

		mem_heap_free(heap);
	}

	btr_pcur_close(&pcur);
	mtr_commit(&mtr);

	return(err);
}