|
Aleph-w 3.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. | |
| 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 110 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 303 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 137 of file pointer_table.H.
References Aleph::DynArray< T >::cut(), free_table, heap_index, Aleph::maps(), Null_Index, pointer_table, and Aleph::DynArray< T >::size().
Referenced by insert_pointer().
|
inlinenoexcept |
Returns the number of pointers currently stored.
Definition at line 332 of file pointer_table.H.
References num_pointers.
Referenced by TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
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 184 of file pointer_table.H.
References Aleph::DynArray< T >::cut(), free_table, Aleph::maps(), 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 572 of file pointer_table.H.
References Aleph::DynArray< T >::cut(), free_table, heap_index, invariant(), Aleph::maps(), num_pointers, pointer_table, and threshold_size.
|
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 344 of file pointer_table.H.
References free_table, and Aleph::DynArray< T >::size().
Referenced by 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 356 of file pointer_table.H.
References heap_index.
Referenced by TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
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 499 of file pointer_table.H.
References ah_range_error_if, heap_index, is_valid_index(), Aleph::maps(), and pointer_table.
|
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 368 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().
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 165 of file pointer_table.H.
References free_table, heap_index, Aleph::maps(), pointer_table, and Aleph::DynArray< T >::size().
Referenced by remove_pointer().
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 405 of file pointer_table.H.
References allocate_above_heap(), heap_index, invariant(), Aleph::maps(), Null_Index, num_pointers, and pointer_table.
Referenced by TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
|
inlineprivate |
Validates the internal consistency of the data structure.
Checks that all invariants hold:
true if all invariants hold, false otherwise.Definition at line 252 of file pointer_table.H.
References free_table, heap_index, Aleph::maps(), num_pointers, pointer_table, and Aleph::DynArray< T >::size().
Referenced by clear(), insert_pointer(), and remove_pointer().
|
inlinenoexcept |
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 217 of file pointer_table.H.
References heap_index, and Aleph::maps().
Referenced by get_pointer(), pointer_matches_with_index(), remove_pointer(), and verify_pointer().
|
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 233 of file pointer_table.H.
References is_valid_index(), Aleph::maps(), and pointer_table.
Referenced by verify_pointer().
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 449 of file pointer_table.H.
References ah_domain_error_if, ah_range_error_if, cleanup_free_table(), Aleph::DynArray< T >::cut(), heap_index, insert_in_free_table(), invariant(), is_valid_index(), Aleph::maps(), num_pointers, pointer_table, Aleph::DynArray< T >::size(), and threshold_size.
|
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 323 of file pointer_table.H.
References pointer_table, and Aleph::DynArray< T >::size().
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 539 of file pointer_table.H.
References ah_domain_error_if, ah_range_error_if, heap_index, is_valid_index(), Aleph::maps(), and pointer_matches_with_index().
Stack of available indices for reuse.
Definition at line 122 of file pointer_table.H.
Referenced by allocate_above_heap(), cleanup_free_table(), clear(), frees(), insert_in_free_table(), and invariant().
|
private |
Next available index at the heap top.
Definition at line 124 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(), and verify_pointer().
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 118 of file pointer_table.H.
Referenced by allocate_above_heap(), insert_pointer(), and TEST().
|
private |
Count of currently stored pointers.
Definition at line 123 of file pointer_table.H.
Referenced by busies(), clear(), insert_pointer(), invariant(), is_empty(), and remove_pointer().
Array mapping indices to pointers.
Definition at line 121 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(), and size().
|
private |
Minimum size to maintain (prevents excessive shrinking)
Definition at line 125 of file pointer_table.H.
Referenced by clear(), get_threshold(), and remove_pointer().