summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2020-06-04 19:46:43 -0700
committerKaz Kylheku <kaz@kylheku.com>2020-06-04 19:46:43 -0700
commitf7b4b05012a9e40deca1bdc72b02638d0be5a386 (patch)
tree5418bcad5bd1aed9a232ef54bba2a8d4151c7e62 /lib.c
parent18f4676d93c4601f76f28470865c2ae589cee095 (diff)
downloadtxr-f7b4b05012a9e40deca1bdc72b02638d0be5a386.tar.gz
txr-f7b4b05012a9e40deca1bdc72b02638d0be5a386.tar.bz2
txr-f7b4b05012a9e40deca1bdc72b02638d0be5a386.zip
seq_iter: null list object if rewind not needed.
* lib.c (seq_iter_init_with_info): Take new Boolean argument indicating whether the iterator needs to support the rewind operation. If this is false, and the object is a list, then we clobber the object, in order to eliminate the GC root. (seq_iter_init, iter_begin, iter_reset): Pass 0 for the new Boolean parameter. (seq_iter_init_with_rewind): New function. (diff, isec): Use seq_iter_init_with_rewind to request an iterator with rewind support for the second sequence.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/lib.c b/lib.c
index c30ed343..8ba048f3 100644
--- a/lib.c
+++ b/lib.c
@@ -503,7 +503,8 @@ void seq_iter_rewind(seq_iter_t *it)
}
static void seq_iter_init_with_info(val self, seq_iter_t *it,
- val obj, seq_info_t si)
+ val obj, seq_info_t si,
+ int support_rewind)
{
it->inf = si;
@@ -519,6 +520,8 @@ static void seq_iter_init_with_info(val self, seq_iter_t *it,
it->ul.len = 0;
it->get = seq_iter_get_list;
it->peek = seq_iter_peek_list;
+ if (!support_rewind)
+ it->inf.obj = nil;
break;
case SEQ_VECLIKE:
it->ui.index = 0;
@@ -587,7 +590,12 @@ static void seq_iter_init_with_info(val self, seq_iter_t *it,
void seq_iter_init(val self, seq_iter_t *it, val obj)
{
- seq_iter_init_with_info(self, it, obj, seq_info(obj));
+ seq_iter_init_with_info(self, it, obj, seq_info(obj), 0);
+}
+
+static void seq_iter_init_with_rewind(val self, seq_iter_t *it, val obj)
+{
+ seq_iter_init_with_info(self, it, obj, seq_info(obj), 1);
}
val seq_getpos(val self, seq_iter_t *it)
@@ -689,7 +697,7 @@ val iter_begin(val obj)
struct seq_iter *si = coerce(struct seq_iter *,
chk_calloc(1, sizeof *si));
si_obj = cobj(coerce(mem_t *, si), seq_iter_s, &seq_iter_ops);
- seq_iter_init_with_info(self, si, obj, sinf);
+ seq_iter_init_with_info(self, si, obj, sinf, 0);
return si_obj;
}
}
@@ -783,7 +791,7 @@ val iter_reset(val iter, val obj)
if (iter->co.cls == seq_iter_s)
{
struct seq_iter *si = coerce(struct seq_iter *, iter->co.handle);
- seq_iter_init_with_info(self, si, obj, sinf);
+ seq_iter_init_with_info(self, si, obj, sinf, 0);
return iter;
}
/* fallthrough */
@@ -10551,7 +10559,7 @@ val diff(val seq1, val seq2, val testfun, val keyfun)
keyfun = default_arg(keyfun, identity_f);
seq_iter_init(self, &si1, seq1);
- seq_iter_init(self, &si2, seq2);
+ seq_iter_init_with_rewind(self, &si2, seq2);
while (seq_get(&si1, &el1)) {
val el1_key = funcall1(keyfun, el1);
@@ -10658,7 +10666,7 @@ val isec(val seq1, val seq2, val testfun, val keyfun)
keyfun = default_arg(keyfun, identity_f);
seq_iter_init(self, &si1, seq1);
- seq_iter_init(self, &si2, seq2);
+ seq_iter_init_with_rewind(self, &si2, seq2);
while (seq_get(&si1, &el1)) {
val el1_key = funcall1(keyfun, el1);