int main(void) {
     int *a; int n; 
     int r; int random; 
     int d;
     scanf("%d", &n); a = getarray(n);
     scanf("%d", &random); // Randomizer
     scanf("%d", &r); // Elements are 0 - r
     fill(random, r, n, a);
     arrayprint(n, a);
     scanf("%d", &d); // Looking for d
     lin_search(d, n, a);
     insertion_sort(n, a);
     arrayprint(n, a);
     lin_search(d, n, a);
     bin_search(d, n, a);
     return 0;
}
Example #2
0
void backwards(int * trace, int * max_used_color,
               int * vertex_max_color, int * current_vertex,
               int * satur_degree, Graph * graph, tuple * base,
               int * popularity, int * coloring,
               int * depth, int upper_bound, int lower_bound) {
  
  // Se determina la posición en la traza donde se encuentra
  // el vértice desde el cual se hace backtracking
  int vertex_position = lin_search(trace, *current_vertex, *depth);
  
  
  // Se averigua si el vértice de donde se parte
  // el backtracking es la raiz
  if (vertex_position == 0) {
    int vertex_color = graph[trace[vertex_position]].color;
    update_all(trace, graph, base, popularity, *depth,
               vertex_position, satur_degree);
    // Quitamos su color del FC
    graph[trace[vertex_position]].FC[vertex_color] = 0;

    // Se determina el máximo color utilizado hasta ahora
    max_color(popularity, max_used_color, upper_bound);
    
    if (valid_FC(graph, trace[vertex_position],lower_bound+1)) {
      *current_vertex = trace[vertex_position];
      *vertex_max_color = 0;
      return;
    }
    else {
      // Se ha llegado a la raiz y no hay mas colores 
      // posibles que introducir
      *current_vertex = -1;
      return;
    }
  }
  else {
    tuple_list * candidates = NULL;
    
    if (vertex_position == -1) {
      // Significa que el vértice donde quedó forward 
      // tuvo FC vació, por lo tanto no se encontró en 
      // la traza. Se llama directamente label
      candidates = label(graph, *depth + 1,
                         *max_used_color, trace, *current_vertex);
      vertex_position = *depth + 1;
    }
    else {
      // Se logró una coloración completa. Por lo tanto:
      // Se decolorean todos los vértices subiendo en el 
      // árbol hasta llegar al vértice de mínimo rango 
      // con el mayor color usado en la coloración parcial actual
      update_all(trace, graph, base, popularity, *depth,
                 vertex_position, satur_degree);
      
      // Se procede a hacer el etiquetado partiendo del vértice
      // con coloración más alta y de rango mínimo. O partiendo 
      // del vértice cuyo FC se hizo vacío
      candidates = label(graph, vertex_position,
                         *max_used_color, trace, *current_vertex);
    }

    // Se verifica que la lista de candidatos del label
    // sea distinta de Nula. En caso contrario no hay más
    // backtracking que hacer y se consiguió una coloración
    while (candidates != NULL) {
      
      int vertex_color = graph[candidates->vertex].color;
 
      // Se decolorean todos los vértices que están a partir
      // de una posición anterior desde donde se hizo labeling 
      // hasta el vértice de rango máximo entre todos los 
      // etiquetados
      update_all(trace, graph, base, popularity,
                 vertex_position - 1, candidates->position,
                 satur_degree);
 
      // Se elimina de su FC el color que tiene actualmente
      graph[candidates->vertex].FC[vertex_color] = 0;
    
      // Se determina el máximo color utilizado hasta ahora
 
      max_color(popularity, max_used_color, upper_bound);
 
      // Se verifica que el vértice de máximo rango etiquetado
      // no tenga un FC vacío. Al ser su FC no vació se hace
      // retornar el algoritmo
    
      if (valid_FC(graph, candidates->vertex, *max_used_color)) {
        // Se determina la posición en la traza
        // del vértice de mínimo rango que tiene
        // el color máximo utilizado
        det_vertex_max_color(graph, trace,*max_used_color,
                             vertex_max_color, candidates->position);
        // Se indica la posición en la traza del vértice 
        // del cual se parte para hacer forward.
        *current_vertex = candidates->vertex;
        *depth = candidates->position;
        free_tuple_list(candidates);
        return;
      }
      else {
        tuple_list * tmp = candidates;
        candidates = candidates->next;
        tmp->next = NULL;
        free_tuple_list(tmp);
      }
    }
  }
  // La lista CP está vacía por lo tanto se retorna el
  // algoritmo con current_vertex siendo cero.
  *current_vertex = -1;
  return;
}
Example #3
0
/* EFUN: "Reverse String Search" */
f_rsrch()
{	return (lin_search (1));
}
Example #4
0
/* EFUN: "String Search" */
f_srch()
{	return (lin_search (0));
}
Example #5
0
main(int   argc,
     char  **argv)
{
    extern int	    optind;	/* for use of getopt() */
    extern char	    *optarg;	/* for use of getopt() */
    int		    ch;		/* command-line option letter */

    static char	    *ProgName = "esig2fea";	/* name of this program */
    static char	    *Version = SCCS_VERSION;	/* program SCCS version */
    static char	    *Date = SCCS_DATE;		/* program SCCS date */

    char	    **field_names = NULL;
    int		    num_fields = 0;
    int		    alloc_fields = 0;

    int		    rflag = NO;	/* -r option specified? */
    char	    *rrange;	/* arguments of -r option */
    long	    start_rec;	/* starting record number */
    long	    end_rec;	/* ending record number */
    long	    num_recs;	/* number of records to read
				   (0 means all up to end of file) */
    long	    num_read;	/* number of records actually read */

    char	    *iname;	/* input file name */
    FILE	    *ifile;	/* input stream */
    FieldList	    list;	/* input field list */
    int		    inord;	/* input: field order or type order? */
    FieldSpec	    **ifields;	/* input fields in field or type order */

    char	    *subtype = NULL;		/* FEA subtype name */
    int		    subtype_code = NONE;	/* numeric subtype code */
    FieldSpec	    *fld;	/* spec of various special fields */

    char	    *oname;	/* output file name */
    FILE	    *ofile;	/* output stream */
    struct header   *ohd;	/* output file header */
    struct fea_data *orec;	/* output record */
    int		    outord = TYPE_ORDER;

    char	    *version;	/* version from input preamble */
    int		    arch;	/* architecture from input preamble */
    long	    pre_size;	/* preamble size */
    long	    hdr_size;	/* header size (bytes) from preamble */
    long	    rec_size;	/* record size from preamble */

    double	    rec_freq;
    double	    start_time_offset;
    double	    *data;
    long	    len, i;


    struct header   *source;	/* embedded source-file header */


    while ((ch = getopt(argc, argv, "f:r:x:FT:")) != EOF)
	switch (ch)
	{
	case 'f':
	    if (num_fields >= alloc_fields)
	    {
		size_t	size;

		alloc_fields = num_fields + 1 + num_fields/2;
		size = (alloc_fields + 1) * sizeof(char *);
		field_names = (char **)
		    ((field_names == NULL)
		     ? malloc(size)
		     : realloc(field_names, size));
	    }
	    field_names[num_fields++] = optarg;
	    field_names[num_fields] = NULL;
	    break;
	case 'r':
	    rflag = YES;
	    rrange = optarg;
	    break;
	case 'x':
	    debug_level = atoi(optarg);
	    break;
	case 'F':
	    outord = FIELD_ORDER;
	    break;
	case 'T':
	    subtype = optarg;
	    break;
	default:
	    SYNTAX;
	    break;
	}

    if (argc - optind > 2)
    {
	fprintf(stderr,
		"%s: too many file names specified.\n", ProgName);
	SYNTAX;
    }
    if (argc - optind < 2)
    {
	fprintf(stderr,
		"%s: too few file names specified.\n", ProgName);
	SYNTAX;
    }


    DebugMsgLevel = debug_level;
    DebugMsgFunc = DebugPrint;

    iname = argv[optind++];
    list = OpenIn(iname, &version,
		  &arch, &pre_size, &hdr_size, &rec_size, &ifile);
    REQUIRE(list != NULL, "read header failed");
    if (ifile == stdin)
	iname = "<stdin>";

    oname = argv[optind++];

    start_rec = 0;
    end_rec = LONG_MAX;
    num_recs = 0;	/* 0 means continue to end of file */

    if (rflag)
    {
	lrange_switch(rrange, &start_rec, &end_rec, 0);
	if (end_rec != LONG_MAX)
	    num_recs = end_rec - start_rec + 1;
    }

    REQUIRE(start_rec >= 0, "can't start before beginning of file");
    REQUIRE(end_rec >= start_rec, "empty range of records specified");

    if (debug_level)
	fprintf(stderr,
		"start_rec: %ld.  end_rec: %ld.  num_recs: %ld.\n",
		start_rec, end_rec, num_recs);

    REQUIRE(GetFieldOrdering(list, &inord),
	    "cant get field ordering of input");

    switch (inord)
    {
    case TYPE_ORDER:
	if (debug_level)
	    fprintf(stderr, "making type-ordered field array.\n");
	ifields = TypeOrder(list);
	break;
    case FIELD_ORDER:
	if (debug_level)
	    fprintf(stderr, "making field-ordered field array.\n");
	ifields = FieldOrder(list);
	break;
    default:
	REQUIRE(0, "input order neither TYPE_ORDER nor FIELD_ORDER");
	break;
    }

    ohd = FieldList_to_fea(list, &orec, field_names, FALSE);

    REQUIRE(ohd != NULL,
	    "failure converting input field list to header & record struct");

    if (subtype != NULL)
    {
	subtype_code = lin_search(fea_file_type, subtype);

	if (subtype_code == -1)
	    fprintf(stderr, "%s: unknown FEA file subtype \"%s\" ignored.\n",
		    ProgName, subtype);
	else
	    ohd->hd.fea->fea_type = subtype_code;
    }

    if (outord == FIELD_ORDER)
	ohd->hd.fea->field_order = YES;

    fld = FindField(list, "recordFreq");

    if (fld != NULL && fld->occurrence == GLOBAL && fld->data != NULL)
    {
	(void) type_convert(1L, (char *) fld->data, ElibTypeToEsps(fld->type),
			    (char *) &rec_freq, FDOUBLE, (void (*)()) NULL);

	*add_genhd_d("record_freq", NULL, 1, ohd) = rec_freq;
    }
    else
	rec_freq = 1.0;

    fld = FindField(list, "startTime");

    if (fld != NULL
	&& fld->occurrence == GLOBAL && fld->data != NULL && rec_freq != 0)
    {
	start_time_offset = start_rec / rec_freq;

	len = FieldLength(fld);
	data = (double *) type_convert(len, (char *) fld->data,
				       ElibTypeToEsps(fld->type),
				       (char *) NULL, FDOUBLE,
				       (void (*)()) NULL);

	if (start_time_offset != 0)
	{
	    for (i = 0; i < len; i++)
		data[i] += start_time_offset;
	}

	(void) add_genhd_d("start_time", data, len, ohd);
    }

    (void) strcpy(ohd->common.prog, ProgName);
    (void) strcpy(ohd->common.vers, Version);
    (void) strcpy(ohd->common.progdate, Date);

    source = FieldList_to_fea(list, NULL, NULL, TRUE);
    add_source_file(ohd, savestring(iname), source);

    add_comment(ohd, get_cmd_line(argc, argv));

    oname = eopen(ProgName,
		  oname, "w", NONE, NONE, NULL, &ofile);
    write_header(ohd, ofile);

    num_read = SkipRecs(ifile, start_rec,
			RecordSize(list, arch), ifields, arch);

    if (num_read != start_rec)
    {
	fprintf(stderr,
		"%s: couldn't reach starting record; only %ld skipped.\n",
		ProgName, num_read);
	exit(0);
    }

    for ( ;
	 num_read <= end_rec && ReadRecord(ifields, arch, ifile);
	 num_read++)
    {
	put_fea_rec(orec, ohd, ofile);
    }

    if (num_read <= end_rec && num_recs != 0)
	fprintf(stderr, "esig2fea: only %ld records read.\n",
		num_read - start_rec);

    exit(0);
    /*NOTREACHED*/
}