9 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
11 #define BIT_SET(set, d) \
12 ( (void) ( set[d >> 3] |= (1 << (d & 7)) ) )
13 #define BIT_ISSET(set, d) \
14 (!(! ( set[d >> 3] & (1 << (d & 7)) ) ))
16 int ANY_BITS_SET(unsigned char *set, size_t bytes)
20 for (i = 0; i < bytes; i++)
28 typedef __uint32_t art_id;
33 cmpstring_p_pp(const void *p1, const void *p2)
35 return strcasecmp((char * const) p1, * (char * const *) p2);
41 int main(int argc, char **argv)
48 art_id titles_read = 0;
62 unsigned char *titles_seen;
63 unsigned char *titles_prev_round;
64 unsigned char *titles_this_round;
65 size_t titles_bitfield_len;
69 printf("Supply article name to look up articles linking to it.\n");
75 * Read all incoming links into memory
78 in_file = fopen("links-incoming.bin", "rb");
79 fseek(in_file, 0, SEEK_END);
80 link_blob_len = ftell(in_file);
82 printf("Link blob size: %zd bytes.\n", link_blob_len);
84 link_blob = malloc(link_blob_len);
86 printf("Failed to allocate memory.\n");
90 if (link_blob_len != fread(link_blob, 1, link_blob_len, in_file)) {
91 printf("Failed to read entire file in one go.\n");
94 printf("Link blob read (%zd bytes).\n", ftell(in_file));
100 linki = malloc(titles * sizeof(art_id*));
101 linkis = malloc(titles * sizeof(art_id));
103 for (i = 0; i < titles; i++) {
106 linkis[i] = *link_blob;
109 linki[i] = link_blob;
110 link_blob += linkis[i];
112 printf("Incoming links prepared.\n");
119 * Read all titles into memory
122 title = malloc(titles * sizeof(title[0]));
124 in_file = fopen("titles-sorted.txt", "r");
125 while (!feof(in_file)) {
126 char *in_line = NULL;
127 ssize_t in_line_len = 0;
130 in_line_len = getline(&in_line, &zero, in_file);
132 /* Ignore empty lines and errors */
133 if (in_line_len < 2) {
138 /* Delete trailing newline */
139 in_line[in_line_len - 1] = '\0';
141 title[titles_read] = in_line;
146 printf("Titles read.\n");
151 /* Look up article ID */
152 cur_title = bsearch(argv[1], title,
153 titles, sizeof(title[0]),
157 printf("TITLE NOT FOUND: %s\n", argv[1]);
161 title_id = cur_title - title;
165 /* Print first degree incoming edges */
166 printf("Article %zd (%s) is linked from %zd articles:\n", title_id, title[title_id], linkis[title_id]);
167 for (i = 0; i < linkis[title_id]; i++) {
168 art_id x = linki[title_id][i];
169 printf(" %s\n", title[x]);
175 printf("\n\n\n\nBuilding table of distances...\n\n");
177 titles_bitfield_len = ROUNDUP(titles / 8, sizeof(size_t));
179 titles_seen = calloc(titles_bitfield_len, 1);
180 titles_prev_round = NULL;
181 titles_this_round = calloc(titles_bitfield_len, 1);
185 BIT_SET(titles_seen, title_id);
186 BIT_SET(titles_this_round, title_id);
188 while (ANY_BITS_SET(titles_this_round, titles_bitfield_len)) {
192 printf("Checking distance %d\n", cur_dist);
194 free(titles_prev_round); /* free is safe to call on NULL */
195 titles_prev_round = titles_this_round;
196 titles_this_round = calloc(titles_bitfield_len, 1);
198 for (i = 0; i < titles; i++) {
201 if (!BIT_ISSET(titles_prev_round, i))
204 for (j = 0; j < linkis[i]; j++) {
205 art_id x = linki[i][j];
206 BIT_SET(titles_this_round, x);
210 for (i = 0; i < titles_bitfield_len; i++) {
211 titles_this_round[i] &= ~titles_seen[i];
212 titles_seen[i] |= titles_this_round[i];
216 /* Result is in titles_prev_round */
220 printf("\n\n\nThe articles with the distance of %zd are:\n\n", cur_dist);
221 for (i = 0; i < titles; i++) {
222 if (BIT_ISSET(titles_prev_round, i)) {
223 printf(" %s\n", title[i]);