Add push_..._realloc to Deque
This commit is contained in:
parent
69edca40f8
commit
29e3940c14
3 changed files with 58 additions and 1 deletions
|
@ -140,6 +140,30 @@ int UDPC_Deque_push_front(UDPC_Deque *deque, const void *data, uint32_t size)
|
|||
return 1;
|
||||
}
|
||||
|
||||
int UDPC_Deque_push_back_realloc(UDPC_Deque *deque, const void *data, uint32_t size)
|
||||
{
|
||||
if(UDPC_Deque_push_back(deque, data, size) == 0)
|
||||
{
|
||||
if(UDPC_Deque_realloc(deque, deque->alloc_size * 2) != 0)
|
||||
{
|
||||
return UDPC_Deque_push_back(deque, data, size);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int UDPC_Deque_push_front_realloc(UDPC_Deque *deque, const void *data, uint32_t size)
|
||||
{
|
||||
if(UDPC_Deque_push_front(deque, data, size) == 0)
|
||||
{
|
||||
if(UDPC_Deque_realloc(deque, deque->alloc_size * 2) != 0)
|
||||
{
|
||||
return UDPC_Deque_push_front(deque, data, size);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t UDPC_Deque_get_available(UDPC_Deque *deque)
|
||||
{
|
||||
return deque->alloc_size - deque->size;
|
||||
|
|
|
@ -40,7 +40,19 @@ int UDPC_Deque_push_back(UDPC_Deque *deque, const void *data, uint32_t size);
|
|||
*/
|
||||
int UDPC_Deque_push_front(UDPC_Deque *deque, const void *data, uint32_t size);
|
||||
|
||||
// TODO add push_back/push_front variants that realloc on not enough free space
|
||||
/*!
|
||||
* \brief Same as push_back, but realloc if not enough free space
|
||||
* Note when realloc occurs, the allocated space is doubled.
|
||||
* \return non-zero on data pushed into Deque success
|
||||
*/
|
||||
int UDPC_Deque_push_back_realloc(UDPC_Deque *deque, const void *data, uint32_t size);
|
||||
|
||||
/*!
|
||||
* \brief Same as push_front, but realloc if not enough free space
|
||||
* Note when realloc occurs, the allocated space is doubled.
|
||||
* \return non-zero on data pushed into Deque success
|
||||
*/
|
||||
int UDPC_Deque_push_front_realloc(UDPC_Deque *deque, const void *data, uint32_t size);
|
||||
|
||||
/*!
|
||||
* \return size in bytes of available data
|
||||
|
|
|
@ -206,6 +206,27 @@ void TEST_DEQUE()
|
|||
}
|
||||
ASSERT_EQ(deque->size, 0);
|
||||
|
||||
// test push_back_realloc
|
||||
ASSERT_EQ(deque->alloc_size, 16 * sizeof(int));
|
||||
for(int x = 0; x < 16; ++x)
|
||||
{
|
||||
ASSERT_NEQ(UDPC_Deque_push_back(deque, &x, sizeof(int)), 0);
|
||||
}
|
||||
int tempInt = 20;
|
||||
ASSERT_EQ(UDPC_Deque_push_back(deque, &tempInt, sizeof(int)), 0);
|
||||
ASSERT_NEQ(UDPC_Deque_push_back_realloc(deque, &tempInt, sizeof(int)), 0);
|
||||
ASSERT_EQ(deque->alloc_size, 32 * sizeof(int));
|
||||
ASSERT_EQ_MEM(UDPC_Deque_get_back_ptr(deque, sizeof(int)), &tempInt, sizeof(int));
|
||||
|
||||
UDPC_Deque_pop_back(deque, sizeof(int));
|
||||
ASSERT_NEQ(UDPC_Deque_realloc(deque, 16 * sizeof(int)), 0);
|
||||
|
||||
// test push_front_realloc
|
||||
ASSERT_EQ(UDPC_Deque_push_front(deque, &tempInt, sizeof(int)), 0);
|
||||
ASSERT_NEQ(UDPC_Deque_push_front_realloc(deque, &tempInt, sizeof(int)), 0);
|
||||
ASSERT_EQ(deque->alloc_size, 32 * sizeof(int));
|
||||
ASSERT_EQ_MEM(UDPC_Deque_get_front_ptr(deque, sizeof(int)), &tempInt, sizeof(int));
|
||||
|
||||
/*
|
||||
for(int x = 0; x < deque->tail / sizeof(int); ++x)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue