/**
- @file vec.c
- @brief See header for details
- $Revision$
- $HeadURL$
+ @file vec.c
+ @brief See header for details
+ $Revision$
+ $HeadURL$
*/
#include "vec.h"
#include <stdlib.h>
{
if (size > p_vec->size)
{
- vec_reserve(p_vec,size);
+ vec_reserve(p_vec,vec_next_capacity(size));
for (p_vec->size; p_vec->size < size; p_vec->size++)
{
p_vec->p_buffer[ p_vec->size ] = data;
}
}
+size_t vec_next_capacity(size_t req_size)
+{
+ size_t next_power = req_size;
+ size_t num_bits = sizeof(size_t) * 8;
+ size_t bit_n;
+
+ /* Find the next highest power of 2 */
+ next_power--;
+ for (bit_n = 1; bit_n < num_bits; bit_n = bit_n << 1)
+ {
+ next_power = next_power | (next_power >> bit_n);
+ }
+ next_power++;
+
+ return next_power;
+}
+
void vec_shrink_to_fit(vec_t* p_vec)
{
p_vec->p_buffer = realloc( p_vec->p_buffer, sizeof(void*) * p_vec->size );
vec_resize( p_vec, p_vec->size + num_elements, NULL );
/* Move the displaced items to the end */
memcpy( &(p_vec->p_buffer[index + num_elements]),
- &(p_vec->p_buffer[index]),
- sizeof(void*) * (p_vec->size - index));
+ &(p_vec->p_buffer[index]),
+ sizeof(void*) * (p_vec->size - index));
/* insert the new items */
va_start(elements, num_elements);
new_size = index + num_elements;
}
/* Compact the remaining data */
memcpy( &(p_vec->p_buffer[start_idx]), /* Destination is beginning of erased range */
- &(p_vec->p_buffer[end_idx+1]), /* Source is end of erased range */
- sizeof(void*) * (p_vec->size - end_idx));
+ &(p_vec->p_buffer[end_idx+1]), /* Source is end of erased range */
+ sizeof(void*) * (p_vec->size - end_idx));
/* Shrink the size */
p_vec->size = p_vec->size - ((end_idx - start_idx) + 1);
ret = true;
*/
void vec_resize(vec_t* p_vec, size_t size, void* data);
+/**
+ * @brief Returns the next power of two that is not less than the desired size.
+ *
+ * @param req_size The desired size of the vector.
+ *
+ * @return The necessary capacity.
+ */
+size_t vec_next_capacity(size_t req_size);
+
/**
* @brief Shrinks the vector's capacity to equal it's size.
*
CHECK( false == p_vec->own_contents );
CHECK( 5 == p_vec->size );
- CHECK( 5 == p_vec->capacity );
+ CHECK( 8 == p_vec->capacity );
CHECK( (void*)0x2A == p_vec->p_buffer[3] );
CHECK( (void*)0x2A == p_vec->p_buffer[4] );
vec_clear( &vector );
CHECK(0 == vector.size);
}
+
+ //TEST(foo)
+ //{
+ // size_t next_power = 9;
+ // size_t num_bits = sizeof(size_t) * 8;
+ // size_t bit_n;
+
+ // next_power--;
+ // for (bit_n = 1; bit_n < num_bits; bit_n = bit_n << 1)
+ // {
+ // next_power = next_power | (next_power >> bit_n);
+ // }
+ // next_power++;
+
+ // std::cout << next_power << std::endl;
+
+ // //int n = 7;
+ // //int exp = 0;
+ // //while(n>1) { n = n>>1; exp++; }
+ // //while(exp>=0) { n = n<<1; exp--; }
+ // //std::cout << n << " " << exp << std::endl;
+ //}
}