Enlightenment CVS committal Author : devilhorns Project : e17 Module : libs/ecore
Dir : e17/libs/ecore/src/lib/ecore Modified Files: ecore_sheap.c Log Message: Format to 'E' style. Note: No functional changes, only formatting. =================================================================== RCS file: /cvs/e/e17/libs/ecore/src/lib/ecore/ecore_sheap.c,v retrieving revision 1.10 retrieving revision 1.11 diff -u -3 -r1.10 -r1.11 --- ecore_sheap.c 6 Jan 2006 17:58:12 -0000 1.10 +++ ecore_sheap.c 23 Jun 2006 06:41:44 -0000 1.11 @@ -18,21 +18,23 @@ * @return A pointer to the newly allocated binary heap on success, NULL on * failure. */ -EAPI Ecore_Sheap *ecore_sheap_new(Ecore_Compare_Cb compare, int size) +EAPI Ecore_Sheap * +ecore_sheap_new(Ecore_Compare_Cb compare, int size) { - Ecore_Sheap *heap = NULL; + Ecore_Sheap *heap = NULL; - heap = (Ecore_Sheap *)malloc(sizeof(Ecore_Sheap)); - if (!heap) - return NULL; - memset(heap, 0, sizeof(Ecore_Sheap)); + heap = (Ecore_Sheap *)malloc(sizeof(Ecore_Sheap)); + if (!heap) + return NULL; + memset(heap, 0, sizeof(Ecore_Sheap)); - if (!ecore_sheap_init(heap, compare, size)) { - FREE(heap); - return NULL; - } + if (!ecore_sheap_init(heap, compare, size)) + { + FREE(heap); + return NULL; + } - return heap; + return heap; } /** @@ -42,23 +44,24 @@ * @param size The number of elements to allow in the heap * @return TRUE on success, FALSE on failure */ -EAPI int ecore_sheap_init(Ecore_Sheap *heap, Ecore_Compare_Cb compare, int size) +EAPI int +ecore_sheap_init(Ecore_Sheap *heap, Ecore_Compare_Cb compare, int size) { - CHECK_PARAM_POINTER_RETURN("heap", heap, FALSE); - - heap->space = size; - if (!compare) - heap->compare = ecore_direct_compare; - else - heap->compare = compare; - heap->order = ECORE_SHEAP_MIN; + CHECK_PARAM_POINTER_RETURN("heap", heap, FALSE); - heap->data = (void **)malloc(heap->space * sizeof(void *)); - if (!heap->data) - return FALSE; - memset(heap->data, 0, heap->space * sizeof(void *)); + heap->space = size; + if (!compare) + heap->compare = ecore_direct_compare; + else + heap->compare = compare; + heap->order = ECORE_SHEAP_MIN; + + heap->data = (void **)malloc(heap->space * sizeof(void *)); + if (!heap->data) + return FALSE; + memset(heap->data, 0, heap->space * sizeof(void *)); - return TRUE; + return TRUE; } /** @@ -69,22 +72,23 @@ * * @param heap The heap to be freed */ -EAPI void ecore_sheap_destroy(Ecore_Sheap *heap) +EAPI void +ecore_sheap_destroy(Ecore_Sheap *heap) { - int i; + int i; - CHECK_PARAM_POINTER("heap", heap); + CHECK_PARAM_POINTER("heap", heap); - /* - * Free data in heap - */ - if (heap->free_func) - for (i = 0; i < heap->size; i++) - heap->free_func(heap->data[i]); + /* + * Free data in heap + */ + if (heap->free_func) + for (i = 0; i < heap->size; i++) + heap->free_func(heap->data[i]); - FREE(heap->data); + FREE(heap->data); - FREE(heap); + FREE(heap); } /** @@ -94,13 +98,14 @@ * @param free_func The function that will free the key data. * @return @c TRUE on successful set, @c FALSE otherwise. */ -EAPI int ecore_sheap_set_free_cb(Ecore_Sheap *heap, Ecore_Free_Cb free_func) +EAPI int +ecore_sheap_set_free_cb(Ecore_Sheap *heap, Ecore_Free_Cb free_func) { - CHECK_PARAM_POINTER_RETURN("heap", heap, FALSE); + CHECK_PARAM_POINTER_RETURN("heap", heap, FALSE); - heap->free_func = free_func; + heap->free_func = free_func; - return TRUE; + return TRUE; } /** @@ -110,85 +115,90 @@ * @return TRUE on success, NULL on failure. Increases the size of the heap if * it becomes larger than available space. */ -EAPI int ecore_sheap_insert(Ecore_Sheap *heap, void *data) +EAPI int +ecore_sheap_insert(Ecore_Sheap *heap, void *data) { - int i; - void *temp; - int parent; - int position; - - CHECK_PARAM_POINTER_RETURN("heap", heap, FALSE); + int i; + void *temp; + int parent; + int position; + + CHECK_PARAM_POINTER_RETURN("heap", heap, FALSE); + + /* + * Increase the size of the allocated data area if there isn't enough + * space available to add this data + */ + if (heap->size >= heap->space) + return FALSE; + + heap->sorted = FALSE; + + /* + * Place the data at the end of the heap initially. Then determine the + * parent and position in the array of it's parent. + */ + heap->data[heap->size] = data; + position = heap->size; + heap->size++; + i = heap->size; + parent = PARENT(i) - 1; + + /* + * Check the order of the heap to decide where to place the inserted + * data. The loop is placed inside the if statement to reduce the + * number of branching decisions that must be predicted. + */ + if (heap->order == ECORE_SHEAP_MIN) + { + while ((position > 0) && heap->compare(heap->data[parent], + heap->data[position]) > 0) + { + + /* + * Swap the data with it's parents to move it up in + * the heap. + */ + temp = heap->data[position]; + heap->data[position] = heap->data[parent]; + heap->data[parent] = temp; + + /* + * Now determine the new position for the next + * iteration of the loop, as well as it's parents + * position. + */ + i = PARENT(i); + position = i - 1; + parent = PARENT(i) - 1; + } + } + else + { + while ((position > 0) && heap->compare(heap->data[parent], + heap->data[position]) < 0) + { + + /* + * Swap the data with it's parents to move it up in + * the heap. + */ + temp = heap->data[position]; + heap->data[position] = heap->data[PARENT(i) - 1]; + heap->data[PARENT(i) - 1] = temp; + + /* + * Now determine the new position for the next + * iteration of the loop, as well as it's parents + * position. + */ + i = PARENT(i); + position = i - 1; + parent = PARENT(i) - 1; + } + } - /* - * Increase the size of the allocated data area if there isn't enough - * space available to add this data - */ - if (heap->size >= heap->space) - return FALSE; - - heap->sorted = FALSE; - - /* - * Place the data at the end of the heap initially. Then determine the - * parent and position in the array of it's parent. - */ - heap->data[heap->size] = data; - position = heap->size; - heap->size++; - i = heap->size; - parent = PARENT(i) - 1; - - /* - * Check the order of the heap to decide where to place the inserted - * data. The loop is placed inside the if statement to reduce the - * number of branching decisions that must be predicted. - */ - if (heap->order == ECORE_SHEAP_MIN) { - while ((position > 0) && heap->compare(heap->data[parent], - heap->data[position]) > 0) { - - /* - * Swap the data with it's parents to move it up in - * the heap. - */ - temp = heap->data[position]; - heap->data[position] = heap->data[parent]; - heap->data[parent] = temp; - - /* - * Now determine the new position for the next - * iteration of the loop, as well as it's parents - * position. - */ - i = PARENT(i); - position = i - 1; - parent = PARENT(i) - 1; - } - } - else { - while ((position > 0) && heap->compare(heap->data[parent], - heap->data[position]) < 0) { - - /* - * Swap the data with it's parents to move it up in - * the heap. - */ - temp = heap->data[position]; - heap->data[position] = heap->data[PARENT(i) - 1]; - heap->data[PARENT(i) - 1] = temp; - - /* - * Now determine the new position for the next - * iteration of the loop, as well as it's parents - * position. - */ - i = PARENT(i); - position = i - 1; - parent = PARENT(i) - 1; - } - } - - return TRUE; + return TRUE; } /** @@ -198,22 +208,23 @@ * @note The extract function maintains the heap properties after the * extract. */ -EAPI void *ecore_sheap_extract(Ecore_Sheap *heap) +EAPI void * +ecore_sheap_extract(Ecore_Sheap *heap) { - void *extreme; + void *extreme; - if (heap->size < 1) - return NULL; + if (heap->size < 1) + return NULL; - heap->sorted = FALSE; + heap->sorted = FALSE; - extreme = heap->data[0]; - heap->size--; - heap->data[0] = heap->data[heap->size]; + extreme = heap->data[0]; + heap->size--; + heap->data[0] = heap->data[heap->size]; - _ecore_sheap_heapify(heap, 1); + _ecore_sheap_heapify(heap, 1); - return extreme; + return extreme; } /** @@ -222,12 +233,13 @@ * @return The top item of the heap on success, NULL on failure. * @note The function does not alter the heap. */ -EAPI void *ecore_sheap_extreme(Ecore_Sheap *heap) +EAPI void * +ecore_sheap_extreme(Ecore_Sheap *heap) { - if (heap->size < 1) - return NULL; + if (heap->size < 1) + return NULL; - return heap->data[0]; + return heap->data[0]; } /** @@ -239,25 +251,26 @@ * @note The heap does not free the old data since it must be passed * in, so the caller can perform the free if desired. */ -EAPI int ecore_sheap_change(Ecore_Sheap *heap, void *item, void *newval) +EAPI int +ecore_sheap_change(Ecore_Sheap *heap, void *item, void *newval) { - int i; + int i; - CHECK_PARAM_POINTER_RETURN("heap", heap, FALSE); + CHECK_PARAM_POINTER_RETURN("heap", heap, FALSE); - for (i = 0; i < heap->size && heap->compare(heap->data[i], item); i++); + for (i = 0; i < heap->size && heap->compare(heap->data[i], item); i++); - if (i < heap->size) - heap->data[i] = newval; - else - return FALSE; + if (i < heap->size) + heap->data[i] = newval; + else + return FALSE; - /* - * FIXME: This is not the correct procedure when a change occurs. - */ - _ecore_sheap_heapify(heap, 1); + /* + * FIXME: This is not the correct procedure when a change occurs. + */ + _ecore_sheap_heapify(heap, 1); - return TRUE; + return TRUE; } /** @@ -265,39 +278,41 @@ * @param heap The heap to change comparison function * @param compare The new function for comparing nodes * @return TRUE on success, FALSE on failure. - * + * * The comparison function is changed to @compare and the heap is heapified * by the new comparison. */ -EAPI int ecore_sheap_set_compare(Ecore_Sheap *heap, Ecore_Compare_Cb compare) +EAPI int +ecore_sheap_set_compare(Ecore_Sheap *heap, Ecore_Compare_Cb compare) { - CHECK_PARAM_POINTER_RETURN("heap", heap, FALSE); + CHECK_PARAM_POINTER_RETURN("heap", heap, FALSE); - if (!compare) - heap->compare = ecore_direct_compare; - else - heap->compare = compare; + if (!compare) + heap->compare = ecore_direct_compare; + else + heap->compare = compare; - _ecore_sheap_update_data(heap); + _ecore_sheap_update_data(heap); - return TRUE; + return TRUE; } /** * Change the order of the heap * @param heap The heap to change the order * @param order The new order of the heap - * + * * Changes the heap order of @heap and re-heapifies the data to this new * order. The default order is a min heap. */ -EAPI void ecore_sheap_set_order(Ecore_Sheap *heap, char order) +EAPI void +ecore_sheap_set_order(Ecore_Sheap *heap, char order) { - CHECK_PARAM_POINTER("heap", heap); + CHECK_PARAM_POINTER("heap", heap); - heap->order = order; + heap->order = order; - _ecore_sheap_update_data(heap); + _ecore_sheap_update_data(heap); } /** @@ -307,29 +322,30 @@ * Sorts the data in the heap into the order that is used for the heap's * data. */ -EAPI void ecore_sheap_sort(Ecore_Sheap *heap) +EAPI void +ecore_sheap_sort(Ecore_Sheap *heap) { - int i = 0; - void **new_data; - - CHECK_PARAM_POINTER("heap", heap); + int i = 0; + void **new_data; - new_data = (void **)malloc(heap->size * sizeof(void *)); + CHECK_PARAM_POINTER("heap", heap); - /* - * Extract the heap and insert into the new data array in order. - */ - while (heap->size > 0) - new_data[i++] = ecore_sheap_extract(heap); - - /* - * Free the old data array and update the heap with the new data, also - * mark as sorted. - */ - FREE(heap->data); - heap->data = new_data; - heap->size = i; - heap->sorted = TRUE; + new_data = (void **)malloc(heap->size * sizeof(void *)); + + /* + * Extract the heap and insert into the new data array in order. + */ + while (heap->size > 0) + new_data[i++] = ecore_sheap_extract(heap); + + /* + * Free the old data array and update the heap with the new data, also + * mark as sorted. + */ + FREE(heap->data); + heap->data = new_data; + heap->size = i; + heap->sorted = TRUE; } /* @@ -340,18 +356,19 @@ * NULL on failure. * @note The data is guaranteed to be in sorted order. */ -EAPI inline void *ecore_sheap_item(Ecore_Sheap *heap, int i) +EAPI inline void * +ecore_sheap_item(Ecore_Sheap *heap, int i) { - if (i >= heap->size) - return NULL; + if (i >= heap->size) + return NULL; - /* - * Make sure the data is sorted so we return the correct value. - */ - if (!heap->sorted) - ecore_sheap_sort(heap); + /* + * Make sure the data is sorted so we return the correct value. + */ + if (!heap->sorted) + ecore_sheap_sort(heap); - return heap->data[i]; + return heap->data[i]; } /* @@ -359,69 +376,71 @@ * @param heap The heap to regain heap properties * @param i The position to start heapifying */ -static void _ecore_sheap_heapify(Ecore_Sheap *heap, int i) +static void +_ecore_sheap_heapify(Ecore_Sheap *heap, int i) { - int extreme; - int left = LEFT(i); - int right = RIGHT(i); - - if (heap->order == ECORE_SHEAP_MIN) { - if (left <= heap->size && heap->compare(heap->data[left - 1], - heap->data[i - 1]) < 0) - extreme = left; - else - extreme = i; - - if (right <= heap->size && heap->compare(heap->data[right - 1], - heap->data[extreme - 1]) < 0) - extreme = right; - } - else { - if (left <= heap->size && heap->compare(heap->data[left - 1], - heap->data[i - 1]) > 0) - extreme = left; - else - extreme = i; - - if (right <= heap->size && heap->compare(heap->data[right - 1], - heap->data[extreme - 1]) > 0) - extreme = right; - } - - /* - * If the data needs to be swapped down the heap, recurse on - * heapifying it's new placement. - */ - if (extreme != i) { - void *temp; - - temp = heap->data[extreme - 1]; - heap->data[extreme - 1] = heap->data[i - 1]; - heap->data[i - 1] = temp; - - _ecore_sheap_heapify(heap, extreme); - } -} - -static void _ecore_sheap_update_data(Ecore_Sheap *heap) -{ - int i, old_size; - void **data; - - /* - * Track the old values from the heap - */ - old_size = heap->size; - data = heap->data; - - /* - * - */ - heap->size = 0; - heap->data = malloc(heap->space * sizeof(void *)); + int extreme; + int left = LEFT(i); + int right = RIGHT(i); + + if (heap->order == ECORE_SHEAP_MIN) + { + if (left <= heap->size && heap->compare(heap->data[left - 1], + heap->data[i - 1]) < 0) + extreme = left; + else + extreme = i; + + if (right <= heap->size && heap->compare(heap->data[right - 1], + heap->data[extreme - 1]) < 0) + extreme = right; + } + else + { + if (left <= heap->size && heap->compare(heap->data[left - 1], + heap->data[i - 1]) > 0) + extreme = left; + else + extreme = i; + + if (right <= heap->size && heap->compare(heap->data[right - 1], + heap->data[extreme - 1]) > 0) + extreme = right; + } + + /* + * If the data needs to be swapped down the heap, recurse on + * heapifying it's new placement. + */ + if (extreme != i) + { + void *temp; + + temp = heap->data[extreme - 1]; + heap->data[extreme - 1] = heap->data[i - 1]; + heap->data[i - 1] = temp; + + _ecore_sheap_heapify(heap, extreme); + } +} + +static void +_ecore_sheap_update_data(Ecore_Sheap *heap) +{ + int i, old_size; + void **data; + + /* + * Track the old values from the heap + */ + old_size = heap->size; + data = heap->data; + + heap->size = 0; + heap->data = malloc(heap->space * sizeof(void *)); - for (i = 0; i < old_size; i++) - ecore_sheap_insert(heap, data[i]); + for (i = 0; i < old_size; i++) + ecore_sheap_insert(heap, data[i]); - FREE(data); + FREE(data); } Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 _______________________________________________ enlightenment-cvs mailing list enlightenment-cvs@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/enlightenment-cvs