将开发过程中较好的代码段做个珍藏,如下代码是关于C语言实现简单的倒排文件索引的代码,希望对各位有所用。
#ifndef INVERT_FILE_H #define INVERT_FILE_H #include<stdio.h> #include<stdlib.h>
typedef struct invertfile { unsigned int tablelen; unsigned int nodecount;
}if_t; typedef struct word{ unsigned int id; }word_t; typedef struct word_frequency{ unsigned int d_id; }wf_t;
#endif
invert.cpp
#include"invert.h"
if (NULL == h) return NULL;
h->tablelen=length;
h->nodecount=0;
for(int i=0;i<length;i++){
h->table[i]=malloc(sizeof(word_t));
w->id=i;
w->refered=0;
w->link=NULL;
}
return h;
} if(w->refered>0){ while(wf){ if(wf->d_id==d_id)return wf; } } return NULL; } if((wf=invertfile_search(h,w_id,d_id))!=NULL){ wf->refered++; } else{ wf->next=w->link; w->link=wf; w->refered++; wf->refered++; wf->d_id=d_id; h->nodecount++; } } for(int i=0;itablelen;i++){ while(wf!=NULL){ cur=wf; free(cur); } free(w); } free(h->table); } for(int i=0;itablelen;i++){ printf(“word_id:%d;”,w->id); while(wf!=NULL){ cur=wf; printf(“d_id:%d,freq:%d;”,cur->d_id,cur->refered); } printf(“n”); } }
测试文件main.cpp
#include"invert.h" int main(){ invertfile_insert(f,1,1); invertfile_insert(f,1,1); invertfile_insert(f,1,3); invertfile_insert(f,2,5); invertfile_traverse(f); invertfile_free(f); }
实验结果:
word_id:0; word_id:1;d_id:3,freq:1;d_id:1,freq:2; word_id:2;d_id:5,freq:1; word_id:3; word_id:4; word_id:5; word_id:6; word_id:7; word_id:8; word_id:9;
|