📄 main.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <io.h>
#include <fcntl.h>
#include <time.h >
#include "linear_hash.h"
#define LONG_BITS (8*sizeof(long))
BUCKET_BUF bucket;
DATA_BUF data;
NTH_BLOCK_BUF nth_blk_relation;
HASH_INFO hash_info = {0, 0, 0};
long DATA_CUR = 0;//记录最后最后读入的数据块号
long NTH_BLK_CUR = 0;//记录最后读入的对应信息块号
long SEARCH_CUR = 0;
BUCKET_BUF buk_buf[BUCKET_BUFFER_SIZE];
BUCKET_BUF buk_over_buf[BUCKET_BUFFER_SIZE];
DATA_BUF dat_buf[DATA_BUFFER_SIZE];
NTH_BLOCK_BUF nth_blk_buf[NTH_BLOCK_BUFFER_SIZE];
FILE *get_blk(FILE *fp);
long judge_add_bucket(long r, long n);
long add_in_which(long key, long last_i);
void transform(char *des, char *sou);
void creat_nth_blk(char *filename);
void init_nth_blk(FILE *fp);
void creat_hash_info(char *filename);
void init_hash_info(FILE *fp);
void creat_hashtab(char *filename);
void init_hash_tab(FILE *fp);
long read_idx(char *filename, long nth_bucket, long blk_no);
long read_dat(char *filename, long blk_no);
long read_hash_info(char *filename);
long read_nth_blk(char *filename, long blk_no);
void write_idx(char *filename, long blk_no);
void write_nth_blk(char *filename, long blk_no);
void write_hash_info(char *filename);
void init_buk_buf();
void init_dat_buf();
void init_nth_blk_buf();
void renew_hash_info(long data_volume);
void add_hash_table_bucket(char *filename, long old_bucket_no);
void renew_nth_blk(long nth_bucket, long bucket_no);
long renew_hash_info_hkey_i(long x);
void init_buk_buf()
{
long i = 0;
while ( i < BUCKET_BUFFER_SIZE )
{
buk_buf[i].from_blk_no = -1;//将buk_buf[i].from_blk_no的值作为此缓冲块的数据是否有效
buk_buf[i].buk.blk_no = -1;
buk_buf[i].nth_bucket = -1;
buk_buf[i].change_flag = 0;
buk_buf[i].buk.rcd_no = 0;
buk_over_buf[i].from_blk_no = -1;
buk_over_buf[i].buk.blk_no = -1;
buk_over_buf[i].nth_bucket = -1;
buk_over_buf[i].change_flag = 0;
buk_over_buf[i].buk.rcd_no = 0;
i++;
}
}
void init_dat_buf()
{
long i = 0;
while ( i < DATA_BUFFER_SIZE )
{
dat_buf[i].from_blk_no = -1;
i++;
}
}
void init_nth_blk_buf()
{
long i = 0;
while ( i < NTH_BLOCK_BUFFER_SIZE )
{
nth_blk_buf[i].from_blk_no = -1;
i++;
}
}
long judge_add_bucket(long r, long n)
{
if ((float)(r+1) <= (float)(HASH_SPACE)*(SATURATION_DEGREE)*(float)(n))
return 1;
else
return 0;
}
long add_in_which(long key, long last_i)
{
return (~((~0)<<(last_i)) & key);
}
FILE *get_blk(FILE *fp)
{
long p = -1;
long i = 0;
if ( fp != NULL )
{
fseek(fp, 0, SEEK_END);
while(i < LONG_IN_BLOCK)
{
fwrite(&p, sizeof(long), 1, fp);
i++;
}
return fp;
}
}
void transform(char *des, char *sou)
{
char buf[10];
long p = 0;
long j = 0;
FILE *fps = NULL;
FILE *fpd = NULL;
fps = fopen(sou,"r");
fpd = fopen(des,"wb+");
if ( fps != NULL && fpd != NULL)
{
fpd = get_blk(fpd);
fseek(fpd, -BLOCK_SIZE, SEEK_END);
while(fgets(buf, 10, fps) != NULL)
{
p = atoi(buf);
fwrite(&p, sizeof(long), 1, fpd);
j++;
if(j == LONG_IN_BLOCK)
{
fpd = get_blk(fpd);
j = 0;
fseek(fpd, -BLOCK_SIZE, SEEK_END);
}
}
fclose(fps);
fclose(fpd);
}
else
{ if (fps == NULL && fpd != NULL)
{
printf("OPEN %s FILE ERROR", sou);
fclose(fpd);
}
if (fps != NULL && fpd == NULL)
{
printf("OPEN %s FILE ERROR", des);
fclose(fps);
}
if (fps == NULL && fpd == NULL)
{
printf("OPEN %s FILE ERROR", sou);
printf("OPEN %s FILE ERROR", des);
}
}
}
void creat_nth_blk(char *filename)
{
FILE *fp = NULL;
fp = fopen(filename, "wb+");
if(fp == NULL)
printf("OPEN %s FILE ERROR", filename);
else
{
fp = get_blk(fp);
fclose(fp);
}
}
void creat_hash_info(char *filename)
{
FILE *fp = NULL;
fp = fopen(filename, "wb+");
if(fp == NULL)
printf("OPEN %s FILE ERROR", filename);
else
{
fp = get_blk(fp);
init_hash_info(fp);
fclose(fp);
}
}
void init_hash_info(FILE *fp)
{
fseek(fp, 0,SEEK_SET);
fwrite(&hash_info.hkey_i, sizeof(long), 1,fp);
fwrite(&hash_info.bucket_no, sizeof(long), 1,fp);
fwrite(&hash_info.rcd_no, sizeof(long), 1,fp);
}
void creat_hashtab(char *filename)
{
FILE *fp = NULL;
fp = fopen(filename, "wb+");
if(fp == NULL)
printf("OPEN %s FILE ERROR", filename);
else
{
fclose(fp);
}
}
void init_hash_tab(FILE *fp)
{
long p = -1;
long q = 0;
if ( fp != NULL )
{
fseek(fp, -BLOCK_SIZE, SEEK_END);
fwrite(&p, sizeof(long), 1,fp);
fwrite(&q, sizeof(long), 1,fp);
}
else
{
printf("INIT HASH TABLE ERROR");
}
}
long read_idx(char *filename, long nth_buk, long blk_no)
{
long i = 0;
long length = 0;
FILE *fp = NULL;
long fd = 0;
fd = open(filename, O_RDWR);
length = filelength(fd);
close(fd);
if((blk_no + 1)*BLOCK_SIZE <= length && blk_no >= 0)
{
fp = fopen(filename,"rb+");
if(fp == NULL)
{
return 0;
}
else
{
bucket.change_flag = 0;
bucket.nth_bucket = nth_buk;
bucket.from_blk_no = blk_no;
fseek(fp, blk_no*BLOCK_SIZE, SEEK_SET);
fread(&bucket.buk.blk_no, sizeof(long), 1, fp);
fread(&bucket.buk.rcd_no, sizeof(long), 1, fp);
while(i < HASH_SPACE)
{
fread(&bucket.buk.hk_addr[i].hkey, sizeof(long), 1, fp);
fread(&bucket.buk.hk_addr[i].to_rcd.blk_no, sizeof(long), 1, fp);
fread(&bucket.buk.hk_addr[i].to_rcd.offset, sizeof(long), 1, fp);
i++;
}
fclose(fp);
return 1;
}
}
else
{
return 0;
}
}
long read_dat(char *filename, long blk_no)
{
long i = 0;
long length = 0;
long sum =0;
long fd = 0;
FILE *fp = NULL;
fd = open(filename, O_RDWR);
length = filelength(fd);
close(fd);
if((blk_no + 1)*BLOCK_SIZE <= length && blk_no >= 0)
{
fp = fopen(filename,"rb+");
if(fp == NULL)
{
return 0;
}
else
{
data.change_flag = 0;
data.from_blk_no = blk_no;
fseek(fp, blk_no*BLOCK_SIZE, SEEK_SET);
while( i < LONG_IN_BLOCK)
{
fread(&data.key[i], sizeof(long), 1, fp);
if( data.key[i] != -1)
{
sum++;
}
i++;
}
fclose(fp);
return sum;//读成功则返回有效数据个数
}
}
else
{
return 0;
}
}
long read_hash_info(char *filename)
{
FILE *fp = NULL;
fp = fopen(filename,"rb+");
if (fp != NULL)
{
fseek(fp, 0, SEEK_SET);
fread(&hash_info.hkey_i, sizeof(long), 1,fp);
fread(&hash_info.bucket_no, sizeof(long), 1,fp);
fread(&hash_info.rcd_no, sizeof(long), 1,fp);
fclose(fp);
return 1;
}
else
{
return 0;
}
}
long read_nth_blk(char *filename, long blk_no)
{
long i = 0;
long length = 0;
long fd = 0;
FILE *fp = NULL;
fd = open(filename, O_RDWR);
length = filelength(fd);
close(fd);
if((blk_no + 1)*BLOCK_SIZE <= length && blk_no >= 0)
{
fp = fopen(filename,"rb+");
if(fp == NULL)
{
return 0;
}
else
{
nth_blk_relation.change_flag = 0;
nth_blk_relation.from_blk_no = blk_no;
fseek(fp, blk_no*BLOCK_SIZE, SEEK_SET);
while( i < NTH_BLOCK_IN_BLOCK)
{
fread(&nth_blk_relation.nth_blk[i].nth_bucket, sizeof(long), 1, fp);
fread(&nth_blk_relation.nth_blk[i].blk_no, sizeof(long), 1, fp);
i++;
}
fclose(fp);
return 1;
}
}
else
{
return 0;
}
}
void write_hash_info(char *filename)
{
FILE *fp = NULL;
fp = fopen(filename,"rb+");
if ( fp == NULL )
printf("OPEN %s FILE ERROR", filename);
else
{
fseek(fp, 0, SEEK_SET);
fwrite(&hash_info.hkey_i, sizeof(long), 1,fp);
fwrite(&hash_info.bucket_no, sizeof(long), 1,fp);
fwrite(&hash_info.rcd_no, sizeof(long), 1,fp);
}
fclose(fp);
}
void write_idx(char *filename, long blk_no)
{
long i = 0;
FILE *fp = NULL;
fp = fopen(filename,"rb+");
if(fp == NULL)
printf("OPEN %s FILE ERROR", filename);
else
{
fseek(fp, blk_no*BLOCK_SIZE, SEEK_SET);
fwrite(&bucket.buk.blk_no, sizeof(long), 1, fp);
fwrite(&bucket.buk.rcd_no, sizeof(long), 1, fp);
while(i < HASH_SPACE)
{
fwrite(&bucket.buk.hk_addr[i].hkey, sizeof(long), 1, fp);
fwrite(&bucket.buk.hk_addr[i].to_rcd.blk_no, sizeof(long), 1, fp);
fwrite(&bucket.buk.hk_addr[i].to_rcd.offset, sizeof(long), 1, fp);
i++;
}
fclose(fp);
}
}
void write_nth_blk(char *filename, long blk_no)
{
FILE *fp = NULL;
long i = 0;
fp = fopen(filename, "rb+");
if (fp != NULL)
{
fseek(fp, blk_no*BLOCK_SIZE, SEEK_SET);
while(i < NTH_BLOCK_IN_BLOCK)
{
fwrite(&nth_blk_relation.nth_blk[i].nth_bucket, sizeof(long), 1, fp);
fwrite(&nth_blk_relation.nth_blk[i].blk_no, sizeof(long), 1, fp);
i++;
}
fclose(fp);
}
else
{
printf("OPEN %s FILE ERROR", filename);
}
}
void write_dat(char *filename, long blk_no)
{
long i = 0;
FILE *fp = NULL;
fp = fopen(filename,"rb+");
if(fp == NULL)
printf("OPEN %s FILE ERROR", filename);
else
{
fseek(fp, blk_no*BLOCK_SIZE, SEEK_SET);
while(i < LONG_IN_BLOCK)
{
fwrite(&data.key[i], sizeof(long), 1, fp);
i++;
}
fclose(fp);
}
}
void creat_result(char *filename)
{
FILE *fp = NULL;
fp = fopen(filename, "w");
fclose(fp);
}
int main()
{
long i = 0;
long j = 0;
long ltime = 0;
clock_t start = 0;
clock_t finish = 0;
char *s[4] = {"all200000.txt", "del5000.txt", "ist5000.txt", "search20000.txt"};
char *d[4] = {"all200000.dat", "del5000.dat", "ist5000.dat", "search20000.dat"};
for ( i;i < 4; i++)
transform(d[i],s[i]);
time(<ime);
printf("%s",ctime(<ime));
init_buk_buf();
init_dat_buf();
init_nth_blk_buf();
creat_hashtab("hash_table.idx");
creat_hash_info("hash_info.dat");
creat_nth_blk("nth_block.dat");
read_hash_info("hash_info.dat");
while(i)
{
printf(" 1 for construct hash table.\n");
printf(" 2 for insert the data in ist5000.txt.\n");
printf(" 3 for find the data in search20000.txt.\n");
printf(" 4 for delete the data in del5000.txt.\n");
printf(" 5 for insert single key into hash table.\n");
printf(" 6 for find single key in hash table.\n");
printf(" 7 for delete single key from hash table.\n");
printf(" 0 for exit.\n");
printf(" Input your choice:");
scanf("%d", &i);
switch (i)
{
case 1:
start = clock();
construct_idx();
finish = clock();
printf("%f second have been took.\n\n",(double) (finish - start)/CLK_TCK);
break;
case 2:
start = clock();
insert_idx("ist5000.dat");
finish = clock();
printf("%f second have been took.\n\n",(double) (finish - start)/CLK_TCK);
break;
case 3:
creat_result("find_result.dat");
start = clock();
find_key("search20000.dat");
finish = clock();
printf("%f second have been took.\n\n",(double) (finish - start)/CLK_TCK);
break;
case 4:
creat_result("delete_result.dat");
start = clock();
delete_key("del5000.dat");
finish = clock();
printf("%f second have been took.\n\n",(double) (finish - start)/CLK_TCK);
break;
case 5:
printf("Input the single key you want to insert (NOT -1):");
scanf("%d", &j);
start = clock();
insert_single(j);
finish = clock();
printf("%f second have been took.\n\n",(double) (finish - start)/CLK_TCK);
break;
case 6:
printf("Input the single key you want to find (NOT -1):");
scanf("%d", &j);
start = clock();
find_single_key(j);
finish = clock();
printf("%f second have been took.\n\n",(double) (finish - start)/CLK_TCK);
break;
case 7:
printf("Input the single key you want to delete (NOT -1):");
scanf("%d", &j);
start = clock();
delete_single_key(j);
finish = clock();
printf("%f second have been took.\n\n",(double) (finish - start)/CLK_TCK);
break;
}
}
write_hash_info("hash_info.dat");
time(<ime);
printf("%s",ctime(<ime));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -