/* ======================================================================= * * PRQ_Initialize * * See interface description. * * ======================================================================= */ void PRQ_Initialize( PRQ* prq, PRQ_COMPARISON_FUNCTION comparison_fn, PRQ_GET_INDEX_FUNCTION get_fn, PRQ_SET_INDEX_FUNCTION set_fn, MEM_POOL* pool, INT32 initial_size, INT32 expansion_factor ) { if ( initial_size <= 0 ) { DevWarn("Non positive priority queue initial size %d. Using 200", initial_size); initial_size = 200; } if ( expansion_factor <= 100 ) { DevWarn("Priority queue expansion factor should be at least 100. " "Was %d using 200",expansion_factor); expansion_factor = 200; } PRQ_comparison_fn(prq) = comparison_fn; PRQ_get_index_fn(prq) = get_fn; PRQ_set_index_fn(prq) = set_fn; PRQ_mem_pool(prq) = pool; PRQ_size(prq) = 0; PRQ_allocated_size(prq) = initial_size; PRQ_expansion_factor(prq) = expansion_factor; PRQ_heap_vector(prq) = TYPE_MEM_POOL_ALLOC_N(void*,pool,initial_size); }
vstring vstr_begin (int len) { if (vstr_max(current_vstring) == 0) { set_vstr_str(current_vstring, (char*) malloc(len)); set_vstr_max(current_vstring, len); } else if (vstr_len(current_vstring) != 0) { DevWarn("vstr_begin before finishing old one?\n"); set_vstr_len(current_vstring, 0); } if (vstr_max(current_vstring) < len) { Realloc_Vstring (¤t_vstring, len); } return current_vstring; }
/* ======================================================================= * * PRQ_InsertElement * * See interface description. * * ======================================================================= */ void PRQ_Insert( PRQ* prq, void* element ) { /* Grow 'heap_vector' if necessary. */ if ( PRQ_size(prq) == PRQ_allocated_size(prq) ) { INT32 new_size = (PRQ_size(prq) * PRQ_expansion_factor(prq)) / 100; if ( new_size <= PRQ_size(prq) ) { DevWarn("Priority queue expansion failed -- forcing expansion by 10"); new_size = PRQ_size(prq) + 10; } PRQ_heap_vector(prq) = TYPE_MEM_POOL_REALLOC_N(void*,PRQ_mem_pool(prq),PRQ_heap_vector(prq), PRQ_allocated_size(prq), new_size); PRQ_allocated_size(prq) = new_size; }