snapraid/tommyds/tommyarray.h

122 lines
3.1 KiB
C
Raw Permalink Normal View History

// SPDX-License-Identifier: BSD-2-Clause
// Copyright (C) 2011 Andrea Mazzoleni
2011-03-01 22:21:30 +01:00
/** \file
2025-12-03 19:10:13 +01:00
* Dynamic array based on segments of exponentially growing size.
2011-03-01 22:21:30 +01:00
*
2014-10-25 17:28:41 +02:00
* This array is able to grow dynamically upon request, without any reallocation.
2011-03-01 22:21:30 +01:00
*
2014-10-25 17:28:41 +02:00
* The grow operation involves an allocation of a new array segment, without reallocating
2025-12-03 19:10:13 +01:00
* the already used memory, and thus **not increasing** the heap fragmentation.
2014-10-25 17:28:41 +02:00
* This also implies that the address of the stored elements never change.
2011-03-01 22:21:30 +01:00
*
* Allocated segments grow in size exponentially.
2011-03-01 22:21:30 +01:00
*/
#ifndef __TOMMYARRAY_H
#define __TOMMYARRAY_H
#include "tommytypes.h"
#include <assert.h> /* for assert */
/******************************************************************************/
/* array */
/**
* Initial and minimal size of the array expressed as a power of 2.
* The initial size is 2^TOMMY_ARRAY_BIT.
*/
#define TOMMY_ARRAY_BIT 6
/**
2014-10-25 17:28:41 +02:00
* Array container type.
* \note Don't use internal fields directly, but access the container only using functions.
2011-03-01 22:21:30 +01:00
*/
typedef struct tommy_array_struct {
2018-04-01 22:36:42 +02:00
void** bucket[TOMMY_SIZE_BIT]; /**< Dynamic array of buckets. */
tommy_size_t bucket_max; /**< Number of buckets. */
tommy_size_t count; /**< Number of initialized elements in the array. */
2014-10-25 17:28:41 +02:00
tommy_uint_t bucket_bit; /**< Bits used in the bit mask. */
2011-03-01 22:21:30 +01:00
} tommy_array;
/**
* Initializes the array.
*/
2025-12-03 19:10:13 +01:00
TOMMY_API void tommy_array_init(tommy_array* array);
2011-03-01 22:21:30 +01:00
/**
* Deinitializes the array.
*/
2025-12-03 19:10:13 +01:00
TOMMY_API void tommy_array_done(tommy_array* array);
2011-03-01 22:21:30 +01:00
/**
2014-10-25 17:28:41 +02:00
* Grows the size up to the specified value.
2011-03-01 22:21:30 +01:00
* All the new elements in the array are initialized with the 0 value.
*/
2025-12-03 19:10:13 +01:00
TOMMY_API void tommy_array_grow(tommy_array* array, tommy_size_t size);
2011-03-01 22:21:30 +01:00
/**
* Gets a reference of the element at the specified position.
* You must be sure that space for this position is already
* allocated calling tommy_array_grow().
*/
2018-04-01 22:36:42 +02:00
tommy_inline void** tommy_array_ref(tommy_array* array, tommy_size_t pos)
2011-03-01 22:21:30 +01:00
{
2014-10-25 17:28:41 +02:00
tommy_uint_t bsr;
2011-03-01 22:21:30 +01:00
2014-10-25 17:28:41 +02:00
assert(pos < array->count);
2011-03-01 22:21:30 +01:00
2014-09-26 23:50:01 +02:00
/* get the highest bit set, in case of all 0, return 0 */
2018-04-01 22:36:42 +02:00
bsr = tommy_ilog2(pos | 1);
2011-03-01 22:21:30 +01:00
2014-09-26 23:50:01 +02:00
return &array->bucket[bsr][pos];
2011-03-01 22:21:30 +01:00
}
/**
* Sets the element at the specified position.
* You must be sure that space for this position is already
* allocated calling tommy_array_grow().
*/
2018-04-01 22:36:42 +02:00
tommy_inline void tommy_array_set(tommy_array* array, tommy_size_t pos, void* element)
2011-03-01 22:21:30 +01:00
{
2013-06-23 21:49:36 +02:00
*tommy_array_ref(array, pos) = element;
2011-03-01 22:21:30 +01:00
}
/**
* Gets the element at the specified position.
* You must be sure that space for this position is already
* allocated calling tommy_array_grow().
*/
2018-04-01 22:36:42 +02:00
tommy_inline void* tommy_array_get(tommy_array* array, tommy_size_t pos)
2011-03-01 22:21:30 +01:00
{
return *tommy_array_ref(array, pos);
}
/**
* Grows and inserts a new element at the end of the array.
*/
2013-06-23 21:49:36 +02:00
tommy_inline void tommy_array_insert(tommy_array* array, void* element)
2011-03-01 22:21:30 +01:00
{
2018-04-01 22:36:42 +02:00
tommy_size_t pos = array->count;
2011-03-01 22:21:30 +01:00
tommy_array_grow(array, pos + 1);
2013-06-23 21:49:36 +02:00
tommy_array_set(array, pos, element);
2011-03-01 22:21:30 +01:00
}
/**
* Gets the initialized size of the array.
*/
2018-04-01 22:36:42 +02:00
tommy_inline tommy_size_t tommy_array_size(tommy_array* array)
2011-03-01 22:21:30 +01:00
{
2014-10-25 17:28:41 +02:00
return array->count;
2011-03-01 22:21:30 +01:00
}
/**
* Gets the size of allocated memory.
*/
2025-12-03 19:10:13 +01:00
TOMMY_API tommy_size_t tommy_array_memory_usage(tommy_array* array);
2011-03-01 22:21:30 +01:00
#endif