diff options
author | Thomas White <taw@bitwiz.org.uk> | 2011-02-08 22:41:19 +0100 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2012-02-22 15:27:13 +0100 |
commit | 06ce382d160021fbc8083d0bd48b2735504a39d5 (patch) | |
tree | 842b3985e6c9311727e61b1c943ae0c7edb00644 | |
parent | de518c82717f8d07a6721e01200deb2437335a5d (diff) |
More work on reflist
-rw-r--r-- | src/reflist.c | 101 | ||||
-rw-r--r-- | tests/list_check.c | 18 |
2 files changed, 71 insertions, 48 deletions
diff --git a/src/reflist.c b/src/reflist.c index 20021ed3..9dcff1cd 100644 --- a/src/reflist.c +++ b/src/reflist.c @@ -16,15 +16,7 @@ #include "reflist.h" -struct _reflection { - - /* Listy stuff */ - unsigned int serial; /* Unique serial number, key */ - struct _reflection *child[2]; /* Child nodes */ - struct _reflection *parent; /* Parent node */ - struct _reflection *next; /* Another reflection with the same - * indices, or NULL */ - struct _reflection *prev; +struct _refldata { signed int h; signed int k; @@ -53,6 +45,20 @@ struct _reflection { }; +struct _reflection { + + /* Listy stuff */ + unsigned int serial; /* Unique serial number, key */ + struct _reflection *child[2]; /* Child nodes */ + struct _reflection *parent; /* Parent node */ + struct _reflection *next; /* Another reflection with the same + * indices, or NULL */ + struct _reflection *prev; + + struct _refldata data; +}; + + struct _reflist { struct _reflection *head; @@ -161,51 +167,51 @@ Reflection *next_found_refl(Reflection *refl) double get_excitation_error(Reflection *refl) { - return refl->excitation_error; + return refl->data.excitation_error; } void get_detector_pos(Reflection *refl, double *x, double *y) { - *x = refl->x; - *y = refl->y; + *x = refl->data.x; + *y = refl->data.y; } void get_indices(Reflection *refl, signed int *h, signed int *k, signed int *l) { - *h = refl->h; - *k = refl->k; - *l = refl->l; + *h = refl->data.h; + *k = refl->data.k; + *l = refl->data.l; } double get_partiality(Reflection *refl) { - return refl->p; + return refl->data.p; } double get_intensity(Reflection *refl) { - return refl->intensity; + return refl->data.intensity; } void get_partial(Reflection *refl, double *r1, double *r2, double *p, int *clamp_low, int *clamp_high) { - *r1 = refl->r1; - *r2 = refl->r2; + *r1 = refl->data.r1; + *r2 = refl->data.r2; *p = get_partiality(refl); - *clamp_low = refl->clamp1; - *clamp_high = refl->clamp2; + *clamp_low = refl->data.clamp1; + *clamp_high = refl->data.clamp2; } int get_scalable(Reflection *refl) { - return refl->scalable; + return refl->data.scalable; } @@ -213,32 +219,32 @@ int get_scalable(Reflection *refl) void set_detector_pos(Reflection *refl, double exerr, double x, double y) { - refl->excitation_error = exerr; - refl->x = x; - refl->y = y; + refl->data.excitation_error = exerr; + refl->data.x = x; + refl->data.y = y; } void set_partial(Reflection *refl, double r1, double r2, double p, double clamp_low, double clamp_high) { - refl->r1 = r1; - refl->r2 = r2; - refl->p = p; - refl->clamp1 = clamp_low; - refl->clamp2 = clamp_high; + refl->data.r1 = r1; + refl->data.r2 = r2; + refl->data.p = p; + refl->data.clamp1 = clamp_low; + refl->data.clamp2 = clamp_high; } void set_int(Reflection *refl, double intensity) { - refl->intensity = intensity; + refl->data.intensity = intensity; } void set_scalable(Reflection *refl, int scalable) { - refl->scalable = scalable; + refl->data.scalable = scalable; } @@ -295,7 +301,7 @@ Reflection *add_refl(RefList *list, INDICES) Reflection *new; new = new_node(SERIAL(h, k, l)); - new->h = h; new->k = k, new->l = l; + new->data.h = h; new->data.k = k, new->data.l = l; if ( list->head == NULL ) { list->head = new; @@ -356,19 +362,23 @@ void delete_refl(Reflection *refl) if ( random() > RAND_MAX/2 ) { - *parent_pos = refl->child[0]; - refl->child[0]->parent = refl->parent; + Reflection *pre; + pre = refl->child[0]; + while ( pre->child[1] != NULL ) pre = pre->child[1]; - /* Now sort out the right child */ - insert_node(refl->child[0], refl->child[1]); + refl->data = pre->data; + refl->serial = pre->serial; + delete_refl(pre); } else { - *parent_pos = refl->child[1]; - refl->child[1]->parent = refl->parent; + Reflection *pre; + pre = refl->child[1]; + while ( pre->child[0] != NULL ) pre = pre->child[0]; - /* Now sort out the left child */ - insert_node(refl->child[1], refl->child[0]); + refl->data = pre->data; + refl->serial = pre->serial; + delete_refl(pre); } @@ -377,16 +387,21 @@ void delete_refl(Reflection *refl) /* One child, left */ *parent_pos = refl->child[0]; refl->child[0]->parent = refl->parent; + free(refl); } else if (refl->child[1] != NULL ) { /* One child, right */ *parent_pos = refl->child[1]; refl->child[1]->parent = refl->parent; + free(refl); - } /* else it was just a leaf node */ + } else { - free(refl); + /* Leaf node */ + free(refl); + + } } diff --git a/tests/list_check.c b/tests/list_check.c index 22ef01e1..a50a8d70 100644 --- a/tests/list_check.c +++ b/tests/list_check.c @@ -54,11 +54,11 @@ static int test_lists(int num_items) int j; int duplicate = 0; - //if ( random() > RAND_MAX/2 ) { + if ( random() > RAND_MAX/2 ) { h = RANDOM_INDEX; k = RANDOM_INDEX; l = RANDOM_INDEX; - //} /* else use the same as last time */ + } /* else use the same as last time */ /* Count the number of times this reflection appeared before */ for ( j=0; j<i; j++ ) { @@ -86,6 +86,10 @@ static int test_lists(int num_items) check[i].dup = duplicate; check[i].found = 0; + if ( (h==-178) && (k==-45) && (l==55)) { + printf("added, now %i %i\n", check[i].dup, i); + } + } /* Iterate over the list and check we find everything */ @@ -150,13 +154,17 @@ static int test_lists(int num_items) delete_refl(refl); check[i].del = 1; + if ( (h==-119) && (k==20) && (l==-69)) { + printf("deleting, now %i\n", check[j].dup); + } /* Update all counts */ - for ( j=0; j<i; j++ ) { + for ( j=0; j<num_items; j++ ) { if ( (check[j].h == h) && (check[j].k == k) && (check[j].l == l) ) { check[j].dup--; + } } @@ -179,7 +187,7 @@ static int test_lists(int num_items) if ( refl != NULL ) { /* Whoops, found it. Was it a duplicate? */ - if ( check[i].dup == 0 ) { + if ( check[i].dup == -1 ) { fprintf(stderr, "Found %3i %i %3i after" " deletion.\n", h, k, l); return 1; @@ -188,7 +196,7 @@ static int test_lists(int num_items) int j; Reflection *c; - for ( j=0; j<check[i].dup; j++ ) { + for ( j=0; j<check[i].dup+1; j++ ) { Reflection *r2; r2 = find_refl(list, h, k, l); if ( r2 == NULL ) { |