设为首页 加入收藏

TOP

C语言实现简单的倒排文件索引(一)
2013-07-22 17:56:54 来源: 作者: 【 】 浏览:260
Tags:语言 实现 简单 文件 索引

  inver.h文件

  [cpp]

  #ifndef INVERT_FILE_H

  #define INVERT_FILE_H

  #include<stdio.h>

  #include<stdlib.h>

  typedef struct _invertfile_ {

  unsigned int tablelen;

  void **table;

  //unsigned int offset;

  unsigned int nodecount;

  }if_t;

  typedef struct _word_{

  unsigned int id;

  unsigned int refered;//

  void *link;

  }word_t;

  typedef struct _word_frequency_{

  unsigned int d_id;

  unsigned int refered;//the num of referenced in the document

  void *next;

  }wf_t;

  if_t* invertfile_create(int length);

  void invertfile_insert(if_t *h,int w_id,int d_id);

  wf_t* invertfile_search(if_t *h,int w_id,int d_id);

  void invertfile_traverse(if_t *h);

  void invertfile_free(if_t *h);

  #endif

  invert.cpp

  [cpp]

  #include"invert.h"

  if_t* invertfile_create(int length){

  if_t *h;

  h = (if_t *)calloc(1,sizeof(if_t));

  if (NULL == h) return NULL;

  h->table =(void **)calloc(length,sizeof(void *));

  h->tablelen=length;

  h->nodecount=0;

  word_t *w;

  for(int i=0;i<length;i++){

  h->table[i]=malloc(sizeof(word_t));

  w=(word_t*)h->table[i];

  w->id=i;

  w->refered=0;

  w->link=NULL;

  }

  return h;

  }

  //check if document d_id have word w_id

  wf_t* invertfile_search(if_t *h,int w_id,int d_id){

  word_t *w;

  wf_t    *wf;

  w=(word_t*)h->table[w_id];

  if(w->refered>0){

  wf=(wf_t*)w->link;

  while(wf){

  if(wf->d_id==d_id)return wf;

  wf=(wf_t*)wf->next;

  }

  }

  return NULL;

  }

  void invertfile_insert(if_t *h,int w_id,int d_id){

  word_t * w=(word_t*)h->table[w_id];

  wf_t * wf;

  if((wf=invertfile_search(h,w_id,d_id))!=NULL){

  wf->refered++;

  }

  else{

  wf=(wf_t *)malloc(sizeof(wf_t));

  wf->next=w->link;

  w->link=wf;

  w->refered++;

  wf->refered++;

  wf->d_id=d_id;

  h->nodecount++;

  }

  }

  void invertfile_free(if_t *h){

  word_t *w;

  wf_t* wf,*cur;

  for(int i=0;i<h->tablelen;i++){

  w=(word_t*)h->table[i];

  wf=(wf_t*)w->link;

  while(wf!=NULL){

  cur=wf;

  wf=(wf_t*)wf->next;

  free(cur);

  }

  free(w);

  }

   

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇用C的库函数获取本地时间 下一篇如何实现C的函数重载

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: