diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2016-10-13 06:21:11 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2016-10-13 06:21:11 -0700 |
commit | 6eac936ebc1626ed3e27f685477d82a0f4e558d1 (patch) | |
tree | 1b68a4860c429561526df35df51edc3dad9004a2 | |
parent | 1c11fe21f49559fbb0f2ee8fa9b702338207e7a0 (diff) | |
download | txr-6eac936ebc1626ed3e27f685477d82a0f4e558d1.tar.gz txr-6eac936ebc1626ed3e27f685477d82a0f4e558d1.tar.bz2 txr-6eac936ebc1626ed3e27f685477d82a0f4e558d1.zip |
Let some sequence functions work on structs.
* lib.c (in, sub, ref, search, rsearch, sel): These
functions now accept struct objects that have the
nullify, car and cdr methods.
-rw-r--r-- | lib.c | 60 |
1 files changed, 32 insertions, 28 deletions
@@ -8054,6 +8054,13 @@ val in(val seq, val item, val testfun, val keyfun) switch (type(seq)) { case NIL: return nil; + case COBJ: + if (seq->co.cls == hash_s) { + val found; + gethash_f(seq, item, mkcloc(found)); + return if3(found, t, nil); + } + /* fallthrough */ case CONS: case LCONS: { @@ -8110,13 +8117,6 @@ val in(val seq, val item, val testfun, val keyfun) return nil; } - case COBJ: - if (seq->co.cls == hash_s) { - val found; - gethash_f(seq, item, mkcloc(found)); - return if3(found, t, nil); - } - /* fallthrough */ default: type_mismatch(lit("in: ~s is not a sequence or hash"), seq, nao); } @@ -8239,6 +8239,7 @@ val sub(val seq, val from, val to) switch (type(seq)) { case NIL: return nil; + case COBJ: case CONS: case LCONS: return sub_list(seq, from, to); @@ -8258,6 +8259,10 @@ val ref(val seq, val ind) switch (type(seq)) { case NIL: return nil; + case COBJ: + if (seq->co.cls == hash_s) + return gethash(seq, ind); + /* fallthrough */ case CONS: case LCONS: return listref(seq, ind); @@ -8267,9 +8272,6 @@ val ref(val seq, val ind) return chr_str(seq, ind); case VEC: return vecref(seq, ind); - case COBJ: - if (seq->co.cls == hash_s) - return gethash(seq, ind); default: type_mismatch(lit("ref: ~s is not a sequence"), seq, nao); } @@ -8455,6 +8457,7 @@ val search(val seq, val key, val testfun, val keyfun) case STR: case LSTR: case VEC: + case COBJ: /* TODO: optimize me */ return search_list(seq, key, testfun, keyfun); default: @@ -8523,6 +8526,7 @@ val rsearch(val seq, val key, val testfun, val keyfun) case STR: case LSTR: case VEC: + case COBJ: /* TODO: optimize me */ return rsearch_list(seq, key, testfun, keyfun); default: @@ -8575,25 +8579,8 @@ val sel(val seq_in, val where_in) switch (type(seq)) { case NIL: return nil; - case CONS: - case LCONS: - { - val idx = zero; - - for (; seq && where; seq = cdr(seq), idx = plus(idx, one)) { - val wh = nil; - - for (; where && lt(wh = car(where), idx); where = cdr(where)) - ; /* empty */ - - if (eql(wh, idx)) - ptail = list_collect (ptail, car(seq)); - } - } - break; case COBJ: - if (!hashp(seq)) - type_mismatch(lit("select: ~s is not a sequence or hash"), seq, nao); + if (hashp(seq)) { val newhash = make_similar_hash(seq); @@ -8609,6 +8596,23 @@ val sel(val seq_in, val where_in) return newhash; } + /* fallthrough */ + case CONS: + case LCONS: + { + val idx = zero; + + for (; seq && where; seq = cdr(seq), idx = plus(idx, one)) { + val wh = nil; + + for (; where && lt(wh = car(where), idx); where = cdr(where)) + ; /* empty */ + + if (eql(wh, idx)) + ptail = list_collect (ptail, car(seq)); + } + } + break; default: { val len = length(seq); |