Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2019-04-17 07:17:24 +0300
committerCampbell Barton <ideasman42@gmail.com>2019-04-17 07:21:24 +0300
commite12c08e8d170b7ca40f204a5b0423c23a9fbc2c1 (patch)
tree8cf3453d12edb177a218ef8009357518ec6cab6a /source/blender/blenlib/intern/BLI_heap.c
parentb3dabc200a4b0399ec6b81f2ff2730d07b44fcaa (diff)
ClangFormat: apply to source, most of intern
Apply clang format as proposed in T53211. For details on usage and instructions for migrating branches without conflicts, see: https://wiki.blender.org/wiki/Tools/ClangFormat
Diffstat (limited to 'source/blender/blenlib/intern/BLI_heap.c')
-rw-r--r--source/blender/blenlib/intern/BLI_heap.c438
1 files changed, 218 insertions, 220 deletions
diff --git a/source/blender/blenlib/intern/BLI_heap.c b/source/blender/blenlib/intern/BLI_heap.c
index 0e5cfd02939..836b11baa85 100644
--- a/source/blender/blenlib/intern/BLI_heap.c
+++ b/source/blender/blenlib/intern/BLI_heap.c
@@ -32,16 +32,16 @@
/***/
struct HeapNode {
- float value;
- uint index;
- void *ptr;
+ float value;
+ uint index;
+ void *ptr;
};
struct HeapNode_Chunk {
- struct HeapNode_Chunk *prev;
- uint size;
- uint bufsize;
- struct HeapNode buf[0];
+ struct HeapNode_Chunk *prev;
+ uint size;
+ uint bufsize;
+ struct HeapNode buf[0];
};
/**
@@ -52,143 +52,141 @@ struct HeapNode_Chunk {
* \note keep type in sync with tot_nodes in heap_node_alloc_chunk.
*/
#define HEAP_CHUNK_DEFAULT_NUM \
- ((uint)((MEM_SIZE_OPTIMAL((1 << 16) - sizeof(struct HeapNode_Chunk))) / sizeof(HeapNode)))
+ ((uint)((MEM_SIZE_OPTIMAL((1 << 16) - sizeof(struct HeapNode_Chunk))) / sizeof(HeapNode)))
struct Heap {
- uint size;
- uint bufsize;
- HeapNode **tree;
-
- struct {
- /* Always keep at least one chunk (never NULL) */
- struct HeapNode_Chunk *chunk;
- /* when NULL, allocate a new chunk */
- HeapNode *free;
- } nodes;
+ uint size;
+ uint bufsize;
+ HeapNode **tree;
+
+ struct {
+ /* Always keep at least one chunk (never NULL) */
+ struct HeapNode_Chunk *chunk;
+ /* when NULL, allocate a new chunk */
+ HeapNode *free;
+ } nodes;
};
/** \name Internal Functions
* \{ */
-#define HEAP_PARENT(i) (((i) - 1) >> 1)
-#define HEAP_LEFT(i) (((i) << 1) + 1)
-#define HEAP_RIGHT(i) (((i) << 1) + 2)
+#define HEAP_PARENT(i) (((i)-1) >> 1)
+#define HEAP_LEFT(i) (((i) << 1) + 1)
+#define HEAP_RIGHT(i) (((i) << 1) + 2)
#define HEAP_COMPARE(a, b) ((a)->value < (b)->value)
-#if 0 /* UNUSED */
-#define HEAP_EQUALS(a, b) ((a)->value == (b)->value)
+#if 0 /* UNUSED */
+# define HEAP_EQUALS(a, b) ((a)->value == (b)->value)
#endif
BLI_INLINE void heap_swap(Heap *heap, const uint i, const uint j)
{
#if 1
- HeapNode **tree = heap->tree;
- HeapNode *pi = tree[i], *pj = tree[j];
- pi->index = j;
- tree[j] = pi;
- pj->index = i;
- tree[i] = pj;
+ HeapNode **tree = heap->tree;
+ HeapNode *pi = tree[i], *pj = tree[j];
+ pi->index = j;
+ tree[j] = pi;
+ pj->index = i;
+ tree[i] = pj;
#elif 0
- SWAP(uint, heap->tree[i]->index, heap->tree[j]->index);
- SWAP(HeapNode *, heap->tree[i], heap->tree[j]);
+ SWAP(uint, heap->tree[i]->index, heap->tree[j]->index);
+ SWAP(HeapNode *, heap->tree[i], heap->tree[j]);
#else
- HeapNode **tree = heap->tree;
- union {
- uint index;
- HeapNode *node;
- } tmp;
- SWAP_TVAL(tmp.index, tree[i]->index, tree[j]->index);
- SWAP_TVAL(tmp.node, tree[i], tree[j]);
+ HeapNode **tree = heap->tree;
+ union {
+ uint index;
+ HeapNode *node;
+ } tmp;
+ SWAP_TVAL(tmp.index, tree[i]->index, tree[j]->index);
+ SWAP_TVAL(tmp.node, tree[i], tree[j]);
#endif
}
static void heap_down(Heap *heap, uint i)
{
- /* size won't change in the loop */
- HeapNode **const tree = heap->tree;
- const uint size = heap->size;
-
- while (1) {
- const uint l = HEAP_LEFT(i);
- const uint r = HEAP_RIGHT(i);
- uint smallest = i;
-
- if (LIKELY(l < size) && HEAP_COMPARE(tree[l], tree[smallest])) {
- smallest = l;
- }
- if (LIKELY(r < size) && HEAP_COMPARE(tree[r], tree[smallest])) {
- smallest = r;
- }
-
- if (UNLIKELY(smallest == i)) {
- break;
- }
-
- heap_swap(heap, i, smallest);
- i = smallest;
- }
+ /* size won't change in the loop */
+ HeapNode **const tree = heap->tree;
+ const uint size = heap->size;
+
+ while (1) {
+ const uint l = HEAP_LEFT(i);
+ const uint r = HEAP_RIGHT(i);
+ uint smallest = i;
+
+ if (LIKELY(l < size) && HEAP_COMPARE(tree[l], tree[smallest])) {
+ smallest = l;
+ }
+ if (LIKELY(r < size) && HEAP_COMPARE(tree[r], tree[smallest])) {
+ smallest = r;
+ }
+
+ if (UNLIKELY(smallest == i)) {
+ break;
+ }
+
+ heap_swap(heap, i, smallest);
+ i = smallest;
+ }
}
static void heap_up(Heap *heap, uint i)
{
- HeapNode **const tree = heap->tree;
+ HeapNode **const tree = heap->tree;
- while (LIKELY(i > 0)) {
- const uint p = HEAP_PARENT(i);
+ while (LIKELY(i > 0)) {
+ const uint p = HEAP_PARENT(i);
- if (HEAP_COMPARE(tree[p], tree[i])) {
- break;
- }
- heap_swap(heap, p, i);
- i = p;
- }
+ if (HEAP_COMPARE(tree[p], tree[i])) {
+ break;
+ }
+ heap_swap(heap, p, i);
+ i = p;
+ }
}
/** \} */
-
/** \name Internal Memory Management
* \{ */
-static struct HeapNode_Chunk *heap_node_alloc_chunk(
- uint tot_nodes, struct HeapNode_Chunk *chunk_prev)
+static struct HeapNode_Chunk *heap_node_alloc_chunk(uint tot_nodes,
+ struct HeapNode_Chunk *chunk_prev)
{
- struct HeapNode_Chunk *chunk = MEM_mallocN(
- sizeof(struct HeapNode_Chunk) + (sizeof(HeapNode) * tot_nodes), __func__);
- chunk->prev = chunk_prev;
- chunk->bufsize = tot_nodes;
- chunk->size = 0;
- return chunk;
+ struct HeapNode_Chunk *chunk = MEM_mallocN(
+ sizeof(struct HeapNode_Chunk) + (sizeof(HeapNode) * tot_nodes), __func__);
+ chunk->prev = chunk_prev;
+ chunk->bufsize = tot_nodes;
+ chunk->size = 0;
+ return chunk;
}
static struct HeapNode *heap_node_alloc(Heap *heap)
{
- HeapNode *node;
-
- if (heap->nodes.free) {
- node = heap->nodes.free;
- heap->nodes.free = heap->nodes.free->ptr;
- }
- else {
- struct HeapNode_Chunk *chunk = heap->nodes.chunk;
- if (UNLIKELY(chunk->size == chunk->bufsize)) {
- chunk = heap->nodes.chunk = heap_node_alloc_chunk(HEAP_CHUNK_DEFAULT_NUM, chunk);
- }
- node = &chunk->buf[chunk->size++];
- }
-
- return node;
+ HeapNode *node;
+
+ if (heap->nodes.free) {
+ node = heap->nodes.free;
+ heap->nodes.free = heap->nodes.free->ptr;
+ }
+ else {
+ struct HeapNode_Chunk *chunk = heap->nodes.chunk;
+ if (UNLIKELY(chunk->size == chunk->bufsize)) {
+ chunk = heap->nodes.chunk = heap_node_alloc_chunk(HEAP_CHUNK_DEFAULT_NUM, chunk);
+ }
+ node = &chunk->buf[chunk->size++];
+ }
+
+ return node;
}
static void heap_node_free(Heap *heap, HeapNode *node)
{
- node->ptr = heap->nodes.free;
- heap->nodes.free = node;
+ node->ptr = heap->nodes.free;
+ heap->nodes.free = node;
}
/** \} */
-
/** \name Public Heap API
* \{ */
@@ -199,64 +197,65 @@ static void heap_node_free(Heap *heap, HeapNode *node)
*/
Heap *BLI_heap_new_ex(uint tot_reserve)
{
- Heap *heap = MEM_mallocN(sizeof(Heap), __func__);
- /* ensure we have at least one so we can keep doubling it */
- heap->size = 0;
- heap->bufsize = MAX2(1u, tot_reserve);
- heap->tree = MEM_mallocN(heap->bufsize * sizeof(HeapNode *), "BLIHeapTree");
+ Heap *heap = MEM_mallocN(sizeof(Heap), __func__);
+ /* ensure we have at least one so we can keep doubling it */
+ heap->size = 0;
+ heap->bufsize = MAX2(1u, tot_reserve);
+ heap->tree = MEM_mallocN(heap->bufsize * sizeof(HeapNode *), "BLIHeapTree");
- heap->nodes.chunk = heap_node_alloc_chunk((tot_reserve > 1) ? tot_reserve : HEAP_CHUNK_DEFAULT_NUM, NULL);
- heap->nodes.free = NULL;
+ heap->nodes.chunk = heap_node_alloc_chunk(
+ (tot_reserve > 1) ? tot_reserve : HEAP_CHUNK_DEFAULT_NUM, NULL);
+ heap->nodes.free = NULL;
- return heap;
+ return heap;
}
Heap *BLI_heap_new(void)
{
- return BLI_heap_new_ex(1);
+ return BLI_heap_new_ex(1);
}
void BLI_heap_free(Heap *heap, HeapFreeFP ptrfreefp)
{
- if (ptrfreefp) {
- uint i;
-
- for (i = 0; i < heap->size; i++) {
- ptrfreefp(heap->tree[i]->ptr);
- }
- }
-
- struct HeapNode_Chunk *chunk = heap->nodes.chunk;
- do {
- struct HeapNode_Chunk *chunk_prev;
- chunk_prev = chunk->prev;
- MEM_freeN(chunk);
- chunk = chunk_prev;
- } while (chunk);
-
- MEM_freeN(heap->tree);
- MEM_freeN(heap);
+ if (ptrfreefp) {
+ uint i;
+
+ for (i = 0; i < heap->size; i++) {
+ ptrfreefp(heap->tree[i]->ptr);
+ }
+ }
+
+ struct HeapNode_Chunk *chunk = heap->nodes.chunk;
+ do {
+ struct HeapNode_Chunk *chunk_prev;
+ chunk_prev = chunk->prev;
+ MEM_freeN(chunk);
+ chunk = chunk_prev;
+ } while (chunk);
+
+ MEM_freeN(heap->tree);
+ MEM_freeN(heap);
}
void BLI_heap_clear(Heap *heap, HeapFreeFP ptrfreefp)
{
- if (ptrfreefp) {
- uint i;
-
- for (i = 0; i < heap->size; i++) {
- ptrfreefp(heap->tree[i]->ptr);
- }
- }
- heap->size = 0;
-
- /* Remove all except the last chunk */
- while (heap->nodes.chunk->prev) {
- struct HeapNode_Chunk *chunk_prev = heap->nodes.chunk->prev;
- MEM_freeN(heap->nodes.chunk);
- heap->nodes.chunk = chunk_prev;
- }
- heap->nodes.chunk->size = 0;
- heap->nodes.free = NULL;
+ if (ptrfreefp) {
+ uint i;
+
+ for (i = 0; i < heap->size; i++) {
+ ptrfreefp(heap->tree[i]->ptr);
+ }
+ }
+ heap->size = 0;
+
+ /* Remove all except the last chunk */
+ while (heap->nodes.chunk->prev) {
+ struct HeapNode_Chunk *chunk_prev = heap->nodes.chunk->prev;
+ MEM_freeN(heap->nodes.chunk);
+ heap->nodes.chunk = chunk_prev;
+ }
+ heap->nodes.chunk->size = 0;
+ heap->nodes.free = NULL;
}
/**
@@ -265,26 +264,26 @@ void BLI_heap_clear(Heap *heap, HeapFreeFP ptrfreefp)
*/
HeapNode *BLI_heap_insert(Heap *heap, float value, void *ptr)
{
- HeapNode *node;
+ HeapNode *node;
- if (UNLIKELY(heap->size >= heap->bufsize)) {
- heap->bufsize *= 2;
- heap->tree = MEM_reallocN(heap->tree, heap->bufsize * sizeof(*heap->tree));
- }
+ if (UNLIKELY(heap->size >= heap->bufsize)) {
+ heap->bufsize *= 2;
+ heap->tree = MEM_reallocN(heap->tree, heap->bufsize * sizeof(*heap->tree));
+ }
- node = heap_node_alloc(heap);
+ node = heap_node_alloc(heap);
- node->ptr = ptr;
- node->value = value;
- node->index = heap->size;
+ node->ptr = ptr;
+ node->value = value;
+ node->index = heap->size;
- heap->tree[node->index] = node;
+ heap->tree[node->index] = node;
- heap->size++;
+ heap->size++;
- heap_up(heap, node->index);
+ heap_up(heap, node->index);
- return node;
+ return node;
}
/**
@@ -292,23 +291,22 @@ HeapNode *BLI_heap_insert(Heap *heap, float value, void *ptr)
*/
void BLI_heap_insert_or_update(Heap *heap, HeapNode **node_p, float value, void *ptr)
{
- if (*node_p == NULL) {
- *node_p = BLI_heap_insert(heap, value, ptr);
- }
- else {
- BLI_heap_node_value_update_ptr(heap, *node_p, value, ptr);
- }
+ if (*node_p == NULL) {
+ *node_p = BLI_heap_insert(heap, value, ptr);
+ }
+ else {
+ BLI_heap_node_value_update_ptr(heap, *node_p, value, ptr);
+ }
}
-
bool BLI_heap_is_empty(const Heap *heap)
{
- return (heap->size == 0);
+ return (heap->size == 0);
}
uint BLI_heap_len(const Heap *heap)
{
- return heap->size;
+ return heap->size;
}
/**
@@ -317,7 +315,7 @@ uint BLI_heap_len(const Heap *heap)
*/
HeapNode *BLI_heap_top(const Heap *heap)
{
- return heap->tree[0];
+ return heap->tree[0];
}
/**
@@ -326,9 +324,9 @@ HeapNode *BLI_heap_top(const Heap *heap)
*/
float BLI_heap_top_value(const Heap *heap)
{
- BLI_assert(heap->size != 0);
+ BLI_assert(heap->size != 0);
- return heap->tree[0]->value;
+ return heap->tree[0]->value;
}
/**
@@ -336,33 +334,33 @@ float BLI_heap_top_value(const Heap *heap)
*/
void *BLI_heap_pop_min(Heap *heap)
{
- BLI_assert(heap->size != 0);
+ BLI_assert(heap->size != 0);
- void *ptr = heap->tree[0]->ptr;
+ void *ptr = heap->tree[0]->ptr;
- heap_node_free(heap, heap->tree[0]);
+ heap_node_free(heap, heap->tree[0]);
- if (--heap->size) {
- heap_swap(heap, 0, heap->size);
- heap_down(heap, 0);
- }
+ if (--heap->size) {
+ heap_swap(heap, 0, heap->size);
+ heap_down(heap, 0);
+ }
- return ptr;
+ return ptr;
}
void BLI_heap_remove(Heap *heap, HeapNode *node)
{
- BLI_assert(heap->size != 0);
+ BLI_assert(heap->size != 0);
- uint i = node->index;
+ uint i = node->index;
- while (i > 0) {
- uint p = HEAP_PARENT(i);
- heap_swap(heap, p, i);
- i = p;
- }
+ while (i > 0) {
+ uint p = HEAP_PARENT(i);
+ heap_swap(heap, p, i);
+ i = p;
+ }
- BLI_heap_pop_min(heap);
+ BLI_heap_pop_min(heap);
}
/**
@@ -372,66 +370,66 @@ void BLI_heap_remove(Heap *heap, HeapNode *node)
*/
void BLI_heap_node_value_update(Heap *heap, HeapNode *node, float value)
{
- if (value < node->value) {
- node->value = value;
- heap_up(heap, node->index);
- }
- else if (value > node->value) {
- node->value = value;
- heap_down(heap, node->index);
- }
+ if (value < node->value) {
+ node->value = value;
+ heap_up(heap, node->index);
+ }
+ else if (value > node->value) {
+ node->value = value;
+ heap_down(heap, node->index);
+ }
}
void BLI_heap_node_value_update_ptr(Heap *heap, HeapNode *node, float value, void *ptr)
{
- node->ptr = ptr; /* only difference */
- if (value < node->value) {
- node->value = value;
- heap_up(heap, node->index);
- }
- else if (value > node->value) {
- node->value = value;
- heap_down(heap, node->index);
- }
+ node->ptr = ptr; /* only difference */
+ if (value < node->value) {
+ node->value = value;
+ heap_up(heap, node->index);
+ }
+ else if (value > node->value) {
+ node->value = value;
+ heap_down(heap, node->index);
+ }
}
float BLI_heap_node_value(const HeapNode *node)
{
- return node->value;
+ return node->value;
}
void *BLI_heap_node_ptr(const HeapNode *node)
{
- return node->ptr;
+ return node->ptr;
}
static bool heap_is_minheap(const Heap *heap, uint root)
{
- if (root < heap->size) {
- if (heap->tree[root]->index != root) {
- return false;
- }
- const uint l = HEAP_LEFT(root);
- if (l < heap->size) {
- if (HEAP_COMPARE(heap->tree[l], heap->tree[root]) || !heap_is_minheap(heap, l)) {
- return false;
- }
- }
- const uint r = HEAP_RIGHT(root);
- if (r < heap->size) {
- if (HEAP_COMPARE(heap->tree[r], heap->tree[root]) || !heap_is_minheap(heap, r)) {
- return false;
- }
- }
- }
- return true;
+ if (root < heap->size) {
+ if (heap->tree[root]->index != root) {
+ return false;
+ }
+ const uint l = HEAP_LEFT(root);
+ if (l < heap->size) {
+ if (HEAP_COMPARE(heap->tree[l], heap->tree[root]) || !heap_is_minheap(heap, l)) {
+ return false;
+ }
+ }
+ const uint r = HEAP_RIGHT(root);
+ if (r < heap->size) {
+ if (HEAP_COMPARE(heap->tree[r], heap->tree[root]) || !heap_is_minheap(heap, r)) {
+ return false;
+ }
+ }
+ }
+ return true;
}
/**
* Only for checking internal errors (gtest).
*/
bool BLI_heap_is_valid(const Heap *heap)
{
- return heap_is_minheap(heap, 0);
+ return heap_is_minheap(heap, 0);
}
/** \} */