summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2017-10-13 07:01:30 -0700
committerKaz Kylheku <kaz@kylheku.com>2017-10-13 07:01:30 -0700
commit3dee4bb2ff4bb18fa2eb4540b6c7d70d487d62af (patch)
tree984c18710f5bf0f4724c077896d51449e4f8e9e0
parenta1fd714931d1149be28784f73626cb99776b2bd7 (diff)
downloadtxr-3dee4bb2ff4bb18fa2eb4540b6c7d70d487d62af.tar.gz
txr-3dee4bb2ff4bb18fa2eb4540b6c7d70d487d62af.tar.bz2
txr-3dee4bb2ff4bb18fa2eb4540b6c7d70d487d62af.zip
find_max: convert to use seq_info.
* lib.c (find_max): Sequence classification rewritten to use seq_info. The cases are almost the same, but refer to si.obj rather than seq. Some care is taken in the list case to not hold a reference to the list head.
-rw-r--r--lib.c37
1 files changed, 17 insertions, 20 deletions
diff --git a/lib.c b/lib.c
index 0968ca2f..41489315 100644
--- a/lib.c
+++ b/lib.c
@@ -8521,19 +8521,17 @@ val rfind(val item, val seq, val testfun, val keyfun)
val find_max(val seq, val testfun, val keyfun)
{
-
- seq = nullify(seq);
-
- if (!seq)
- return nil;
+ seq_info_t si = seq_info(seq);
testfun = default_arg(testfun, greater_f);
keyfun = default_arg(keyfun, identity_f);
- switch (type(seq)) {
- case COBJ:
- if (seq->co.cls == hash_s) {
- val hiter = hash_begin(seq);
+ switch (si.kind) {
+ case SEQ_NIL:
+ return nil;
+ case SEQ_HASHLIKE:
+ {
+ val hiter = hash_begin(si.obj);
val cell = hash_next(hiter);
val maxelt = cell;
val maxkey = if2(cell, funcall1(keyfun, cell));
@@ -8548,11 +8546,9 @@ val find_max(val seq, val testfun, val keyfun)
return maxelt;
}
- /* fallthrough */
- case CONS:
- case LCONS:
+ case SEQ_LISTLIKE:
{
- val maxelt = car(seq);
+ val maxelt = car(z(si.obj));
val maxkey = funcall1(keyfun, maxelt);
gc_hint(seq);
@@ -8565,28 +8561,29 @@ val find_max(val seq, val testfun, val keyfun)
maxelt = elt;
}
}
+
return maxelt;
}
- case STR:
- case LSTR:
- case LIT:
- case VEC:
+ case SEQ_VECLIKE:
{
- val maxelt = ref(seq, zero);
+ val vec = si.obj;
+ val maxelt = ref(vec, zero);
val maxkey = funcall1(keyfun, maxelt);
- val len = length(seq);
+ val len = length(vec);
val i;
for (i = zero; lt(i, len); i = succ(i)) {
- val elt = ref(seq, i);
+ val elt = ref(vec, i);
val key = funcall1(keyfun, elt);
if (funcall2(testfun, key, maxkey)) {
maxkey = key;
maxelt = elt;
}
}
+
return maxelt;
}
+ case SEQ_NOTSEQ:
default:
uw_throwf(error_s, lit("find-max: unsupported object ~s"), seq, nao);
}