diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2020-05-13 18:03:39 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2020-05-13 18:03:39 -0700 |
commit | 0786f1422cf0a949fb0e0daf98852123ced19c9a (patch) | |
tree | 5f0c481309f34702a5c97f77d01fcda940c911df | |
parent | 74c257cc189dff497bf92d6652a95031011aa8f2 (diff) | |
download | txr-0786f1422cf0a949fb0e0daf98852123ced19c9a.tar.gz txr-0786f1422cf0a949fb0e0daf98852123ced19c9a.tar.bz2 txr-0786f1422cf0a949fb0e0daf98852123ced19c9a.zip |
lib: use seq-info for sort and shuffle.
* lib.c (sort, shuffle): Switch to seq_info. For consistency
with sort, shuffle now handles hashes in the same peculiar
way.
* txr.1: Document hash behavior for sort and shuffle.
-rw-r--r-- | lib.c | 44 | ||||
-rw-r--r-- | txr.1 | 20 |
2 files changed, 45 insertions, 19 deletions
@@ -8867,31 +8867,38 @@ static void sort_vec(val vec, val lessfun, val keyfun) quicksort(vec, lessfun, keyfun, 0, len); } -val sort(val seq_in, val lessfun, val keyfun) +val sort(val seq, val lessfun, val keyfun) { - val seq_orig = seq_in; - val seq = nullify(seq_in); - - if (!seq) - return make_like(nil, seq_orig); + val self = lit("sort"); + seq_info_t si = seq_info(seq); keyfun = default_arg(keyfun, identity_f); lessfun = default_arg(lessfun, less_f); - if (consp(seq)) + switch (si.kind) { + case SEQ_NIL: + return nil; + case SEQ_VECLIKE: + case SEQ_HASHLIKE: + sort_vec(seq, lessfun, keyfun); + return seq; + case SEQ_LISTLIKE: return sort_list(seq, lessfun, keyfun); + case SEQ_NOTSEQ: + unsup_obj(self, seq); + } - sort_vec(seq, lessfun, keyfun); - return seq; + abort(); } val shuffle(val seq) { - switch (type(seq)) { - case NIL: + seq_info_t si = seq_info(seq); + + switch (si.kind) { + case SEQ_NIL: return nil; - case CONS: - case LCONS: + case SEQ_LISTLIKE: if (cdr(seq)) { val v = shuffle(vec_list(seq)); @@ -8901,11 +8908,8 @@ val shuffle(val seq) rplaca(l, ref(v, i)); } return seq; - case LIT: - uw_throwf(error_s, lit("shuffle: ~s is a literal"), seq, nao); - case STR: - case LSTR: - case VEC: + case SEQ_VECLIKE: + case SEQ_HASHLIKE: { val rs = random_state; val n = length(seq); @@ -8923,9 +8927,11 @@ val shuffle(val seq) return seq; } - default: + case SEQ_NOTSEQ: type_mismatch(lit("shuffle: ~s is not a sequence"), seq, nao); } + + abort(); } static val multi_sort_less(val funcs_cons, val llist, val rlist) @@ -32602,6 +32602,19 @@ For strings and vectors, .code sort is not stable. +The +.code sort +function can be applied to hashes. It produces meaningful behavior +for a hash table which contains +.I N +keys which are the integers from 0 to +.IR "N - 1" . +Such as hash is treated as if it were a vector. The values are sorted +and re-assigned to sorted order to the integer keys. +The behavior of +.code sort +is not specified for hashes whose contents do not conform to this convention. + .coNP Function @ grade .synb .mets (grade < sequence >> [ lessfun <> [ keyfun ]]) @@ -32668,6 +32681,13 @@ The rearrangement depends on pseudo-random numbers obtained from the .code rand function. +The +.code shuffle +function supports hash tables in a manner analogous to the way +.code sort +supports hash tables; the same remarks apply as in the description +of that function. + .coNP Function @ sort-group .synb .mets (sort-group < sequence >> [ keyfun <> [ lessfun ]]) |