int heap_insert(binary_heap *a, node key) { register int i; /* if the heap already has the max number of elements we do not * allow more elements to be added */ if (a->heap_size >= a->max_elems) { print_error("Heap capacity exceeded, new element not added."); return -1; } /* increase the heap size to accomidate the new node, and set the * inital position of this node to be the last node in the heap */ i = ++(a->heap_size); /* traverse the parth from the leaf to the root to find the a proper * place for the new element */ while (i > 1 && compare_priority(key,a->elements[PARENT(i)])) { a->elements[i] = a->elements[PARENT(i)]; i = PARENT(i); } /* insert the element at the position that was determined */ a->elements[i] = key; // heapify??? heapify(a, i); return i; }
void heap_insert(binary_heap *a, heap_node key) { int i; if(a->heap_size + 1 >= a->heap_cap) { a->heap_cap += 100; a->elements = (heap_node *)realloc(a->elements, a->heap_cap * sizeof(heap_node)); } /* increase the heap size to accomidate the new node, and set the * inital position of this node to be the last node in the heap */ i = ++(a->heap_size); /* traverse the parth from the leaf to the root to find the a proper * place for the new element */ while(i > 1 && compare_priority(&key, &a->elements[PARENT(i)]) > 0) { a->elements[i] = a->elements[PARENT(i)]; i = PARENT(i); } /* insert the element at the position that was determined */ a->elements[i] = key; }
void heapify(binary_heap *a, int i) { register int l, r, largest = i; l = LEFT(i); r = RIGHT(i); /* check the left child */ if((l <= a->heap_size && (compare_priority(&a->elements[l], &a->elements[i]) > 0))) largest = l; /* check the right child */ if(r <= a->heap_size && (compare_priority(&a->elements[r], &a->elements[largest]) > 0)) largest = r; if(largest != i) { /* swap nodes largest and i, then heapify */ swap_node(a->elements, i, largest); heapify(a, largest); } }
int heap_propagate_up(binary_heap *a, int node_indx) { int i = node_indx, k = PARENT(i); while(i > 1 && compare_priority(&a->elements[i], &a->elements[k]) >= 0) { swap_node(a->elements, i, k); //a->elements[i] = a->elements[PARENT(i)]; i = PARENT(i); k = PARENT(i); } return i; }
int heap_node_find(binary_heap *a, heap_node node, int start_indx) { int res; if(start_indx > a->heap_size) return -1; if(a->elements[start_indx].key != node.key) if(compare_priority(&a->elements[start_indx], &node) < 0) return -1; if(a->elements[start_indx].key == node.key) return start_indx; if(a->elements[start_indx].key != node.key) if(compare_priority(&a->elements[start_indx], &node) >= 0) res = heap_node_find(a, node, 2*start_indx); if(res == -1) res = heap_node_find(a, node, 2*start_indx + 1); return res; }
void heapify(binary_heap *a,int i) { register int l,r,largest; l = LEFT(i); r = RIGHT(i); /* check the left child */ largest = ((l <= a->heap_size && compare_priority(a->elements[l],a->elements[i])) ? l : i); /* check the right child */ if (r <= a->heap_size && compare_priority(a->elements[r],a->elements[largest])) largest = r; if (largest != i) { /* swap nodes largest and i, then heapify */ SWAP(node,a->elements[i],a->elements[largest]); heapify(a,largest); } }
static gint compare(gint sort_method, gboolean reverse, PurpleBlistNode *node1, PurpleBlistNode *node2) { gint ret = 0; switch(sort_method) { case SORT_METHOD_NOTHING: ret = compare_nothing(node1, node2); break; case SORT_METHOD_NAME: ret = compare_name(node1, node2); break; case SORT_METHOD_LAST_NAME: ret = compare_last_name(node1, node2); break; case SORT_METHOD_STATUS: ret = compare_status(node1, node2); break; case SORT_METHOD_ONOFFLINE: ret = compare_onoffline(node1, node2); break; case SORT_METHOD_PROTOCOL: ret = compare_protocol(node1, node2); break; case SORT_METHOD_PRIORITY: ret = compare_priority(node1, node2); break; case SORT_METHOD_ONOFFLINETIME: ret = compare_onofflinetime(node1, node2); break; case SORT_METHOD_LOGSIZE: ret = compare_logsize(node1, node2); break; case SORT_METHOD_ACCOUNT: ret = compare_account(node1, node2); break; } if(reverse) { ret *= (-1); } return ret; }
void keyboard_manager::setup_hash () { unsigned int i, index, hashkey; vector <keysym_t *> sorted_keymap; uint16_t hash_bucket_size[KEYSYM_HASH_BUCKETS]; // size of each bucket memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); // determine hash bucket size for (i = 0; i < keymap.size (); ++i) for (int j = min (keymap [i]->range, KEYSYM_HASH_BUCKETS) - 1; j >= 0; --j) { hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; ++hash_bucket_size [hashkey]; } // now we know the size of each bucket // compute the index of each bucket hash [0] = 0; for (index = 0, i = 1; i < KEYSYM_HASH_BUCKETS; ++i) { index += hash_bucket_size [i - 1]; hash [i] = index; } // and allocate just enough space sorted_keymap.insert (sorted_keymap.begin (), index + hash_bucket_size [i - 1], 0); memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); // fill in sorted_keymap // it is sorted in each bucket for (i = 0; i < keymap.size (); ++i) for (int j = min (keymap [i]->range, KEYSYM_HASH_BUCKETS) - 1; j >= 0; --j) { hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; index = hash [hashkey] + hash_bucket_size [hashkey]; while (index > hash [hashkey] && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) { sorted_keymap [index] = sorted_keymap [index - 1]; --index; } sorted_keymap [index] = keymap [i]; ++hash_bucket_size [hashkey]; } keymap.swap (sorted_keymap); #ifndef NDEBUG // check for invariants for (i = 0; i < KEYSYM_HASH_BUCKETS; ++i) { index = hash[i]; for (int j = 0; j < hash_bucket_size [i]; ++j) { if (keymap [index + j]->range == 1) assert (i == (keymap [index + j]->keysym & KEYSYM_HASH_MASK)); if (j) assert (compare_priority (keymap [index + j - 1], keymap [index + j]) >= 0); } } // this should be able to detect most possible bugs for (i = 0; i < sorted_keymap.size (); ++i) { keysym_t *a = sorted_keymap[i]; for (int j = 0; j < a->range; ++j) { int index = find_keysym (a->keysym + j, a->state); assert (index >= 0); keysym_t *b = keymap [index]; assert (i == index // the normally expected result || IN_RANGE_INC (a->keysym + j, b->keysym, b->keysym + b->range) && compare_priority (a, b) <= 0); // is effectively the same or a closer match } } #endif }