int node_compare(ast_node_t tok1, ast_node_t tok2) { if(tok1==tok2) { return 0; } if(isNil(tok1)) { return isNil(tok2)?0:-1; } else if(isNil(tok2)) { return 1; } else if(isAtom(tok1)) { return isPair(tok2) ? 1 : isAtom(tok2) ? strcmp(node_compare_tag(Value(tok1)), node_compare_tag(Value(tok2))) : 0; } else if(isPair(tok1)) { if(isPair(tok2)) { int ret = node_compare(Car(tok1), Car(tok2)); return ret?ret:node_compare(Cdr(tok1), Cdr(tok2)); } else { return 1; } } return tok1>tok2?1:-1; }
/* sanity test is tightly coupled with implementation, so when you changed * the interval tree implementation, change this code also. */ static enum interval_iter sanity_cb(struct interval_node *node, void *args) { __u64 max_high = node->in_max_high; struct interval_node *tmp, *parent; int left = 1, has = 0, nr = 0; parent = node->in_parent; node->in_parent = NULL; interval_for_each(tmp, node) { if ((left && node_compare(tmp, node) > 0) || (!left && node_compare(tmp, node) < 0)) error("interval tree sanity test\n"); if (tmp->in_max_high > max_high) { dprintf("max high sanity check, max_high is %llu," "child max_high: %llu"__S"\n", max_high, tmp->in_max_high, __F(&tmp->in_extent)); goto err; } else if (tmp->in_max_high == max_high) { has = 1; } if (tmp == node) { left = 0; continue; } } if (!has) { int count; err: count = 1; dprintf("node"__S":%llu Child list:\n", node->in_extent.start, node->in_extent.end, node->in_max_high); interval_for_each(tmp, node) { dprintf(""__S":%llu ", __F(&tmp->in_extent), tmp->in_max_high); if (count++ == 8) { dprintf("\n"); count = 1; } }
/*Busca el elemento key en el arbol y devuelve un puntero al nodo que machea ese key *si existe, sino devuelve Leaf.*/ edgeNode network_search(AbbNet net, u32 key){ /*TODO*/ networkNode pivot; bool found; pivot = net->tree; notFound = false while(pivot != Leaf) if(!node_compare(pivot, Leaf)){ if(key < node_getKey(pivot)){ pivot = pivot->left; }else if(node_getKey(pivot) < key){ pivot = pivot->right; } } return pivot->edge; }
static int binsearch(const void * buff, const void * item, uint64_t count, uint8_t field_size, uint64_t * offset) { int mid = count / 2; int item_size = field_size + sizeof(uint64_t); uint64_t * current = (uint64_t *)buff + (mid * item_size); int rv = node_compare(current, item, &field_size); if (rv == 0) { *offset = *(uint64_t *)(current + field_size); return 0; } if (count == 0) return -1; if (rv > 0) return binsearch(buff, item, mid, field_size, offset); return binsearch(current + item_size, item, count - mid, field_size, offset); }
void list_sort(list_head * list, int (*node_compare)(list_node *, list_node *)) { struct list_link *p, *q, *t; list_head tmp; int merges = 0; int k = 1; int psize, qsize; if (list_empty(list)) return; do { INIT_LIST_HEAD(&tmp); p = list->next; merges = 0; psize = qsize = 0; while (p != list) { merges++; q = p; while (q != list && psize < k) { q = q->next; psize++; } qsize = k; while (psize || (qsize && q != list)) { if (psize && (qsize == 0 || q == list || node_compare(p, q) <= 0)) { t = p; p = p->next; psize--; } else if (qsize == 0) { printf("whoaa. qsize is zero\n"); exit (1); } else { t = q; q = q->next; qsize--; } list_del(t); list_add_tail(t, &tmp); } p = q; } if (!list_empty(list)) { printf("whoaa. initial list not empty\n"); exit (1); } list_splice(&tmp, list); k *= 2; //printf("done w sort pass %d %d\n", k, merges); } while (merges > 1); }