コード例 #1
0
ファイル: lz77.c プロジェクト: in3pid/lz77
void decompress(int in, int out)
{
  match_t m;
  window_t w;
  window_init(&w, BUFSIZE);
  while (read(in, &m, sizeof m) == sizeof m) { 
    if (match_distance(m) == 0) {
      int n = match_length(m);
      char tab[64];
      if (!read_tab(in, tab, n)) {
	printf("illegal state\n");
      }
      window_append_tab(&w, tab, n);
    }
    else {
      window_append_match(&w, m);
    }
    if (window_should_flush(&w)) {
      write(out, w.start, w.block_size);
      window_flush(&w);
    }
  }
  if (0 < w.cursor-w.start) write(out, w.start, w.cursor-w.start);
  window_free(&w);
}
コード例 #2
0
ファイル: options.c プロジェクト: eviebrock/git
/* Determine which option is best match. */
static int find_option(char *prog, char *name)
{
  int sofar = -1;
  int sofar_length = 0;
  int i;
  int ambiguous = 0;
  for (i = 0; i < option_count; i++) {
    int length = match_length(options[i].name, name);
    if (length > sofar_length) {
      sofar = i;
      sofar_length = length;
      ambiguous = 0;
    } else if (length > 0 && length == sofar_length) {
      ambiguous = 1;
    }
  }
  if (sofar_length == 0) {
    fprintf(stderr, "No match found to option '%s'\n", name);
    usage(prog);
  } else if (ambiguous) {
    fprintf(stderr, "Ambiguous option: '%s'\n", name);
    usage(prog);
  }
  return sofar;
}
コード例 #3
0
ファイル: lz77.c プロジェクト: in3pid/lz77
void compress(int in, int out)
{
  match_t m;
  window_t w;
  char *p;
  char *end;
  char buf[BUFSIZE];
  int length;
  char tab[64];
  int n = 0;

  window_init(&w, BUFSIZE);

  while ((length = read(in, buf, BUFSIZE)) > 0) {
    p = buf;
    end = buf+length;
    while (p < end) {
      m = window_match(&w, p, end);
      if (match_length(m) <= 1) {
	if (n >= match_length_max) {
	  write_tab(out, tab, match_length_max);
	  n = 0;
	}
	tab[n++] = *p;
	window_append(&w, *p);
	p++;
      }
      else {
	if (n) {
	  write_tab(out, tab, n);
	  n = 0;
	}
	write(out, &m, sizeof m);
	window_append_match(&w, m);
	p += match_length(m);
      }
      window_flush(&w);
    }
  }
  if (n) write_tab(out, tab, n);
  window_free(&w);
}
コード例 #4
0
ファイル: struct_map.c プロジェクト: ivanamihalek/struct
int map_consistence (  int NX, int NY, Map * combined_map, Map *map1, Map *map2,
		       double *total_ptr,  double *gap_score) {
    int i,j;
    double val1, val2;
    double total = 0;
    double aln_score;

    if (!NX) NX = map1->x2y_size; /* TODO: rename */
    if (!NY) NY = map1->y2x_size; /* TODO: rename */
    
    for (i=0; i<NX; i++) {
	for (j=0; j<NY; j++) {
	    val1 =  map1->sse_pair_score[i][j];
	    val2 =  map2->sse_pair_score[i][j];

	    if ( val1 > val2) {
		combined_map->sse_pair_score[i][j]  = val1;
		combined_map->cosine[i][j] = map1->cosine[i][j];
	    } else {
		combined_map->sse_pair_score[i][j]  = val2;
		combined_map->cosine[i][j] = map2->cosine[i][j];
	    }
	}
    }
      

    /* Needleman on combined sse_pair_score */
    needleman_wunsch (NX, NY, combined_map->sse_pair_score,
		      combined_map->x2y,  combined_map->y2x, &aln_score );
    /* how do lengths compare? */
    combined_map->matches = match_length (NX, combined_map->x2y);

    if ( ! combined_map->matches ) {
	combined_map->assigned_score = 0.0;
    } else {
	for (i=0; i<NX; i++) {
	    j = combined_map->x2y[i];
	    if (j<0) continue;
	    total += combined_map->sse_pair_score[i][j];
	}
	combined_map->assigned_score = total;
    }
    /* pass NULL for total_ptr  if I don't need it */
    if (total_ptr) *total_ptr = combined_map->assigned_score;
    
    return  0;
}
コード例 #5
0
ファイル: struct_map.c プロジェクト: ivanamihalek/struct
int map_assigned_score ( Representation *X_rep,  Map* map) {

    int i,j;
    int NX = X_rep->N_full;
    
    map->size = 0;
    map->assigned_score = 0;
    for (i=0; i < NX; i++ ) {
	j =  map->x2y[i];
	if ( j <  0 )  continue;
	
	map->assigned_score += map->sse_pair_score[i][j];
    }
    
    /* what's the difference btw this and map->size?*/
    map->matches = match_length (NX, map->x2y);
    return 0;    
}
コード例 #6
0
ファイル: lz77_enc.c プロジェクト: MonteCarlos/anim64
unsigned int lz77_pack(unsigned char* dst, const unsigned char* src) {
    unsigned int written = 0;

    read_index = 0;

    while (read_index < PACK_SIZE) {
        int best_match_index = -1;
        unsigned char best_length = 0;
        int match_index = read_index - 1;
        while (match_index >= 0 && match_index >= read_index - 0xfe) {
            unsigned char length = match_length(src, match_index);
            if (length > best_length) {
                best_match_index = match_index;
                best_length = length;
            }
            --match_index;
        }
        if (best_length > 3) {
            // assert(distance < 0xff);
            // printf("[%x %x]", distance, best_length);
            *dst++ = ENCODED_FLAG;
            *dst++ = read_index - best_match_index;
            *dst++ = best_length;
            written += 3;
            read_index += best_length;
        } else {
            char byte = src[read_index++];
            // printf("%x ", 0xff & byte);
            *dst++ = byte;
            ++written;
            if (byte == ENCODED_FLAG) {
                *dst++ = 0xff;
                ++written;
            }
        }
    }
    return written;
}
コード例 #7
0
ファイル: rc_test_helpers.c プロジェクト: Haifen/Mesa-3D
int parse_rc_normal_instruction(
	struct rc_instruction * inst,
	const char * inst_str)
{
	const char * regex_str = "[[:digit:]: ]*([[:upper:][:digit:]]+)(_SAT)*[ ]*([^,;]*)[, ]*([^,;]*)[, ]*([^,;]*)[, ]*([^;]*)";
	int i;
	regmatch_t matches[REGEX_INST_MATCHES];
	struct inst_tokens tokens;

	/* Execute the regex */
	if (!regex_helper(regex_str, inst_str, matches, REGEX_INST_MATCHES)) {
		return 0;
	}
	memset(&tokens, 0, sizeof(tokens));

	/* Create Tokens */
	tokens.Opcode.String = inst_str + matches[1].rm_so;
	tokens.Opcode.Length = match_length(matches, 1);
	if (matches[2].rm_so > -1) {
		tokens.Sat.String = inst_str + matches[2].rm_so;
		tokens.Sat.Length = match_length(matches, 2);
	}


	/* Fill out the rest of the instruction. */
	inst->Type = RC_INSTRUCTION_NORMAL;

	for (i = 0; i < MAX_RC_OPCODE; i++) {
		const struct rc_opcode_info * info = rc_get_opcode_info(i);
		unsigned int first_src = 3;
		unsigned int j;
		if (strncmp(tokens.Opcode.String, info->Name, tokens.Opcode.Length)) {
			continue;
		}
		inst->U.I.Opcode = info->Opcode;
		if (info->HasDstReg) {
			char * dst_str;
			tokens.Dst.String = inst_str + matches[3].rm_so;
			tokens.Dst.Length = match_length(matches, 3);
			first_src++;

			dst_str = malloc(sizeof(char) * (tokens.Dst.Length + 1));
			strncpy(dst_str, tokens.Dst.String, tokens.Dst.Length);
			dst_str[tokens.Dst.Length] = '\0';
			init_rc_normal_dst(inst, dst_str);
			free(dst_str);
		}
		for (j = 0; j < info->NumSrcRegs; j++) {
			char * src_str;
			tokens.Srcs[j].String =
				inst_str + matches[first_src + j].rm_so;
			tokens.Srcs[j].Length =
				match_length(matches, first_src + j);

			src_str = malloc(sizeof(char) *
						(tokens.Srcs[j].Length + 1));
			strncpy(src_str, tokens.Srcs[j].String,
						tokens.Srcs[j].Length);
			src_str[tokens.Srcs[j].Length] = '\0';
			init_rc_normal_src(inst, j, src_str);
		}
		if (info->HasTexture) {
			/* XXX: Will this always be XYZW ? */
			inst->U.I.TexSwizzle = RC_SWIZZLE_XYZW;
		}
		break;
	}
	return 1;
}
コード例 #8
0
ファイル: rc_test_helpers.c プロジェクト: Haifen/Mesa-3D
/**
 * Initialize the destination for the instruction based on dst_str.
 *
 * NOTE: Warning in init_rc_normal_instruction() applies to this function as
 * well.
 *
 * @param dst_str A string that represents the destination register.  The format
 * for this string is the same that is output by rc_program_print.
 * @return 1 On success, 0 on failure
 */
int init_rc_normal_dst(
	struct rc_instruction * inst,
	const char * dst_str)
{
	const char * regex_str = "([[:lower:]]*)\\[*([[:digit:]]*)\\]*(\\.*[[:lower:]]*)";
	regmatch_t matches[REGEX_DST_MATCHES];
	struct dst_tokens tokens;
	unsigned int i;

	/* Execute the regex */
	if (!regex_helper(regex_str, dst_str, matches, REGEX_DST_MATCHES)) {
		fprintf(stderr, "Failed to execute regex for dst register.\n");
		return 0;
	}

	/* Create Tokens */
	tokens.File.String = dst_str + matches[1].rm_so;
	tokens.File.Length = match_length(matches, 1);
	tokens.Index.String = dst_str + matches[2].rm_so;
	tokens.Index.Length = match_length(matches, 2);
	tokens.WriteMask.String = dst_str + matches[3].rm_so;
	tokens.WriteMask.Length = match_length(matches, 3);

	/* File Type */
	if (!strncmp(tokens.File.String, "temp", tokens.File.Length)) {
		inst->U.I.DstReg.File = RC_FILE_TEMPORARY;
	} else if (!strncmp(tokens.File.String, "output", tokens.File.Length)) {
		inst->U.I.DstReg.File = RC_FILE_OUTPUT;
	} else if (!strncmp(tokens.File.String, "none", tokens.File.Length)) {
		inst->U.I.DstReg.File = RC_FILE_NONE;
		return 1;
	} else {
		fprintf(stderr, "Unknown dst register file type.\n");
		return 0;
	}

	/* File Index */
	errno = 0;
	inst->U.I.DstReg.Index = strtol(tokens.Index.String, NULL, 10);

	if (errno > 0) {
		fprintf(stderr, "Could not convert dst register index\n");
		return 0;
	}

	/* WriteMask */
	if (tokens.WriteMask.Length == 0) {
		inst->U.I.DstReg.WriteMask = RC_MASK_XYZW;
	} else {
		inst->U.I.DstReg.WriteMask = 0;
		/* The first character should be '.' */
		if (tokens.WriteMask.String[0] != '.') {
			fprintf(stderr, "1st char of writemask is not valid.\n");
			return 0;
		}
		for (i = 1; i < tokens.WriteMask.Length; i++) {
			switch(tokens.WriteMask.String[i]) {
			case 'x':
				inst->U.I.DstReg.WriteMask |= RC_MASK_X;
				break;
			case 'y':
				inst->U.I.DstReg.WriteMask |= RC_MASK_Y;
				break;
			case 'z':
				inst->U.I.DstReg.WriteMask |= RC_MASK_Z;
				break;
			case 'w':
				inst->U.I.DstReg.WriteMask |= RC_MASK_W;
				break;
			default:
				fprintf(stderr, "Unknown swizzle in writemask: %c\n",
							tokens.WriteMask.String[i]);
				return 0;
			}
		}
	}
	DBG("Dst Reg File=%u Index=%d Writemask=%d\n",
			inst->U.I.DstReg.File,
			inst->U.I.DstReg.Index,
			inst->U.I.DstReg.WriteMask);
	return 1;
}
コード例 #9
0
ファイル: rc_test_helpers.c プロジェクト: Haifen/Mesa-3D
/**
 * Initialize the source register at index src_index for the instruction based
 * on src_str.
 *
 * NOTE: Warning in init_rc_normal_instruction() applies to this function as
 * well.
 *
 * @param src_str A string that represents the source register.  The format for
 * this string is the same that is output by rc_program_print.
 * @return 1 On success, 0 on failure
 */
int init_rc_normal_src(
	struct rc_instruction * inst,
	unsigned int src_index,
	const char * src_str)
{
	const char * regex_str = "(-*)(\\|*)([[:lower:]]*)\\[*([[:digit:]]*)\\]*(\\.*[[:lower:]_]*)";
	regmatch_t matches[REGEX_SRC_MATCHES];
	struct src_tokens tokens;
	struct rc_src_register * src_reg = &inst->U.I.SrcReg[src_index];
	unsigned int i;

	/* Execute the regex */
	if (!regex_helper(regex_str, src_str, matches, REGEX_SRC_MATCHES)) {
		fprintf(stderr, "Failed to execute regex for src register.\n");
		return 0;
	}

	/* Create Tokens */
	tokens.Negate.String = src_str + matches[1].rm_so;
	tokens.Negate.Length = match_length(matches, 1);
	tokens.Abs.String = src_str + matches[2].rm_so;
	tokens.Abs.Length = match_length(matches, 2);
	tokens.File.String = src_str + matches[3].rm_so;
	tokens.File.Length = match_length(matches, 3);
	tokens.Index.String = src_str + matches[4].rm_so;
	tokens.Index.Length = match_length(matches, 4);
	tokens.Swizzle.String = src_str + matches[5].rm_so;
	tokens.Swizzle.Length = match_length(matches, 5);

	/* Negate */
	if (tokens.Negate.Length  > 0) {
		src_reg->Negate = RC_MASK_XYZW;
	}

	/* Abs */
	if (tokens.Abs.Length > 0) {
		src_reg->Abs = 1;
	}

	/* File */
	if (!strncmp(tokens.File.String, "temp", tokens.File.Length)) {
		src_reg->File = RC_FILE_TEMPORARY;
	} else if (!strncmp(tokens.File.String, "input", tokens.File.Length)) {
		src_reg->File = RC_FILE_INPUT;
	} else if (!strncmp(tokens.File.String, "const", tokens.File.Length)) {
		src_reg->File = RC_FILE_CONSTANT;
	} else if (!strncmp(tokens.File.String, "none", tokens.File.Length)) {
		src_reg->File = RC_FILE_NONE;
	}

	/* Index */
	errno = 0;
	src_reg->Index = strtol(tokens.Index.String, NULL, 10);
	if (errno > 0) {
		fprintf(stderr, "Could not convert src register index.\n");
		return 0;
	}

	/* Swizzle */
	if (tokens.Swizzle.Length == 0) {
		src_reg->Swizzle = RC_SWIZZLE_XYZW;
	} else {
		int str_index = 1;
		src_reg->Swizzle = RC_MAKE_SWIZZLE_SMEAR(RC_SWIZZLE_UNUSED);
		if (tokens.Swizzle.String[0] != '.') {
			fprintf(stderr, "First char of swizzle is not valid.\n");
			return 0;
		}
		for (i = 0; i < 4 && str_index < tokens.Swizzle.Length;
							i++, str_index++) {
			if (tokens.Swizzle.String[str_index] == '-') {
				src_reg->Negate |= (1 << i);
				str_index++;
			}
			switch(tokens.Swizzle.String[str_index]) {
			case 'x':
				SET_SWZ(src_reg->Swizzle, i, RC_SWIZZLE_X);
				break;
			case 'y':
				SET_SWZ(src_reg->Swizzle, i, RC_SWIZZLE_Y);
				break;
			case 'z':
				SET_SWZ(src_reg->Swizzle, i, RC_SWIZZLE_Z);
				break;
			case 'w':
				SET_SWZ(src_reg->Swizzle, i, RC_SWIZZLE_W);
				break;
			case '1':
				SET_SWZ(src_reg->Swizzle, i, RC_SWIZZLE_ONE);
				break;
			case '0':
				SET_SWZ(src_reg->Swizzle, i, RC_SWIZZLE_ZERO);
				break;
			case 'H':
				SET_SWZ(src_reg->Swizzle, i, RC_SWIZZLE_HALF);
				break;
			case '_':
				SET_SWZ(src_reg->Swizzle, i, RC_SWIZZLE_UNUSED);
				break;
			default:
				fprintf(stderr, "Unknown src register swizzle: %c\n",
						tokens.Swizzle.String[str_index]);
				return 0;
			}
		}
	}
	DBG("File=%u index=%u swizzle=%x negate=%u abs=%u\n",
			src_reg->File, src_reg->Index, src_reg->Swizzle,
			src_reg->Negate, src_reg->Abs);
	return 1;
}