Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions hhss/c/prelex.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,31 @@
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);
lastpos = array_size(db) - 1; /* last elem idx = len - 1 */

for (int k = 0; k < datcnt; k++) {
rv = rand_range(0, lastpos - 1); /* [0, lp - 1] */
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);
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);

// in order to prevent duplication
lastpos--;
}
rv = rand_range(0, lastpos); /* the last pick (no swap) */
curr = array_get(db, rv);
array_append(ret, curr->run, curr->len + 1);

return ret;
}
Expand Down
Loading