Esempio n. 1
0
int
main(int argc, char **argv)
{
    struct key *k;
	struct hashtable *h;
    struct value *v, *found;
    struct hashtable_itr *itr;
	char line[4096];
	struct url_list_t *list;
    int i;
	
	if(argc < 3) {
		fprintf(stderr, "usage: tester hash_table_length url_file\n");
		exit(1);
	}
	
	list = calloc(1, sizeof(*list));
    h = create_hashtable(atoi(argv[1]), hashfromkey, equalkeys);
    if (NULL == h) exit(-1); /*oom*/

	FILE *url_file = fopen(argv[2], "r");
	assert(url_file);
	
	while(fgets(line, 4096, url_file)) {
		if(isspace(*line))
			continue;
		k = calloc(1, sizeof(*k));
		k->str = strdup(md5_string_get(line));
		v = calloc(1, sizeof(*v));
		v->id = strdup(line);

		url_list_insert(&list, line);
		insert_some(h, k, v);
	}
        
    printf("After insertion, hashtable contains %u items.\n", hashtable_count(h));

	struct url_list_t *head = list;
	int found_num = 0;
	for(; head; head = head->next) {
		struct key k;
		
		if(head->url == NULL)
			break;
		k.str = md5_string_get(head->url);
		if(NULL == (found = search_some(h, &k))) {
			printf("BUG can not find %s\n", k.str);
		}
		else 
			printf("Found::: %s %s\n", k.str, found->id);
		found_num ++;
	}
	printf("Total find number %d, hash congestion %u, hash table length %d\n", found_num, h->same_hash, h->tablelength);
	hashtable_destroy(h, 0);
    return 0;
}
void addcalledge(callnodeptr fromcn, callnodeptr tocn){
  
  KEY *k1;
  SM_AllocateStruct(smKey, k1);
  k1->goal = tocn->id;
  
  // #ifdef INCR_DEBUG
  //  printf("%d-->%d",fromcn->id,tocn->id);	
  // #endif
  
  
  if (NULL == search_some(fromcn->outedges->hasht,k1)) {
    insert_some(fromcn->outedges->hasht,k1,tocn);

#ifdef INCR_DEBUG	
    printf("Inedges of %d = ",tocn->id);
    temp=tocn->inedges;
    while(temp!=NULL){
      printf("\t%d",temp->inedge_node->callnode->id);
      temp=temp->next;
    }
    printf("\n");
#endif
    
    add_calledge(&(tocn->inedges),fromcn->outedges);      
    call_edge_count_gl++;
    fromcn->outcount++;
    
#ifdef INCR_DEBUG		
    if(IsNonNULL(fromcn->goal)){
      sfPrintGoal(stdout,(VariantSF)fromcn->goal,NO);printf("(%d)",fromcn->id);
    }else
      printf("(%d)",fromcn->id);
    
    if(IsNonNULL(tocn->goal)){
      printf("-->");	
      sfPrintGoal(stdout,(VariantSF)tocn->goal,NO);printf("(%d)",tocn->id);
    }
    printf("\n");	
#endif
    
    
  }

#ifdef INCR_DEBUG
	printf("Inedges of %d = ",tocn->id);
	temp=tocn->inedges;
	while(temp!=NULL){
		printf("\t%d",temp->inedge_node->callnode->id);
		temp=temp->next;
	}
	printf("\n---------------------------------\n");
#endif
}
void xsb_compute_scc(SCCNode * nodes,int * dfn_stack,int node_from, int * dfn_top,
		     struct hashtable* hasht,int * dfn,int * component ) {
  struct hashtable_itr *itr;
  struct hashtable* edges_hash;
  CPtr sf;
  Integer node_to;
  int j;

  //  printf("xsb_compute_scc for %d %p %s/%d dfn %d dfn_top %d\n",
  //	 node_from,nodes[node_from].node,
  //	 get_name(TIF_PSC(subg_tif_ptr(nodes[node_from].node))),
  //	 get_arity(TIF_PSC(subg_tif_ptr(nodes[node_from].node))),*dfn,*dfn_top);
  nodes[node_from].low = nodes[node_from].dfn = (*dfn)++;
  dfn_stack[*dfn_top] = node_from;
  nodes[node_from].stack = *dfn_top;
  (*dfn_top)++;
  edges_hash = subg_callnode_ptr(nodes[node_from].node)->outedges->hasht;
  itr = hashtable1_iterator(edges_hash);       
  if (hashtable1_count(edges_hash) > 0) {
    //    printf("found %d edges\n",hashtable1_count(edges_hash));
    do {
      sf = ((callnodeptr) hashtable1_iterator_value(itr))-> goal;
      node_to = (Integer) search_some(hasht, (void *)sf);
      //      printf("edge from %p to %p (%d)\n",(void *)nodes[node_from].node,sf,node_to);
      if (nodes[node_to].dfn == 0) {
	xsb_compute_scc(nodes,dfn_stack,(int)node_to, dfn_top,hasht,dfn,component );
	if (nodes[node_to].low < nodes[node_from].low) 
	  nodes[node_from].low = nodes[node_to].low;
	}	  
	else if (nodes[node_to].dfn < nodes[node_from].dfn  && nodes[node_to].component == 0) {
	  if (nodes[node_to].low < nodes[node_from].low) { nodes[node_from].low = nodes[node_to].low; }
	}
      } while (hashtable1_iterator_advance(itr));
    //    printf("nodes[%d] low %d dfn %d\n",node_from,nodes[node_from].low, nodes[node_from].dfn);
    if (nodes[node_from].low == nodes[node_from].dfn) {
      for (j = (*dfn_top)-1 ; j >= nodes[node_from].stack ; j--) {
	//	printf(" pop %d and assign %d\n",j,*component);
	nodes[dfn_stack[j]].component = *component;
      }
      (*component)++;       *dfn_top = j+1;
    }
  } 
  else nodes[node_from].component = (*component)++;
}
int  get_incr_sccs(CTXTdeclc Cell listterm) {
  Cell orig_listterm, intterm, node;
    long int node_num=0;
    int i = 0, dfn, component = 1;     int * dfn_stack; int dfn_top = 0, ret;
    SCCNode * nodes;
    struct hashtable_itr *itr;     struct hashtable* hasht; 
    XSB_Deref(listterm);
    hasht = create_hashtable1(HASH_TABLE_SIZE, hashid, equalkeys);
    orig_listterm = listterm;
    intterm = get_list_head(listterm);
    XSB_Deref(intterm);
    //    printf("listptr %p @%p\n",listptr,(CPtr) int_val(*listptr));
    insert_some(hasht,(void *) oint_val(intterm),(void *) node_num);
    node_num++; 

    listterm = get_list_tail(listterm);
    XSB_Deref(listterm);
    while (!isnil(listterm)) {
      intterm = get_list_head(listterm);
      XSB_Deref(intterm);
      node = oint_val(intterm);
      if (NULL == search_some(hasht, (void *)node)) {
	insert_some(hasht,(void *)node,(void *)node_num);
	node_num++;
      }
      listterm = get_list_tail(listterm);
      XSB_Deref(listterm);
    }
    nodes = (SCCNode *) mem_calloc(node_num, sizeof(SCCNode),OTHER_SPACE); 
    dfn_stack = (int *) mem_alloc(node_num*sizeof(int),OTHER_SPACE); 
    listterm = orig_listterm;; 
    //printf("listptr %p @%p\n",listptr,(void *)int_val(*(listptr)));
    intterm = get_list_head(listterm);
    XSB_Deref(intterm);
    nodes[0].node = (CPtr) oint_val(intterm);
    listterm = get_list_tail(listterm);
    XSB_Deref(listterm);
    i = 1;
    while (!isnil(listterm)) {
      intterm = get_list_head(listterm);
      XSB_Deref(intterm);
      node = oint_val(intterm);
      nodes[i].node = (CPtr) node;
      listterm = get_list_tail(listterm);
      XSB_Deref(listterm);
      i++;
    }
    itr = hashtable1_iterator(hasht);       
    SQUASH_LINUX_COMPILER_WARN(itr);
    //    do {
    //      printf("k %p val %p\n",hashtable1_iterator_key(itr),hashtable1_iterator_value(itr));
    //    } while (hashtable1_iterator_advance(itr));

    listterm = orig_listterm;
    //    printf("2: k %p v %p\n",(void *) int_val(*listptr),
    //	   search_some(hasht,(void *) int_val(*listptr)));

    //    while (!isnil(*listptr)) {  now all wrong...
    //      listptr = listptr + 1;
    //      node = int_val(*clref_val(listptr));
    //      printf("2: k %p v %p\n",(CPtr) node,search_some(hasht,(void *) node));
    //      listptr = listptr + 1;
    //    }
    dfn = 1;
    for (i = 0; i < node_num; i++) {
      if (nodes[i].dfn == 0) 
	xsb_compute_scc(nodes,dfn_stack,i,&dfn_top,hasht,&dfn,&component);
      //      printf("++component for node %d is %d (high %d)\n",i,nodes[i].component,component);
    }
    ret = return_scc_list(CTXTc  nodes, node_num);
    hashtable1_destroy(hasht,0);
    mem_dealloc(nodes,node_num*sizeof(SCCNode),OTHER_SPACE); 
    mem_dealloc(dfn_stack,node_num*sizeof(int),OTHER_SPACE); 
    return ret;
}