commit cc49809c25f641f3d1d83ecf24b104c0c9cc7e35
parent db4b47c8a31166c48e65882949b4efc8d22c5378
Author: Bor Grošelj Simić <bor.groseljsimic@telemach.net>
Date: Sun, 25 Apr 2021 15:36:40 +0200
simplify duplicate member pruning in tagged unions
Signed-off-by: Bor Grošelj Simić <bor.groseljsimic@telemach.net>
Diffstat:
1 file changed, 8 insertions(+), 10 deletions(-)
diff --git a/src/type_store.c b/src/type_store.c
@@ -361,19 +361,17 @@ tagged_cmp(const void *ptr_a, const void *ptr_b)
static size_t
tagged_init(struct type *type, struct type_tagged_union **tu, size_t nmemb)
{
+ // Sort by ID
+ qsort(tu, nmemb, sizeof(tu[0]), tagged_cmp);
+
// Prune duplicates
- for (size_t i = 1; i < nmemb; ++i)
- for (size_t j = 0; j < i; ++j) {
- if (tu[j]->type->id == tu[i]->type->id) {
- memmove(&tu[i], &tu[i + 1], (nmemb - i - 1)
- * sizeof(struct type_tagged_union *));
- --nmemb;
- break;
+ size_t nmemb_dedup = 1;
+ for (size_t i = 1; i < nmemb; ++i) {
+ if (tu[i]->type->id != tu[nmemb_dedup - 1]->type->id) {
+ tu[nmemb_dedup++] = tu[i];
}
}
-
- // Sort by ID
- qsort(tu, nmemb, sizeof(tu[0]), tagged_cmp);
+ nmemb = nmemb_dedup;
// First one free
type->tagged = *tu[0];