Exemplo n.º 1
0
//rehashing function
int rehash(CHTbl *htbl)
{
 void *key;
 void *data;
 int i, j, oldbuckets, size;
 
 oldbuckets = htbl->buckets;
 htbl->buckets *= 2;
 for(i = oldbuckets; i < htbl->buckets; ++i)
  list_init(&htbl->table[i], list_dest_elm, NULL, list_print_elm);

 htbl->h = f[((*fcptr)++)%3];

 for(i = 0; i < oldbuckets; ++i)
 {
  size = (&(htbl->table[i]))->size;
  for(j = 0; j < size; ++j)
  {
   list_rem_next(&(htbl->table[i]), NULL, (const void**) &key, (const void**) &data);
   htbl->size--;
   chtbl_insert(&htbl, key, data);
  }
 }
 return 0;
}
Exemplo n.º 2
0
//read words and definitions from a file and add them to the table
int readfile(CHTbl *htbl, char *file, FILE *wfile)
{
 FILE *fp; 
 if((fp = fopen(file, "r")) == NULL)
  return -1;
 size_t size = sizeof(char)*500;
 char *word = malloc(sizeof(char)*50);
 char *def = malloc(sizeof(char)*500);
 int i, j;
 i = fscanf(fp, " %s ", word);
 j = getline(&def, &size, fp);   


 while((i != -1)&&(j != -1))
 {
  if(!chtbl_insert(&htbl, word, def))
   printtolog(wfile, htbl);
  if((word = malloc(sizeof(char)*50))==NULL)
  {
   fclose(fp);
   return -1;
  }
  if((def = malloc(sizeof(char)*500))==NULL)
  {
   fclose(fp);
   return -1;
  }
  i = fscanf(fp, " %s ", word);
  j = getline(&def, &size, fp); 
 }

 free(word);
 free(def);
 fclose(fp);

 return 0;  
}
int main (int argc, char *argv[]) {

        FILE *LINKDBFILE;
        FILE *INDEXFILE;

	int unixipes, unixclassa;
	struct in_addr addr;
	int count;
        int ranged;
        struct linkdb_block linkdbPost;
	unsigned int DocID;
	off64_t offset;
        unsigned int lastDocID;
	FILE *IPDBHA;
	unsigned int ipadress, classaadress;
	CHTbl htbl;
	CHTbl htbl_classa;

	unsigned int *prti;
        if (argc < 4) {
                printf("Dette programet tar inn en linkdb fil og lager Brank\n\n\tUsage: ./searchIndexLinkdb LINKDBFILE INDEXFILE\n");
                exit(0);
        }

        if ((LINKDBFILE = (FILE *)fopen64(argv[1],"rb")) == NULL) {
                printf("Cant read linkdb ");
                perror(argv[1]);
                exit(1);
        }


        if ((INDEXFILE = fopen(argv[2],"rb")) == NULL) {
                printf("Cant read index ");
                perror(argv[2]);
                exit(1);
        }

        if ((IPDBHA = fopen(IPDBPATH,"rb")) == NULL) {
                printf("Cant read linkdb ");
                perror(IPDBPATH);
                exit(1);
        }

	DocID = atol(argv[3]);

	printf("links ti %u:\n",DocID);

	//finner offset
	fseek(INDEXFILE,DocID * sizeof(offset),SEEK_SET);
	fread(&offset,sizeof(offset),1,INDEXFILE);

	//søker til offsetten
	if(fseeko64(LINKDBFILE,offset,SEEK_SET) == -1) {
		perror("seek");
		exit(1);
	}


	//inaliserer hashen
	chtbl_init(&htbl, PRIME_TBLSIZ, h, match, free);
	chtbl_init(&htbl_classa, PRIME_TBLSIZ, h, match, free);
	

	unixipes = 0;
	unixclassa = 0;
	count = 0;
        while (!feof(LINKDBFILE)) {


                if (fread(&linkdbPost,sizeof(linkdbPost),1,LINKDBFILE) == 0) {
			perror("read");
			exit(1);
		}

		

		if (linkdbPost.DocID_to == DocID) {
			fseek(IPDBHA,linkdbPost.DocID_from * sizeof(ipadress),SEEK_SET);
                        fread(&ipadress,sizeof(ipadress),1,IPDBHA);
			addr.s_addr = ipadress;
			classaadress = ipadress & 0xffffff;

			prti = &ipadress;

			if (chtbl_lookup(&htbl,(void *)&prti) == 0) {
				//printf("set ip før\n");
			}
			else {
				//siden denne dataen skal ligge i hashen må vi ha et egetminne om råde på hippen for $
                                prti = malloc(sizeof(unsigned int));
				*prti = ipadress;
                                chtbl_insert(&htbl,(void *)prti);
				++unixipes;

			}


			prti = &classaadress;

			if (chtbl_lookup(&htbl_classa,(void *)&prti) == 0) {
				//printf("set ip før\n");
			}
			else {
				//siden denne dataen skal ligge i hashen må vi ha et egetminne om råde på hippen for $
                                prti = malloc(sizeof(unsigned int));
				*prti = classaadress;
                                chtbl_insert(&htbl_classa,(void *)prti);
				++unixclassa;

			}

			//printf("%u (%s) -> %u %u\n",linkdbPost.DocID_from,inet_ntoa(addr),linkdbPost.DocID_to,ipadress);
		}
		else {
			//printf("end %u\n",linkdbPost.DocID_to);
			//printf("last: %u -> %u\n",linkdbPost.DocID_from,linkdbPost.DocID_to);
			break;
		}

		++count;
	}
	chtbl_destroy(&htbl);
	chtbl_destroy(&htbl_classa);

	fclose(LINKDBFILE);
	fclose(INDEXFILE);
	fclose(IPDBHA);
	
	printf("%i links\nUnike IPer %i\nunix class A %i\n",count,unixipes,unixclassa);
}
Exemplo n.º 4
0
int main(int argc, char **argv) {

CHTbl              htbl;

char               *data,
                   c;

int                retval,
                   i;

/*****************************************************************************
*                                                                            *
*  Initialize the chained hash table.                                        *
*                                                                            *
*****************************************************************************/

if (chtbl_init(&htbl, TBLSIZ, h_char, match_char, free) != 0)
   return 1;

/*****************************************************************************
*                                                                            *
*  Perform some chained hash table operations.                               *
*                                                                            *
*****************************************************************************/

for (i = 0; i < TBLSIZ; i++) {

   if ((data = (char *)malloc(sizeof(char))) == NULL)
      return 1;

   *data = ((5 + (i * 6)) % 23) + 'A';

   if (chtbl_insert(&htbl, data) != 0)
      return 1;

   print_table(&htbl);

}

for (i = 0; i < TBLSIZ; i++) {

   if ((data = (char *)malloc(sizeof(char))) == NULL)
      return 1;

   *data = ((3 + (i * 4)) % 23) + 'a';

   if (chtbl_insert(&htbl, data) != 0)
      return 1;

   print_table(&htbl);

}

if ((data = (char *)malloc(sizeof(char))) == NULL)
   return 1;

*data = 'd';

if ((retval = chtbl_insert(&htbl, data)) != 0)
   free(data);

fprintf(stdout, "Trying to insert d again...Value=%d (1=OK)\n", retval);

if ((data = (char *)malloc(sizeof(char))) == NULL)
   return 1;

*data = 'G';

if ((retval = chtbl_insert(&htbl, data)) != 0)
   free(data);

fprintf(stdout, "Trying to insert G again...Value=%d (1=OK)\n", retval);

fprintf(stdout, "Removing d, G, and S\n");

c = 'd';
data = &c;

if (chtbl_remove(&htbl, (void **)&data) == 0)
   free(data);

c = 'G';
data = &c;

if (chtbl_remove(&htbl, (void **)&data) == 0)
   free(data);

c = 'S';
data = &c;

if (chtbl_remove(&htbl, (void **)&data) == 0)
   free(data);

print_table(&htbl);

if ((data = (char *)malloc(sizeof(char))) == NULL)
   return 1;

*data = 'd';

if ((retval = chtbl_insert(&htbl, data)) != 0)
   free(data);

fprintf(stdout, "Trying to insert d again...Value=%d (0=OK)\n", retval);

if ((data = (char *)malloc(sizeof(char))) == NULL)
   return 1;

*data = 'G';

if ((retval = chtbl_insert(&htbl, data)) != 0)
   free(data);

fprintf(stdout, "Trying to insert G again...Value=%d (0=OK)\n", retval);

print_table(&htbl);

fprintf(stdout, "Inserting X and Y\n");

if ((data = (char *)malloc(sizeof(char))) == NULL)
   return 1;

*data = 'X';

if (chtbl_insert(&htbl, data) != 0)
   return 1;

if ((data = (char *)malloc(sizeof(char))) == NULL)
   return 1;

*data = 'Y';

if (chtbl_insert(&htbl, data) != 0)
   return 1;

print_table(&htbl);

c = 'X';
data = &c;

if (chtbl_lookup(&htbl, (void **)&data) == 0)
   fprintf(stdout, "Found an occurrence of X\n");
else
   fprintf(stdout, "Did not find an occurrence of X\n");

c = 'Z';
data = &c;

if (chtbl_lookup(&htbl, (void **)&data) == 0)
   fprintf(stdout, "Found an occurrence of Z\n");
else
   fprintf(stdout, "Did not find an occurrence of Z\n");

/*****************************************************************************
*                                                                            *
*  Destroy the chained hash table.                                           *
*                                                                            *
*****************************************************************************/

fprintf(stdout, "Destroying the hash table\n");
chtbl_destroy(&htbl);

return 0;

}
Exemplo n.º 5
0
int main()
{
 void *data;
 char *command1, *command2, *def;
 int result;
 FILE *fp = fopen("hashlog.txt", "w");
 CHTbl *htbl = malloc(sizeof(CHTbl));
 chtbl_init(htbl, 4, g3, vstrcmp, NULL);
 fprintf(fp, "Load factor\tOccupancy\n", (float)(htbl->size/htbl->buckets), htbl->buckets);
 while(1)
 {
  printf(">");
  command1 = malloc(sizeof(char)*10);
  command2 = malloc(sizeof(char)*50);
  def = malloc(sizeof(char)*1000);
  result = (parseline(command1, command2, def));


  switch(result){
   case -1:
    printf("Error: invalid use of 'add'\n");
    free(command2);
    free(def);
    break;
   case -2:
    printf("Error: invalid use of 'delete'\n");
    free(command2);
    free(def);
    break;
   case -3:
    printf("Error: invalid use of 'find'\n");
    free(command2);
    free(def);
    break;
   case -4:
    printf("Error: command not recognized\n");
    free(command2);
    free(def);
    break;
   case -5:
    printf("Error: no filename given");
    free(command2);
    free(def);
   case 1:
    if(!(chtbl_insert(&htbl, command2, def)))
    {
     printf("Added %s to dictionary\n", command2);
     printtolog(fp, htbl);
    }
    else printf("Error - %s not added to dictionary\n", command2);
    break;
   case 2:
    free(def);
    if(!htbl_remove(htbl, command2))
    {
     printf("Deleted %s from dictionary\n", command2);
     printtolog(fp, htbl);
    }
    else printf("Error - %s not found\n", command2);
    free(command2);
    break;
   case 3:
    free(def);
    if(!chtbl_lookup(htbl, command2, &data))
    {
     printf("%s\n", command2);
     printf("%s\n", (char *)data);
    }
    else printf("%s not found in dictionary\n", command2);
    free(command2);
    break;
   case 4:
    free(command2);
    free(def);
    print_table(htbl);
    break;
   case 5:
    free(command1);
    free(command2);
    free(def);
    destroy_table(htbl);
    free(htbl);
    fclose(fp);
    return 0;
    break; 
   case 6:
    if(!readfile(htbl, command2, fp))
     printf("File scanned to dictionary\n");
    else printf("Error - file not scanned\n");
    free(command2);
    break;
   case 7:
    find2(htbl, command2, def);
    free(command2);
    free(def);
    break;
   case 8:
    ptest(htbl, command2, fp);
    break;
   }
 
 free(command1);
 }
}