Skip to content

Commit

Permalink
seqmap: use an array instead of linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
DankRank committed Aug 22, 2023
1 parent fc8057b commit 4e3adcd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 37 deletions.
12 changes: 4 additions & 8 deletions thecl/eclmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,20 +236,16 @@ eclmap_rebuild(
eclmap_t *emap)
{
char **p;
size_t count = 0;
seqmap_entry_t *ent;
list_for_each(emap->ins_names, ent)
++count;
list_for_each(emap->timeline_ins_names, ent)
++count;
size_t count = emap->ins_names->len + emap->timeline_ins_names->len;

free(emap->mnem_set);
emap->mnem_set = p = malloc(count * sizeof(char*));
emap->mnem_set_len = count;

list_for_each(emap->ins_names, ent)
seqmap_entry_t *ent;
seqmap_for_each(emap->ins_names, ent)
*p++ = ent->value;
list_for_each(emap->timeline_ins_names, ent)
seqmap_for_each(emap->timeline_ins_names, ent)
*p++ = ent->value;

qsort(emap->mnem_set, emap->mnem_set_len, sizeof(char*), strcmp_indirect);
Expand Down
46 changes: 21 additions & 25 deletions util/seqmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,39 +37,39 @@
#include "program.h"
#include "util.h"

seqmap_t *
seqmap_new()
{
seqmap_t *map = malloc(sizeof(seqmap_t));
memset(map, 0, sizeof(seqmap_t));
return map;
}

void
seqmap_free(
seqmap_t *map)
{
if (!map)
return;
seqmap_entry_t *ent;

if (!map) return;

list_for_each(map, ent) {
if (ent && ent->flags & SEQMAP_FLAG_ALLOC) {
if (ent) {
free(ent->value);
}
free(ent);
}
}

list_free_nodes(map);

seqmap_for_each(map, ent)
if (ent->flags & SEQMAP_FLAG_ALLOC)
free(ent->value);
free(map->ptr);
free(map);
}

/* Allocates new seqmap entry, prepends it to a seqmap, and returns it */
/* Allocates new seqmap entry, appends it to a seqmap, and returns it */
static seqmap_entry_t *
seqmap_prepend_new(
seqmap_t *map,
int key)
{
seqmap_entry_t *ent = malloc(sizeof(seqmap_entry_t));
util_vec_ensure(&map->ptr, &map->cap, map->len+1, sizeof(seqmap_entry_t));
seqmap_entry_t *ent = &map->ptr[map->len++];
ent->key = key;
ent->value = NULL;
ent->flags = SEQMAP_FLAG_ALLOC;
list_prepend_new(map, ent);
return ent;
}

Expand All @@ -89,11 +89,9 @@ seqmap_get(
int key)
{
seqmap_entry_t *ent;
list_for_each(map, ent) {
if (ent && ent->key == key) {
seqmap_for_each(map, ent)
if (ent->key == key)
break;
}
}
return ent;
}

Expand All @@ -103,11 +101,9 @@ seqmap_find(
const char *value)
{
seqmap_entry_t *ent;
list_for_each(map, ent) {
if (ent && ent->value && !strcmp(ent->value, value)) {
seqmap_for_each(map, ent)
if (ent->value && !strcmp(ent->value, value))
break;
}
}
return ent;
}

Expand Down
15 changes: 12 additions & 3 deletions util/seqmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include "list.h"
#include "util.h"

#define SEQMAP_FLAG_ALLOC (1<<0)
typedef struct seqmap_entry_t {
Expand All @@ -43,7 +43,11 @@ typedef struct seqmap_entry_t {
char *value;
} seqmap_entry_t;

typedef list_t seqmap_t;
typedef struct seqmap_t {
seqmap_entry_t *ptr;
size_t len;
size_t cap;
} seqmap_t;

typedef int (*seqmap_setfunc_t)(
void *state,
Expand All @@ -54,8 +58,13 @@ typedef int (*seqmap_controlfunc_t)(
int linenum,
const char *cline);

/* Iterates through the data in the seqmap. */
#define seqmap_for_each(map, var) \
for (size_t node = (map)->len; \
(void)((var) = node ? &(map)->ptr[node-1] : NULL), node; \
node--)
/* Allocates and initalizes a new seqmap */
#define seqmap_new() ((seqmap_t*)list_new())
seqmap_t *seqmap_new(void);
/* Frees a seqmap */
void seqmap_free(seqmap_t *map);
/* Sets an entry in a seqmap */
Expand Down
2 changes: 1 addition & 1 deletion util/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ util_makepath(
}

/* Typical amortized O(1) dynamic array resize function */
static int
int
util_vec_ensure(
void *data, /* It's actually void **. This is done to avoid casts. */
size_t *cap,
Expand Down
7 changes: 7 additions & 0 deletions util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ const char* util_printfloat(
void util_makepath(
const char* path);

/* Typical amortized O(1) dynamic array resize function */
int util_vec_ensure(
void *data, /* It's actually void **. This is done to avoid casts. */
size_t *cap,
size_t size,
size_t element_size);

/* Scan directories recursively. After use, result should be freed manually. */
int util_scan_files(
const char* dir,
Expand Down

0 comments on commit 4e3adcd

Please sign in to comment.