fix seg fault in dictionary creation
This commit is contained in:
@@ -25,26 +25,42 @@ struct hashmap_GC *createHashmap_GC() {
|
||||
t->inline_count = 0;
|
||||
return t;
|
||||
}
|
||||
void hashmap_GC_to_array(struct hashmap_GC *t, struct node_GC**array,
|
||||
size_t *array_length) {
|
||||
|
||||
static int compare_node_asc(const void *a, const void *b) {
|
||||
const struct node_GC *na = (const struct node_GC *)a;
|
||||
const struct node_GC *nb = (const struct node_GC *)b;
|
||||
|
||||
// Ascending order (smallest order first)
|
||||
if (na->order < nb->order) return -1;
|
||||
if (na->order > nb->order) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void hashmap_GC_to_array(struct hashmap_GC *t, struct node_GC **array,
|
||||
size_t *array_length) {
|
||||
size_t array_size = 8;
|
||||
*array_length = 0;
|
||||
*array = ar_alloc(array_size * sizeof(struct node_GC));
|
||||
|
||||
for (size_t i = 0; i < t->inline_count; i++) {
|
||||
if (*array_length >=array_size) {
|
||||
array_size*=2;
|
||||
*array=ar_realloc(*array, array_size * sizeof(struct node_GC));
|
||||
if (*array_length >= array_size) {
|
||||
array_size *= 2;
|
||||
*array = ar_realloc(*array, array_size * sizeof(struct node_GC));
|
||||
}
|
||||
(*array)[(*array_length)++] = t->inline_values[i];
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < t->size; i++) {
|
||||
if (!t->list[i]) continue;
|
||||
if (*array_length >=array_size) {
|
||||
array_size*=2;
|
||||
*array=ar_realloc(*array, array_size * sizeof(struct node_GC));
|
||||
if (!t->list[i])
|
||||
continue;
|
||||
if (*array_length >= array_size) {
|
||||
array_size *= 2;
|
||||
*array = ar_realloc(*array, array_size * sizeof(struct node_GC));
|
||||
}
|
||||
(*array)[(*array_length)++] = *t->list[i];
|
||||
}
|
||||
|
||||
qsort(*array, *array_length, sizeof(struct node_GC), compare_node_asc);
|
||||
}
|
||||
|
||||
void clear_hashmap_GC(struct hashmap_GC *t) {
|
||||
|
||||
Reference in New Issue
Block a user