Esempio n. 1
0
int array_append(struct array* a, void* v) {
	if (a == NULL) {
		return EINVAL;
	}

	return array_insert(a, v, a->num);
}
Esempio n. 2
0
void int_node(Token *tk)
{// integer data type declaration statement without initialization
	tk->stype = Int;
	next(tk);
	if (tk->id == ';' || !tk->id)
		PANIC("useless typename in empty declaration", tk->line);
	while (tk->id) {
		if (tk->id == Id) {
			if (peek(tk) != '[') {
				if (lookup(tk->beg, Int, tk->line, true) < 0)
					int_insert(tk->beg);
				else PANIC("redefinition", tk->line);
			} else {
				char *beg = (char *)malloc(VAR_LENGTH * sizeof(char));
				strcpy(beg, tk->beg);
				Array *arr = (Array *)malloc(sizeof(Array));
				array_dec(tk, arr);
				if (lookup(beg, Arr, tk->line, true) < 0)
					array_insert(beg, arr);
				else PANIC("redefinition", tk->line);
				free(beg);
				free(arr);
			}
			next(tk);
			if (tk->id == ';') break;
			else if (tk->id == ',') next(tk);
		} else PANIC("illegal declaration", tk->line);
	}
	if (!tk->id) PANIC("expected semicolon", tk->line);
	tk->stype = 0;
}
Esempio n. 3
0
void createExpression(array_t * expression, YAP_Term t)
/* returns the expression as an array_t of terms (cubes) starting from the prolog lists of terms
each term is an array_t of factors obtained from a prolog list of factors
each factor is a couple (index of variable, index of value) obtained from a prolog list containing 
two integers
*/
{
     	YAP_Term  termTerm,factorTerm;
	factor f;	
	int i,j;
	array_t * term;

	i=0;
	while(YAP_IsPairTerm(t))
	{
		term=array_alloc(factor,0);
		termTerm=YAP_HeadOfTerm(t);
		j=0;
		while(YAP_IsPairTerm(termTerm))
		{
			factorTerm=YAP_HeadOfTerm(termTerm);
			f.var=YAP_IntOfTerm(YAP_HeadOfTerm(factorTerm));
			f.value=YAP_IntOfTerm(YAP_HeadOfTerm(YAP_TailOfTerm(factorTerm)));
			array_insert(factor,term,j,f);
			termTerm=YAP_TailOfTerm(termTerm);
			j++;
		}
		array_insert(array_t *,expression,i,term);
		t=YAP_TailOfTerm(t);
		i++;
	}
}
Esempio n. 4
0
int rotate_log(struct rotator *rr,char *filename) {
  char *dir,*base,*oldname,*newname;
  DIR *dirh;
  struct dirent des,*de;
  struct array *names;
  struct assoc *changes,*orders;
  int order,i,n;

  dirbasename(filename,&dir,&base);
  log_debug(("Rotating in '%s'",dir));
  dirh = opendir(dir);
  if(!dirh) {
    log_error(("Could not rotate log file"));
    return 1;
  }
  names = array_create(type_free,0);
  changes = assoc_create(0,0,type_free,0);
  orders = assoc_create(0,0,type_free,0);
  while(1) {
    if(readdir_r(dirh,&des,&de)) {
      log_error(("Problem reading directory during log rotation"));
      break;
    }
    if(!de) { break; }
    order = matching_file(rr,filename,de->d_name,&newname);
    if(order == MATCH_IGNORE) { continue; }
    if(order == MATCH_DELETE) {
      log_debug(("delete %s",de->d_name));
      unlink(de->d_name);
    } else {
      oldname = strdup(de->d_name);
      array_insert(names,oldname);
      assoc_set(changes,oldname,newname);
      assoc_set(orders,oldname,make_int(order));
    }
  }
  if(closedir(dirh)) {
    log_error(("Could not closedir during log rotation!"));
    /* But continue: what else to do? */
  }
  array_sort(names,sort_names,orders);
  n = array_length(names);
  for(i=0;i<n;i++) {
    oldname = array_index(names,i);
    newname = assoc_lookup(changes,oldname);
    log_warn(("name=%s -> %s",oldname,newname));
    rename_file(rr,dir,oldname,newname);
  }
  assoc_release(changes);
  assoc_release(orders);
  array_release(names);
  free(dir);
  free(base);
  return 0;
}
Esempio n. 5
0
//use recursion
void array_insert(CArray *array,int index,int element){
    int temp;
    if (index < array->size) {
        //swap element and array[index]
        temp = array->p[index];
        array->p[index] = element;
        element = temp;
        array_insert(array,++index,element);
    }
    else array_append(array, element);
};
Esempio n. 6
0
/**********************************************************************
 * improve_one_blob
 *
 * Start with the current word of blobs and its classification.  Find
 * the worst blobs and try to divide it up to improve the ratings.
 *********************************************************************/
CHOICES_LIST improve_one_blob(TWERD *word,
                              CHOICES_LIST char_choices,
                              int fx,
                              INT32 *blob_number,
                              SEAMS *seam_list,
                              DANGERR *fixpt,
                              STATE *this_state,
                              STATE *correct_state,
                              INT32 pass) {
  TBLOB *pblob;
  TBLOB *blob;
  INT16 x = 0;
  float rating_ceiling = MAX_FLOAT32;
  CHOICES answer;
  SEAM *seam;

  do {
    *blob_number = select_blob_to_split (char_choices, rating_ceiling);
    if (*blob_number == -1)
      return (NULL);

    seam = attempt_blob_chop (word, *blob_number, *seam_list);
    if (seam != NULL)
      break;
    /* Must split null blobs */
    answer = (CHOICES) array_value (char_choices, *blob_number);
    if (answer == NIL)
      return (NULL);             /* Try different blob */
    rating_ceiling = best_probability (answer);
  }
  while (!blob_skip);
  /* Split OK */
  for (blob = word->blobs, pblob = NULL; x < *blob_number; x++) {
    pblob = blob;
    blob = blob->next;
  }

  *seam_list =
    insert_seam (*seam_list, *blob_number, seam, blob, word->blobs);

  free_choices ((CHOICES) array_value (char_choices, *blob_number));

  answer =
    classify_blob (pblob, blob, blob->next, NULL, fx, "improve 1:", Red,
    this_state, correct_state, pass, *blob_number);
  char_choices = array_insert (char_choices, *blob_number, answer);

  answer =
    classify_blob (blob, blob->next, blob->next->next, NULL, fx, "improve 2:",
    Yellow, this_state, correct_state, pass, *blob_number + 1);
  array_value (char_choices, *blob_number + 1) = (char *) answer;

  return (char_choices);
}
Esempio n. 7
0
static void state_insert(view *V, int i, state *s)
{
    state *p;

    if ((p = (state *) array_insert(V->list, i, V->n, sizeof (state))))
    {
        V->list = p;
        V->n   += 1;

        memcpy(V->list + i, s, sizeof (state));
    }
}
Esempio n. 8
0
int main()
{
    CArray array;
    array_initial(&array);
    
    array_recap(&array, 10);
    assert(array_capacity(&array) == 10);
    
    //////////////////////////////////////////////////////////////////////////
    for (int i = 0; i < 20; ++i)
    {
        array_append(&array, i);
    }
    assert(array_size(&array) == 20);
    
//    printarray(&array);
    
    for (int i = 0; i < array_size(&array); ++i)
    {
        assert(*array_at(&array, i) == i);
    }
    
    //////////////////////////////////////////////////////////////////////////
    CArray array2, array3;
    array_initial(&array2);
    array_initial(&array3);
    
    array_copy(&array, &array2);

    assert(array_compare(&array, &array2) == true);
    array_copy(&array, &array3);
    assert(array_compare(&array, &array3) == true);
//    printarray(&array2);
    
    //////////////////////////////////////////////////////////////////////////
    array_insert(&array2, 2, 3);
    assert(array_compare(&array, &array2) == false);
//    printarray(&array2);

    
    //////////////////////////////////////////////////////////////////////////
    *array_at(&array3, 2) = 5;
    assert(array_compare(&array, &array3) == false);
//    printarray(&array3);

    //////////////////////////////////////////////////////////////////////////
    array_destroy(&array);
    array_destroy(&array2);
    array_destroy(&array3);
    
    return 0;
}
Esempio n. 9
0
void createVars(array_t * vars, YAP_Term t,DdManager * mgr, array_t * bVar2mVar,int create_dot,  char inames[1000][20])
/* adds the boolean variables to the BDD and returns
an array_t containing them (array_t is defined in the util library of glu)
returns also the names of the variables to be used to save the ADD in dot format
 */
{
     	YAP_Term  varTerm,probTerm;
	int varIndex,nVal,i,b;
	variable v;	
	char numberVar[10],numberBit[10];
	double p;
	b=0;

	while(YAP_IsPairTerm(t))
	{
		varTerm=YAP_HeadOfTerm(t);
		varIndex=YAP_IntOfTerm(YAP_HeadOfTerm(varTerm));

      		varTerm=YAP_TailOfTerm(varTerm);
		nVal=YAP_IntOfTerm(YAP_HeadOfTerm(varTerm));
		varTerm=YAP_TailOfTerm(varTerm);
      		probTerm=YAP_HeadOfTerm(varTerm);
		v.nVal=nVal;
		v.nBit=(int)ceil(log(nVal)/log(2));
		v.probabilities=array_alloc(double,0);
		v.booleanVars=array_alloc(DdNode *,0);
		for (i=0;i<nVal;i++)
		{
			if (create_dot)
			{
				strcpy(inames[b+i],"X");
				sprintf(numberVar,"%d",varIndex);
				strcat(inames[b+i],numberVar);
				strcat(inames[b+i],"_");
				sprintf(numberBit,"%d",i);
				strcat(inames[b+i],numberBit);
			}
			p=YAP_FloatOfTerm(YAP_HeadOfTerm(probTerm));
			array_insert(double,v.probabilities,i,p);
			probTerm=YAP_TailOfTerm(probTerm);
			array_insert(DdNode *,v.booleanVars,i,Cudd_bddIthVar(mgr,b+i));
			array_insert(int,bVar2mVar,b+i,varIndex);
		}
		Cudd_MakeTreeNode(mgr,b,nVal,MTR_FIXED);
		b=b+nVal;
		array_insert(variable,vars,varIndex,v);
		t=YAP_TailOfTerm(t);
	}
}
Esempio n. 10
0
void array_quick(int* arr, int len)
{
	int k;
	if(len <= 1)
	{
		return;
	}
	if(len <= 5)
	{
		array_insert(arr, len);
	}
	k = array_partion(arr, len);
	array_quick(arr, k);
	array_quick(&arr[k+1], len - k - 1);
}
/**
 * Load all available test suites, or optionally only selected ones.
 */
static array_t *load_suites(test_configuration_t configs[],
							test_runner_init_t init, char *cfg)
{
	array_t *suites;
	bool old = FALSE;
	int i;

	library_init(cfg, "test-runner");

	test_setup_handler();

	if (init && !init(TRUE))
	{
		library_deinit();
		return NULL;
	}
	lib->plugins->status(lib->plugins, LEVEL_CTRL);

	if (lib->leak_detective)
	{
		old = lib->leak_detective->set_state(lib->leak_detective, FALSE);
	}

	suites = array_create(0, 0);

	for (i = 0; configs[i].suite; i++)
	{
		if (configs[i].feature.type == 0 ||
			lib->plugins->has_feature(lib->plugins, configs[i].feature))
		{
			array_insert(suites, -1, configs[i].suite());
		}
	}
	filter_suites(suites);

	if (lib->leak_detective)
	{
		lib->leak_detective->set_state(lib->leak_detective, old);
	}

	if (init)
	{
		init(FALSE);
	}
	library_deinit();

	return suites;
}
Esempio n. 12
0
uint8_t _update_nlbl_map(
  graph_t *g, uint16_t ninputs, uint16_t inidx, nlbl_map_t *map) {

  graph_label_t *lbl;
  int64_t        i;
  int64_t        tmp;
  uint32_t       j;
  uint32_t       lblidx;
  uint32_t       nnodes;
  array_t        nodeids;
  array_t       *pnodeids;

  tmp = -1;

  nnodes = graph_num_nodes(g);

  array_set(&(map->sizes), inidx, &nnodes);

  for (i = 0; i < nnodes; i++) {
    
    lbl = graph_get_nodelabel(g, i);

    switch (array_insert_sorted(&(map->labels), lbl, 1, &lblidx)) {

      case 0:
        if (array_create(&nodeids, sizeof(int64_t), ninputs)) goto fail; 
        for (j = 0; j < ninputs; j++) array_set(&nodeids, j, &tmp);
        
        if (array_insert(&(map->nodeids), lblidx, &nodeids))  goto fail;

        break;

      case 1:
        break;
      default: goto fail;
    }

    pnodeids = array_getd(&(map->nodeids), lblidx);
    array_set(pnodeids, inidx, &i);
  }

  return 0;
fail:
  return 1;
}
Esempio n. 13
0
static
void ds_file_group_insert_ds_file_info_sorted(
		struct ctf_fs_ds_file_group *ds_file_group,
		struct ctf_fs_ds_file_info *ds_file_info)
{
	guint i;

	/* Find the spot where to insert this ds_file_info. */
	for (i = 0; i < ds_file_group->ds_file_infos->len; i++) {
		struct ctf_fs_ds_file_info *other_ds_file_info =
			g_ptr_array_index(ds_file_group->ds_file_infos, i);

		if (ds_file_info->begin_ns < other_ds_file_info->begin_ns) {
			break;
		}
	}

	array_insert(ds_file_group->ds_file_infos, ds_file_info, i);
}
Esempio n. 14
0
/**
 * @name insert_seam
 *
 * Add another seam to a collection of seams at a particular location
 * in the seam array.
 */
SEAMS insert_seam(SEAMS seam_list,
                  int index,
                  SEAM *seam,
                  TBLOB *left_blob,
                  TBLOB *first_blob) {
  SEAM *test_seam;
  TBLOB *blob;
  int test_index;
  int list_length;

  list_length = array_count(seam_list);
  for (test_index=0, blob=first_blob->next;
       test_index < index;
       test_index++, blob=blob->next) {
    test_seam = (SEAM *) array_value(seam_list, test_index);
    if (test_index + test_seam->widthp >= index) {
      test_seam->widthp++;       /*got in the way */
    } else if (test_seam->widthp + test_index == index - 1) {
      test_seam->widthp = account_splits_right(test_seam, blob);
      if (test_seam->widthp < 0) {
        cprintf("Failed to find any right blob for a split!\n");
        print_seam("New dud seam", seam);
        print_seam("Failed seam", test_seam);
      }
    }
  }
  for (test_index=index, blob=left_blob->next;
       test_index < list_length;
       test_index++, blob=blob->next) {
    test_seam = (SEAM *) array_value(seam_list, test_index);
    if (test_index - test_seam->widthn < index) {
      test_seam->widthn++;       /*got in the way */
    } else if (test_index - test_seam->widthn == index) {
      test_seam->widthn = account_splits_left(test_seam, first_blob, blob);
      if (test_seam->widthn < 0) {
        cprintf("Failed to find any left blob for a split!\n");
        print_seam("New dud seam", seam);
        print_seam("Failed seam", test_seam);
      }
    }
  }
  return (array_insert (seam_list, index, seam));
}
Esempio n. 15
0
/*
 * task_fd
 *
 * make sure the task has a hold on this fd.
 */
static scamper_fd_t *task_fd(scamper_task_t *t, scamper_fd_t *fd)
{
  if(fd == NULL)
    return NULL;

  if(array_find((void **)t->fds, t->fdc, fd, (array_cmp_t)task_fd_cmp) == NULL)
    {
      if(array_insert((void ***)&t->fds, &t->fdc, fd,
		      (array_cmp_t)task_fd_cmp) != 0)
	{
	  scamper_fd_free(fd);
	  return NULL;
	}
    }
  else
    {
      /* already have a hold of the fd */
      scamper_fd_free(fd);
    }
  return fd;
}
Esempio n. 16
0
int scamper_dealias_prefixscan_xs_add(scamper_dealias_t *dealias,
				      scamper_addr_t *addr)
{
  scamper_dealias_prefixscan_t *prefixscan = dealias->data;
  int tmp;

  if(array_find((void **)prefixscan->xs, prefixscan->xc, addr,
		(array_cmp_t)scamper_addr_cmp) != NULL)
    return 0;

  if((tmp = prefixscan->xc) == 65535)
    return -1;

  if(array_insert((void ***)&prefixscan->xs, &tmp, addr,
		  (array_cmp_t)scamper_addr_cmp) != 0)
    return -1;

  scamper_addr_use(addr);
  prefixscan->xc++;
  return 0;
}
Esempio n. 17
0
void func_call(struct context *context, enum Opcode op, struct byte_array *program, struct variable *indexable)
{
    struct variable *func = context->runtime ? (struct variable*)variable_pop(context): NULL;

    struct variable *s = src(context, op, program);
    if (!context->runtime)
        return;

    if (indexable)
        array_insert(s->list, 0, indexable); // self

    vm_call_src(context, func);

    struct variable *result = (struct variable*)stack_peek(context->operand_stack, 0);
    bool resulted = (result && result->type == VAR_SRC);

    if (!resulted) { // need a result for an expression, so pretend it returned nil
        struct variable *v = variable_new_src(context, 0);
        array_add(v->list, variable_new_nil(context));
        stack_push(context->operand_stack, v);
    }
}
Esempio n. 18
0
const char *quark(const char *str)
{
  Array *bucket;
  char *value;
  size_t index;

  init_quarks();

  bucket = quarks->items[hash_function(str, 0) % HASH_SIZE];
  value = array_find_not(bucket, (array_item_predicate_t)strcmp, str);

  if (!value)
    {
      value = strdup(str);
      array_append(bucket, value);

      index = array_lower_bound(quarks_index, value, ptr_less);
      array_insert(quarks_index, index, value);
    }

  return value;
}
Esempio n. 19
0
static void push_list(struct context *context, struct byte_array *program)
{
    int32_t num_items = serial_decode_int(program);
    DEBUGPRINT("LST %d", num_items);
    if (!context->runtime)
        VM_DEBUGPRINT("\n");
    struct array *items = array_new();

    struct map *map = NULL;
    while (num_items--) {
        struct variable* v = variable_pop(context);
        if (v->type == VAR_MAP) {
            if (!map)
                map = map_new(context, NULL);
            map_update(map, v->map); // mapped values are stored in the map, not list
        }
        else
            array_insert(items, 0, v);
    }
    struct variable *list = variable_new_list(context, items);
    list->map = map;
    DEBUGPRINT(": %s\n", variable_value_str(context, list));
    variable_push(context, list);
}
Esempio n. 20
0
bool array_insert_int(array_t *array, uint64_t key, void *value) {
	return array_insert(array, 8, (uint8_t*)&key, value, true);
}
Esempio n. 21
0
bool array_insert_int(array_t *array, uint64_t key, void *value) {
	MAKE_KEY_BIGENDIAN();
	return array_insert(array, 8, (uint8_t*)&real_key, value, true);
}
Esempio n. 22
0
/// Adds an item to the end of the array. If \a item is 0, an unitialized item
/// is added to the array that may be populated by different means.
/// \return Returns a pointer to the location in the array.
void*
array_add(array_t *self, const void *item) {
	assert(self);
	return array_insert(self, self->size, item);
}
Esempio n. 23
0
END_TEST

START_TEST(test_enumerate)
{
	array_t *array;
	int i, *x, y[6] = {0, 1, 2, 3, 4, 5};
	enumerator_t *enumerator;

	array = array_create(sizeof(y[0]), 0);

	array_insert(array, ARRAY_TAIL, &y[0]);
	array_insert(array, ARRAY_TAIL, &y[1]);
	array_insert(array, ARRAY_TAIL, &y[2]);
	array_insert(array, ARRAY_TAIL, &y[3]);
	array_insert(array, ARRAY_TAIL, &y[4]);
	array_insert(array, ARRAY_TAIL, &y[5]);

	ck_assert_int_eq(array_count(array), 6);

	/* 0, 1, 2, 3, 4, 5 */

	i = 0;
	enumerator = array_create_enumerator(array);
	while (enumerator->enumerate(enumerator, &x))
	{
		ck_assert_int_eq(*x, y[i]);
		i++;
	}
	enumerator->destroy(enumerator);
	ck_assert_int_eq(i, 6);

	i = 0;
	enumerator = array_create_enumerator(array);
	while (enumerator->enumerate(enumerator, &x))
	{
		ck_assert_int_eq(*x, y[i]);
		if (i == 0 || i == 3 || i == 5)
		{
			array_remove_at(array, enumerator);
		}
		i++;
	}
	enumerator->destroy(enumerator);
	ck_assert_int_eq(i, 6);
	ck_assert_int_eq(array_count(array), 3);

	/* 1, 2, 4 */

	i = 0;
	enumerator = array_create_enumerator(array);
	while (enumerator->enumerate(enumerator, &x))
	{
		switch (i++)
		{
			case 0:
				ck_assert_int_eq(*x, y[1]);
				break;
			case 1:
				ck_assert_int_eq(*x, y[2]);
				break;
			case 2:
				ck_assert_int_eq(*x, y[4]);
				break;
			default:
				ck_assert(0);
		}
	}
	enumerator->destroy(enumerator);

	array_compress(array);

	i = 0;
	enumerator = array_create_enumerator(array);
	while (enumerator->enumerate(enumerator, &x))
	{
		switch (i++)
		{
			case 0:
				ck_assert_int_eq(*x, y[1]);
				break;
			case 1:
				ck_assert_int_eq(*x, y[2]);
				break;
			case 2:
				ck_assert_int_eq(*x, y[4]);
				break;
			default:
				ck_assert(0);
		}
	}
	enumerator->destroy(enumerator);

	array_destroy(array);
}
Esempio n. 24
0
int scamper_dealias_radargun_fudge(scamper_dealias_t *dealias,
				   scamper_dealias_probedef_t *def,
				   scamper_dealias_probedef_t **defs, int *cnt,
				   int fudge)
{
  scamper_dealias_radargun_t *rg = dealias->data;
  scamper_dealias_probe_t *pr, *pr_a, *pr_b;
  scamper_dealias_reply_t *re, *re_a, *re_b, *re_c;
  dealias_resolv_t *dr = NULL;
  dealias_resolv_t *drd;
  uint32_t pid, x;
  int i, j, k, bs, inseq, d = 0;

  if(dealias->method != SCAMPER_DEALIAS_METHOD_RADARGUN)
    goto err;

  if((dr = malloc_zero(sizeof(dealias_resolv_t) * rg->probedefc)) == NULL)
    goto err;

  for(x=0; x<dealias->probec; x++)
    {
      pr = dealias->probes[x];
      pid = pr->def->id;

      /*
       * if this probedef has already been determined to be useless for
       * alias resolution, skip it
       */
      if(dr[pid].probec < 0)
	continue;

      if(pr->replyc > 1)
	{
	  if(dr[pid].probes != NULL)
	    free(dr[pid].probes);
	  dr[pid].probec = -1;

	  if(pr->def == def)
	    goto done;
	  continue;
	}

      /* total number of probes transmitted */
      dr[pid].probet++;

      if(pr->replyc == 0)
	continue;

      re = pr->replies[0];

      /*
       * with three replies, do some basic checks to see if we should
       * continue considering this probedef.
       */
      if(dr[pid].probec == 2)
	{
	  pr_a = dr[pid].probes[0];
	  pr_b = dr[pid].probes[1];
	  re_a = pr_a->replies[0];
	  re_b = pr_b->replies[0];

	  if((re->ipid == pr->ipid && re_a->ipid == pr_a->ipid &&
	      re_b->ipid == pr_b->ipid) ||
	     (re->ipid == re_a->ipid && re->ipid == re_b->ipid))
	    {
	      free(dr[pid].probes);
	      dr[pid].probec = -1;

	      if(pr->def == def)
		goto done;
	      continue;
	    }
	}

      if(array_insert((void ***)&dr[pid].probes,&dr[pid].probec,pr,NULL) != 0)
	goto err;
    }

  /* figure out if we should byteswap the ipid sequence */
  if(dr[def->id].probec < 3)
    goto done;
  re_a = dr[def->id].probes[0]->replies[0];
  re_b = dr[def->id].probes[1]->replies[0];
  re_c = dr[def->id].probes[2]->replies[0];
  if(re_a->ipid < re_b->ipid)
    i = re_b->ipid - re_a->ipid;
  else
    i = 0x10000 + re_b->ipid - re_a->ipid;
  if(re_b->ipid < re_c->ipid)
    i += re_c->ipid - re_b->ipid;
  else
    i += 0x10000 + re_c->ipid - re_b->ipid;
  if(byteswap16(re_a->ipid) < byteswap16(re_b->ipid))
    j = byteswap16(re_b->ipid) - byteswap16(re_a->ipid);
  else
    j = 0x10000 + byteswap16(re_b->ipid) - byteswap16(re_a->ipid);
  if(byteswap16(re_b->ipid) < byteswap16(re_c->ipid))
    j += byteswap16(re_c->ipid) - byteswap16(re_b->ipid);
  else
    j += 0x10000 + byteswap16(re_c->ipid) - byteswap16(re_b->ipid);
  if(i < j)
    bs = 0;
  else
    bs = 1;

  /* for each probedef, consider if it could be an alias */
  drd = &dr[def->id]; d = 0;
  for(pid=0; pid<rg->probedefc; pid++)
    {
      if(&rg->probedefs[pid] == def || dr[pid].probec < 3)
	continue;

      j = 0; k = 0;

      /* get the first ipid */
      if(timeval_cmp(&drd->probes[j]->tx, &dr[pid].probes[k]->tx) < 0)
	pr_a = drd->probes[j++];
      else
	pr_a = dr[pid].probes[k++];

      for(;;)
	{
	  if(timeval_cmp(&drd->probes[j]->tx, &dr[pid].probes[k]->tx) < 0)
	    pr_b = drd->probes[j++];
	  else
	    pr_b = dr[pid].probes[k++];

	  if((inseq = dealias_fudge_inseq(pr_a, pr_b, bs, fudge)) == 0)
	    break;

	  if(j == drd->probec || k == dr[pid].probec)
	    break;
	}

      /*
       * if the pairs do not appear to have insequence IP-ID values, then
       * abandon
       */
      if(inseq == 0)
	continue;

      defs[d++] = &rg->probedefs[pid];
      if(d == *cnt)
	break;
    }

 done:
  *cnt = d;
  for(x=0; x<rg->probedefc; x++)
    if(dr[x].probec > 0)
      free(dr[x].probes);
  return 0;

 err:
  if(dr != NULL)
    {
      for(x=0; x<rg->probedefc; x++)
	if(dr[x].probec > 0)
	  free(dr[x].probes);
    }
  return -1;
}
Esempio n. 25
0
END_TEST

START_TEST(test_append_obj)
{
	array_t *array;
	int i, x, y[6] = {0, 1, 2, 3, 4, 5};

	array = array_create(sizeof(y[0]), 0);

	for (i = 0; i < 4; i++)
	{
		ck_assert_int_eq(array_count(array), 0);

		array_insert(array, ARRAY_HEAD, &y[3]);
		array_insert(array, ARRAY_TAIL, &y[4]);
		ck_assert_int_eq(array_count(array), 2);;

		/* 3, 4 */

		ck_assert(array_get(array, ARRAY_HEAD, &x));
		ck_assert_int_eq(x, 3);
		ck_assert(array_get(array, 1, &x));
		ck_assert_int_eq(x, 4);
		ck_assert(array_get(array, ARRAY_TAIL, &x));
		ck_assert_int_eq(x, 4);
		ck_assert(!array_get(array, 3, &x));

		array_insert(array, ARRAY_HEAD, &y[1]);
		array_insert(array, 1, &y[2]);
		ck_assert_int_eq(array_count(array), 4);

		/* 1, 2, 3, 4 */

		array_insert(array, ARRAY_TAIL, &y[5]);
		array_insert(array, ARRAY_HEAD, &y[0]);
		ck_assert_int_eq(array_count(array), 6);

		/* 0, 1, 2, 3, 4, 5 */

		ck_assert(array_remove(array, ARRAY_TAIL, &x));
		ck_assert_int_eq(x, 5);
		ck_assert(array_remove(array, 4, &x));
		ck_assert_int_eq(x, 4);

		if (i < 3)
		{
			array_compress(array);
		}

		/* 0, 1, 2, 3 */

		ck_assert(array_remove(array, ARRAY_HEAD, &x));
		ck_assert_int_eq(x, 0);
		ck_assert(array_remove(array, ARRAY_HEAD, &x));
		ck_assert_int_eq(x, 1);

		if (i < 2)
		{
			array_compress(array);
		}

		/* 2, 3 */

		ck_assert(array_remove(array, ARRAY_TAIL, &x));
		ck_assert_int_eq(x, 3);
		ck_assert(array_remove(array, ARRAY_HEAD, &x));
		ck_assert_int_eq(x, 2);

		if (i < 1)
		{
			array_compress(array);
		}

		ck_assert_int_eq(array_count(array), 0);

		ck_assert(array_remove(array, ARRAY_HEAD, NULL) == FALSE);
		ck_assert(array_remove(array, ARRAY_TAIL, NULL) == FALSE);
	}

	array_destroy(array);
}
Esempio n. 26
0
int main(int argc, const char* argv[]) {
    array_t(int) a = NULL;
    test(array_size(a) == 0);
    test(array_capacity(a) == 0);

    array_alloc(a, 0, destructed_element_count_destructor);
    test(array_size(a) == 0);
    test(array_capacity(a) == 0);


    array_append(a, 1);
    test(array_size(a) == 1);
    test(array_capacity(a) >= 1);
    test(a[0] == 1);


    array_append(a, 2);
    test(array_size(a) == 2);
    test(array_capacity(a) >= 2);
    test(a[0] == 1);
    test(a[1] == 2);


    array_append(a, 3);
    test(array_size(a) == 3);
    test(array_capacity(a) >= 3);
    test(a[0] == 1);
    test(a[1] == 2);
    test(a[2] == 3);


    array_insert(a, 0, 0);
    test(array_size(a) == 4);
    test(array_capacity(a) >= 4);
    test(a[0] == 0);
    test(a[1] == 1);
    test(a[2] == 2);
    test(a[3] == 3);


    array_reserve(a, 16);
    test(array_size(a) == 4);
    test(array_capacity(a) == 16);
    test(a[0] == 0);
    test(a[1] == 1);
    test(a[2] == 2);
    test(a[3] == 3);


    array_shrink(a);
    test(array_size(a) == 4);
    test(array_capacity(a) == 4);
    test(a[0] == 0);
    test(a[1] == 1);
    test(a[2] == 2);
    test(a[3] == 3);


    array_remove(a, 0);
    test(array_size(a) == 3);
    test(array_capacity(a) == 4);
    test(a[0] == 1);
    test(a[1] == 2);
    test(a[2] == 3);
    test(destructed_element_count == 1);
    destructed_element_count = 0;


    array_remove_unordered(a,0);
    test(array_size(a) == 2);
    test(array_capacity(a) == 4);
    test(a[0] == 3);
    test(a[1] == 2);
    test(destructed_element_count == 1);
    destructed_element_count = 0;


    array_clear(a);
    test(array_size(a) == 0);
    test(array_capacity(a) >= 0);
    test(destructed_element_count == 2);
    destructed_element_count = 0;


    array_append(a, 0);
    array_append(a, 1);
    array_append(a, 2);
    test(array_size(a) == 3);
    test(array_capacity(a) >= 3);
    test(destructed_element_count == 0);


    array_free(a);
    test(a == NULL);
    test(array_size(a) == 0);
    test(array_capacity(a) == 0);
    test(destructed_element_count == 3);
    destructed_element_count = 0;


    enum { TEST_LENGTH = 1024 };


    array_alloc(a, 0, destructed_element_count_destructor);
    for (int i = 0; i < TEST_LENGTH; ++i) {
        array_append(a, i);
        test(a[i] == i);
    }
    test(array_size(a) == TEST_LENGTH);
    test(array_capacity(a) >= TEST_LENGTH);
    for (int i = 0; i < TEST_LENGTH; ++i) {
        test(a[i] == i);
    }
    {
        int i = 0;
        const int* const end = array_end(a);
        for (int* itr = array_begin(a); itr < end; ++itr) {
            test(*itr == i++);
        }
    }
    {
        int i = 0;
        while (array_size(a)) {
            test(a[0] == i++);
            array_remove(a,0);
        }
        test(array_size(a) == 0);
        test(array_capacity(a) >= TEST_LENGTH);
        test(destructed_element_count == TEST_LENGTH);
        destructed_element_count = 0;
    }
    array_free(a);
    test(a == NULL);
    test(array_size(a) == 0);
    test(array_capacity(a) == 0);


    array_alloc(a, 0, destructed_element_count_destructor);
    for (int i = 0; i < TEST_LENGTH; ++i) {
        array_insert(a, 0, i);
    }
    test(array_size(a) == TEST_LENGTH);
    test(array_capacity(a) >= TEST_LENGTH);
    for (int i = 0; i < TEST_LENGTH; ++i) {
        test(a[i] == (TEST_LENGTH - 1) - i);
    }
    array_free(a);
    test(a == NULL);
    test(array_size(a) == 0);
    test(array_capacity(a) == 0);


    puts("array tests passed");
}
Esempio n. 27
0
array *
array_prepend(array *a, void *data) { 
  return array_insert(a, data, -1);
}
Esempio n. 28
0
array *
array_push(array *a, void *data) { 
  return array_insert(a, data, a->n);
}
Esempio n. 29
0
/* Add v to A at the start of the array */
void array_insert_MTF(array_t **A, void *v) {
    array_insert(A, v);
    move_to_front((*A), (*A)->size - 1);
}
Esempio n. 30
0
array *
array_append(array *a, void *data) { 
  return array_insert(a, data, a->n);
}