| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* sequence.d - sequence routines |
| 6 |
* |
| 7 |
* Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya |
| 8 |
* Copyright (c) 1990 Giuseppe Attardi |
| 9 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 10 |
* |
| 11 |
* See file 'LICENSE' for the copyright details. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <ecl/ecl.h> |
| 16 |
#include <limits.h> |
| 17 |
#include <ecl/ecl-inl.h> |
| 18 |
#include <ecl/internal.h> |
| 19 |
|
| 20 |
cl_index_pair |
| 21 |
ecl_sequence_start_end(cl_object fun, cl_object sequence, |
| 22 |
cl_object start, cl_object end) |
| 23 |
{ |
| 24 |
cl_index_pair p; |
| 25 |
cl_index l; |
| 26 |
p.length = l = ecl_length(sequence); |
| 27 |
unlikely_if (!ECL_FIXNUMP(start) || ecl_fixnum_minusp(start)) { |
| 28 |
FEwrong_type_key_arg(fun, @[:start], start, @[unsigned-byte]); |
| 29 |
} |
| 30 |
p.start = ecl_fixnum(start); |
| 31 |
if (Null(end)) { |
| 32 |
p.end = l; |
| 33 |
} else { |
| 34 |
unlikely_if (!ECL_FIXNUMP(end) || ecl_fixnum_minusp(end)) { |
| 35 |
FEwrong_type_key_arg(fun, @[:end], end, |
| 36 |
ecl_read_from_cstring("(OR NULL UNSIGNED-BYTE)")); |
| 37 |
} |
| 38 |
p.end = ecl_fixnum(end); |
| 39 |
unlikely_if (p.end > l) { |
| 40 |
cl_object fillp = ecl_make_fixnum(l); |
| 41 |
FEwrong_type_key_arg(fun, @[:end], end, |
| 42 |
ecl_make_integer_type(start, fillp)); |
| 43 |
} |
| 44 |
} |
| 45 |
unlikely_if (p.end < p.start) { |
| 46 |
FEwrong_type_key_arg(fun, @[:start], start, |
| 47 |
ecl_make_integer_type(ecl_make_fixnum(0), |
| 48 |
ecl_make_fixnum(p.end))); |
| 49 |
} |
| 50 |
return p; |
| 51 |
} |
| 52 |
|
| 53 |
cl_object |
| 54 |
si_sequence_start_end(cl_object fun, cl_object sequence, cl_object start, cl_object end) |
| 55 |
{ |
| 56 |
cl_index_pair p = ecl_sequence_start_end(fun, sequence, start, end); |
| 57 |
@(return ecl_make_fixnum(p.start) ecl_make_fixnum(p.end) |
| 58 |
ecl_make_fixnum(p.length)); |
| 59 |
} |
| 60 |
|
| 61 |
cl_object |
| 62 |
cl_elt(cl_object x, cl_object i) |
| 63 |
{ |
| 64 |
@(return ecl_elt(x, ecl_to_size(i))); |
| 65 |
} |
| 66 |
|
| 67 |
cl_object |
| 68 |
ecl_elt(cl_object seq, cl_fixnum index) |
| 69 |
{ |
| 70 |
cl_fixnum i; |
| 71 |
cl_object l; |
| 72 |
|
| 73 |
if (index < 0) |
| 74 |
goto E; |
| 75 |
switch (ecl_t_of(seq)) { |
| 76 |
case t_list: |
| 77 |
for (i = index, l = seq; i > 0; --i) { |
| 78 |
if (!LISTP(l)) goto E0; |
| 79 |
if (Null(l)) goto E; |
| 80 |
l = ECL_CONS_CDR(l); |
| 81 |
} |
| 82 |
if (!LISTP(l)) goto E0; |
| 83 |
if (Null(l)) goto E; |
| 84 |
return ECL_CONS_CAR(l); |
| 85 |
|
| 86 |
#ifdef ECL_UNICODE |
| 87 |
case t_string: |
| 88 |
#endif |
| 89 |
case t_vector: |
| 90 |
case t_bitvector: |
| 91 |
case t_base_string: |
| 92 |
if (index >= seq->vector.fillp) goto E; |
| 93 |
return ecl_aref_unsafe(seq, index); |
| 94 |
default: |
| 95 |
E0: |
| 96 |
FEtype_error_sequence(seq); |
| 97 |
} |
| 98 |
E: |
| 99 |
FEtype_error_index(seq, index); |
| 100 |
} |
| 101 |
|
| 102 |
cl_object |
| 103 |
si_elt_set(cl_object seq, cl_object index, cl_object val) |
| 104 |
{ |
| 105 |
@(return ecl_elt_set(seq, ecl_to_size(index), val)); |
| 106 |
} |
| 107 |
|
| 108 |
cl_object |
| 109 |
ecl_elt_set(cl_object seq, cl_fixnum index, cl_object val) |
| 110 |
{ |
| 111 |
cl_fixnum i; |
| 112 |
cl_object l; |
| 113 |
|
| 114 |
if (index < 0) |
| 115 |
goto E; |
| 116 |
switch (ecl_t_of(seq)) { |
| 117 |
case t_list: |
| 118 |
for (i = index, l = seq; i > 0; --i) { |
| 119 |
if (!LISTP(l)) goto E0; |
| 120 |
if (Null(l)) goto E; |
| 121 |
l = ECL_CONS_CDR(l); |
| 122 |
} |
| 123 |
if (!LISTP(l)) goto E0; |
| 124 |
if (Null(l)) goto E; |
| 125 |
ECL_RPLACA(l, val); |
| 126 |
return val; |
| 127 |
|
| 128 |
#ifdef ECL_UNICODE |
| 129 |
case t_string: |
| 130 |
#endif |
| 131 |
case t_vector: |
| 132 |
case t_bitvector: |
| 133 |
case t_base_string: |
| 134 |
if (index >= seq->vector.fillp) goto E; |
| 135 |
return ecl_aset_unsafe(seq, index, val); |
| 136 |
default: |
| 137 |
E0: |
| 138 |
FEtype_error_sequence(seq); |
| 139 |
} |
| 140 |
E: |
| 141 |
FEtype_error_index(seq, index); |
| 142 |
} |
| 143 |
|
| 144 |
cl_object |
| 145 |
ecl_subseq(cl_object sequence, cl_index start, cl_index limit) |
| 146 |
{ |
| 147 |
switch (ecl_t_of(sequence)) { |
| 148 |
case t_list: |
| 149 |
if (start) |
| 150 |
sequence = ecl_nthcdr(start, sequence); |
| 151 |
{ |
| 152 |
cl_object x = ECL_NIL; |
| 153 |
cl_object *z = &x; |
| 154 |
while (!Null(sequence) && (limit--)) { |
| 155 |
if (ECL_ATOM(sequence)) |
| 156 |
FEtype_error_cons(sequence); |
| 157 |
z = &ECL_CONS_CDR(*z = ecl_list1(ECL_CONS_CAR(sequence))); |
| 158 |
sequence = ECL_CONS_CDR(sequence); |
| 159 |
} |
| 160 |
return x; |
| 161 |
} |
| 162 |
#ifdef ECL_UNICODE |
| 163 |
case t_string: |
| 164 |
#endif |
| 165 |
case t_vector: |
| 166 |
case t_bitvector: |
| 167 |
case t_base_string: { |
| 168 |
cl_index size; |
| 169 |
cl_object x; |
| 170 |
if (start > sequence->vector.fillp) { |
| 171 |
x = ecl_alloc_simple_vector(0, ecl_array_elttype(sequence)); |
| 172 |
} else { |
| 173 |
size = sequence->vector.fillp - start; |
| 174 |
if (size > limit) |
| 175 |
size = limit; |
| 176 |
x = ecl_alloc_simple_vector(size, ecl_array_elttype(sequence)); |
| 177 |
ecl_copy_subarray(x, 0, sequence, start, size); |
| 178 |
} |
| 179 |
return x; |
| 180 |
} |
| 181 |
default: |
| 182 |
FEtype_error_sequence(sequence); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
cl_object |
| 187 |
ecl_copy_seq(cl_object sequence) |
| 188 |
{ |
| 189 |
return ecl_subseq(sequence, 0, MOST_POSITIVE_FIXNUM); |
| 190 |
} |
| 191 |
|
| 192 |
@(defun subseq (sequence start &optional end) |
| 193 |
cl_index_pair p; |
| 194 |
@ |
| 195 |
p = ecl_sequence_start_end(@[subseq], sequence, start, end); |
| 196 |
sequence = ecl_subseq(sequence, p.start, p.end - p.start); |
| 197 |
@(return sequence); |
| 198 |
@) |
| 199 |
|
| 200 |
cl_object |
| 201 |
cl_copy_seq(cl_object x) |
| 202 |
{ |
| 203 |
@(return ecl_subseq(x, 0, MOST_POSITIVE_FIXNUM)); |
| 204 |
} |
| 205 |
|
| 206 |
cl_object |
| 207 |
cl_length(cl_object x) |
| 208 |
{ |
| 209 |
@(return ecl_make_fixnum(ecl_length(x))); |
| 210 |
} |
| 211 |
|
| 212 |
cl_fixnum |
| 213 |
ecl_length(cl_object x) |
| 214 |
{ |
| 215 |
cl_fixnum i; |
| 216 |
|
| 217 |
switch (ecl_t_of(x)) { |
| 218 |
case t_list: |
| 219 |
/* INV: A list's length always fits in a fixnum */ |
| 220 |
i = 0; |
| 221 |
loop_for_in(x) { |
| 222 |
i++; |
| 223 |
} end_loop_for_in; |
| 224 |
return(i); |
| 225 |
|
| 226 |
#ifdef ECL_UNICODE |
| 227 |
case t_string: |
| 228 |
#endif |
| 229 |
case t_vector: |
| 230 |
case t_base_string: |
| 231 |
case t_bitvector: |
| 232 |
return(x->vector.fillp); |
| 233 |
|
| 234 |
default: |
| 235 |
FEtype_error_sequence(x); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
cl_object |
| 240 |
cl_reverse(cl_object seq) |
| 241 |
{ |
| 242 |
cl_object output, x; |
| 243 |
|
| 244 |
switch (ecl_t_of(seq)) { |
| 245 |
case t_list: { |
| 246 |
for (x = seq, output = ECL_NIL; !Null(x); x = ECL_CONS_CDR(x)) { |
| 247 |
if (!LISTP(x)) goto E; |
| 248 |
output = CONS(ECL_CONS_CAR(x), output); |
| 249 |
} |
| 250 |
break; |
| 251 |
} |
| 252 |
#ifdef ECL_UNICODE |
| 253 |
case t_string: |
| 254 |
#endif |
| 255 |
case t_vector: |
| 256 |
case t_bitvector: |
| 257 |
case t_base_string: |
| 258 |
output = ecl_alloc_simple_vector(seq->vector.fillp, ecl_array_elttype(seq)); |
| 259 |
ecl_copy_subarray(output, 0, seq, 0, seq->vector.fillp); |
| 260 |
ecl_reverse_subarray(output, 0, seq->vector.fillp); |
| 261 |
break; |
| 262 |
default: |
| 263 |
E: |
| 264 |
FEtype_error_sequence(seq); |
| 265 |
} |
| 266 |
@(return output); |
| 267 |
} |
| 268 |
|
| 269 |
cl_object |
| 270 |
cl_nreverse(cl_object seq) |
| 271 |
{ |
| 272 |
switch (ecl_t_of(seq)) { |
| 273 |
case t_list: { |
| 274 |
cl_object x, y, z; |
| 275 |
for (x = seq, y = ECL_NIL; !Null(x); ) { |
| 276 |
if (!LISTP(x)) FEtype_error_list(x); |
| 277 |
z = x; |
| 278 |
x = ECL_CONS_CDR(x); |
| 279 |
if (x == seq) FEcircular_list(seq); |
| 280 |
ECL_RPLACD(z, y); |
| 281 |
y = z; |
| 282 |
} |
| 283 |
seq = y; |
| 284 |
break; |
| 285 |
} |
| 286 |
#ifdef ECL_UNICODE |
| 287 |
case t_string: |
| 288 |
#endif |
| 289 |
case t_vector: |
| 290 |
case t_base_string: |
| 291 |
case t_bitvector: |
| 292 |
ecl_reverse_subarray(seq, 0, seq->vector.fillp); |
| 293 |
break; |
| 294 |
default: |
| 295 |
FEtype_error_sequence(seq); |
| 296 |
} |
| 297 |
@(return seq); |
| 298 |
} |