|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
A dynamic table for managing void pointers with index recycling. More...
#include <pointer_table.H>
Public Member Functions | |
| Pointer_Table (size_t initial_size=0) | |
| Constructs a Pointer_Table with optional initial capacity. | |
| Pointer_Table (const Pointer_Table &other)=default | |
| Copies the pointers, the free indices and the threshold of another table. | |
| Pointer_Table (Pointer_Table &&other) noexcept | |
Takes the pointers and the free indices of another table, which is left empty, as Pointer_Table(). | |
| Pointer_Table & | operator= (const Pointer_Table &other)=default |
| Copies the pointers, the free indices and the threshold of another table. | |
| Pointer_Table & | operator= (Pointer_Table &&other) noexcept |
| Exchanges this table with another, which is left with the previous pointers of this one. | |
| void | swap (Pointer_Table &other) noexcept |
| Exchanges all the state of this table and another. | |
| long | size () const noexcept |
| Returns the current capacity of the pointer table. | |
| long | busies () const noexcept |
| Returns the number of pointers currently stored. | |
| long | frees () const noexcept |
| Returns the number of recyclable indices. | |
| long | get_heap_index () const noexcept |
| Returns the current heap index (high water mark). | |
| long | get_threshold () const noexcept |
| Returns the threshold size. | |
| bool | is_empty () const noexcept |
| Checks if the table is empty. | |
| long | insert_pointer (void *ptr) |
| Inserts a pointer and returns its assigned index. | |
| void | remove_pointer (long i) |
| Removes the pointer at the given index. | |
| void * | get_pointer (long i) const |
| Retrieves the pointer at the given index. | |
| void * | verify_pointer (long i, void *ptr) const |
| Verifies that a pointer matches the one stored at an index. | |
| void | clear () |
| Clears all pointers from the table. | |
Static Public Attributes | |
| static constexpr long | Null_Index = -1 |
| Sentinel value indicating an invalid or null index. | |
Private Member Functions | |
| long | allocate_above_heap () |
| Allocates an index from the free table. | |
| void | insert_in_free_table (long i) |
| Adds an index to the free table for future reuse. | |
| void | cleanup_free_table (long new_heap_index) |
| Removes indices from free_table that are >= new_heap_index. | |
| bool | is_valid_index (long i) const noexcept |
| Checks if an index is within valid bounds. | |
| bool | pointer_matches_with_index (long i, const void *ptr) const noexcept |
| Checks if a pointer matches the one stored at an index. | |
| bool | invariant () const |
| Validates the internal consistency of the data structure. | |
Private Attributes | |
| DynArray< void * > | pointer_table |
| Array mapping indices to pointers. | |
| DynArray< long > | free_table |
| Stack of available indices for reuse. | |
| long | num_pointers |
| Count of currently stored pointers. | |
| long | heap_index |
| Next available index at the heap top. | |
| long | threshold_size |
| Minimum size to maintain (prevents excessive shrinking) | |
A dynamic table for managing void pointers with index recycling.
Pointer_Table provides efficient storage and retrieval of void pointers using integer indices. When pointers are removed, their indices are recycled for future insertions, preventing unbounded index growth.
The data structure maintains two internal arrays:
insert_pointer(): Strong guarantee (no state change on failure)remove_pointer(): Strong guarantee (throws on invalid index)verify_pointer(): Strong guarantee (throws on mismatch)Definition at line 111 of file pointer_table.H.
|
inline |
Constructs a Pointer_Table with optional initial capacity.
Creates an empty pointer table with the specified initial capacity. The initial_size sets the threshold below which the table will not shrink, preventing excessive memory reallocation for tables that frequently grow and shrink.
| [in] | initial_size | Initial capacity and shrink threshold. Defaults to 0 (fully dynamic). |
Definition at line 304 of file pointer_table.H.
|
default |
Copies the pointers, the free indices and the threshold of another table.
| [in] | other | The table to copy. |
| std::bad_alloc | If there is not enough memory. |
|
inlinenoexcept |
Takes the pointers and the free indices of another table, which is left empty, as Pointer_Table().
The implicit move took the arrays but copied the counters: the moved-from table then read entries that it no longer had. Its threshold is reset too, since its array no longer has that size.
| [in,out] | other | The table to move from. |
Definition at line 333 of file pointer_table.H.
|
inlineprivate |
Allocates an index from the free table.
Attempts to reuse a previously freed index from the free table. This supports index recycling to minimize index growth.
Null_Index if none available.Definition at line 138 of file pointer_table.H.
References Aleph::and, Aleph::DynArray< T >::cut(), free_table, heap_index, Null_Index, pointer_table, and Aleph::DynArray< T >::size().
Referenced by insert_pointer().
|
inlinenoexcept |
Returns the number of pointers currently stored.
Definition at line 404 of file pointer_table.H.
References num_pointers.
Referenced by TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
|
inlineprivate |
Removes indices from free_table that are >= new_heap_index.
When the heap contracts, indices that were in the free table but are now beyond the heap boundary must be removed. This maintains the invariant that all free_table indices are valid.
| [in] | new_heap_index | The new heap boundary. |
Definition at line 185 of file pointer_table.H.
References Aleph::DynArray< T >::cut(), free_table, and Aleph::DynArray< T >::size().
Referenced by remove_pointer().
|
inline |
Clears all pointers from the table.
Removes all pointers and resets the table to its initial state. Does NOT delete the pointed-to objects - that is the caller's responsibility.
Definition at line 654 of file pointer_table.H.
References Aleph::DynArray< T >::cut(), free_table, heap_index, invariant(), num_pointers, pointer_table, and threshold_size.
Referenced by TEST_F(), TEST_F(), TEST_F(), TEST_F(), and Moved_From_Case< Pointer_Table >::use().
|
inlinenoexcept |
Returns the number of recyclable indices.
These are indices that were previously used but are now available for reuse via insert_pointer().
Definition at line 416 of file pointer_table.H.
References free_table, and Aleph::DynArray< T >::size().
Referenced by Moved_From_Case< Pointer_Table >::is_empty(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
|
inlinenoexcept |
Returns the current heap index (high water mark).
The heap index represents the next available index at the top of the allocation. All valid indices are in [0, heap_index).
Definition at line 428 of file pointer_table.H.
References heap_index.
Referenced by Moved_From_Case< Pointer_Table >::is_empty(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and Moved_From_Case< Pointer_Table >::use().
|
inline |
Retrieves the pointer at the given index.
Returns the pointer stored at index i without any validation. Use verify_pointer() if you need to confirm the pointer matches an expected value.
| [in] | i | The index to retrieve. |
i, or nullptr if the slot is free.| std::range_error | If i is out of valid range. |
Definition at line 575 of file pointer_table.H.
References ah_range_error_if, heap_index, is_valid_index(), and pointer_table.
Referenced by TEST_F(), and Moved_From_Case< Pointer_Table >::use().
|
inlinenoexcept |
Returns the threshold size.
The threshold is the minimum size the table maintains. The table will not shrink below this size.
Definition at line 440 of file pointer_table.H.
References threshold_size.
Referenced by TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
|
inlineprivate |
Adds an index to the free table for future reuse.
Marks the given index as available by setting its pointer to nullptr and adding it to the free table stack.
| [in] | i | The index to mark as free. Must be valid and in use. |
0 <= i < heap_index pointer_table[i] != nullptr (index must be in use)Definition at line 166 of file pointer_table.H.
References Aleph::and, free_table, heap_index, pointer_table, and Aleph::DynArray< T >::size().
Referenced by remove_pointer().
|
inline |
Inserts a pointer and returns its assigned index.
Adds the given pointer to the table, either reusing a previously freed index or allocating a new one at the heap top.
| [in] | ptr | The pointer to insert. May be nullptr (though this is discouraged as it complicates removal). |
remove_pointer() or verify_pointer() calls.Definition at line 477 of file pointer_table.H.
References allocate_above_heap(), heap_index, invariant(), Null_Index, num_pointers, and pointer_table.
Referenced by Moved_From_Case< Pointer_Table >::make(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and Moved_From_Case< Pointer_Table >::use().
|
inlineprivate |
Validates the internal consistency of the data structure.
Checks that all invariants hold:
true if all invariants hold, false otherwise.Definition at line 253 of file pointer_table.H.
References Aleph::and, free_table, heap_index, num_pointers, pointer_table, and Aleph::DynArray< T >::size().
Referenced by clear(), insert_pointer(), and remove_pointer().
|
inlinenoexcept |
Checks if the table is empty.
true if no pointers are stored, false otherwise.Definition at line 449 of file pointer_table.H.
References num_pointers.
Referenced by Moved_From_Case< Pointer_Table >::is_empty(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and Moved_From_Case< Pointer_Table >::use().
|
inlineprivatenoexcept |
Checks if an index is within valid bounds.
An index is valid if it is non-negative and less than the current heap index (the high water mark).
| [in] | i | The index to validate. |
true if the index is within bounds, false otherwise.Definition at line 218 of file pointer_table.H.
References Aleph::and, and heap_index.
Referenced by get_pointer(), pointer_matches_with_index(), remove_pointer(), and verify_pointer().
|
default |
Copies the pointers, the free indices and the threshold of another table.
| [in] | other | The table to copy. |
| std::bad_alloc | If there is not enough memory. |
|
inlinenoexcept |
Exchanges this table with another, which is left with the previous pointers of this one.
| [in,out] | other | The table to move from. |
Definition at line 361 of file pointer_table.H.
References swap().
|
inlineprivatenoexcept |
Checks if a pointer matches the one stored at an index.
| [in] | i | The index to check. Must be valid. |
| [in] | ptr | The pointer to compare against. |
true if the stored pointer equals ptr, false otherwise.is_valid_index(i) must be true.Definition at line 234 of file pointer_table.H.
References is_valid_index(), and pointer_table.
Referenced by verify_pointer().
|
inline |
Removes the pointer at the given index.
Removes the pointer at index i from the table. The index becomes available for reuse by subsequent insert_pointer() calls.
If the removed index is at the top of the heap, the heap contracts automatically, potentially freeing memory.
| [in] | i | The index of the pointer to remove. |
| std::range_error | If i is out of valid range. |
| std::domain_error | If index i is already free (nullptr). |
Definition at line 521 of file pointer_table.H.
References ah_domain_error_if, ah_range_error_if, Aleph::and, cleanup_free_table(), Aleph::DynArray< T >::cut(), heap_index, insert_in_free_table(), invariant(), is_valid_index(), num_pointers, pointer_table, Aleph::DynArray< T >::size(), and threshold_size.
Referenced by TEST_F(), TEST_F(), TEST_F(), and Moved_From_Case< Pointer_Table >::use().
|
inlinenoexcept |
Returns the current capacity of the pointer table.
This is the size of the underlying array, not the number of pointers currently stored.
Definition at line 395 of file pointer_table.H.
References pointer_table, and Aleph::DynArray< T >::size().
|
inlinenoexcept |
Exchanges all the state of this table and another.
| [in,out] | other | The table to exchange with. |
Definition at line 374 of file pointer_table.H.
References free_table, heap_index, num_pointers, pointer_table, Aleph::DynArray< T >::swap(), and threshold_size.
Referenced by operator=().
|
inline |
Verifies that a pointer matches the one stored at an index.
This method is useful for validating that a cached index still refers to the expected pointer, detecting use-after-free or index reuse scenarios.
| [in] | i | The index to verify. |
| [in] | ptr | The expected pointer value. |
ptr).| std::range_error | If i is out of valid range. |
| std::domain_error | If the stored pointer doesn't match ptr. |
Definition at line 617 of file pointer_table.H.
References ah_domain_error_if, ah_range_error_if, heap_index, is_valid_index(), and pointer_matches_with_index().
Referenced by TEST_F(), and Moved_From_Case< Pointer_Table >::use().
|
private |
Stack of available indices for reuse.
Definition at line 123 of file pointer_table.H.
Referenced by allocate_above_heap(), cleanup_free_table(), clear(), frees(), insert_in_free_table(), invariant(), and swap().
|
private |
Next available index at the heap top.
Definition at line 125 of file pointer_table.H.
Referenced by allocate_above_heap(), clear(), get_heap_index(), get_pointer(), insert_in_free_table(), insert_pointer(), invariant(), is_valid_index(), remove_pointer(), swap(), and verify_pointer().
|
staticconstexpr |
Sentinel value indicating an invalid or null index.
This constant is used internally to indicate that no valid index is available (e.g., when the free table is empty).
Definition at line 119 of file pointer_table.H.
Referenced by allocate_above_heap(), insert_pointer(), and TEST().
|
private |
Count of currently stored pointers.
Definition at line 124 of file pointer_table.H.
Referenced by busies(), clear(), insert_pointer(), invariant(), is_empty(), remove_pointer(), and swap().
|
private |
Array mapping indices to pointers.
Definition at line 122 of file pointer_table.H.
Referenced by allocate_above_heap(), clear(), get_pointer(), insert_in_free_table(), insert_pointer(), invariant(), pointer_matches_with_index(), remove_pointer(), size(), and swap().
|
private |
Minimum size to maintain (prevents excessive shrinking)
Definition at line 126 of file pointer_table.H.
Referenced by clear(), get_threshold(), remove_pointer(), and swap().