Debian Codegraph @

src/c/vector_push.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * vector_push.d - vector optimizations
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 <ecl/internal.h>
17
18 static cl_object
19 extend_vector(cl_object v, cl_index amount)
20 {
21 cl_object other;
22 cl_index new_length;
23 unlikely_if (!ECL_VECTORP(v)) {
24 FEwrong_type_nth_arg(@[vector-push-extend],1,v,@[vector]);
25 }
26 if (!ECL_ADJUSTABLE_ARRAY_P(v))
27 FEerror("vector-push-extend: the array ~S is not adjustable.",
28 1, v);
29 if (v->vector.dim >= ECL_ARRAY_DIMENSION_LIMIT)
30 FEerror("Can't extend the array.", 0);
31 if (amount == 0)
32 amount = v->vector.dim / 2 + 1;
33 new_length = v->vector.dim + amount;
34 if (new_length > ECL_ARRAY_DIMENSION_LIMIT)
35 new_length = ECL_ARRAY_DIMENSION_LIMIT;
36 other = si_make_vector(cl_array_element_type(v),
37 ecl_make_fixnum(new_length), ECL_T,
38 ecl_make_fixnum(v->vector.fillp),
39 ECL_NIL, ecl_make_fixnum(0));
40 ecl_copy_subarray(other, 0, v, 0, v->vector.fillp);
41 return si_replace_array(v, other);
42 }
43
44 ecl_character
45 ecl_string_push_extend(cl_object s, ecl_character c)
46 {
47 switch(ecl_t_of(s)) {
48 #ifdef ECL_UNICODE
49 case t_string:
50 #endif
51 case t_base_string:
52 /* We use the fact that both string types are
53 byte-compatible except for the data. */
54 if (s->base_string.fillp >= s->base_string.dim) {
55 s = extend_vector(s, 0);
56 }
57 ecl_char_set(s, s->base_string.fillp++, c);
58 return c;
59 default:
60 FEwrong_type_nth_arg(@[vector-push-extend],1,s,@[string]);
61 }
62 }
63
64 cl_object
65 cl_vector_push(cl_object value, cl_object v)
66 {
67 cl_index f = ecl_fixnum(cl_fill_pointer(v));
68 if (f >= v->vector.dim) {
69 @(return ECL_NIL);
70 } else {
71 ecl_aset1(v, v->vector.fillp, value);
72 @(return ecl_make_fixnum(v->vector.fillp++));
73 }
74 }
75
76 @(defun vector-push-extend (value v &optional (extent ecl_make_fixnum(0)))
77 @
78 {
79 cl_index f = ecl_fixnum(cl_fill_pointer(v));
80 if (f >= v->vector.dim) {
81 v = extend_vector(v, ecl_to_size(extent));
82 }
83 ecl_aset1(v, v->vector.fillp, value);
84 @(return ecl_make_fixnum(v->vector.fillp++));
85 }
86 @)