9 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
12 typedef __uint32_t bitfield_type;
14 #define BIT_SET(set, d) \
15 ( (void) ( set[d >> 5] |= (1 << (d & 31)) ) )
16 #define BIT_ISSET(set, d) \
17 (!(! ( set[d >> 5] & (1 << (d & 31)) ) ))
19 int ANY_BITS_SET(bitfield_type *set, size_t elems)
23 for (i = 0; i < elems; i++)
31 typedef __uint32_t art_id;
36 cmpstring_p_pp(const void *p1, const void *p2)
38 return strcasecmp((char * const) p1, * (char * const *) p2);
44 int main(int argc, char **argv)
50 size_t title_blob_len;
53 art_id titles_read = 0;
67 bitfield_type *titles_seen;
68 bitfield_type *titles_prev_round;
69 bitfield_type *titles_this_round;
70 size_t titles_bitfield_elems;
71 size_t titles_bitfield_bytes;
75 printf("Supply article name to look up articles linking to it.\n");
81 * Read all incoming links into memory
84 in_file = fopen("links-incoming.bin", "rb");
85 fseek(in_file, 0, SEEK_END);
86 link_blob_len = ftell(in_file);
88 printf("Link blob size: %zd bytes.\n", link_blob_len);
90 link_blob = malloc(link_blob_len);
92 printf("Failed to allocate memory.\n");
96 if (link_blob_len != fread(link_blob, 1, link_blob_len, in_file)) {
97 printf("Failed to read entire file in one go.\n");
100 printf("Link blob read (%zd bytes).\n", ftell(in_file));
106 linki = malloc(titles * sizeof(art_id*));
107 linkis = malloc(titles * sizeof(art_id));
109 for (i = 0; i < titles; i++) {
112 linkis[i] = *link_blob;
115 linki[i] = link_blob;
116 link_blob += linkis[i];
118 printf("Incoming links prepared.\n");
125 * Read all titles into memory
130 in_file = fopen("titles-sorted.txt", "r");
131 fseek(in_file, 0, SEEK_END);
132 title_blob_len = ftell(in_file);
134 printf("Title blob size: %zd bytes.\n", title_blob_len);
136 title_blob = malloc(title_blob_len + 1);
138 printf("Failed to allocate memory.\n");
142 if (title_blob_len != fread(title_blob, 1, title_blob_len, in_file)) {
143 printf("Failed to read entire file in one go.\n");
146 printf("Title blob read (%zd bytes).\n", ftell(in_file));
149 title_blob[title_blob_len] = '\0';
151 title = malloc((titles + 1) * sizeof(title[0]));
153 title[0] = title_blob;
155 while (title_blob = strchr(title_blob, '\n')) {
156 title_blob[0] = '\0';
158 title[titles_read] = title_blob;
162 /* Last title will be an empty string, as the file ends with a newline. */
163 printf("Titles prepared: %d of %d.\n", titles_read - 1, titles);
168 /* Look up article ID */
169 cur_title = bsearch(argv[1], title,
170 titles, sizeof(title[0]),
174 printf("TITLE NOT FOUND: %s\n", argv[1]);
178 title_id = cur_title - title;
182 /* Print first degree incoming edges */
183 printf("Article %zd (%s) is linked from %zd articles:\n", title_id, title[title_id], linkis[title_id]);
184 for (i = 0; i < linkis[title_id]; i++) {
185 art_id x = linki[title_id][i];
186 printf(" %s\n", title[x]);
192 printf("\n\n\n\nBuilding table of distances...\n\n");
194 titles_bitfield_elems = ROUNDUP(titles / 8 / sizeof(bitfield_type), sizeof(bitfield_type));
195 titles_bitfield_bytes = titles_bitfield_elems * sizeof(bitfield_type);
197 titles_seen = calloc(titles_bitfield_bytes, 1);
198 titles_prev_round = NULL;
199 titles_this_round = calloc(titles_bitfield_bytes, 1);
203 BIT_SET(titles_seen, title_id);
204 BIT_SET(titles_this_round, title_id);
206 while (ANY_BITS_SET(titles_this_round, titles_bitfield_elems)) {
210 printf("Checking distance %d\n", cur_dist);
212 free(titles_prev_round); /* free is safe to call on NULL */
213 titles_prev_round = titles_this_round;
214 titles_this_round = calloc(titles_bitfield_bytes, 1);
216 for (i = 0; i < titles; i++) {
219 if (!BIT_ISSET(titles_prev_round, i))
222 for (j = 0; j < linkis[i]; j++) {
223 art_id x = linki[i][j];
224 BIT_SET(titles_this_round, x);
228 for (i = 0; i < titles_bitfield_elems; i++) {
229 titles_this_round[i] &= ~titles_seen[i];
230 titles_seen[i] |= titles_this_round[i];
234 /* Result is in titles_prev_round */
238 printf("\n\n\nThe articles with the distance of %zd are:\n\n", cur_dist);
239 for (i = 0; i < titles; i++) {
240 if (BIT_ISSET(titles_prev_round, i)) {
241 printf(" %s\n", title[i]);