Exemplo n.º 1
0
int
main ()
{
    hashtable_t* hashtablePtr;
    long data[] = {3, 1, 4, 1, 5, 9, 2, 6, 8, 7, -1};
    long i;

    puts("Starting...");

    hashtablePtr = hashtable_alloc(1, &hash, &comparePairs, -1, -1);

    for (i = 0; data[i] >= 0; i++) {
        insertInt(hashtablePtr, &data[i]);
        assert(*(long*)hashtable_find(hashtablePtr, &data[i]) == data[i]);
    }

    for (i = 0; data[i] >= 0; i++) {
        removeInt(hashtablePtr, &data[i]);
        assert(hashtable_find(hashtablePtr, &data[i]) == NULL);
    }

    hashtable_free(hashtablePtr);

    puts("Done.");

    return 0;
}
Exemplo n.º 2
0
sp_image *osfy_image_create(sp_session *session, const byte image_id[20]) {
    sp_image *image;

    image = (sp_image *)hashtable_find(session->hashtable_images, image_id);
    if(image) {
        {
            char buf[41];
            hex_bytes_to_ascii(image->id, buf, 20);
            DSFYDEBUG("Returning existing image '%s'\n", buf);
        }

        return image;
    }

    image = malloc(sizeof(sp_image));

    memcpy(image->id, image_id, sizeof(image->id));
    image->format = SP_IMAGE_FORMAT_UNKNOWN;
    image->data = NULL;

    image->error = SP_ERROR_RESOURCE_NOT_LOADED;

    image->callback = NULL;
    image->userdata = NULL;

    image->is_loaded = 0;
    image->ref_count = 0;

    image->hashtable = session->hashtable_images;
    hashtable_insert(image->hashtable, image->id, image);

    return image;
}
Exemplo n.º 3
0
static int handle_enter_barrier(struct rpc_desc* desc,
				void *_msg, size_t size)
{
	struct cluster_barrier_id *id = ((struct cluster_barrier_id *) _msg);
	struct cluster_barrier_core *core_bar;
	struct cluster_barrier *barrier;
	krgnodemask_t nodes;

	rpc_unpack(desc, 0, &nodes, sizeof(krgnodemask_t));

	barrier = hashtable_find (barrier_table, id->key);
	BUG_ON(!barrier);

	core_bar = &barrier->core[id->toggle];

	if (krgnodes_empty(core_bar->nodes_to_wait)) {
		krgnodes_copy(core_bar->nodes_in_barrier, nodes);
		krgnodes_copy(core_bar->nodes_to_wait, nodes);
	}
	else
		BUG_ON(!krgnodes_equal(core_bar->nodes_in_barrier, nodes));

	krgnode_clear(desc->client, core_bar->nodes_to_wait);

	if (krgnodes_empty(core_bar->nodes_to_wait)) {
                rpc_async_m(RPC_EXIT_BARRIER, &core_bar->nodes_in_barrier,
			    id, sizeof (struct cluster_barrier_id));
	}

	return 0;
}
Exemplo n.º 4
0
struct cluster_barrier *alloc_cluster_barrier(unique_id_t key)
{
	struct cluster_barrier *barrier;
	int r, i;

	if (!key)
		key = get_unique_id(&barrier_id_root);

	if (hashtable_find (barrier_table, key))
		return ERR_PTR(-EEXIST);

	barrier = kmalloc (sizeof(struct cluster_barrier), GFP_KERNEL);
	if (!barrier)
		return ERR_PTR(-ENOMEM);

	for (i = 0; i < 2; i++) {
		krgnodes_clear (barrier->core[i].nodes_in_barrier);
		krgnodes_clear (barrier->core[i].nodes_to_wait);
		init_waitqueue_head(&barrier->core[i].waiting_tsk);
		barrier->core[i].in_barrier = 0;
	}
	spin_lock_init(&barrier->lock);
	barrier->id.key = key;
	barrier->id.toggle = 0;

	r = hashtable_add (barrier_table, barrier->id.key, barrier);
	if (r) {
		kfree (barrier);
		return ERR_PTR(r);
	}

	return barrier;
}
Exemplo n.º 5
0
uint deps_find(const struct deps *deps, uint dep, uint *iter) {
	uint i, hash = dephash(array_get(&deps->names, dep));

	while ((i = hashtable_find(&deps->hashtable, hash, iter)) != -1)
		if (dep == i || deps_match(deps, dep, i))
			break;
	return i;
}
Exemplo n.º 6
0
void pagetable_remove(hashtable *webpage_table, char *path)
{
  /* build a webpage_table_item with the right key to search for */
  webpage_table_item lookfor;
  lookfor.key = path;

  webpage_table_item *item = hashtable_find(webpage_table, &lookfor);
  if(item != NULL)
    webpage_table_item_free(item);
}
Exemplo n.º 7
0
void run() {

  unsigned i;
  gasnett_tick_t start, end;

  hashtable_create(TABLE_SIZE);

  BARRIER();

  start = gasnett_ticks_now();
  for (i = 0; i < WRITES_PER_PROC; ++i) {
    hashtable_insert(grt_id * WRITES_PER_PROC + i);
  }
  end = gasnett_ticks_now();
  unsigned time = ((unsigned) gasnett_ticks_to_us(end - start));
  printf("processor %u: insertion time=%f us\n", 
	 grt_id, (double) time);
  fflush(stdout);
  grt_write(0, time, &times[grt_id]);

  BARRIER();

  /* sanity check */
  for (i = 0; i < WRITES_PER_PROC; ++i) {
    grt_word_t num = grt_id * WRITES_PER_PROC + i;
    grt_bool_t found = hashtable_find(num);
    if (!found) {
      fprintf(stderr, "processor %d: expected to find %d\n", 
	      grt_id, (int) num);
    }
  }

  BARRIER();

  if (grt_id == 0) {
    time = 0, max_time = 0;
    for (i = 0; i < grt_num_procs; ++i) {
      gasnett_tick_t this_time = times[i];
      time += this_time;
      if (this_time >= max_time) max_time = this_time;
    }
    time_per_op = ((float) time) / NUM_WRITES;
    printf("total CPU time=%f us\n", (double) time);
    printf("time per operation=%f us\n", time_per_op);
    printf("max time=%f us\n", (double) max_time);
  }

  BARRIER();

  hashtable_destroy();

  BARRIER();

}
Exemplo n.º 8
0
rs_long_t rs_signature_find_match(rs_signature_t *sig, rs_weak_sum_t weak_sum, void const *buf, size_t len)
{
    rs_block_match_t m;
    rs_block_sig_t *b;

    rs_signature_check(sig);
    rs_block_match_init(&m, sig, weak_sum, NULL, buf, len);
    if ((b = hashtable_find(sig->hashtable, &m))) {
        return (rs_long_t)rs_block_sig_idx(sig, b) * sig->block_len;
    }
    return -1;
}
Exemplo n.º 9
0
webpage_table_item *pagetable_find(pagetable *pages, char *path)
{
  if(pages == NULL) {
    return NULL;
  }

  /* build a webpage_table_item with the right key to search for */
  webpage_table_item lookfor;
  lookfor.key = path;

  return hashtable_find(pages, &lookfor); 
}
Exemplo n.º 10
0
int main(int argc, char* argv[])
{
    hashtable_t* table = NULL;
    hashnode_t* node = NULL;
    int i;
    int size;

    table = (hashtable_t*)malloc(sizeof(hashtable_t));
    assert(table != NULL);

    size = 50;
    hashtable_init(table, size);
    for (i = 0; i < size; i++) {
        assert(table->buckets[i] == NULL);
    }
    assert(table->size == 50);

    hashtable_insert(table, "abcd");
    node = hashtable_find(table, "abcd");
    assert(node != NULL);


    hashtable_insert(table, "bcde");
    node = hashtable_find(table, "bcde");
    assert(node != NULL);

    size = 80;
    hashtable_resize(&table, size);
    assert(table->size == size);

    node = hashtable_find(table, "abcd");
    assert(node != NULL);
    node = hashtable_find(table, "bcde");
    assert(node != NULL);

    hashtable_clear(table);
    free(table);

    return 0;
}
Exemplo n.º 11
0
int hashtable_get(hashtable_t *hashtable, void *key, void **value)
{
    int keyHash = hashtable->hash(key, hashtable->len(key));
    int i;
    
    if ((i = hashtable_find(hashtable, keyHash)) != -1)
    {
        if (value)
            *value = hashtable->entries[i].value;

        return 1;
    }

    return 0;
}
Exemplo n.º 12
0
void hashtable_remove(hashtable_t *hashtable, void *key)
{
    int keyHash = hashtable->hash(key, hashtable->len(key));
    int i;

    if ((i = hashtable_find(hashtable, keyHash)) != -1)
    {
        int totalLen = (hashtable->entry_count - i) * sizeof(hashtableentry_t);

        if (totalLen > 0)
            memcpy(&hashtable->entries[i], &hashtable->entries[i + 1], totalLen);

        hashtable_expand(hashtable, -1);
    }
}
Exemplo n.º 13
0
void hashtable_set(hashtable_t *hashtable, void *key, void *value)
{
    int keyHash = hashtable->hash(key, hashtable->len(key));
    hashtableentry_t *ptr;
    int i;

    if ((i = hashtable_find(hashtable, keyHash)) != -1)
    {
        hashtable->entries[i].value = value;
        return;
    }

    ptr = hashtable_expand(hashtable, 1);
    ptr->keyHash = keyHash;
    ptr->value = value;
}
Exemplo n.º 14
0
static int handle_exit_barrier(struct rpc_desc* desc,
			       void *_msg, size_t size)
{
	struct cluster_barrier_id *id = ((struct cluster_barrier_id *) _msg);
	struct cluster_barrier_core *core_bar;
	struct cluster_barrier *barrier;

	barrier = hashtable_find (barrier_table, id->key);
	BUG_ON(!barrier);

	core_bar = &barrier->core[id->toggle];
	core_bar->in_barrier = 0;

	wake_up (&core_bar->waiting_tsk);

	return 0;
}
Exemplo n.º 15
0
void retrieve_ict(struct Hashtable *cntTable, struct Hashtable *ictTable)
{
	unsigned long i;
	struct Item *aItem, *aContactItem, *aIctPairItem;
	struct Contact *aContact, *bContact;
	struct Pair *aPair, *aIctPair;
	struct ICT *aIct;
	char key[64]; 

	if(cntTable == NULL || ictTable == NULL)
		return;
	for(i=0;i<cntTable->size;i++) {
		aItem = cntTable->head[i];
		while(aItem!=NULL) {
			aPair = (struct Pair*)aItem->datap;
			/* set up the coresponding pair in the ict pair table */
			sprintf(key, "%s,%s", aPair->vName1, aPair->vName2);
			aIctPairItem = hashtable_find(ictTable, key);
			if(aIctPairItem == NULL) {
				aIctPair = (struct Pair*)malloc(sizeof(struct Pair));
				pair_init_func(aIctPair);
				strncpy(aIctPair->vName1, aPair->vName1, strlen(aPair->vName1)+1);
				strncpy(aIctPair->vName2, aPair->vName2, strlen(aPair->vName2)+1);
				hashtable_add(ictTable, key, aIctPair);
			} else { 
				aIctPair = (struct Pair*)aIctPairItem->datap;
			}

			aContactItem = aPair->contents.head;
			while(aContactItem!=NULL && aContactItem->next!=NULL) {
				aContact = (struct Contact*)aContactItem->datap;
				bContact = (struct Contact*)aContactItem->next->datap;
				aIct = (struct ICT*)malloc(sizeof(struct ICT));
				aIct->timestamp = bContact->startAt;
				aIct->ict = bContact->startAt-aContact->endAt;
				duallist_add_to_tail(&aIctPair->contents, aIct);
				aContactItem = aContactItem->next;
			}
			aItem = aItem->next;
		}
	}

}
Exemplo n.º 16
0
rs_result rs_build_hash_table(rs_signature_t *sig)
{
    rs_block_match_t m;
    rs_block_sig_t *b;
    int i;

    rs_signature_check(sig);
    sig->hashtable = hashtable_new(sig->count);
    if (!sig->hashtable)
        return RS_MEM_ERROR;
    for (i = 0; i < sig->count; i++) {
        b = rs_block_sig_ptr(sig, i);
        rs_block_match_init(&m, sig, b->weak_sum, &b->strong_sum, NULL, 0);
        if (!hashtable_find(sig->hashtable, &m))
            hashtable_add(sig->hashtable, b);
    }
    hashtable_stats_init(sig->hashtable);
    return RS_DONE;
}
Exemplo n.º 17
0
uint deps_add_evr(struct deps *deps, const char *name, int flags, uint epoch,
		const char *version, const char *release) {
	uint i, iter = 0, hash, size = array_get_size(&deps->names), ver, rel, nam;

	nam = strings_add(deps->strings, name);
	ver = strings_add(deps->strings, version != NULL ? version : "");
	rel = strings_add(deps->strings, release != NULL ? release : "");

	if (hashtable_resize(&deps->hashtable)) {
		for (i = 0; i < size; i++) {
			hash = dephash(array_get(&deps->names, i));
			hashtable_add(&deps->hashtable, i, hash);
		}
	}

	hash = dephash(nam);

	while ((i = hashtable_find(&deps->hashtable, hash, &iter)) != -1)
		if (array_get(&deps->names, i) == nam &&
				array_get(&deps->epochs, i) == epoch &&
				array_get(&deps->vers, i) == ver &&
				array_get(&deps->rels, i) == rel &&
				array_get(&deps->flags, i) == flags)
			break;
	if (i != -1)
		/* already stored */
		return i;

	i = size;
	array_set(&deps->names, i, nam);
	array_set(&deps->epochs, i, epoch);
	array_set(&deps->vers, i, ver);
	array_set(&deps->rels, i, rel);
	array_set(&deps->flags, i, flags);
	hashtable_add_dir(&deps->hashtable, i, hash, iter);

	return i;
}
Exemplo n.º 18
0
double vector_entropy(unsigned long *vector, int nItems)
{
	int i;
	struct Item *aSingleItem;
	struct Single *aSingle;
	struct Hashtable singleTable;
	char key[128];
	double pi, rst = 0;

	hashtable_init(&singleTable, 100, (unsigned long(*)(void*))sdbm, (int(*)(void*, void*))single_has_name);
	for(i=0;i<nItems;i++) {
	      sprintf(key, "%ld", vector[i]);
	      aSingleItem = hashtable_find(&singleTable, key);
	      if(aSingleItem == NULL) {
		      aSingle = (struct Single*)malloc(sizeof(struct Single));
		      aSingle->t1 = vector[i];
		      aSingle->count = 1;
		      hashtable_add(&singleTable, key, aSingle);
	      } else {
		      aSingle = (struct Single*)aSingleItem->datap;
		      aSingle->count ++;
	      }
        }
	for (i=0;i<singleTable.size;i++) {
	      aSingleItem = singleTable.head[i];
	      while (aSingleItem != NULL) {
			aSingle = (struct Single*)aSingleItem->datap;
			pi = aSingle->count*1.0/nItems;
			rst -= pi*log(pi)/log(2);	
			aSingleItem = aSingleItem->next;
	      }
	}
	hashtable_destroy(&singleTable, free);
	return rst;

}
Exemplo n.º 19
0
int krgsyms_register(enum krgsyms_val v, void* p)
{
	if( (v < 0) || (v >= KRGSYMS_TABLE_SIZE) ){
		printk("krgsyms_register: Incorrect krgsym value (%d)\n", v);
		BUG();
		return -1;
	};

	if(krgsyms_table[v])
		printk("krgsyms_register_symbol(%d, %p): value already set in table\n",
					 v, p);

	if(hashtable_find(krgsyms_htable, (unsigned long)p) != NULL)
	{
		printk("krgsyms_register_symbol(%d, %p): value already set in htable\n",
					 v, p);
		BUG();
	}

	hashtable_add(krgsyms_htable, (unsigned long)p, (void*)v);
	krgsyms_table[v] = p;

	return 0;
};
Exemplo n.º 20
0
int main(int argc, char **argv)
{
	FILE *fsource, *fdump;
	struct Region *region, *aRegion;
	char dumpfile[256], *directory, buf[32];

	struct Hashtable traces, routes;
	struct Item *aItem, *tItem, *bItem;
	struct Trace *aTrace, *bTrace, *dTrace;
	struct Busroute *aRoute;
	struct Duallist roads;

	double wdist=WEIGHT_DISTANCE_ERROR,  wangle = WEIGHT_ANGLE_ERROR, wlength = WEIGHT_LENGTH, wturns = WEIGHT_TURNS;

	unsigned long i;
	int defact = 0;
	char *defactd;


	if(argc < 2) {
	      printf("Usage: %s [-d directory] [-o defact_dir] [-w wdist wangle wlength wturns] .map (.ogd|.lst ...)\n", argv[0]);
	      exit(1);
	}

	directory = ".";
	defactd = "./defect";
	while(argv[1][0] == '-') {
	      switch(argv[1][1]) {
	      case 'o':
		      defact = 1;
		      defactd = argv[2];
		      argc-=2;
		      argv+=2;
		      break;

	      case 'd':
		      directory = argv[2];
		      argc-=2;
		      argv+=2;
		      break;
	      case 'w':
		      wdist = atof(argv[2]);
		      wangle = atof(argv[3]);
		      wlength = atof(argv[4]);
		      wturns = atof(argv[5]);
		      argc -= 5;
		      argv += 5;
		      break;
	      default:
	      	      printf("Usage: %s [-d directory] [-o defact_dir] [-w wdist wangle wlength wturns] .map (.ogd|.lst ...)\n", argv[0]);
	      }
	}

	region = NULL;
	if((fsource=fopen(argv[1], "rb"))!=NULL) {
	      region = region_load_func(fsource, NULL, -1, -1);
	      fclose(fsource);
	      argc--;
	      argv++;
	}

	hashtable_init(&traces, 2000, (unsigned long(*)(void*))sdbm, (int(*)(void*, void*))trace_has_name);
	hashtable_init(&routes, 100, (unsigned long(*)(void*))sdbm, (int(*)(void*, void*))route_has_name);
	while(region && argc > 1) {
		if((fsource=fopen(argv[1], "r"))!=NULL) {
			printf("Loading %s file ...\n", argv[1]);
			load_source_file(fsource, region, &traces, (void*(*)(int, FILE*, struct Region *, void *, time_t *, time_t *, struct Box *))load_trace_with_hashtable, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, &routes, (void*(*)(FILE*, struct Region*, void *))load_route_with_hashtable, NULL, NULL, NULL);
			fclose(fsource);
		}

		argc--;
		argv++;
	}


	if(traces.count != 0) {
	      for (i = 0; i<traces.size; i++) {
		      aItem = traces.head[i];
		      while(aItem != NULL ) {
				aTrace = (struct Trace*)aItem->datap;
				aRegion = NULL;
				bTrace = NULL;
				dTrace = NULL;

				if((aTrace->type == FILE_ORIGINAL_GPS_BUS|| aTrace->type == FILE_MODIFIED_GPS_BUS) && routes.count) {
					duallist_init(&roads);
					sprintf(buf, "%s_upway", aTrace->onRoute);
  					tItem = hashtable_find(&routes, buf);
					if(tItem) {
						aRoute = (struct Busroute*)tItem->datap;
						bItem = aRoute->path->roads.head;
						while(bItem) {
							duallist_add_to_tail(&roads, bItem->datap);
							bItem = bItem->next;
						}	
					}
					sprintf(buf, "%s_downway", aTrace->onRoute);
  					tItem = hashtable_find(&routes, buf);
					if(tItem) {
						aRoute = (struct Busroute*)tItem->datap;
						bItem = aRoute->path->roads.head;
						while(bItem) {
							duallist_add_to_tail(&roads, bItem->datap);
							bItem = bItem->next;
						}	
					}

					aRegion = build_region_with_roads(&roads);
					if(aRegion) {
		 				amend_trace(aRegion, aTrace, wdist, wangle, wlength, wturns, &bTrace, &dTrace);
						region_free_func(aRegion);
					}
					duallist_destroy(&roads, NULL);

				} else { 
		 			amend_trace(region, aTrace, wdist, wangle, wlength, wturns, &bTrace, &dTrace); // entrance of the mapmatching code
				}

#ifdef DEBUG
                sprintf(dumpfile, "transVecDump_%s.txt",aTrace->vName);
				if( (fdump = fopen(dumpfile, "w"))!=NULL) {
					trace_dump_transVecHistory(fdump, aTrace);
					fflush(fdump);
					fclose(fdump);
				}
				
#endif

				if(bTrace) {
					mkdir(directory,0777);
					sprintf(dumpfile, "%s/%s.mgd", directory, aTrace->vName);
					if( (fdump = fopen(dumpfile, "w"))!=NULL) {
						trace_dump_func(fdump, bTrace);
						fflush(fdump);
						fclose(fdump);
					}
					trace_free_func(bTrace);
				}

				if(dTrace) {
					if(defact && dTrace->reports.nItems) {
						mkdir(defactd,0777);
						sprintf(dumpfile, "%s/%s.ogd", defactd, aTrace->vName);
						if( (fdump = fopen(dumpfile, "w"))!=NULL) {
							trace_dump_func(fdump, dTrace);
							fflush(fdump);
							fclose(fdump);
						}
					}
					trace_free_func(dTrace);
				}
				trace_free_func((struct Trace*)hashtable_pick(&traces, aTrace->vName));

			      	aItem = aItem->next;
		      }
	      }
	} 
	 
	hashtable_destroy(&traces, (void(*)(void*))trace_free_func);
	hashtable_destroy(&routes, (void(*)(void*))route_free_func);
	region_free_func(region);
	return 0;
}
Exemplo n.º 21
0
/* Test driver for hashtable. */
int main(int argc, char **argv)
{
    /* Test key_hashtable instance. */
    hashtable_t *kt;
    hashtable_iter_t ki;
    key_t k1, k2;

    key_init(&k1, 1);
    key_init(&k2, 2);
    assert((kt = key_hashtable_new(16)) != NULL);
    assert(key_hashtable_add(kt, &k1) == &k1);
    assert(key_hashtable_find(kt, &k1) == &k1);
    assert(key_hashtable_find(kt, &k2) == NULL);
    assert(key_hashtable_iter(&ki, kt) == &k1);
    assert(key_hashtable_next(&ki) == NULL);

    /* Test hashtable instance. */
    hashtable_t *t;
    entry_t entry[256];
    entry_t e;
    match_t m;
    int i;

    entry_init(&e, 0);
    for (i = 0; i < 256; i++)
        entry_init(&entry[i], i);

    /* Test hashtable_new() */
    t = hashtable_new(256);
    assert(t->size == 512);
    assert(t->count == 0);
    assert(t->etable != NULL);
    assert(t->ktable != NULL);

    /* Test hashtable_add() */
    assert(hashtable_add(t, &e) == &e); /* Added duplicated copy. */
    assert(hashtable_add(t, &entry[0]) == &entry[0]);   /* Added duplicated instance. */
    for (i = 0; i < 256; i++)
        assert(hashtable_add(t, &entry[i]) == &entry[i]);
    assert(t->count == 258);

    /* Test hashtable_find() */
    match_init(&m, 0);
    assert(hashtable_find(t, &m) == &e);        /* Finds first duplicate added. */
    assert(m.value == m.source);        /* match_cmp() updated m.value. */
    for (i = 1; i < 256; i++) {
        match_init(&m, i);
        assert(hashtable_find(t, &m) == &entry[i]);
        assert(m.value == m.source);    /* match_cmp() updated m.value. */
    }
    match_init(&m, 256);
    assert(hashtable_find(t, &m) == NULL);      /* Find missing entry. */
    assert(m.value == 0);       /* match_cmp() didn't update m.value. */
#ifndef HASHTABLE_NSTATS
    assert(t->find_count == 257);
    assert(t->match_count == 256);
    assert(t->hashcmp_count >= 256);
    assert(t->entrycmp_count >= 256);
    hashtable_stats_init(t);
    assert(t->find_count == 0);
    assert(t->match_count == 0);
    assert(t->hashcmp_count == 0);
    assert(t->entrycmp_count == 0);
#endif

    /* Test hashtable iterators */
    entry_t *p;
    hashtable_iter_t iter;
    int count = 0;
    for (p = hashtable_iter(&iter, t); p != NULL; p = hashtable_next(&iter)) {
        assert(p == &e || (&entry[0] <= p && p <= &entry[255]));
        count++;
    }
    assert(count == 258);
    hashtable_free(t);

    return 0;
}
Exemplo n.º 22
0
int main() {
  Queue *q = queue_new();
  HashTable *h = hashtable_new(512, header_hash_fn, header_eq_fn);
  HashEnum e;
  IsHeader *hp; 
  int i, j;

  printf("Testing Queue...\n");
  queue_insert(q, 1);
  queue_insert(q, 2);
  queue_insert(q, 3);
  queue_insert(q, 4);
  queue_insert(q, 5);
  queue_remove(q);
  queue_insert(q, 1);
  queue_insert(q, 2);
  queue_insert(q, 3);
  queue_insert(q, 4);
  queue_remove(q);
  queue_remove(q);
  queue_insert(q, 5);
  while (!queue_isempty(q))
    printf("%u ", (unsigned)queue_remove(q));
  printf("\n");

  printf("Testing Hash...\n");

  for (i=1; i<1024; i++)
    hashtable_insert(h, i, malloc(sizeof(IsHeader)));

  j = 0;
  e = hashenum_create(h);
  while((hp = hashenum_next(&e)) != NULL) {
    hp->offset = header_hash_fn(j);
    for (i=0; i<32; i++)
      hp->offsets[i] = i;
    hp->name = malloc(sizeof(char)*64);
    sprintf(hp->name, "I am number %d", j++);
  }

  for (j=10; j<800; j+=31) {
    hp = hashtable_find(h, (uint16)j);
    if (hp == NULL) {
      printf("Did not find key %d\n", j);
      continue;
    }
    printf("Found key %d - offset: %d name: %s\n", j, hp->offset, hp->name);
    printf("Offsets:");
    for (i=0; i<32; i++)
      printf(" %d", hp->offsets[i]);
    printf("\n");
  }

  printf("*** Super Test ***\n");

  e = hashenum_create(h);
  while((hp = hashenum_next(&e)) != NULL) {
    printf("%d ", hp->offset%1024 );
  }
  printf("\n");

  hashtable_freecontents(h);
  free(h);
  free(q);

  return 0;
}
Exemplo n.º 23
0
enum krgsyms_val krgsyms_export(void* p)
{
	return (enum krgsyms_val)hashtable_find(krgsyms_htable, (unsigned long)p);
};
Exemplo n.º 24
0
struct cache_object *cache_find(struct cache *c, void *ptr, int *id)
{
	*id = hashtable_hash(c->h, ptr);
	return ((struct cache_object *) hashtable_find(c->h, ptr, *id));
}
Exemplo n.º 25
0
int main(int argc, char **argv)
{
	FILE *fsource, *fdump;
	struct Region *region, *aRegion;
	int insertMode = INSERT_MODE_AVGSPEED;
	int outputMode = OUTPUT_MODE_CELL;
	int interval;
	char dumpfile[256], *directory, buf[32];

	struct Hashtable traces, routes;
	struct Item *aItem, *bItem, *tItem;
	struct Trace *aTrace, *bTrace;
	unsigned long i;
	struct Duallist roads;
	struct Busroute *aRoute;

	if(argc < 3) {
		printf("%s is used to retrieve required spots of vehicles from mgd files. New GPS reports are generated according to the given model.\n", argv[0]);
	      	printf("Usage: %s [-insert avgspeed|traffic] [-output cell|interval seconds] [-d directory] .map (.mgd|.lst ...)\n", argv[0]);
	      	exit(1);
	}

	directory = ".";
	while(argv[1][0] == '-') {
	      switch(argv[1][1]) {
	      case 'i':
		      if(strcmp(argv[2], "traffic")==0)
			  insertMode = INSERT_MODE_TRAFFIC;
		      else if(strcmp(argv[2], "avgspeed")==0)
			  insertMode = INSERT_MODE_AVGSPEED;
		      argc-=2;
		      argv+=2;
		      break;
	      case 'd':
		      directory = argv[2];
		      argc-=2;
		      argv+=2;
		      break;

	      case 'o':
		      if(argv[2][0]=='c') {
			      outputMode = OUTPUT_MODE_CELL;
			      argc-=2;
			      argv+=2;
		      } else if(argv[2][0] == 'i') {
			      outputMode = OUTPUT_MODE_INTERVAL;
			      interval = atoi(argv[3]);
			      argc-=3;
			      argv+=3;
		      }
		      break;

	      default:
	              printf("Usage: %s [-mode avgspeed|traffic] [-cell] [-interval seconds] .map (.mgd|.lst ...)\n", argv[0]);
	      }
	}

	region = NULL;
	if((fsource=fopen(argv[1], "rb"))!=NULL) {
	      region = region_load_func(fsource, NULL, -1, -1);
	      fclose(fsource);
	      argc--;
	      argv++;
	}

	hashtable_init(&traces, 2000, (unsigned long(*)(void*))sdbm, (int(*)(void*, void*))trace_has_name);
	hashtable_init(&routes, 100, (unsigned long(*)(void*))sdbm, (int(*)(void*, void*))route_has_name);
	while(region && argc > 1) {
		if((fsource=fopen(argv[1], "r"))!=NULL) {
			printf("Loading %s file ...\n", argv[1]);
			load_source_file(fsource, region, &traces, (void*(*)(int, FILE*, struct Region *, void *, time_t *, time_t *, struct Box *))load_trace_with_hashtable, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, &routes, (void*(*)(FILE*, struct Region*, void *))load_route_with_hashtable, NULL, NULL, NULL);
			fclose(fsource);
		}

		argc--;
		argv++;
	}


	if(traces.count != 0) {
	      for (i = 0; i<traces.size; i++) {
		      aItem = traces.head[i];
		      while(aItem != NULL ) {
				aTrace = (struct Trace*)aItem->datap;
				aRegion = NULL;
				bTrace = NULL;

				if(aTrace->type == FILE_MODIFIED_GPS_BUS && routes.count) {
					duallist_init(&roads);
					sprintf(buf, "%s_upway", aTrace->onRoute);
  					tItem = hashtable_find(&routes, buf);
					if(tItem) {
						aRoute = (struct Busroute*)tItem->datap;
						bItem = aRoute->path->roads.head;
						while(bItem) {
							duallist_add_to_tail(&roads, bItem->datap);
							bItem = bItem->next;
						}	
					}
					sprintf(buf, "%s_downway", aTrace->onRoute);
  					tItem = hashtable_find(&routes, buf);
					if(tItem) {
						aRoute = (struct Busroute*)tItem->datap;
						bItem = aRoute->path->roads.head;
						while(bItem) {
							duallist_add_to_tail(&roads, bItem->datap);
							bItem = bItem->next;
						}	
					}

					aRegion = build_region_with_roads(&roads);
					if(aRegion) {
		 				bTrace = insert_reports(aRegion, aTrace, insertMode, outputMode, interval);
						region_free_func(aRegion);
					}
					duallist_destroy(&roads, NULL);

				} else { 
		 			bTrace = insert_reports(region, aTrace, insertMode, outputMode, interval);
				}

				if(bTrace) {
					mkdir(directory,0777);
					sprintf(dumpfile, "%s/%s.mgd", directory, bTrace->vName);
					if( (fdump = fopen(dumpfile, "w"))!=NULL) {
						trace_dump_func(fdump, bTrace);
						fflush(fdump);
						fclose(fdump);
					}
					trace_free_func(bTrace);
				}
				trace_free_func((struct Trace*)hashtable_pick(&traces, aTrace->vName));

			      	aItem = aItem->next;
		      }
	      }
	} 
	 
	hashtable_destroy(&traces, (void(*)(void*))trace_free_func);
	hashtable_destroy(&routes, (void(*)(void*))route_free_func);
	region_free_func(region);
	return 0;
}
Exemplo n.º 26
0
void load_contacts_with_hashtable(FILE *fsource, struct Region *aRegion, struct Hashtable *table, int mode, time_t *startAt, time_t *endAt)
{
	char buf[128], *vName1, *vName2, *tm1,*tm2, *strp, key[128];
	struct Contact *newContact;
	struct Item *aPairItem, *anEgoItem, *aLinkmanItem;
	struct Pair *aPair = NULL;
	struct Ego *anEgo = NULL;
	struct Linkman *aLinkman = NULL;

	time_t sAt, eAt;
	time_t tableStartAt, tableEndAt;
	struct Point aPoint;
	int xNumber, yNumber;
	int needed;
	int first;

	if(table == NULL)
		return;
	first = 1;
	while(fgets(buf, 128, fsource)) {

		vName1 = strtok(buf, ",");
		vName2 = strtok(NULL, ",");
		tm1 = strtok(NULL, ",");
		tm2 = strtok(NULL, ",");
		strp = strtok(NULL, ",");
		aPoint.x = atof(strp);
		strp = strtok(NULL, ",");
		aPoint.y = atof(strp);
		strp = strtok(NULL, ",");
		xNumber = atoi(strp);
		strp = strtok(NULL, ",");
		yNumber = atoi(strp);
		sAt = strtot(tm1);
		eAt = strtot(tm2);

		if(aRegion == NULL && *startAt == 0 && *endAt == 0) {
			needed = 1;
		} else if (aRegion == NULL && *startAt == 0 && *endAt) {
			if (eAt <= *endAt) needed = 1; else needed = 0;
		} else if (aRegion == NULL && *startAt && *endAt == 0) {
			if (sAt >= *startAt) needed = 1; else needed = 0;
		} else if (aRegion == NULL && *startAt && *endAt) {
			if (eAt <= *endAt && sAt >= *startAt) needed = 1; else needed = 0;
		} else if (aRegion && *startAt == 0 && *endAt == 0) {
			if (is_point_in_polygon(&aPoint, aRegion->chosen_polygon))
				needed = 1; else needed = 0;
		} else if (aRegion && *startAt == 0 && *endAt) {
			if (eAt <= *endAt && is_point_in_polygon(&aPoint, aRegion->chosen_polygon))
				needed = 1; else needed = 0;
		} else if (aRegion && *startAt && *endAt == 0) {
			if (is_point_in_polygon(&aPoint, aRegion->chosen_polygon) && sAt >= *startAt)
				needed = 1; else needed = 0;
		} else if (aRegion && *startAt && *endAt) {
			if (is_point_in_polygon(&aPoint, aRegion->chosen_polygon) && sAt >= *startAt && eAt <= *endAt)
				needed = 1; else needed = 0;
		} 
		if( needed ) {	
			newContact = (struct Contact*)malloc(sizeof(struct Contact));
			newContact->gPoint.x = aPoint.x;
			newContact->gPoint.y = aPoint.y;
			newContact->xNumber = xNumber;
			newContact->yNumber = yNumber;
			newContact->startAt = sAt;
			newContact->endAt = eAt;

			if(mode == PAIRWISE_TABLE) {
				sprintf(key, "%s,%s", vName1, vName2);
				aPairItem = hashtable_find(table, key);
				if(aPairItem == NULL) {
					aPair = (struct Pair*)malloc(sizeof(struct Pair));
					pair_init_func(aPair);
					strncpy(aPair->vName1, vName1, strlen(vName1)+1);
					strncpy(aPair->vName2, vName2, strlen(vName2)+1);
					hashtable_add(table, key, aPair);
					aPair->color.integer = rand();
					aPair->startAt = sAt;
					aPair->endAt = eAt;
					if(first) {
						tableStartAt = aPair->startAt;
						tableEndAt = aPair->endAt;
						first = 0;
					}	
				} else {
					aPair = (struct Pair*)aPairItem->datap;
					if(sAt < aPair->startAt) {
						aPair->startAt = sAt;
						if(aPair->startAt < tableStartAt)
							tableStartAt = aPair->startAt;
					}
					if(eAt > aPair->endAt) {
						aPair->endAt = eAt;
						if(aPair->endAt > tableEndAt)
							tableEndAt = aPair->endAt;
					}
				}

				newContact->fromPair = aPair;
				duallist_add_to_tail(&aPair->contents, newContact);
			} else {
				/* Ego vName1 */
				anEgoItem = hashtable_find(table, vName1);
				if(anEgoItem == NULL) {
					anEgo = (struct Ego*)malloc(sizeof(struct Ego));
					ego_init_func(anEgo);
					strncpy(anEgo->vName, vName1, strlen(vName1)+1);
					hashtable_add(table, vName1, anEgo);
					anEgo->startAt = sAt;
					anEgo->endAt = eAt;
					if(first) {
						tableStartAt = anEgo->startAt;
						tableEndAt = anEgo->endAt;
						first = 0;
					}	
				} else {
					anEgo = (struct Ego*)anEgoItem->datap;
					if(sAt < anEgo->startAt) {
						anEgo->startAt = sAt;
						if(anEgo->startAt < tableStartAt)
							tableStartAt = anEgo->startAt;
					}
					if(eAt > anEgo->endAt) {
						anEgo->endAt = eAt;
						if(anEgo->endAt > tableEndAt)
							tableEndAt = anEgo->endAt;
					}
				}

				aLinkmanItem = duallist_find(&anEgo->linkmen, vName2, (int(*)(void*,void*))linkman_has_name);
				if(aLinkmanItem == NULL) {
					aLinkman = (struct Linkman*)malloc(sizeof(struct Linkman));
					linkman_init_func(aLinkman);
					aLinkman->color.integer=rand();
					strncpy(aLinkman->vName, vName2, strlen(vName2)+1);
					duallist_add_to_tail(&anEgo->linkmen, aLinkman);
				} else 
					aLinkman = (struct Linkman*)aLinkmanItem->datap;
				newContact->fromPair = NULL;
				duallist_add_to_tail(&aLinkman->contacts, newContact);

				/* Ego vName2 */
				anEgoItem = hashtable_find(table, vName2);
				if(anEgoItem == NULL) {
					anEgo = (struct Ego*)malloc(sizeof(struct Ego));
					ego_init_func(anEgo);
					strncpy(anEgo->vName, vName2, strlen(vName2)+1);
					hashtable_add(table, vName2, anEgo);
					anEgo->startAt = sAt;
					anEgo->endAt = eAt;
				} else {
					anEgo = (struct Ego*)anEgoItem->datap;
					if(sAt < anEgo->startAt) {
						anEgo->startAt = sAt;
					}
					if(eAt > anEgo->endAt) {
						anEgo->endAt = eAt;
					}
				}

				aLinkmanItem = duallist_find(&anEgo->linkmen, vName1, (int(*)(void*,void*))linkman_has_name);
				if(aLinkmanItem == NULL) {
					aLinkman = (struct Linkman*)malloc(sizeof(struct Linkman));
					linkman_init_func(aLinkman);
					aLinkman->color.integer=rand();
					strncpy(aLinkman->vName, vName1, strlen(vName1)+1);
					duallist_add_to_tail(&anEgo->linkmen, aLinkman);
				} else 
					aLinkman = (struct Linkman*)aLinkmanItem->datap;
				duallist_add_to_tail(&aLinkman->contacts, contact_copy_func(newContact));
			}
		}

	}
	if(table->count) {
		*startAt = tableStartAt;
		*endAt = tableEndAt;
	}
}
Exemplo n.º 27
0
int main( int   argc,
          char *argv[] )
{
  char *ictevolving=NULL, *ictevolving_a=NULL;
  char *fromDay=NULL;
  char key[128];
  char buf[1024];
  char *strp, *strp1;
  FILE *fsource;
  
  struct Item *aContactItem;
  struct Contact *aContact, *bContact;

  struct Hashtable pairTable;
  struct Hashtable globalTable;

  struct Item *aPairItem;
  struct Pair *aPair;
  struct Item *aPairResultItem;
  struct Pair_Result *aPairResult;

  int magicNumber;
  int periods = 1;
  unsigned long *X;
  unsigned long *X_n;
  unsigned long *X_a;

  unsigned long pairTableSize = 10e6;
  int tGran = DEFAULT_MEETING_TEMPORAL_GRAN;
  int cGran = DEFAULT_MEETING_COUNTING_GRAN;
  unsigned long k;

  unsigned long T;
  int ii, jj;
  FILE *fdump, *fdump1;
  double entropy1, entropy2, jointEntropy;

  struct Item *aIctItem;
  struct ICT *aIct;

  time_t fromTime = 0;

  time_t previousTime;
  time_t interval = 0;
  int t1, i;

  struct Item *tempItem;


  if(argc < 3) {
	printf("Usage: %s [-t T meeting_temporal_gran(sec)] [-gc meeting_counting_gran(times)] [-o fromTime interval periods] [-w ict_nmi ict_nmia] pair_list .cont ...\n", argv[0]);
	exit(1);
  }
  while(argv[1][0] == '-') {
	switch(argv[1][1]) {
	case 't':
		T = atol(argv[2]);
		tGran = atoi(argv[3]);
		argc-=3;
		argv+=3;
		break;

	case 'g':
		if(argv[1][2] == 'c')
			cGran = atoi(argv[2]);
		argc-=2;
		argv+=2;
		break;

	case 'o':
		fromDay = argv[2];
		interval = atoi(argv[3]);
		periods = atoi(argv[4]);
		argc-=4;
		argv+=4;
		break;

	case 'w':
		ictevolving = argv[2];
		ictevolving_a = argv[3];
		argc-=3;
		argv+=3;
		break;
		
	default:
		printf("Usage: %s [-t T meeting_temporal_gran(sec)] [-gc meeting_counting_gran(times)] [-o fromTime interval periods] [-w ict_nmi ict_nmia] pair_list .cont ...\n", argv[0]);
		exit(1);
	}
  }

	
  int numSlices = T/tGran;

  X = (unsigned long*)malloc(sizeof(unsigned long)*(numSlices)+1);
  X_n = (unsigned long*)malloc(sizeof(unsigned long)*(numSlices)+1);
  X_a = (unsigned long*)malloc(sizeof(unsigned long)*(numSlices)+1);

  fromTime = strtot(fromDay);

  hashtable_init(&globalTable, pairTableSize, (unsigned long(*)(void*))sdbm, (int(*)(void*, void*))pair_result_has_names);
  if((fsource=fopen(argv[1], "r"))!=NULL) {
	  while(fgets(buf, 1024, fsource)) {
		  strp = strtok(buf, ",");
		  strp1 = strtok(NULL, ",");
		  sprintf(key, "%s,%s", strp, strp1);
		  aPairResult = (struct Pair_Result*)malloc(sizeof(struct Pair_Result));
		  pair_result_init_func(aPairResult);
		  aPairResult->redundency=(double*)malloc(sizeof(double)*periods);
		  memset(aPairResult->redundency, 0,sizeof(double)* periods);

		  aPairResult->redundency_a=(double*)malloc(sizeof(double)*periods);
		  memset(aPairResult->redundency_a, 0, sizeof(double)*periods);

		  aPairResult->X_a=(unsigned long*)malloc(sizeof(unsigned long)*(numSlices)+1);
		  memset(aPairResult->X_a, 0, sizeof(unsigned long)*(numSlices)+1);

		  strncpy(aPairResult->vName1, strp, strlen(strp)+1);
		  strncpy(aPairResult->vName2, strp1, strlen(strp1)+1);
		  aPairResult->numContacts = 0;

		  hashtable_add(&globalTable, key, aPairResult);
	  }
	  fclose(fsource);
	  argv++;
	  argc--;
  }

  while(argc>1) {
	if((fsource=fopen(argv[1], "r"))!=NULL) {
		fscanf(fsource, "%d\n", &magicNumber);
		printf("Loading %s file ...\n", argv[1]);
		if(magicNumber == FILE_CONTACT) {
			hashtable_init(&pairTable, pairTableSize, (unsigned long(*)(void*))sdbm, (int(*)(void*, void*))pair_has_names);
			load_contacts_with_hashtable(fsource, NULL, &pairTable, PAIRWISE_TABLE, NULL, NULL);

			for (k=0;k<pairTable.size;k++) {
				aPairItem = pairTable.head[k];
				while (aPairItem != NULL) {
					aPair = (struct Pair*)aPairItem->datap;

					/* set up the coresponding pair in the global pair table */
					sprintf(key, "%s,%s", aPair->vName1, aPair->vName2);
					aPairResultItem = hashtable_find(&globalTable, key);
					if(aPairResultItem == NULL) {
						aPairItem = aPairItem->next;
						continue;
					} else {
						aPairResult = (struct Pair_Result*)aPairResultItem->datap;
						aPairResult->numContacts += aPair->contents.nItems;
					}

					aContactItem = aPair->contents.head;
					if(aPairResult->last1EndAt != 0) {
						aContact = (struct Contact*)aContactItem->datap;
						aIct = (struct ICT*)malloc(sizeof(struct ICT));
						aIct->timestamp = aContact->startAt;
						aIct->ict = aContact->startAt-aPairResult->last1EndAt;
						duallist_add_to_tail(&aPairResult->icts, aIct);
					}
					while(aContactItem!=NULL && aContactItem->next!=NULL) {
						aContact = (struct Contact*)aContactItem->datap;
						bContact = (struct Contact*)aContactItem->next->datap;
						aIct = (struct ICT*)malloc(sizeof(struct ICT));
						aIct->timestamp = bContact->startAt;
						aIct->ict = bContact->startAt-aContact->endAt;
						duallist_add_to_tail(&aPairResult->icts, aIct);
						aContactItem = aContactItem->next;
					}

					aContact = (struct Contact*)aPair->contents.head->prev->datap;
					aPairResult->last1StartAt = aContact->startAt;
					aPairResult->last1EndAt = aContact->endAt;


					aPairItem = aPairItem->next;
				}
			}

		} else {
			printf("Wrong file type! File %s has been ignored.\n", argv[1]);
		}
		fclose(fsource);
		hashtable_destroy(&pairTable, (void(*)(void*))pair_free_func);
	}
	argc--;
	argv++;
  }


  if(ictevolving != NULL) {


	for (k=0;k<globalTable.size;k++) {
		aPairResultItem = globalTable.head[k];
		while (aPairResultItem != NULL) {
			aPairResult = (struct Pair_Result*)aPairResultItem->datap;
			sprintf(key, "%s,%s", aPairResult->vName1, aPairResult->vName2);

			/* set up X */
			memset(X, 0, sizeof(unsigned long)*(numSlices)+1);
			if(aPairResult->icts.head !=NULL) {
				aIctItem = aPairResult->icts.head->prev;
				aIct = (struct ICT*)aIctItem->datap;
				while(aIctItem!=aPairResult->icts.head ) {
					if( aIct->timestamp >= fromTime && aIct->timestamp < fromTime+interval) {
						t1 = aIct->ict/tGran;
						if(t1<numSlices)
							X[t1] ++;
						else
							X[numSlices] ++;
					}
					if(aIct->timestamp < fromTime)
						break;
					aIctItem = aIctItem->prev;
					aIct = (struct ICT*)aIctItem->datap;
				}
			}
			entropy1 = vector_entropy(X, numSlices+1);
			if(entropy1 == 0) { 
				tempItem = aPairResultItem->next;
			 	pair_result_free_func(hashtable_pick(&globalTable, key));
				aPairResultItem = tempItem;
				continue;
			}
			previousTime = fromTime;
			for(i=0;i<periods;i++) {
				/* set up X-n */
				previousTime = previousTime - interval;
				memset(X_n, 0, sizeof(unsigned long)*(numSlices)+1);
				while(aIctItem != aPairResult->icts.head && aIct->timestamp >= previousTime ) {
					t1 = aIct->ict/tGran;
					if(t1<numSlices)
						X_n[t1] ++;
					else
						X_n[numSlices] ++;
					aIctItem = aIctItem->prev;
					aIct = (struct ICT*)aIctItem->datap;
				}

				/* set up sum X-1:X-n */
				for(jj=0;jj<(numSlices+1);jj++) {
					aPairResult->X_a[jj] += X_n[jj];
					X_a[jj] = aPairResult->X_a[jj];
				}
				for(jj=0;jj<(numSlices+1);jj++) {
					X[jj] = ceil(X[jj]*1.0/cGran);
					X_n[jj] = ceil(X_n[jj]*1.0/cGran);
					X_a[jj] = ceil(X_a[jj]*1.0/cGran);
				}
				entropy2 = vector_entropy(X_n, numSlices+1);
				jointEntropy = vectors_joint_entropy(X, X_n, numSlices+1);
				aPairResult->redundency[i] = (entropy1+entropy2-jointEntropy)/entropy1;

				entropy2 = vector_entropy(X_a, numSlices+1);
				jointEntropy = vectors_joint_entropy(X, X_a, numSlices+1);
				aPairResult->redundency_a[i] = (entropy1+entropy2-jointEntropy)/(entropy1+entropy2);
					
			}

			aPairResultItem = aPairResultItem->next;
		}
	}


	fdump = fopen(ictevolving, "w");
	fdump1 = fopen(ictevolving_a, "w");
	for (k=0;k<globalTable.size;k++) {
	      aPairResultItem = globalTable.head[k];
	      while (aPairResultItem != NULL) {
		      	aPairResult = (struct Pair_Result*)aPairResultItem->datap;
			for (ii=0;ii<periods;ii++) {
				fprintf(fdump, "%.4lf ", aPairResult->redundency[ii]);
				fprintf(fdump1, "%.4lf ", aPairResult->redundency_a[ii]);
			}
			fprintf(fdump, "\n");
			fprintf(fdump1, "\n");
		     	aPairResultItem = aPairResultItem->next;
	      }
	}
	fclose(fdump);
	fclose(fdump1);

  }

  hashtable_destroy(&globalTable, (void(*)(void*))pair_result_free_func);
  free(X);
  free(X_n);
  free(X_a);
  return 0;
}
Exemplo n.º 28
0
void filter_postproc_relsets(msieve_obj *obj, merge_t *merge) {

	/* external interface to the postprocessing phase */

	uint32 i, j;
	uint32 cycle_bins[NUM_CYCLE_BINS + 2] = {0};
	uint32 max_ideal_list_size;
	uint32 num_relations;
	relation_set_t *relset_array = merge->relset_array;
	uint32 num_relsets = merge->num_relsets;
	uint32 num_ideals;
	hashtable_t h;
	ideal_list_t ideal_list;
	heap_t active_heap;
	heap_t inactive_heap;
	merge_aux_t *aux;

	logprintf(obj, "commencing cycle optimization\n");

	/* we will call the relation sets 'cycles' here, but they
	   are treated the same as in the merge phase. Instead of a
	   list of ideals, each cycle has a list of relations that
	   occur in that cycle. We assign one 'ideal' to each relation
	   that appears in the collection of cycles, and minimize that.
	   Note that we assume that the merge phase has already 
	   cancelled out any relations that appear more than once in
	   a cycle */

	hashtable_init(&h, 1, 0);
	max_ideal_list_size = 0;
	for (i = num_relations = 0; i < num_relsets; i++) {
		relation_set_t *r = relset_array + i;
		uint32 curr_num_relations = r->num_relations;

		max_ideal_list_size = MAX(max_ideal_list_size, 
					  curr_num_relations);
		num_relations += curr_num_relations;

		/* initialize the cycle. There are no small ideals,
		   and cycles that have only one relation will never
		   be optimized to have zero relations, so skip those */

		r->num_small_ideals = 0;
		r->num_large_ideals = 0;
		r->num_active_ideals = 0;
		if (curr_num_relations == 1)
			continue;

		r->num_large_ideals = curr_num_relations;
		r->num_active_ideals = curr_num_relations;
		r->data = (uint32 *)xrealloc(r->data, 2 * curr_num_relations * 
						sizeof(uint32));

		/* every unique relation is assigned a number */

		for (j = 0; j < curr_num_relations; j++) {
			hashtable_find(&h, r->data + j, 
					r->data + curr_num_relations + j, 
					NULL);
		}

		/* relation lists occur in sorted order */

		qsort(r->data + curr_num_relations, 
			(size_t)curr_num_relations,
			sizeof(uint32), compare_uint32);
	}
	num_ideals = merge->num_ideals = hashtable_get_num(&h);
	hashtable_free(&h);
	logprintf(obj, "start with %u relations\n", num_relations);

	/* initialize the heaps for tracking cycles; all cycles
	   start out active. Then add each cycle to the heaps;
	   each relation that occurs in more than one cycle is
	   heapified */

	aux = (merge_aux_t *)xmalloc(sizeof(merge_aux_t));
	heap_init(&active_heap);
	heap_init(&inactive_heap);
	ideal_list_init(&ideal_list, num_ideals, 1);

	for (i = 0; i < num_relsets; i++) {
		relation_set_t *r = relset_array + i;

		if (r->num_large_ideals > 1) {
			heap_add_relset(&active_heap, &inactive_heap,
					&ideal_list, r, i, 1);
		}
	}

	/* for each relation, starting with the relations that
	   occur in the fewst cycles,
	   - remove all the cycles containing that relation
	   - optimize the collection of cycles
	   - put the cycles back into the heap
	   - make the relation inactive
	*/

	num_relations = 0;
	while (active_heap.num_ideals > 0) {

		uint32 ideal = heap_remove_best(&active_heap, 
						&ideal_list);

		load_next_relset_group(aux, &active_heap, 
				&inactive_heap, &ideal_list, 
				relset_array, ideal, 1);

		num_relations += do_merges_core(aux);

		ideal_list.list[ideal].active = 0;

		store_next_relset_group(aux, &active_heap, 
					&inactive_heap, &ideal_list, 
					relset_array);
	}

	logprintf(obj, "pruned %u relations\n", num_relations);
	logprintf(obj, "memory use: %.1f MB\n", (double)
			get_merge_memuse(relset_array, num_relsets,
						&ideal_list) / 1048576);

	/* print statistics on the final collection of cycles */

	for (i = j = 0; i < num_relsets; i++) {
		relation_set_t *r = relset_array + i;
		j = MAX(j, r->num_relations);

		if (r->num_relations > NUM_CYCLE_BINS)
			cycle_bins[NUM_CYCLE_BINS+1]++;
		else
			cycle_bins[r->num_relations]++;

		r->num_small_ideals = 0;
		r->num_large_ideals = 0;
	}
	logprintf(obj, "distribution of cycle lengths:\n");
	for (i = 1; i < NUM_CYCLE_BINS + 1; i++)
		logprintf(obj, "%u relations: %u\n", i, cycle_bins[i]);
	logprintf(obj, "%u+ relations: %u\n", i, cycle_bins[i]);
	logprintf(obj, "heaviest cycle: %u relations\n", j);

	heap_free(&active_heap);
	heap_free(&inactive_heap);
	ideal_list_free(&ideal_list);
	free(aux);
}
void process_frames(){
	if(queue_nitems(receiver) == 0){
		return;
	}
	size_t len = 0;
	FRAME *f = queue_peek(receiver, &len);
	int source_nodenumber = find_nodenumber(f->payload.source);
	if(node_buffer[source_nodenumber].busy){
		free(f);
		return;
	}
	node_buffer[source_nodenumber].busy = true;
	f = queue_remove(receiver, &len);
	//printf("Received frames for message #%d Expecting %d\n", f->payload.mesg_seq_no, node_buffer[source_nodenumber].mesg_seq_no_to_receive);	
	//If ack has not been received at the sender side and an old frame from an old message has been received
	if(f->payload.mesg_seq_no < node_buffer[source_nodenumber].mesg_seq_no_to_receive){
		//printf("Message received is old, dropped :-( Message #%d\n", f->payload.mesg_seq_no);
		node_buffer[source_nodenumber].busy = false;
		free(f);
		process_frames();
		return;
	}
	if(f->payload.mesg_seq_no > node_buffer[source_nodenumber].mesg_seq_no_to_receive){
		queue_add(receiver, f, len);
		//printf("Message received is new, pushed back ;-) Message #%d\n", f->payload.mesg_seq_no);
		node_buffer[source_nodenumber].busy = false;
		free(f);
                //process_frames();
                return;
	}
	int seq_no = f->payload.A;
	char seq_str[5];
	sprintf(seq_str, "%d", seq_no);
	if(seq_no == node_buffer[source_nodenumber].next_seq_number_to_add){
		//send ack here
		create_ack(f->payload);
		// add to the incomplete data object
		//printf("Frame appending %d with length %d | MSG #%d\n",seq_no, f->payload.len, f->payload.mesg_seq_no);
		memcpy(&node_buffer[source_nodenumber].incomplete_data[0] + node_buffer[source_nodenumber].bytes_added, &f->payload.data[0], f->payload.len);
		node_buffer[source_nodenumber].bytes_added += f->payload.len;
		node_buffer[source_nodenumber].next_seq_number_to_add++;
		while(true){
			int next_seq = node_buffer[source_nodenumber].next_seq_number_to_add;
			char next_seq_str[5];
			sprintf(next_seq_str, "%d", next_seq);
			size_t plen;
			PACKET *pkt = hashtable_find(node_buffer[source_nodenumber].ooo_packets, next_seq_str, &plen);
			if(plen == 0)
				break;
			//printf("In While loop:Next frame %d found in HT\n",next_seq);
			pkt = hashtable_remove(node_buffer[source_nodenumber].ooo_packets, next_seq_str, &plen);
			create_ack(*pkt);
			memcpy(&node_buffer[source_nodenumber].incomplete_data[0] + node_buffer[source_nodenumber].bytes_added, &pkt->data, pkt->len);
			node_buffer[source_nodenumber].bytes_added += pkt->len;
			node_buffer[source_nodenumber].next_seq_number_to_add++;
		}
		// check for the last packet
		if(f->payload.flag_offset == 1) {
			//printf("\t\t\t\t\t\tBytes in reconstructed message is %d and is sent by %d\n",node_buffer[source_nodenumber].bytes_added, source_nodenumber);
			CHECK(CNET_write_application((char*)&node_buffer[source_nodenumber].incomplete_data[0], &node_buffer[source_nodenumber].bytes_added));
			node_buffer[source_nodenumber].next_seq_number_to_add = 0;
			memset(node_buffer[source_nodenumber].incomplete_data, '\0', MAX_MESSAGE_SIZE);
			hashtable_free(node_buffer[source_nodenumber].ooo_packets);
			// Overriding default bucket size of 1023
			node_buffer[source_nodenumber].ooo_packets = hashtable_new(256);
			printf("\t\t\t\t\t\tSuccessfully Written to Application. Bytes in the reconstructed message #%d are %d sent by %d.\n", 
				f->payload.mesg_seq_no, node_buffer[source_nodenumber].bytes_added, source_nodenumber);
			node_buffer[source_nodenumber].bytes_added = 0; 		
			node_buffer[source_nodenumber].mesg_seq_no_to_receive++; //= f->payload.mesg_seq_no;
		}
	} else {
		size_t plen;
		hashtable_find(node_buffer[source_nodenumber].ooo_packets, seq_str, &plen);
		if(plen == 0){
			hashtable_add(node_buffer[source_nodenumber].ooo_packets, seq_str, &f->payload, len - sizeof(uint32_t));
		}
	}
	node_buffer[source_nodenumber].busy = false;
	process_frames();
	free(f);
}
Exemplo n.º 30
0
void load_contact_samples_with_hashtable(FILE *fsource, struct Region *aRegion, struct Hashtable *table, time_t *startAt, time_t *endAt)
{
	char buf[128], *vName1, *vName2, *tm, *strp, key[128];
	struct ContactSample *newContactSample;
	struct Item *aPairItem;
	struct Pair *aPair = NULL;
	time_t timestamp;
	time_t tableStartAt, tableEndAt;
	struct Point aPoint, bPoint;
	double distance;
	int rAngle, rSpeed;
	int needed;
	int first;
	
	if(table == NULL)
		return;

	first = 1;
	while(fgets(buf, 128, fsource)) {
		vName1 = strtok(buf, ",");
		vName2 = strtok(NULL, ",");
		tm = strtok(NULL, ",");
		strp = strtok(NULL, ",");
		aPoint.x = atof(strp);
		strp = strtok(NULL, ",");
		aPoint.y = atof(strp);
		strp = strtok(NULL, ",");
		bPoint.x = atof(strp);
		strp = strtok(NULL, ",");
		bPoint.y = atof(strp);
		strp = strtok(NULL, ",");
		distance = atof(strp);
		strp = strtok(NULL, ",");
		rAngle = atoi(strp);
		strp = strtok(NULL, ",");
		rSpeed = atoi(strp);
		timestamp = strtot(tm);

		if(aRegion == NULL && *startAt == 0 && *endAt == 0) {
			needed = 1;
		} else if (aRegion == NULL && *startAt == 0 && *endAt) {
			if (timestamp <= *endAt) needed = 1; else needed = 0;
		} else if (aRegion == NULL && *startAt && *endAt == 0) {
			if (timestamp >= *startAt) needed = 1; else needed = 0;
		} else if (aRegion == NULL && *startAt && *endAt) {
			if (timestamp <= *endAt && timestamp >= *startAt) needed = 1; else needed = 0;
		} else if (aRegion && *startAt == 0 && *endAt == 0) {
			if (is_point_in_polygon(&aPoint, aRegion->chosen_polygon) && is_point_in_polygon(&bPoint, aRegion->chosen_polygon) )
				needed = 1; else needed = 0;
		} else if (aRegion && *startAt == 0 && *endAt) {
			if (timestamp <= *endAt && is_point_in_polygon(&aPoint, aRegion->chosen_polygon) && is_point_in_polygon(&bPoint, aRegion->chosen_polygon))
				needed = 1; else needed = 0;
		} else if (aRegion && *startAt && *endAt == 0) {
			if (is_point_in_polygon(&aPoint, aRegion->chosen_polygon) && is_point_in_polygon(&bPoint, aRegion->chosen_polygon) && timestamp >= *startAt)
				needed = 1; else needed = 0;
		} else if (aRegion && *startAt && *endAt) {
			if (is_point_in_polygon(&aPoint, aRegion->chosen_polygon) && is_point_in_polygon(&bPoint, aRegion->chosen_polygon) && timestamp >= *startAt && timestamp <= *endAt)
				needed = 1; else needed = 0;
		} 
		if( needed ) {	
			newContactSample = (struct ContactSample*)malloc(sizeof(struct ContactSample));
			newContactSample->timestamp = timestamp;
			newContactSample->gPoint1.x = aPoint.x;
			newContactSample->gPoint1.y = aPoint.y;
			newContactSample->gPoint2.x = bPoint.x;
			newContactSample->gPoint2.y = bPoint.y;
			newContactSample->distance = distance;
			newContactSample->rSpeed = rSpeed;
			newContactSample->rAngle = rAngle;

			sprintf(key, "%s,%s", vName1, vName2);
			aPairItem = hashtable_find(table, key);
			if(aPairItem == NULL) {
				aPair = (struct Pair*)malloc(sizeof(struct Pair));
				pair_init_func(aPair);
				strncpy(aPair->vName1, vName1, strlen(vName1)+1);
				strncpy(aPair->vName2, vName2, strlen(vName2)+1);
				hashtable_add(table, key, aPair);
				aPair->startAt = timestamp;
				aPair->endAt = timestamp;
				if(first) {
					tableStartAt = aPair->startAt;
					tableEndAt = aPair->endAt;
					first = 0;
				}	
			} else {
				aPair = (struct Pair*)aPairItem->datap;
				if(timestamp < aPair->startAt) {
					aPair->startAt = timestamp;
					if(aPair->startAt < tableStartAt)
						tableStartAt = aPair->startAt;
				}
				if(timestamp > aPair->endAt) {
					aPair->endAt = timestamp;
					if(aPair->endAt > tableEndAt)
						tableEndAt = aPair->endAt;
				}
			}
			newContactSample->fromPair = aPair;
			duallist_add_to_tail(&aPair->contents, newContactSample);
		}
	}

	if(table->count) {
		*startAt = tableStartAt;
		*endAt = tableEndAt;
	}
}