Ejemplo n.º 1
0
/* Operates on three CHARSXP (internal) arguments. Returns STRSXP vector.
 * Returned strings alternate between "outside" and "inside" blocks.
 * E.g. "Four .(score) and seven .(years)"
 * returns c("Four ", "score", " and seven ", "years")
 */
SEXP find_subst_expressions(SEXP str, SEXP begin_delim, SEXP end_delim) {
  const char *s, *b, *e;
  const char *p, *start;
  const char *before, *begin, *end, *after;
  cetype_t enc;
  step_t step;
  int nBlocks, i;
  SEXP out;

  assert_type(str, CHARSXP);
  assert_type(begin_delim, CHARSXP);
  assert_type(end_delim, CHARSXP);

  step = get_stepper(str, &s, &enc);

  if (getCharCE(begin_delim) != enc) {
    b = Rf_reEnc(CHAR(begin_delim), getCharCE(begin_delim), enc, 0);
  } else {
    b = CHAR(begin_delim);
  }

  if (getCharCE(end_delim) != enc) {
    e = Rf_reEnc(CHAR(end_delim), getCharCE(end_delim), enc, 0);
  } else {
    e = CHAR(end_delim);
  }

  /* Scan once to count how many blocks we need, then scan again (ugh) */
  nBlocks = 0;
  p = s;
  while(p && *p) {
    if ( (p = block_search(p, b, e, NULL, NULL, NULL, NULL, step)) ) {
      nBlocks++;
    }
  }

  out = PROTECT(allocVector(STRSXP, 2*nBlocks+1));
  start = s;
  p = s;
  for (i = 0; i < nBlocks; i++) {
    p = block_search(p, b, e,
                     &before, &begin, &end, &after,
                     step);
    /* extract leading unescaped block, then escaped block */
    if (p) {
      SET_STRING_ELT(out, 2*i, mkCharLenCE(start, before-start, enc));
      SET_STRING_ELT(out, 2*i+1, mkCharLenCE(begin, end-begin, enc));
      start = after;
    }
  }
  /* then the rest of the string. */
  SET_STRING_ELT(out, 2*i, mkCharCE(start, enc));

  UNPROTECT(1);
  return out;
}
Ejemplo n.º 2
0
int ally_get(allies *al, const struct faction *f)
{
    for (; al; al =  al->next) {
        int i = block_search(al, f);
        if (i != BLOCKSIZE) {
            return al->status[i];
        }
    }
    return 0;
}
Ejemplo n.º 3
0
int main (void)
{
    create_list (&list);

    int     i = block_search (list, 4);

    i < list.length ? 
        printf ("key : %d, %c\n", list.r[i].key, list.r[i].other_data) :
        printf ("this key isn't exist!\n");

    return 0;
}
Ejemplo n.º 4
0
void ally_set(allies **p_al, struct faction *f, int status)
{
    while (*p_al) {
        allies *al = *p_al;
        int i = block_search(al, f);
        if (i != BLOCKSIZE) {
            if (status == 0) {
                if (--al->num != i) {
                    al->factions[i] = al->factions[al->num];
                    al->status[i] = al->status[al->num];
                    /* TODO: repair heap up or down */
                }
                else if (al->num == 0) {
                    *p_al = al->next;
                    free(al);
                    return;
                }
            }
            else {
                al->status[i] = status;
            }
            return;
        }
        if (al->num < BLOCKSIZE) {
            if (status > 0) {
                block_insert(al, f, status);
            }
            return;
        }
        p_al = &al->next;
    }
    if (status > 0) {
        *p_al = calloc(1, sizeof(allies));
        block_insert(*p_al, f, status);
    }
}
Ejemplo n.º 5
0
int
main(int argc, char **argv)
{
    argc = 0;
	argv = NULL;

	int i = 0;
	int found = 0;
    /* test set funtionality */
    uint64_t array1[len] = {3,4,7,8,10,13,15,23,27,30};
	uint64_t array2[len] = {1,2,3,6,13,30,41,55,66,70};
	
    /* test linear search */
    while(i!=len)
    {
        if((linear_search(array1, len, array2[i]))==9)
        {
            printf("[LINEAR SEARCH] - Search last entry  - OK\n");
        }

	    if((linear_search(array1, len, array2[i]))==0)
		{
		    printf("[LINEAR SEARCH] - Search first entry - OK\n");
		}
		
		if((linear_search(array1, len, array2[i]))==5)
		{
		    printf("[LINEAR SEARCH] - Search middle entry - OK\n");
		}
		
		if((linear_search(array1, len, 31))!=-1)
		{
		    found = 1;
		}
		
        i++;
    }
	
	if(found == 0)
	{
	    printf("[LINEAR SEARCH] - Search larger entry - OK\n\n");
	}
	
    /* test binary search */
	/* Refresh counters */
	i = 0;
	found = 0;
    while(i!=len)
    {
        if((binary_search(array1, len, array2[i]))==9)
        {
            printf("[BINARY SEARCH] - Search last entry  - OK\n");
        }

	    if((binary_search(array1, len, array2[i]))==0)
		{
		    printf("[BINARY SEARCH] - Search first entry - OK\n");
		}
		
		if((binary_search(array1, len, array2[i]))==5)
		{
		    printf("[BINARY SEARCH] - Search middle entry - OK\n");
		}
		
		if((binary_search(array1, len, 31))!=-1)
		{
		    found = 1;
		}
		
        i++;
    }
	
	if(found == 0)
	{
	    printf("[BINARY SEARCH] - Search larger entry - OK\n\n");
	}
	
    /* test interpolation search */
    /* Refresh counters */
	i = 0;
	found = 0;
    while(i!=len)
    {
        if((interpolation_search(array1, len, array2[i]))==9)
        {
            printf("[INTERPOLATION SEARCH] - Search last entry  - OK\n");
        }

	    if((interpolation_search(array1, len, array2[i]))==0)
		{
		    printf("[INTERPOLATION SEARCH] - Search first entry - OK\n");
		}
		
		if((interpolation_search(array1, len, array2[i]))==5)
		{
		    printf("[INTERPOLATION SEARCH] - Search middle entry - OK\n");
		}
		
		if((interpolation_search(array1, len, 31))!=-1)
		{
		    found = 1;
		}
		
        i++;
    }
	
	if(found == 0)
	{
	    printf("[INTERPOLATION SEARCH] - Search larger entry - OK\n\n");
	}
	
    /* test exponential search */
    /* Refresh counters */
	i = 0;
	found = 0;
    while(i!=len)
    {
        if((exponential_search(array1, len, array2[i]))==9)
        {
            printf("[EXPONENTIAL SEARCH] - Search last entry  - OK\n");
        }

	    if((exponential_search(array1, len, array2[i]))==0)
		{
		    printf("[EXPONENTIAL SEARCH] - Search first entry - OK\n");
		}
		
		if((exponential_search(array1, len, array2[i]))==5)
		{
		    printf("[EXPONENTIAL SEARCH] - Search middle entry - OK\n");
		}
		
		if((exponential_search(array1, len, 31))!=-1)
		{
		    found = 1;
		}
		
        i++;
    }
	
	if(found == 0)
	{
	    printf("[EXPONENTIAL SEARCH] - Search larger entry - OK\n\n");
	}
	
    /* test block search */
    /* Refresh counters */
	i = 0;
	found = 0;
    while(i!=len)
    {
        if((block_search(array1, len, array2[i], bsize))==9)
        {
            printf("[BLOCK SEARCH] - Search last entry  - OK\n");
        }

	    if((block_search(array1, len, array2[i], bsize))==0)
		{
		    printf("[BLOCK SEARCH] - Search first entry - OK\n");
		}
		
		if((block_search(array1, len, array2[i], bsize))==5)
		{
		    printf("[BLOCK SEARCH] - Search middle entry - OK\n");
		}
		
		if((block_search(array1, len, 31, bsize))!=-1)
		{
		    found = 1;
		}
		
        i++;
    }
	
	if(found == 0)
	{
	    printf("[BLOCK SEARCH] - Search larger entry - OK\n\n");
	}
	
	return EXIT_SUCCESS;
}