31 assert( POLAR_IS_HASH_TABLE(self) );
33 POLAR_HASH_TABLE_GET_CLASS(self)->insert_node(self, node_new);
39 assert( POLAR_IS_HASH_TABLE(self) );
42 return ( (self->count_entries + ((self->mask_buckets + 1) >> 2)) >= (self->mask_buckets + 1) );
48 assert( POLAR_IS_HASH_TABLE(self) );
50 if( self->count_entries > 0 )
56 self->array_buckets = NULL;
63 assert( POLAR_IS_HASH_TABLE(self) );
64 assert( idx_bucket >= 0 );
66 if( (self->array_buckets != NULL) && (idx_bucket <= self->mask_buckets) )
75 assert( idx_bucket >= 0 );
77 if( self->array_buckets != NULL )
78 return (
polar_hash_table_node *)polar_paged_array_swap_element(self->array_buckets, idx_bucket, node_new);
80 else if ( (idx_bucket <= self->mask_buckets) && (node_new != NULL) )
82 self->array_buckets = polar_paged_array_new(NULL);
83 polar_paged_array_set_element(self->array_buckets, idx_bucket, node_new);
89POLAR_FUNCTION_INTERNAL
void
94 assert( POLAR_IS_HASH_TABLE(self) );
95 assert( idx_bucket >= 0 );
97 if( idx_bucket <= self->mask_buckets )
103 if( self->array_buckets != NULL )
106 else if( POLAR_IS_HASH_TABLE_NODE(node_new) )
107 self->array_buckets = polar_paged_array_new(NULL);
113 polar_paged_array_swap_element(self->array_buckets, idx_bucket, node_new);
116POLAR_FUNCTION_INTERNAL
void
121 assert( POLAR_IS_HASH_TABLE(self) );
122 assert( idx_bucket >= 0 );
123 assert( idx_bucket <= self->mask_buckets );
125 if( self->array_buckets != NULL )
127 node_current = (
polar_list_node *)polar_paged_array_swap_element(self->array_buckets, idx_bucket, NULL);
128 self->count_entries -= polar_list_node_list_free( &node_current );
132POLAR_FUNCTION_INTERNAL
void
135 intptr_t count_buckets_new, mask_buckets_new;
141 assert( POLAR_IS_HASH_TABLE(self) );
143 if( self->array_buckets != NULL )
149 count_buckets_new = ( (self->mask_buckets + 1) << 1 );
150 mask_buckets_new = ( count_buckets_new - 1 );
152 array_buckets_new = polar_paged_array_new(NULL);
154 for( i = 0; i <= self->mask_buckets; i++ )
158 while( node_current != NULL )
162 idx_new = ( polar_hash_table_node_get_hash_value(node_current) & mask_buckets_new );
165 polar_paged_array_swap_element(array_buckets_new, idx_new, node_current);
167 node_current = node_next;
173 self->array_buckets = array_buckets_new;
174 self->mask_buckets = mask_buckets_new;
188 self->id_node_type = POLAR_TYPE_HASH_TABLE_NODE;
197 polar_hash_table_clear(self);
200 return POLAR_RUNTIME_OBJECT_CLASS(superclass_polar_hash_table)->finalize( (
polar_runtime_object *)self );
203POLAR_FUNCTION_INTERNAL
void
208 assert( POLAR_IS_HASH_TABLE_NODE(node_new) );
209 assert( polar_hash_table_has_node(self, polar_hash_table_node_get_key(node_new)) == NO );
211 if( polar_hash_table_must_grow(self) == NO )
215 polar_hash_table_grow(self);
217 idx_bucket = ( polar_hash_table_node_get_hash_value(node_new) & self->mask_buckets );
218 polar_hash_table_set_bucket(self, idx_bucket, node_new);
220 self->count_entries++;
223POLAR_FUNCTION_INTERNAL
void
227 superclass_polar_hash_table = POLAR_RUNTIME_OBJECT_CLASS(self)->class_superclass;
233 self->insert_node = _polar_hash_table_insert_node;
238 .name_type =
"polar_hash_table",
241 .type_class_init = (polar_func_runtime_object_class_lifecycle)_polar_hash_table_class_init
246POLAR_FUNCTION_INTERNAL OBJC_FUNCTION_CONSTANT polar_internal_type
247polar_hash_table_get_type(
void )
249 static polar_internal_type pt_polar_hash_table = POLAR_INTERNAL_TYPE_INVALID;
251 if( pt_polar_hash_table != POLAR_INTERNAL_TYPE_INVALID )
252 return pt_polar_hash_table;
254 pt_polar_hash_table = polar_internal_type_init( POLAR_TYPE_RUNTIME_OBJECT, &class_polar_hash_table,
255 &pti_polar_hash_table );
257 return pt_polar_hash_table;
261polar_hash_table_new( intptr_t count_buckets_initial, polar_internal_type id_node_type )
265 assert( count_buckets_initial >= 2 );
266 assert( (count_buckets_initial & (count_buckets_initial - 1)) == 0 );
267 assert( polar_internal_type_is_a(id_node_type, POLAR_TYPE_HASH_TABLE_NODE) );
269 result = (
polar_hash_table *)polar_runtime_object_new(POLAR_TYPE_HASH_TABLE);
270 result->mask_buckets = ( count_buckets_initial - 1 );
271 result->id_node_type = id_node_type;
282 char node_memory[512];
284 assert( POLAR_IS_HASH_TABLE(self) );
287 polar_runtime_object_preallocated_init( &node_memory[0], self->id_node_type, YES );
289 polar_hash_table_node_set_key(node_with_key_desired, key_desired);
291 idx_bucket = ( polar_hash_table_node_get_hash_value(node_with_key_desired) & self->mask_buckets );
293 result = polar_hash_table_get_bucket(self, idx_bucket);
294 while( result != NULL )
296 if( polar_hash_table_node_is_equal_to(result, node_with_key_desired) == NO )
303 polar_runtime_object_preallocated_finalize(node_with_key_desired);
314 polar_hash_table_node_set_key(result, key_desired);
316 polar_hash_table_insert_node(self, result);
326 assert( POLAR_IS_HASH_TABLE(self) );
328 node_with_key_desired = (
polar_hash_table_node *)alloca( polar_internal_type_get_instance_size(self->id_node_type) );
329 polar_runtime_object_preallocated_init(node_with_key_desired, self->id_node_type, YES);
330 polar_hash_table_node_set_key(node_with_key_desired, key_desired);
332 node_previous = NULL;
333 idx_bucket = ( polar_hash_table_node_get_hash_value(node_with_key_desired) & self->mask_buckets );
335 result = polar_hash_table_get_bucket(self, idx_bucket);
336 while( result != NULL )
338 if( polar_hash_table_node_is_equal_to(result, node_with_key_desired) == NO )
340 node_previous = result;
346 if( node_previous != NULL )
347 POLAR_LIST_NODE(node_previous)->node_next = POLAR_LIST_NODE(result)->node_next;
350 polar_paged_array_set_element(self->array_buckets, idx_bucket, ((
polar_list_node *)result)->node_next);
352 POLAR_LIST_NODE(result)->node_next = NULL;
353 self->count_entries--;
355 polar_hash_table_maybe_free_buckets(self);
360 polar_runtime_object_preallocated_finalize(node_with_key_desired);
365POLAR_FUNCTION_INTERNAL
void
370 assert( POLAR_IS_HASH_TABLE(self) );
372 if( self->count_entries > 0 )
374 for( i = 0; i <= self->mask_buckets; i++ )
375 polar_hash_table_empty_bucket(self, i);
377 self->count_entries = 0;
380 polar_hash_table_maybe_free_buckets(self);
Definition polar-hash-table.h:34
Definition polar-hash-table-node.h:25
Definition polar-hash-table.h:25
Definition polar-internal-type.h:34
Definition polar-list-node.h:25
Definition polar-paged-array.h:25
Definition polar-runtime-object.h:36
Definition polar-runtime-object.h:31