bounds.c is a small program to parse ZCTA files, and write an adjacency cache to...
[limo.git] / modules / custom / domination / data / bounds.c
blob57ebaedd2ea7c5a56e5c196b5d0010782e1d8f93
2 #include <cstdio>
3 #include <ctime>
6 typedef struct Region {
7 int corner_count;
8 float *corners;
9 Region():corner_count(0),corners(NULL) { }
12 typedef struct Node {
13 int v;
14 Node *next;
15 Node():v(0),next(NULL) {}
16 Node(int v, Node *next):v(v),next(next) {}
18 typedef struct List {
19 int item;
20 short len;
21 Node *last;
22 Node *first;
23 List():item(0),len(0),first(NULL),last(NULL) { }
26 Region *regions = new Region[99999];
27 List *adj = new List[99999];
29 void list_append(List *list, int item) {
30 if ( list->first == NULL ) {
31 list->first = new Node(item, NULL);
32 list->last = list->first;
33 } else {
34 list->last->next = new Node(item,NULL);
35 list->last = list->last->next;
37 list->len += 1;
40 typedef void *iter_func(int item);
42 void list_iter(List *list, iter_func f) {
43 Node *cur = list->first;
44 for( ;cur != NULL;cur = cur->next ) {
45 (*f)(cur->v);
49 void print_item(int item) {
50 printf("item: %d", item);
53 float fabs(float x) {
54 if ( x < 0.0 ) {
55 return -1 * x;
57 return x;
60 bool share_a_corner(Region *a, Region *b) {
61 for ( int ca = 0; ca < a->corner_count; ca += 2) {
62 float ax = a->corners[ca];
63 float ay = a->corners[ca+1];
64 for ( int cb = 0; cb < b->corner_count; cb += 2) {
65 float bx = b->corners[cb];
66 float by = b->corners[cb+1];
67 if ( ax == bx && ay == by )
68 return 1;
71 return 0;
74 int main() {
75 FILE *f = fopen("dat.cache", "rb");
76 clock_t start = clock();
77 printf("CLOCKS_PER_SEC: %d\r\n", CLOCKS_PER_SEC);
78 int region_count = 0;
79 while ( ! feof(f) ) {
80 int id = 0;
81 short num_v = 0;
82 if ( fread(&id, sizeof(int), 1, f) == 0 ) break;
83 if ( fread(&num_v, sizeof(short), 1, f) == 0 ) break;
84 int num_floats = num_v*2;
85 float *row = new float[num_floats];
86 if ( fread(row, sizeof(float), num_floats, f) < num_floats ) break;
87 regions[id].corner_count = num_v;
88 regions[id].corners = row;
89 region_count++;
91 printf("Loading %d boundaries took: %d ms\r\n", region_count, (clock()- start));
92 fclose(f);
93 start = clock();
94 for ( int i = 0; i < 99999; i++ ) {
95 Region *a = &regions[i];
96 clock_t row_start = clock();
97 // printf("Starting region %d\r\n", i);
98 // fflush(NULL);
99 if ( a->corner_count > 0 ) {
100 for ( int j = 0; j < 99999; j++ ) {
101 Region *b = &regions[j];
102 if ( b->corner_count > 0 ) {
103 if ( share_a_corner(a, b) == 1 ) {
104 // printf("Connecting %d -> %d\r\n", i, j);
105 // fflush(NULL);
106 list_append(&adj[i], j);
111 int elapsed = (clock() - start);
112 float pct_complete = i/99999.0;
113 int remaining = elapsed * ((1 - pct_complete) / pct_complete);
114 printf("[%.2f%% Elapsed: %d s ETA: %d s] Region %d took %d ms \r", pct_complete*100, elapsed/1000, remaining/1000, i, (clock() - row_start));
115 fflush(NULL);
117 printf("Computing adj took: %d ms\r\n", (clock() - start));
118 FILE *out = fopen("adj.cache","wb");
119 for ( int i = 0; i < 99999; i++) {
120 List *list = &adj[i];
121 if ( list->len == 0 ) continue;
122 printf("index: %d (%d items): ", i, list->len);
123 fwrite(&i, sizeof(int), 1, out);
124 fwrite(&list->len, sizeof(short), 1, out);
125 Node *cur = list->first;
126 while( cur != NULL ) {
127 fwrite(&cur->v, sizeof(int), 1, out);
128 printf("%d ", cur->v);
129 cur = cur->next;
131 printf("\r\n");
133 fclose(out);