From 9d8e712f907ed203a1d9c678060fe5c02cd606bc Mon Sep 17 00:00:00 2001 From: Doohyeon Won Date: Fri, 24 Jul 2026 17:29:06 +0900 Subject: [PATCH 1/2] fix: improve prelex logic --- hhss/c/prelex.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/hhss/c/prelex.c b/hhss/c/prelex.c index 4221ee7..34e64c4 100644 --- a/hhss/c/prelex.c +++ b/hhss/c/prelex.c @@ -7,16 +7,18 @@ extern array_t *prelex(array_t *db, int datcnt) { int lastpos, rv; ret = array_create(); - lastpos = array_size(db); + lastpos = array_size(db) - 1; /* last elem idx = len - 1 */ + datcnt--; /* the last pick won't swap */ + /* pick datcnt - 1 times */ for (int k = 0; k < datcnt; k++) { - rv = rand_range(0, lastpos - 1); /* [0, lp - 1] */ + rv = rand_range(0, lastpos); /* [0, lp] */ curr = array_get(db, rv); - last = array_get(db, lastpos - 1); + last = array_get(db, lastpos); // swap - array_set(db, lastpos - 1, curr); + array_set(db, lastpos, curr); array_set(db, rv, last); array_append(ret, curr->run, curr->len + 1); @@ -24,6 +26,9 @@ extern array_t *prelex(array_t *db, int datcnt) { // in order to prevent duplication lastpos--; } + rv = rand_range(0, lastpos); /* the last pick */ + curr = array_get(db, rv); + array_append(ret, curr->run, curr->len + 1); return ret; } From 91328202cec69a04b116c48914d97be9d16f9e82 Mon Sep 17 00:00:00 2001 From: Doohyeon Won Date: Sun, 26 Jul 2026 15:43:12 +0900 Subject: [PATCH 2/2] fix: use a new variable for clear intention --- hhss/c/prelex.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/hhss/c/prelex.c b/hhss/c/prelex.c index 34e64c4..a5899b6 100644 --- a/hhss/c/prelex.c +++ b/hhss/c/prelex.c @@ -4,14 +4,14 @@ extern array_t *prelex(array_t *db, int datcnt) { array_t *ret; line_t *curr, *last; - int lastpos, rv; + int lastpos, pickcnt, rv; ret = array_create(); lastpos = array_size(db) - 1; /* last elem idx = len - 1 */ - datcnt--; /* the last pick won't swap */ - /* pick datcnt - 1 times */ - for (int k = 0; k < datcnt; k++) { + pickcnt = datcnt - 1; /* -1 since the last pick won't swap */ + /* pick & swap (datcnt - 1) times */ + for (int k = 0; k < pickcnt; k++) { rv = rand_range(0, lastpos); /* [0, lp] */ curr = array_get(db, rv); @@ -26,7 +26,7 @@ extern array_t *prelex(array_t *db, int datcnt) { // in order to prevent duplication lastpos--; } - rv = rand_range(0, lastpos); /* the last pick */ + rv = rand_range(0, lastpos); /* the last pick (no swap) */ curr = array_get(db, rv); array_append(ret, curr->run, curr->len + 1);