BLI_Stack add BLI_stack_peek, BLI_stack_discard
also remove own incorrect assert
This commit is contained in:
@@ -46,6 +46,9 @@ void BLI_stack_push(BLI_Stack *stack, const void *src) ATTR_NONNULL();
|
|||||||
void BLI_stack_pop_n(BLI_Stack *stack, void *dst, unsigned int n) ATTR_NONNULL();
|
void BLI_stack_pop_n(BLI_Stack *stack, void *dst, unsigned int n) ATTR_NONNULL();
|
||||||
void BLI_stack_pop(BLI_Stack *stack, void *dst) ATTR_NONNULL();
|
void BLI_stack_pop(BLI_Stack *stack, void *dst) ATTR_NONNULL();
|
||||||
|
|
||||||
|
void *BLI_stack_peek(BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
|
||||||
|
void BLI_stack_discard(BLI_Stack *stack) ATTR_NONNULL();
|
||||||
|
|
||||||
size_t BLI_stack_count(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
|
size_t BLI_stack_count(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
|
||||||
|
|
||||||
bool BLI_stack_is_empty(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
|
bool BLI_stack_is_empty(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
|
||||||
|
@@ -48,8 +48,6 @@
|
|||||||
((void)0, (((char *)(_stack)->chunk_curr->data) + \
|
((void)0, (((char *)(_stack)->chunk_curr->data) + \
|
||||||
((_stack)->elem_size * (_stack)->chunk_index)))
|
((_stack)->elem_size * (_stack)->chunk_index)))
|
||||||
|
|
||||||
#define IS_POW2(a) (((a) & ((a) - 1)) == 0)
|
|
||||||
|
|
||||||
struct StackChunk {
|
struct StackChunk {
|
||||||
struct StackChunk *next;
|
struct StackChunk *next;
|
||||||
char data[0];
|
char data[0];
|
||||||
@@ -96,11 +94,6 @@ BLI_Stack *BLI_stack_new_ex(const size_t elem_size, const char *description,
|
|||||||
/* force init */
|
/* force init */
|
||||||
stack->chunk_index = stack->chunk_elem_max - 1;
|
stack->chunk_index = stack->chunk_elem_max - 1;
|
||||||
|
|
||||||
/* ensure we have a correctly rounded size */
|
|
||||||
BLI_assert((IS_POW2(stack->elem_size) == 0) ||
|
|
||||||
(IS_POW2((stack->chunk_elem_max * stack->elem_size) +
|
|
||||||
(sizeof(struct StackChunk) + MEM_SIZE_OVERHEAD))));
|
|
||||||
|
|
||||||
return stack;
|
return stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,6 +175,31 @@ void BLI_stack_pop(BLI_Stack *stack, void *dst)
|
|||||||
BLI_assert(BLI_stack_is_empty(stack) == false);
|
BLI_assert(BLI_stack_is_empty(stack) == false);
|
||||||
|
|
||||||
memcpy(dst, CHUNK_LAST_ELEM(stack), stack->elem_size);
|
memcpy(dst, CHUNK_LAST_ELEM(stack), stack->elem_size);
|
||||||
|
|
||||||
|
BLI_stack_discard(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BLI_stack_pop_n(BLI_Stack *stack, void *dst, unsigned int n)
|
||||||
|
{
|
||||||
|
BLI_assert(n <= BLI_stack_count(stack));
|
||||||
|
|
||||||
|
while (n--) {
|
||||||
|
BLI_stack_pop(stack, dst);
|
||||||
|
dst = (void *)((char *)dst + stack->elem_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void *BLI_stack_peek(BLI_Stack *stack)
|
||||||
|
{
|
||||||
|
BLI_assert(BLI_stack_is_empty(stack) == false);
|
||||||
|
|
||||||
|
return CHUNK_LAST_ELEM(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BLI_stack_discard(BLI_Stack *stack)
|
||||||
|
{
|
||||||
|
BLI_assert(BLI_stack_is_empty(stack) == false);
|
||||||
|
|
||||||
#ifdef USE_TOTELEM
|
#ifdef USE_TOTELEM
|
||||||
stack->totelem--;
|
stack->totelem--;
|
||||||
#endif
|
#endif
|
||||||
@@ -198,16 +216,6 @@ void BLI_stack_pop(BLI_Stack *stack, void *dst)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BLI_stack_pop_n(BLI_Stack *stack, void *dst, unsigned int n)
|
|
||||||
{
|
|
||||||
BLI_assert(n <= BLI_stack_count(stack));
|
|
||||||
|
|
||||||
while (n--) {
|
|
||||||
BLI_stack_pop(stack, dst);
|
|
||||||
dst = (void *)((char *)dst + stack->elem_size);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t BLI_stack_count(const BLI_Stack *stack)
|
size_t BLI_stack_count(const BLI_Stack *stack)
|
||||||
{
|
{
|
||||||
#ifdef USE_TOTELEM
|
#ifdef USE_TOTELEM
|
||||||
|
@@ -88,6 +88,29 @@ TEST(stack, String)
|
|||||||
BLI_stack_free(stack);
|
BLI_stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(stack, Peek)
|
||||||
|
{
|
||||||
|
const int tot = SIZE;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
BLI_Stack *stack;
|
||||||
|
const short in[] = {1, 10, 100, 1000};
|
||||||
|
|
||||||
|
stack = BLI_stack_new(sizeof(*in), __func__);
|
||||||
|
|
||||||
|
for (i = 0; i < tot; i++) {
|
||||||
|
BLI_stack_push(stack, &in[i % ARRAY_SIZE(in)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = tot - 1; i >= 0; i--, BLI_stack_discard(stack)) {
|
||||||
|
short *ret = (short *)BLI_stack_peek(stack);
|
||||||
|
EXPECT_EQ(*ret, in[i % ARRAY_SIZE(in)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
EXPECT_EQ(BLI_stack_is_empty(stack), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST(stack, Reuse)
|
TEST(stack, Reuse)
|
||||||
{
|
{
|
||||||
const int sizes[] = {3, 11, 81, 400, 999, 12, 1, 9721, 7, 99, 5, 0};
|
const int sizes[] = {3, 11, 81, 400, 999, 12, 1, 9721, 7, 99, 5, 0};
|
||||||
|
Reference in New Issue
Block a user