#include #include #include #include #include "index.h" struct Index* InitIndex() { struct Index *index = (struct Index*)malloc(sizeof(struct Index)); index->filelist = NULL; index->indexlist = NULL; index->relation = NULL; int i; for (i = 0; i < 26; i++) { index->indexidx[i] = NULL; } return index; } void DestroyIndex(struct Index *index) { struct FileList *filetrav, *filelast; struct IndexList *indextrav, *indexlast; struct Relation *reltrav, *rellast; filetrav = index->filelist; while (filetrav != NULL) { filelast = filetrav; filetrav = filetrav->next; free(filelast->filename); free(filelast); } indextrav = index->indexlist; while (indextrav != NULL) { indexlast = indextrav; indextrav = indextrav->next; free(indexlast->keyword); free(indexlast); } reltrav = index->relation; while (reltrav != NULL) { rellast = reltrav; reltrav = reltrav->next; free(rellast); } free(index); } void Insert(struct Index *index, char *keyword, char *filename, int offset) { assert(index); struct FileList *file = InsertFile(index, filename); struct IndexList *word = InsertKeyword(index, keyword); MakeRelation(index, word, file, offset); } struct IndexList* InsertKeyword(struct Index *index, char *keyword) { struct IndexList *indextrav = index->indexlist, *indexlast = NULL; int i, j = keyword[0] - 'a'; if (indextrav == NULL) // empty list { indextrav = index->indexlist = (struct IndexList*)malloc(sizeof(struct IndexList)); indextrav->next = NULL; } else if ((index->indexidx[j] == NULL) || (strcmp(index->indexidx[j]->keyword, keyword) > 0)) // index index needs updating { j -= 1; while ((j >= 0) && (index->indexidx[j] == NULL)) j -= 1; if (j < 0) // will become head of list { indextrav = (struct IndexList*)malloc(sizeof(struct IndexList)); indextrav->next = index->indexlist; index->indexlist = indextrav; } else // insert before first of same { indextrav = index->indexidx[j]; while (indextrav->next != NULL) { if (indextrav->next->keyword[0] >= keyword[0]) break; indextrav = indextrav->next; } indexlast = indextrav->next; indextrav->next = (struct IndexList*)malloc(sizeof(struct IndexList)); indextrav = indextrav->next; indextrav->next = indexlast; } index->indexidx[keyword[0] - 'a'] = indextrav; } else { indextrav = index->indexidx[j]; while (indextrav != NULL) { i = strcmp(indextrav->keyword, keyword); if (i == 0) return indextrav; if (i > 0) { indexlast = indexlast->next = (struct IndexList*)malloc(sizeof(struct IndexList)); indexlast->next = indextrav; indextrav = indexlast; break; } indexlast = indextrav; indextrav = indextrav->next; } if (indextrav == NULL) { indextrav = indexlast->next = (struct IndexList*)malloc(sizeof(struct IndexList)); indextrav->next = NULL; } } indextrav->len = strlen(keyword) + 1; if ((indextrav->len % 4) != 0) indextrav->len += 4 - (indextrav->len % 4); indextrav->keyword = (char*)malloc(indextrav->len); strcpy(indextrav->keyword, keyword); return indextrav; } struct FileList* InsertFile(struct Index *index, char *filename) { struct FileList *filetrav = index->filelist, *filelast; if (filetrav == NULL) { filetrav = index->filelist = (struct FileList*)malloc(sizeof(struct FileList)); filetrav->next = NULL; } else { while (filetrav != NULL) { if (strcmp(filetrav->filename, filename) == 0) return filetrav; filelast = filetrav; filetrav = filetrav->next; } filetrav = (struct FileList*)malloc(sizeof(struct FileList)); filetrav->next = index->filelist; index->filelist = filetrav; } filetrav->len = strlen(filename) + 1; if ((filetrav->len % 4) != 0) filetrav->len += 4 - (filetrav->len % 4); filetrav->filename = (char*)malloc(filetrav->len); strcpy(filetrav->filename, filename); return filetrav; } void MakeRelation(struct Index *index, struct IndexList *word, struct FileList *file, int offset) { struct Relation *reltrav = index->relation; if (reltrav == NULL) { reltrav = index->relation = (struct Relation*)malloc(sizeof(struct Relation)); reltrav->next = NULL; } else { reltrav = (struct Relation*)malloc(sizeof(struct Relation)); reltrav->next = index->relation; index->relation = reltrav; } reltrav->word = word; reltrav->file = file; reltrav->offset = offset; } void Print(struct Index *index) { struct FileList *filetrav = index->filelist; struct IndexList *indextrav = index->indexlist; struct Relation *reltrav = index->relation; printf("Filenames: "); if (filetrav == NULL) printf("** EMPTY **"); else while (filetrav != NULL) { if (filetrav != index->filelist) printf(", "); printf("%s", filetrav->filename); filetrav = filetrav->next; } printf("\n"); printf("Keywords: "); if (indextrav == NULL) printf("** EMPTY **"); else while (indextrav != NULL) { if (indextrav != index->indexlist) printf(", "); printf("%s", indextrav->keyword); indextrav = indextrav->next; } printf("\n"); printf("Relations: "); if (reltrav == NULL) printf("** EMPTY **"); else while (reltrav != NULL) { if (reltrav != index->relation) printf(", "); printf("(%s,%s,%i)", reltrav->word->keyword, reltrav->file->filename, reltrav->offset); reltrav = reltrav->next; } printf("\n"); }