use the first element in the table

before 0 was used as a sentinel for checking if an element
was used, so table[0] could not be used.
This commit is contained in:
Sébastien Loriot 2022-04-06 12:02:12 +02:00
parent 157658848b
commit e059d68e94
1 changed files with 6 additions and 8 deletions

View File

@ -37,8 +37,7 @@ class chained_map_elem
template <typename T, typename Allocator> template <typename T, typename Allocator>
class chained_map class chained_map
{ {
static constexpr std::size_t nullptrKEY = 0; static constexpr std::size_t nullptrKEY = (std::numeric_limits<std::size_t>::max)();
static constexpr std::size_t NONnullptrKEY = 1;
chained_map_elem<T>* table; chained_map_elem<T>* table;
chained_map_elem<T>* table_end; chained_map_elem<T>* table_end;
@ -146,7 +145,6 @@ void chained_map<T, Allocator>::init_table(std::size_t n)
{ p->succ = nullptr; { p->succ = nullptr;
p->k = nullptrKEY; p->k = nullptrKEY;
} }
table->k = NONnullptrKEY;
} }
@ -178,7 +176,7 @@ void chained_map<T, Allocator>::rehash()
chained_map_item p; chained_map_item p;
for(p = old_table + 1; p < old_table_mid; p++) // the first element is never used (nullptrKEY vs NONnullptrKEY) for(p = old_table; p < old_table_mid; p++)
{ std::size_t x = p->k; { std::size_t x = p->k;
if ( x != nullptrKEY ) // list p is non-empty if ( x != nullptrKEY ) // list p is non-empty
{ chained_map_item q = HASH(x); { chained_map_item q = HASH(x);
@ -243,7 +241,7 @@ chained_map<T, Allocator>::chained_map(const chained_map<T, Allocator>& D)
{ {
init_table(D.table_size); init_table(D.table_size);
for(chained_map_item p = D.table + 1; p < D.free; p++) for(chained_map_item p = D.table; p < D.free; p++)
{ if (p->k != nullptrKEY || p >= D.table + D.table_size) { if (p->k != nullptrKEY || p >= D.table + D.table_size)
{ insert(p->k,p->i); { insert(p->k,p->i);
//D.copy_inf(p->i); // see chapter Implementation //D.copy_inf(p->i); // see chapter Implementation
@ -263,7 +261,7 @@ chained_map<T, Allocator>& chained_map<T, Allocator>::operator=(const chained_ma
init_table(D.table_size); init_table(D.table_size);
for(chained_map_item p = D.table + 1; p < D.free; p++) for(chained_map_item p = D.table; p < D.free; p++)
{ if (p->k != nullptrKEY || p >= D.table + D.table_size) { if (p->k != nullptrKEY || p >= D.table + D.table_size)
{ insert(p->k,p->i); { insert(p->k,p->i);
//copy_inf(p->i); // see chapter Implementation //copy_inf(p->i); // see chapter Implementation
@ -285,7 +283,7 @@ void chained_map<T, Allocator>::clear_entries()
if(!table) if(!table)
return; return;
for(chained_map_item p = table + 1; p < free; p++) for(chained_map_item p = table; p < free; p++)
if (p->k != nullptrKEY || p >= table + table_size) if (p->k != nullptrKEY || p >= table + table_size)
p->i = T(); p->i = T();
} }
@ -322,7 +320,7 @@ template <typename T, typename Allocator>
void chained_map<T, Allocator>::statistics() const void chained_map<T, Allocator>::statistics() const
{ std::cout << "table_size: " << table_size <<"\n"; { std::cout << "table_size: " << table_size <<"\n";
std::size_t n = 0; std::size_t n = 0;
for (chained_map_item p = table + 1; p < table + table_size; p++) for (chained_map_item p = table; p < table + table_size; p++)
if (p ->k != nullptrKEY) n++; if (p ->k != nullptrKEY) n++;
std::size_t used_in_overflow = free - (table + table_size ); std::size_t used_in_overflow = free - (table + table_size );
n += used_in_overflow; n += used_in_overflow;