Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
pointer_table.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
80# ifndef POINTER_TABLE_H
81# define POINTER_TABLE_H
82
83# include <tpl_dynArray.H>
84# include <ah-errors.H>
85# include <iostream>
86# include <utility>
87
112{
113public:
119 static constexpr long Null_Index = -1;
120
121private:
127
139 {
140 if (free_table.size() == 0)
141 return Null_Index;
142
143 long ret_val = free_table[free_table.size() - 1];
144
145 assert(ret_val >= 0 and ret_val < heap_index);
146 assert(pointer_table[ret_val] == nullptr);
147
149
150 return ret_val;
151 }
152
167 {
168 assert(i >= 0 and i < heap_index);
169
170 pointer_table[i] = nullptr;
172 }
173
185 void cleanup_free_table(long new_heap_index)
186 {
187 if (free_table.size() == 0)
188 return;
189
190 // Remove all indices >= new_heap_index from free_table
191 // We need to compact the free_table, keeping only valid indices
192 long write_pos = 0;
193 for (long read_pos = 0; read_pos < static_cast<long>(free_table.size()); ++read_pos)
194 {
195 if (free_table[read_pos] < new_heap_index)
196 {
197 if (write_pos != read_pos)
198 free_table[write_pos] = free_table[read_pos];
199 ++write_pos;
200 }
201 }
202
203 if (write_pos < static_cast<long>(free_table.size()))
204 free_table.cut(write_pos);
205 }
206
218 bool is_valid_index(long i) const noexcept
219 {
220 return i >= 0 and i < heap_index;
221 }
222
234 bool pointer_matches_with_index(long i, const void *ptr) const noexcept
235 {
236 assert(is_valid_index(i));
237 return pointer_table(i) == ptr;
238 }
239
253 bool invariant() const
254 {
255 // Basic count check
256 if (num_pointers < 0)
257 return false;
258
259 // When empty
260 if (num_pointers == 0)
261 return heap_index == 0 and free_table.size() == 0;
262
263 // Non-empty: top of heap must be non-null
264 if (heap_index <= 0 or pointer_table(heap_index - 1) == nullptr)
265 return false;
266
267 // Verify count consistency
268 if (num_pointers != heap_index - static_cast<long>(free_table.size()))
269 return false;
270
271 // Verify all free_table indices are valid and point to nullptr
272 for (size_t j = 0; j < free_table.size(); ++j)
273 {
274 long idx = free_table(j);
275 if (idx < 0 or idx >= heap_index)
276 return false;
277 if (pointer_table(idx) != nullptr)
278 return false;
279 }
280
281 return true;
282 }
283
284public:
304 Pointer_Table(size_t initial_size = 0)
305 : pointer_table(initial_size), free_table(0),
306 num_pointers(0), heap_index(0), threshold_size(static_cast<long>(initial_size))
307 {
308 // DynArray initializes to default (nullptr for pointers)
309 // No explicit initialization loop needed
310 }
311
319 Pointer_Table(const Pointer_Table & other) = default;
320
333 Pointer_Table(Pointer_Table && other) noexcept
334 : pointer_table(std::move(other.pointer_table)), free_table(std::move(other.free_table)),
335 num_pointers(std::exchange(other.num_pointers, 0)),
336 heap_index(std::exchange(other.heap_index, 0)),
337 threshold_size(std::exchange(other.threshold_size, 0))
338 {
339 // empty
340 }
341
350 Pointer_Table & operator = (const Pointer_Table & other) = default;
351
362 {
363 swap(other);
364 return *this;
365 }
366
374 void swap(Pointer_Table & other) noexcept
375 {
376 pointer_table.swap(other.pointer_table);
377 free_table.swap(other.free_table);
378 std::swap(num_pointers, other.num_pointers);
379 std::swap(heap_index, other.heap_index);
380 std::swap(threshold_size, other.threshold_size);
381 }
382
395 long size() const noexcept { return pointer_table.size(); }
396
404 long busies() const noexcept { return num_pointers; }
405
416 long frees() const noexcept { return free_table.size(); }
417
428 long get_heap_index() const noexcept { return heap_index; }
429
440 long get_threshold() const noexcept { return threshold_size; }
441
449 bool is_empty() const noexcept { return num_pointers == 0; }
450
477 long insert_pointer(void *ptr)
478 {
479 assert(invariant());
480
481 long ret_val = allocate_above_heap();
482
483 if (ret_val == Null_Index)
484 ret_val = heap_index++;
485
486 pointer_table[ret_val] = ptr;
487 ++num_pointers;
488
489 assert(invariant());
490
491 return ret_val;
492 }
493
521 void remove_pointer(long i)
522 {
523 assert(invariant());
524
525 if (not is_valid_index(i))
526 {
527 ah_range_error_if(true) << "index " << i << " out of range [0, "
528 << heap_index << ")";
529 }
530
531 if (pointer_table[i] == nullptr)
532 {
533 ah_domain_error_if(true) << "index " << i << " is not busy (already free)";
534 }
535
536 if (i == heap_index - 1)
537 {
538 // Removing from top of heap - contract the heap
539 pointer_table[heap_index - 1] = nullptr;
540
541 // Find new heap top (first non-null from the top)
542 while (heap_index > 0 and pointer_table[heap_index - 1] == nullptr)
543 --heap_index;
544
545 // CRITICAL FIX: Remove invalid indices from free_table
547 }
548 else
550
551 // Shrink pointer_table if below threshold
552 if (heap_index <= threshold_size and pointer_table.size() > static_cast<size_t>(threshold_size))
554
555 --num_pointers;
556
557 assert(invariant());
558 }
559
575 void * get_pointer(long i) const
576 {
577 if (not is_valid_index(i))
578 {
579 ah_range_error_if(true) << "index " << i << " out of range [0, "
580 << heap_index << ")";
581 }
582 return pointer_table(i);
583 }
584
617 void * verify_pointer(long i, void *ptr) const
618 {
619 if (not is_valid_index(i))
620 {
621 ah_range_error_if(true) << "index " << i << " out of range [0, "
622 << heap_index << ")";
623 }
624
625 if (not pointer_matches_with_index(i, ptr))
626 {
627 ah_domain_error_if(true) << "pointer at index " << i
628 << " does not match expected value";
629 }
630
631 return ptr;
632 }
633
654 void clear()
655 {
657 free_table.cut(0);
658 num_pointers = 0;
659 heap_index = 0;
660
661 assert(invariant());
662 }
663
664# ifdef DEBUG
680 void print_parameters() const
681 {
682 std::cout << "Number of pointers = " << num_pointers << '\n'
683 << "Pointer table size = " << pointer_table.size() << '\n'
684 << "Free table size = " << free_table.size() << '\n'
685 << "Threshold = " << threshold_size << '\n'
686 << "Heap index = " << heap_index << '\n';
687 if (heap_index > 0)
688 std::cout << "pointer_table[" << heap_index - 1 << "]= "
689 << pointer_table(heap_index - 1) << '\n';
690 }
691
696 bool check_invariant() const { return invariant(); }
697# endif
698};
699
700
701# endif // POINTER_TABLE_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
Definition ah-errors.H:356
void print_parameters()
Definition btreepic.C:2197
void swap(DynArray< T > &array) noexcept(std::is_nothrow_swappable_v< T >)
Swap in constant time array with this
void cut(const size_t new_dim=0)
Cut the array to a new dimension; that is, it reduces the dimension of array and frees the remaining ...
size_t size() const noexcept
Return the current dimension of array.
A dynamic table for managing void pointers with index recycling.
void * verify_pointer(long i, void *ptr) const
Verifies that a pointer matches the one stored at an index.
Pointer_Table & operator=(const Pointer_Table &other)=default
Copies the pointers, the free indices and the threshold of another table.
long frees() const noexcept
Returns the number of recyclable indices.
void remove_pointer(long i)
Removes the pointer at the given index.
long num_pointers
Count of currently stored pointers.
bool invariant() const
Validates the internal consistency of the data structure.
long get_threshold() const noexcept
Returns the threshold size.
long busies() const noexcept
Returns the number of pointers currently stored.
Pointer_Table(Pointer_Table &&other) noexcept
Takes the pointers and the free indices of another table, which is left empty, as Pointer_Table().
bool is_valid_index(long i) const noexcept
Checks if an index is within valid bounds.
long threshold_size
Minimum size to maintain (prevents excessive shrinking)
long allocate_above_heap()
Allocates an index from the free table.
bool is_empty() const noexcept
Checks if the table is empty.
long get_heap_index() const noexcept
Returns the current heap index (high water mark).
long size() const noexcept
Returns the current capacity of the pointer table.
static constexpr long Null_Index
Sentinel value indicating an invalid or null index.
void swap(Pointer_Table &other) noexcept
Exchanges all the state of this table and another.
DynArray< void * > pointer_table
Array mapping indices to pointers.
Pointer_Table(const Pointer_Table &other)=default
Copies the pointers, the free indices and the threshold of another table.
long insert_pointer(void *ptr)
Inserts a pointer and returns its assigned index.
DynArray< long > free_table
Stack of available indices for reuse.
bool pointer_matches_with_index(long i, const void *ptr) const noexcept
Checks if a pointer matches the one stored at an index.
void insert_in_free_table(long i)
Adds an index to the free table for future reuse.
Pointer_Table(size_t initial_size=0)
Constructs a Pointer_Table with optional initial capacity.
long heap_index
Next available index at the heap top.
void clear()
Clears all pointers from the table.
void * get_pointer(long i) const
Retrieves the pointer at the given index.
void cleanup_free_table(long new_heap_index)
Removes indices from free_table that are >= new_heap_index.
and
Check uniqueness with explicit hash + equality functors.
Lazy and scalable dynamic array implementation.