-
Notifications
You must be signed in to change notification settings - Fork 1
/
posting.c
224 lines (198 loc) · 5.97 KB
/
posting.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; -*-
*
* Copyright (c) 2008 Jeremy English <jhe@jeremyenglish.org>
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. No representations are made about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*
* Created: 02-November-2008
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include "posting.h"
#include "eprintf.h"
/* add_front: Cons and element onto the first of a list */
Posting *add_front(Posting *listp, Posting *newp){
newp->next = listp;
return newp;
}
/* apply: execute fn for each element of listp */
void apply(Posting *listp,
void (*fn)(Posting*, void*), void *arg) {
for (; listp != NULL; listp = listp->next)
(*fn)(listp, arg); /* call the function */
}
/* print_posting: print the frequency and the docid */
static void print_posting(Posting *p, void *arg){
char *fmt;
fmt = (char *) arg;
printf(fmt, p->data.frequency, p->data.docid);
}
static void write_posting(Posting *p, void *arg){
FILE *fp;
int outsz;
fp = (FILE *) arg;
outsz = fwrite(&(p->data), sizeof(p->data), 1, fp);
if (outsz != 1){
fprintf(stderr,"%s: An error occured while writing to the posting file.\noutsz = %d error = %d\n", progname(), outsz, ferror(fp));
exit(1);
}
}
/* print_posting_list: Applies the function print_posting to each
element of the posting list */
void print_posting_list(Posting *p){
apply(p,print_posting, "Frequency: %d; Document Id: %d\n");
}
/* write_posting_list: Write all of the posting nodes out to a binary
file */
void write_posting_list(Posting *p, FILE *fp){
apply(p,write_posting,fp);
}
/* freeall: Free each element of the list */
void freeall(Posting *p){
Posting *next;
for(; p != NULL; p = next){
next = p->next;
free(p);
}
}
/* get_posting_list: Read a number of posting from a certain offset
from the beginning of a file. */
Posting *get_posting_list(FILE *fp, int num_postings, int offset){
int sz = sizeof(PostingData);
PostingData pd;
Posting *list;
int n,i;
list = NULL;
fseek(fp, offset, SEEK_SET);
for (i = 0; i < num_postings; i++){
if ((n = fread(&pd,sz,1,fp)) != 1){
int errcode = ferror(fp);
fprintf(stderr,"%s Could not read record at offset %d. Error code %d\n",
progname(), offset, errcode);
exit(1);
}
else {
Posting *p;
p = emalloc(sizeof(*p));
p->data.frequency = pd.frequency;
p->data.docid = pd.docid;
list = add_front(list,p);
}
}
return list;
}
void append_unique_posting_list(FILE *fp, Posting **listp, int num_postings, int offset, int use_doc_freq){
int sz = sizeof(PostingData);
PostingData pd;
Posting *list;
int n, i;
fseek(fp, offset, SEEK_SET);
for (i = 0; i < num_postings; i++){
if ((n = fread(&pd,sz,1,fp)) != 1){
int errcode = ferror(fp);
fprintf(stderr,"%s Could not read record at offset %d. Error code %d\n",
progname(), offset, errcode);
exit(1);
}
else {
Posting *p;
/* Don't add duplicates. But if we find one then update
it's frequency. */
list = *listp;
for (; list != NULL; list = list->next){
if (list->data.docid == pd.docid){
list->data.frequency += use_doc_freq ? pd.frequency : 1;
break;
}
}
if (list == NULL){
p = emalloc(sizeof(*p));
p->data.frequency = use_doc_freq ? pd.frequency : 1;
p->data.docid = pd.docid;
*listp = add_front(*listp,p);
}
}
}
}
Posting *find_posting(Posting *listp, uint32_t docid){
for (; listp != NULL; listp = listp->next){
if (listp->data.docid == docid){
return listp;
}
}
return NULL;
}
/* posting_length: Returns the lengt of the posting list. */
uint32_t posting_length(Posting *listp){
uint32_t i=0;
for (; listp != NULL; listp = listp->next)
i++;
return i;
}
static Posting *merge(Posting *a, Posting *b){
Posting *c;
Posting *head;
int firstpass = 1;
do{
if (a == NULL && b != NULL){
if (firstpass)
head = b;
else
c->next = b;
c = b;
b = b->next;
}
else if (a != NULL && b == NULL){
if (firstpass)
head = a;
else
c->next = a;
c = a;
a = a->next;
}
else if (a->data.frequency >= b->data.frequency){
if (firstpass)
head = a;
else
c->next = a;
c = a;
a = a->next;
}
else{
if (firstpass)
head = b;
else
c->next = b;
c = b;
b = b->next;
}
firstpass = 0;
} while(! (a == NULL && b == NULL));
return head;
}
Posting *sort_posting_list(Posting *listp, uint32_t len){
if (len <= 1){
return listp;
}
else {
int p1 = floor(len/2);
int p2 = len - p1;
Posting *a;
Posting *b;
int i;
a = listp;
for (i = 2; i <= p1; i++)
listp = listp->next;
b = listp->next;
listp->next = NULL;
return merge(sort_posting_list(a, p1), sort_posting_list(b, p2));
}
}