File: replace.c

package info (click to toggle)
calcium 0.4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,756 kB
  • sloc: ansic: 62,836; python: 2,827; sh: 518; makefile: 163
file content (214 lines) | stat: -rw-r--r-- 4,808 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
80
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
/*
    Copyright (C) 2021 Fredrik Johansson

    This file is part of Calcium.

    Calcium is free software: you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.  See <http://www.gnu.org/licenses/>.
*/

#include "fexpr.h"

/*
   Sets res_view to a view of expr replaced by {xs, ys, len} and returns whether
   any replacement has been made.

   If no replacement has been made, res_view will refer to the data of expr,
   with an alloc field of 0.

   If expr as a whole has been replaced with some entry in ys, res_view will
   refer to the data of that value in ys, also with an alloc field of 0.

   Otherwise, expr will be initialized to a new expression, with a nonzero
   alloc field.
 */
int
_fexpr_replace_vec(fexpr_t res_view, const fexpr_t expr, fexpr_srcptr xs, fexpr_srcptr ys, slong len)
{
    slong i, nargs;
    fexpr_t func, new_func, arg;
    fexpr_struct tmp_args[4];
    fexpr_ptr args;
    int changed;

    for (i = 0; i < len; i++)
    {
        if (fexpr_equal(expr, xs + i))
        {
            res_view->data = ys[i].data;
            res_view->alloc = 0;
            return 1;
        }
    }

    if (fexpr_is_atom(expr))
    {
        res_view->data = expr->data;
        res_view->alloc = 0;
        return 0;
    }

    nargs = fexpr_nargs(expr);

    fexpr_view_func(func, expr);
    changed = _fexpr_replace_vec(new_func, func, xs, ys, len);

    if (nargs >= 5)
        args = flint_malloc(sizeof(fexpr_struct) * nargs);
    else
        args = tmp_args;

    if (nargs >= 1)
    {
        fexpr_view_arg(arg, expr, 0);

        for (i = 0; i < nargs; i++)
        {
            changed |= _fexpr_replace_vec(args + i, arg, xs, ys, len);

            if (i < nargs - 1)
                fexpr_view_next(arg);
        }
    }

    if (changed)
    {
        fexpr_init(res_view);
        fexpr_call_vec(res_view, new_func, args, nargs);

        if (new_func->alloc != 0)
            flint_free(new_func->data);

        for (i = 0; i < nargs; i++)
        {
            if (args[i].alloc != 0)
                flint_free(args[i].data);
        }
    }
    else
    {
        res_view->data = expr->data;
        res_view->alloc = 0;
    }

    if (nargs >= 5)
        flint_free(args);

    return changed;
}

int
fexpr_replace_vec(fexpr_t res, const fexpr_t expr, const fexpr_vec_t xs, const fexpr_vec_t ys)
{
    int changed;
    slong num_rules;
    fexpr_t res_view;

    num_rules = xs->length;

    if (num_rules != ys->length)
    {
        flint_printf("fexpr_replace_vec: vectors don't match\n");
        flint_abort();
    }

    if (num_rules == 0)
    {
        fexpr_set(res, expr);
        return 0;
    }

    changed = _fexpr_replace_vec(res_view, expr, xs->entries, ys->entries, num_rules);

    if (changed)
    {
        if (res_view->alloc != 0)
        {
            /* We have new data */
            fexpr_swap(res, res_view);
            fexpr_clear(res_view);
        }
        else
        {
            /* Must be a view of some ys; we assume that res is not aliased
               with any entry in ys. */
            fexpr_set(res, res_view);
        }
    }
    else
    {
        fexpr_set(res, expr);
    }

    return changed;
}

int
fexpr_replace(fexpr_t res, const fexpr_t expr, const fexpr_t x, const fexpr_t y)
{
    int changed;
    fexpr_t res_view;

    changed = _fexpr_replace_vec(res_view, expr, x, y, 1);

    if (changed)
    {
        if (res_view->alloc != 0)
        {
            /* We have new data */
            fexpr_swap(res, res_view);
            fexpr_clear(res_view);
        }
        else
        {
            /* Must be a view of some ys; we assume that res is not aliased
               with any entry in ys. */
            fexpr_set(res, res_view);
        }
    }
    else
    {
        fexpr_set(res, expr);
    }

    return changed;
}

int
fexpr_replace2(fexpr_t res, const fexpr_t expr, const fexpr_t x1, const fexpr_t y1, const fexpr_t x2, const fexpr_t y2)
{
    int changed;
    fexpr_t res_view;
    fexpr_struct tmp[4];

    tmp[0] = *x1;
    tmp[1] = *x2;
    tmp[2] = *y1;
    tmp[3] = *y2;

    changed = _fexpr_replace_vec(res_view, expr, tmp, tmp + 2, 2);

    if (changed)
    {
        if (res_view->alloc != 0)
        {
            /* We have new data */
            fexpr_swap(res, res_view);
            fexpr_clear(res_view);
        }
        else
        {
            /* Must be a view of some ys; we assume that res is not aliased
               with any entry in ys. */
            fexpr_set(res, res_view);
        }
    }
    else
    {
        fexpr_set(res, expr);
    }

    return changed;
}