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)
47 size_t title_blob_len;
50 art_id titles_read = 0;
64 unsigned char *titles_seen;
65 unsigned char *titles_prev_round;
66 unsigned char *titles_this_round;
67 size_t titles_bitfield_len;
71 printf("Supply article name to look up articles linking to it.\n");
77 * Read all incoming links into memory
80 in_file = fopen("links-incoming.bin", "rb");
81 fseek(in_file, 0, SEEK_END);
82 link_blob_len = ftell(in_file);
84 printf("Link blob size: %zd bytes.\n", link_blob_len);
86 link_blob = malloc(link_blob_len);
88 printf("Failed to allocate memory.\n");
92 if (link_blob_len != fread(link_blob, 1, link_blob_len, in_file)) {
93 printf("Failed to read entire file in one go.\n");
96 printf("Link blob read (%zd bytes).\n", ftell(in_file));
102 linki = malloc(titles * sizeof(art_id*));
103 linkis = malloc(titles * sizeof(art_id));
105 for (i = 0; i < titles; i++) {
108 linkis[i] = *link_blob;
111 linki[i] = link_blob;
112 link_blob += linkis[i];
114 printf("Incoming links prepared.\n");
121 * Read all titles into memory
126 in_file = fopen("titles-sorted.txt", "r");
127 fseek(in_file, 0, SEEK_END);
128 title_blob_len = ftell(in_file);
130 printf("Title blob size: %zd bytes.\n", title_blob_len);
132 title_blob = malloc(title_blob_len + 1);
134 printf("Failed to allocate memory.\n");
138 if (title_blob_len != fread(title_blob, 1, title_blob_len, in_file)) {
139 printf("Failed to read entire file in one go.\n");
142 printf("Title blob read (%zd bytes).\n", ftell(in_file));
145 title_blob[title_blob_len] = '\0';
147 title = malloc((titles + 1) * sizeof(title[0]));
149 title[0] = title_blob;
151 while (title_blob = strchr(title_blob, '\n')) {
152 title_blob[0] = '\0';
154 title[titles_read] = title_blob;
158 /* Last title will be an empty string, as the file ends with a newline. */
159 printf("Titles prepared: %d of %d.\n", titles_read - 1, titles);
164 /* Look up article ID */
165 cur_title = bsearch(argv[1], title,
166 titles, sizeof(title[0]),
170 printf("TITLE NOT FOUND: %s\n", argv[1]);
174 title_id = cur_title - title;
178 /* Print first degree incoming edges */
179 printf("Article %zd (%s) is linked from %zd articles:\n", title_id, title[title_id], linkis[title_id]);
180 for (i = 0; i < linkis[title_id]; i++) {
181 art_id x = linki[title_id][i];
182 printf(" %s\n", title[x]);
188 printf("\n\n\n\nBuilding table of distances...\n\n");
190 titles_bitfield_len = ROUNDUP(titles / 8, sizeof(size_t));
192 titles_seen = calloc(titles_bitfield_len, 1);
193 titles_prev_round = NULL;
194 titles_this_round = calloc(titles_bitfield_len, 1);
198 BIT_SET(titles_seen, title_id);
199 BIT_SET(titles_this_round, title_id);
201 while (ANY_BITS_SET(titles_this_round, titles_bitfield_len)) {
205 printf("Checking distance %d\n", cur_dist);
207 free(titles_prev_round); /* free is safe to call on NULL */
208 titles_prev_round = titles_this_round;
209 titles_this_round = calloc(titles_bitfield_len, 1);
211 for (i = 0; i < titles; i++) {
214 if (!BIT_ISSET(titles_prev_round, i))
217 for (j = 0; j < linkis[i]; j++) {
218 art_id x = linki[i][j];
219 BIT_SET(titles_this_round, x);
223 for (i = 0; i < titles_bitfield_len; i++) {
224 titles_this_round[i] &= ~titles_seen[i];
225 titles_seen[i] |= titles_this_round[i];
229 /* Result is in titles_prev_round */
233 printf("\n\n\nThe articles with the distance of %zd are:\n\n", cur_dist);
234 for (i = 0; i < titles; i++) {
235 if (BIT_ISSET(titles_prev_round, i)) {
236 printf(" %s\n", title[i]);