sequence.c 9.97 KB
Newer Older
1
2
3
4
5
6
7
8
/* -*- mode: c -*- */
/*
    sequence.d -- Sequence routines.
*/
/*
    Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
    Copyright (c) 1990, Giuseppe Attardi.
    Copyright (c) 2001, Juan Jose Garcia Ripoll.
9
    Copyright (c) 2011-2012,2021, Jean-Claude Beaudoin.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

    MKCL is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3 of the License, or (at your option) any later version.

    See file '../../Copyright' for full details.
*/

#include <mkcl/mkcl.h>
#include <mkcl/internal.h>
#include <mkcl/mkcl-inl.h>


mkcl_object
mk_cl_elt(MKCL, mkcl_object x, mkcl_object i)
{
  mkcl_call_stack_check(env);
28
  mkcl_return_value(mkcl_elt(env, x, mkcl_integer_to_word(env, i)));
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
}

mkcl_object
mkcl_elt(MKCL, mkcl_object seq, mkcl_word index)
{
  mkcl_word i;
  mkcl_object l;

  if (index < 0)
    goto E;
  if (mkcl_Null(seq)) goto E;
  switch (mkcl_type_of(seq))
    {
    case mkcl_t_cons:
      for (i = index, l = seq;  i > 0;  --i) {
	if (!MKCL_LISTP(l)) goto E0;
	if (mkcl_Null(l)) goto E;
	l = MKCL_CONS_CDR(l);
      }
      if (!MKCL_LISTP(l)) goto E0;
      if (mkcl_Null(l)) goto E;
      return MKCL_CONS_CAR(l);

    case mkcl_t_string:
      if (index >= seq->string.fillp)
	goto E;
      return(MKCL_CODE_CHAR(seq->string.self[index]));

    case mkcl_t_vector:
    case mkcl_t_bitvector:
      if (index >= seq->vector.fillp)
	goto E;
      return(mkcl_aref_index(env, seq, index));

    case mkcl_t_base_string:
      if (index >= seq->base_string.fillp)
	goto E;
      return(MKCL_CODE_CHAR(seq->base_string.self[index]));

    default:
    E0:
      mkcl_FEtype_error_sequence(env, seq);
    }
 E:
  mkcl_FEtype_error_seq_index(env, seq, MKCL_MAKE_FIXNUM(index));
}

mkcl_object
mk_si_elt_set(MKCL, mkcl_object seq, mkcl_object index, mkcl_object val)
{
  mkcl_call_stack_check(env);
80
  mkcl_return_value(mkcl_elt_set(env, seq, mkcl_integer_to_word(env, index), val));
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
}

mkcl_object
mkcl_elt_set(MKCL, mkcl_object seq, mkcl_word index, mkcl_object val)
{
  mkcl_word i;
  mkcl_object l;

  if (index < 0)
    goto E;
  if (mkcl_Null(seq)) goto E;
  switch (mkcl_type_of(seq))
    {
    case mkcl_t_cons:
      for (i = index, l = seq;  i > 0;  --i) {
	if (!MKCL_LISTP(l)) goto E0;
	if (mkcl_Null(l)) goto E;
	l = MKCL_CONS_CDR(l);
      }
      if (!MKCL_LISTP(l)) goto E0;
      if (mkcl_Null(l)) goto E;
      MKCL_RPLACA(l, val);
      return val;

    case mkcl_t_string:
      if (index >= seq->string.fillp)
	goto E;
      /* INV: mkcl_char_code() checks the type of `val' */
      seq->string.self[index] = mkcl_char_code(env, val);
      return(val);

    case mkcl_t_vector:
    case mkcl_t_bitvector:
      if (index >= seq->vector.fillp)
	goto E;
      return(mkcl_aset_index(env, seq, index, val));

    case mkcl_t_base_string:
      if (index >= seq->base_string.fillp)
	goto E;
      /* INV: mkcl_char_code() checks the type of `val' */
122
      seq->base_string.self[index] = mkcl_base_char_code(env, val);
123
124
125
126
127
128
129
130
131
132
      return(val);

    default:
    E0:
      mkcl_FEtype_error_sequence(env, seq);
    }
 E:
  mkcl_FEtype_error_seq_index(env, seq, MKCL_MAKE_FIXNUM(index));
}

133
134
135
136
137
138
139
140
141
mkcl_object mk_cl_subseq(MKCL, mkcl_narg narg, mkcl_object sequence, mkcl_object start, ...)
{
  mkcl_call_stack_check(env);
  {

    mkcl_object x = mk_cl_Cnil;
    mkcl_word s, e;
    mkcl_word i;
    mkcl_object end = mk_cl_Cnil;
142
    MKCL_RECEIVE_1_OPTIONAL_ARGUMENT(env, MK_CL_listen, narg, 2, start, &end);
143
144
145
146
147
148

    s = mkcl_integer_to_index(env, start);
    if (mkcl_Null(end))
      e = -1;
    else
      e = mkcl_integer_to_index(env, end);
149
  
150
151
152
153
154
155
    if (mkcl_Null(sequence)) {
      if (s > 0)
        goto ILLEGAL_START_END;
      if (e > 0)
        goto ILLEGAL_START_END;
      mkcl_return_value(mk_cl_Cnil);
156
    }
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    switch (mkcl_type_of(sequence))
      {
      case mkcl_t_cons:
        if (e >= 0)
          if ((e -= s) < 0)
            goto ILLEGAL_START_END;
        while (s-- > 0) {
          if (MKCL_ATOM(sequence))
            goto ILLEGAL_START_END;
          sequence = MKCL_CDR(sequence);
        }
        if (e < 0)
          return mk_cl_copy_list(env, sequence);
        { mkcl_object *z = &x;
          for (i = 0;  i < e;  i++) {
            if (MKCL_ATOM(sequence))
              goto ILLEGAL_START_END;
            z = &MKCL_CONS_CDR(*z = mkcl_list1(env, MKCL_CAR(sequence)));
            sequence = MKCL_CDR(sequence);
          }
        }
        mkcl_return_value(x);

      case mkcl_t_string:
        if (s > sequence->string.fillp)
          goto ILLEGAL_START_END;
        if (e < 0)
          e = sequence->string.fillp;
        else if (e < s || e > sequence->string.fillp)
          goto ILLEGAL_START_END;
        x = mkcl_alloc_simple_vector(env, e - s, mkcl_array_elttype(env, sequence));
        mkcl_copy_subarray(env, x, 0, sequence, s, e-s);
        mkcl_return_value(x);

      case mkcl_t_vector:
      case mkcl_t_bitvector:
        if (s > sequence->vector.fillp)
          goto ILLEGAL_START_END;
        if (e < 0)
          e = sequence->vector.fillp;
        else if (e < s || e > sequence->vector.fillp)
          goto ILLEGAL_START_END;
        x = mkcl_alloc_simple_vector(env, e - s, mkcl_array_elttype(env, sequence));
        mkcl_copy_subarray(env, x, 0, sequence, s, e-s);
        mkcl_return_value(x);
      case mkcl_t_base_string:
        if (s > sequence->base_string.fillp)
          goto ILLEGAL_START_END;
        if (e < 0)
          e = sequence->base_string.fillp;
        else if (e < s || e > sequence->base_string.fillp)
          goto ILLEGAL_START_END;
        x = mkcl_alloc_simple_vector(env, e - s, mkcl_array_elttype(env, sequence));
        mkcl_copy_subarray(env, x, 0, sequence, s, e-s);
        mkcl_return_value(x);
      
      default:
        mkcl_FEtype_error_sequence(env, sequence);
      }
216

217
218
219
220
221
222
  ILLEGAL_START_END:
    mkcl_FEerror(env,
                 "~S and ~S are illegal as :START and :END~%"
                 "for the sequence ~S.", 3, start, end, sequence);
  }
}
223
224
225
226
227

mkcl_object
mk_cl_copy_seq(MKCL, mkcl_object x)
{
  mkcl_call_stack_check(env);
228
  return mk_cl_subseq(env, 2, x, MKCL_MAKE_FIXNUM(0));
229
230
231
232
233
234
}

mkcl_object
mk_cl_length(MKCL, mkcl_object x)
{
  mkcl_call_stack_check(env);
235
  mkcl_return_value(mkcl_make_unsigned_integer(env, mkcl_length(env, x)));
236
237
238
239
240
241
242
243
244
}

mkcl_index
mkcl_length(MKCL, mkcl_object x)
{
  if ( mkcl_Null(x) ) return(0);
  else
    switch (mkcl_type_of(x))
      {
245
246
247
248
249
      case mkcl_t_cons: {
	register mkcl_index i = 0; /* INV: A list's length always fits in a fixnum */

#if 0 
	mkcl_loop_for_in(env, x) { /* This is the unsafe version (on circular list). JCB */
250
251
	  i++;
	} mkcl_end_loop_for_in;
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#else
        {
          register mkcl_object fast, slow;
          
          fast = slow = x;
          for (i = 0; !mkcl_Null(fast); i++, fast = MKCL_CONS_CDR(fast)) {
            if (!MKCL_CONSP(fast)) {
              mkcl_FEtype_error_proper_sequence(env, x);
            }
            if (slow == fast && i != 0)
              mkcl_FEtype_error_proper_sequence(env, x); /* Circular list! */
            if (i & 1) { /* move only on odd beat. */
              slow = MKCL_CONS_CDR(slow);
            }
          }
        }
#endif
        return(i);
      }
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
      case mkcl_t_string:
	return(x->string.fillp);
      case mkcl_t_base_string:
	return(x->base_string.fillp);
      case mkcl_t_vector:
      case mkcl_t_bitvector:
	return(x->vector.fillp);

      default:
	mkcl_FEtype_error_sequence(env, x);
      }
}

mkcl_index mkcl_base_string_length(MKCL, mkcl_object x)
{
  if (MKCL_BASE_STRING_P(x))
    return(x->base_string.fillp);
  else
    mkcl_FEtype_error_base_string(env, x);
}

mkcl_index mkcl_string_length(MKCL, mkcl_object x)
{
  if (MKCL_BASE_STRING_P(x))
    return(x->base_string.fillp);
  else if (MKCL_STRINGP(x))
    return(x->string.fillp);
  else
    mkcl_FEtype_error_string(env, x);
}


mkcl_object
mk_cl_reverse(MKCL, mkcl_object seq)
{
  mkcl_object output, x;

  if ( mkcl_Null(seq) ) return(mk_cl_Cnil);
  mkcl_call_stack_check(env);

  switch (mkcl_type_of(seq))
    {
    case mkcl_t_cons: {
      for (x = seq, output = mk_cl_Cnil; !mkcl_Null(x); x = MKCL_CONS_CDR(x)) {
	if (!MKCL_LISTP(x)) goto E;
	output = MKCL_CONS(env, MKCL_CONS_CAR(x), output);
      }
      break;
    }
    case mkcl_t_string:
      output = mkcl_alloc_simple_vector(env, seq->string.fillp, mkcl_array_elttype(env, seq));
      mkcl_copy_subarray(env, output, 0, seq, 0, seq->string.fillp);
      mkcl_reverse_subarray(env, output, 0, seq->string.fillp);
      break;
    case mkcl_t_base_string:
      output = mkcl_alloc_simple_vector(env, seq->base_string.fillp, mkcl_array_elttype(env, seq));
      mkcl_copy_subarray(env, output, 0, seq, 0, seq->base_string.fillp);
      mkcl_reverse_subarray(env, output, 0, seq->base_string.fillp);
      break;
    case mkcl_t_vector:
    case mkcl_t_bitvector:
      output = mkcl_alloc_simple_vector(env, seq->vector.fillp, mkcl_array_elttype(env, seq));
      mkcl_copy_subarray(env, output, 0, seq, 0, seq->vector.fillp);
      mkcl_reverse_subarray(env, output, 0, seq->vector.fillp);
      break;
    default:
    E:
      mkcl_FEtype_error_sequence(env, seq);
    }
340
  mkcl_return_value(output);
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
}

mkcl_object
mk_cl_nreverse(MKCL, mkcl_object seq)
{
  if ( mkcl_Null(seq) ) return(mk_cl_Cnil);

  mkcl_call_stack_check(env);
  switch (mkcl_type_of(seq))
    {
    case mkcl_t_cons:
      {
	mkcl_object x, y, z;
	for (x = seq, y = mk_cl_Cnil; !mkcl_Null(x); ) {
	  if (!MKCL_LISTP(x)) mkcl_FEtype_error_list(env, x);
	  z = x;
	  x = MKCL_CONS_CDR(x);
	  if (x == seq) mkcl_FEcircular_list(env, seq);
	  MKCL_RPLACD(z, y);
	  y = z;
	}
	seq = y;
      }
      break;
    case mkcl_t_string:
      mkcl_reverse_subarray(env, seq, 0, seq->string.fillp);
      break;
    case mkcl_t_base_string:
      mkcl_reverse_subarray(env, seq, 0, seq->base_string.fillp);
      break;
    case mkcl_t_vector:
    case mkcl_t_bitvector:
      mkcl_reverse_subarray(env, seq, 0, seq->vector.fillp);
      break;
    default:
      mkcl_FEtype_error_sequence(env, seq);
    }
378
  mkcl_return_value(seq);
379
}