-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposting_list.c
96 lines (84 loc) · 2.79 KB
/
posting_list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "map.h"
#include "posting_list.h"
//create a new posting list node and initialize it
struct post_list_node *new_plist_node(int word_length)
{
struct post_list_node *list_node_current;
if (word_length == 0)
{
list_node_current = (struct post_list_node *) malloc(sizeof(struct post_list_node));
}
else
{
list_node_current = (struct post_list_node *) malloc(sizeof(struct post_list_node) + (word_length+1)*sizeof(char));
}
if (list_node_current == NULL)
{
printf("Not enough memory in heap\n");
exit(EXIT_FAILURE);
}
list_node_current->line_id = 0;
list_node_current->freq = 0;
list_node_current->post_next = NULL;
return list_node_current;
}
void free_post_list(struct post_list_node *post_list_head)
{
if (post_list_head->post_next != NULL)
free_post_list(post_list_head->post_next);
free(post_list_head);
}
struct post_list_node *update_post_list(struct post_list_node *post_list_head, struct word *current_word)
{
struct post_list_node *post_list_temp;
post_list_temp = post_list_head;
while (1)
{
//first word in posting_list
if (post_list_temp == NULL)
{
post_list_temp = new_plist_node(current_word->word_length);
post_list_temp->first_line_id = current_word->number_of_line;
memcpy(post_list_temp->actual_word, current_word->actual_word, current_word->word_length);
post_list_temp->line_id = current_word->number_of_line;
post_list_temp->freq++;
return post_list_temp;
}
//same word in posting list, but not first
else if (post_list_temp != NULL && post_list_temp->post_next == NULL)
{
//same word, same line
if (post_list_temp->line_id == current_word->number_of_line)
{
post_list_temp->freq++;
return post_list_head;
}
//same word, different line
else
{
post_list_temp->post_next = new_plist_node(0);
post_list_temp->post_next->line_id = current_word->number_of_line;
post_list_temp->post_next->freq++;
return post_list_head;
}
}
//point to post_next to continue searching
else
post_list_temp = post_list_temp->post_next;
}
}
struct post_list_node *search_post_list(struct post_list_node *post_list_head, struct word *cli_word)
{
while (1)
{
if (post_list_head == NULL)
return NULL;
if (post_list_head->line_id == cli_word->number_of_line)
return post_list_head;
else
post_list_head = post_list_head->post_next;
}
}