From d96b63b6b05da979c297a151f3cf33c0f94e8bb3 Mon Sep 17 00:00:00 2001 From: Jesse Dailey Date: Fri, 30 Jan 2009 12:48:44 -0500 Subject: [PATCH] bounds.c is a small program to parse ZCTA files, and write an adjacency cache to disk. --- modules/custom/domination/data/bounds.c | 135 ++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 modules/custom/domination/data/bounds.c diff --git a/modules/custom/domination/data/bounds.c b/modules/custom/domination/data/bounds.c new file mode 100644 index 0000000..57ebaed --- /dev/null +++ b/modules/custom/domination/data/bounds.c @@ -0,0 +1,135 @@ + +#include +#include + + +typedef struct Region { + int corner_count; + float *corners; + Region():corner_count(0),corners(NULL) { } +}; + +typedef struct Node { + int v; + Node *next; + Node():v(0),next(NULL) {} + Node(int v, Node *next):v(v),next(next) {} +}; +typedef struct List { + int item; + short len; + Node *last; + Node *first; + List():item(0),len(0),first(NULL),last(NULL) { } +}; + +Region *regions = new Region[99999]; +List *adj = new List[99999]; + +void list_append(List *list, int item) { + if ( list->first == NULL ) { + list->first = new Node(item, NULL); + list->last = list->first; + } else { + list->last->next = new Node(item,NULL); + list->last = list->last->next; + } + list->len += 1; +} + +typedef void *iter_func(int item); + +void list_iter(List *list, iter_func f) { + Node *cur = list->first; + for( ;cur != NULL;cur = cur->next ) { + (*f)(cur->v); + } +} + +void print_item(int item) { + printf("item: %d", item); +} + +float fabs(float x) { + if ( x < 0.0 ) { + return -1 * x; + } + return x; +} + +bool share_a_corner(Region *a, Region *b) { + for ( int ca = 0; ca < a->corner_count; ca += 2) { + float ax = a->corners[ca]; + float ay = a->corners[ca+1]; + for ( int cb = 0; cb < b->corner_count; cb += 2) { + float bx = b->corners[cb]; + float by = b->corners[cb+1]; + if ( ax == bx && ay == by ) + return 1; + } + } + return 0; +} + +int main() { + FILE *f = fopen("dat.cache", "rb"); + clock_t start = clock(); + printf("CLOCKS_PER_SEC: %d\r\n", CLOCKS_PER_SEC); + int region_count = 0; + while ( ! feof(f) ) { + int id = 0; + short num_v = 0; + if ( fread(&id, sizeof(int), 1, f) == 0 ) break; + if ( fread(&num_v, sizeof(short), 1, f) == 0 ) break; + int num_floats = num_v*2; + float *row = new float[num_floats]; + if ( fread(row, sizeof(float), num_floats, f) < num_floats ) break; + regions[id].corner_count = num_v; + regions[id].corners = row; + region_count++; + } + printf("Loading %d boundaries took: %d ms\r\n", region_count, (clock()- start)); + fclose(f); + start = clock(); + for ( int i = 0; i < 99999; i++ ) { + Region *a = ®ions[i]; + clock_t row_start = clock(); + // printf("Starting region %d\r\n", i); + // fflush(NULL); + if ( a->corner_count > 0 ) { + for ( int j = 0; j < 99999; j++ ) { + Region *b = ®ions[j]; + if ( b->corner_count > 0 ) { + if ( share_a_corner(a, b) == 1 ) { + // printf("Connecting %d -> %d\r\n", i, j); + // fflush(NULL); + list_append(&adj[i], j); + } + } + } + } + int elapsed = (clock() - start); + float pct_complete = i/99999.0; + int remaining = elapsed * ((1 - pct_complete) / pct_complete); + 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)); + fflush(NULL); + } + printf("Computing adj took: %d ms\r\n", (clock() - start)); + FILE *out = fopen("adj.cache","wb"); + for ( int i = 0; i < 99999; i++) { + List *list = &adj[i]; + if ( list->len == 0 ) continue; + printf("index: %d (%d items): ", i, list->len); + fwrite(&i, sizeof(int), 1, out); + fwrite(&list->len, sizeof(short), 1, out); + Node *cur = list->first; + while( cur != NULL ) { + fwrite(&cur->v, sizeof(int), 1, out); + printf("%d ", cur->v); + cur = cur->next; + } + printf("\r\n"); + } + fclose(out); +} + -- 2.11.4.GIT