4
4
// comparable tag
5
5
#define cmptag (v ) (isfixnum(v) ? TAG_NUM : tag(v))
6
6
7
- static value_t eq_class (htable_t * table , value_t key )
7
+ static value_t eq_class (fl_context_t * fl_ctx , htable_t * table , value_t key )
8
8
{
9
9
value_t c = (value_t )ptrhash_get (table , (void * )key );
10
10
if (c == (value_t )HT_NOTFOUND )
11
- return NIL ;
11
+ return fl_ctx -> NIL ;
12
12
if (c == key )
13
13
return c ;
14
- return eq_class (table , c );
14
+ return eq_class (fl_ctx , table , c );
15
15
}
16
16
17
- static void eq_union (htable_t * table , value_t a , value_t b ,
18
- value_t c , value_t cb )
17
+ static void eq_union (fl_context_t * fl_ctx , htable_t * table , value_t a ,
18
+ value_t b , value_t c , value_t cb )
19
19
{
20
- value_t ca = (c == NIL ? a : c );
21
- if (cb != NIL )
20
+ value_t ca = (c == fl_ctx -> NIL ? a : c );
21
+ if (cb != fl_ctx -> NIL )
22
22
ptrhash_put (table , (void * )cb , (void * )ca );
23
23
ptrhash_put (table , (void * )a , (void * )ca );
24
24
ptrhash_put (table , (void * )b , (void * )ca );
25
25
}
26
26
27
- static value_t bounded_compare (value_t a , value_t b , int bound , int eq );
28
- static value_t cyc_compare (value_t a , value_t b , htable_t * table , int eq );
27
+ static value_t bounded_compare (fl_context_t * fl_ctx , value_t a , value_t b , int bound , int eq );
28
+ static value_t cyc_compare (fl_context_t * fl_ctx , value_t a , value_t b , htable_t * table , int eq );
29
29
30
- static value_t bounded_vector_compare (value_t a , value_t b , int bound , int eq )
30
+ static value_t bounded_vector_compare (fl_context_t * fl_ctx , value_t a , value_t b , int bound , int eq )
31
31
{
32
32
size_t la = vector_size (a );
33
33
size_t lb = vector_size (b );
34
34
size_t m , i ;
35
35
if (eq && (la != lb )) return fixnum (1 );
36
36
m = la < lb ? la : lb ;
37
37
for (i = 0 ; i < m ; i ++ ) {
38
- value_t d = bounded_compare (vector_elt (a ,i ), vector_elt (b ,i ),
38
+ value_t d = bounded_compare (fl_ctx , vector_elt (a ,i ), vector_elt (b ,i ),
39
39
bound - 1 , eq );
40
- if (d == NIL || numval (d )!= 0 ) return d ;
40
+ if (d == fl_ctx -> NIL || numval (d )!= 0 ) return d ;
41
41
}
42
42
if (la < lb ) return fixnum (-1 );
43
43
if (la > lb ) return fixnum (1 );
@@ -46,14 +46,14 @@ static value_t bounded_vector_compare(value_t a, value_t b, int bound, int eq)
46
46
47
47
// strange comparisons are resolved arbitrarily but consistently.
48
48
// ordering: number < cprim < function < vector < cvalue < symbol < cons
49
- static value_t bounded_compare (value_t a , value_t b , int bound , int eq )
49
+ static value_t bounded_compare (fl_context_t * fl_ctx , value_t a , value_t b , int bound , int eq )
50
50
{
51
51
value_t d ;
52
52
53
53
compare_top :
54
54
if (a == b ) return fixnum (0 );
55
55
if (bound <= 0 )
56
- return NIL ;
56
+ return fl_ctx -> NIL ;
57
57
int taga = tag (a );
58
58
int tagb = cmptag (b );
59
59
int c ;
@@ -64,29 +64,29 @@ static value_t bounded_compare(value_t a, value_t b, int bound, int eq)
64
64
return (numval (a ) < numval (b )) ? fixnum (-1 ) : fixnum (1 );
65
65
}
66
66
if (iscprim (b )) {
67
- if (cp_class ((cprim_t * )ptr (b )) == wchartype )
67
+ if (cp_class ((cprim_t * )ptr (b )) == fl_ctx -> wchartype )
68
68
return fixnum (1 );
69
- return fixnum (numeric_compare (a , b , eq , 1 , NULL ));
69
+ return fixnum (numeric_compare (fl_ctx , a , b , eq , 1 , NULL ));
70
70
}
71
71
return fixnum (-1 );
72
72
case TAG_SYM :
73
73
if (eq ) return fixnum (1 );
74
74
if (tagb < TAG_SYM ) return fixnum (1 );
75
75
if (tagb > TAG_SYM ) return fixnum (-1 );
76
- return fixnum (strcmp (symbol_name (a ), symbol_name (b )));
76
+ return fixnum (strcmp (symbol_name (fl_ctx , a ), symbol_name (fl_ctx , b )));
77
77
case TAG_VECTOR :
78
78
if (isvector (b ))
79
- return bounded_vector_compare (a , b , bound , eq );
79
+ return bounded_vector_compare (fl_ctx , a , b , bound , eq );
80
80
break ;
81
81
case TAG_CPRIM :
82
- if (cp_class ((cprim_t * )ptr (a )) == wchartype ) {
83
- if (!iscprim (b ) || cp_class ((cprim_t * )ptr (b )) != wchartype )
82
+ if (cp_class ((cprim_t * )ptr (a )) == fl_ctx -> wchartype ) {
83
+ if (!iscprim (b ) || cp_class ((cprim_t * )ptr (b )) != fl_ctx -> wchartype )
84
84
return fixnum (-1 );
85
85
}
86
- else if (iscprim (b ) && cp_class ((cprim_t * )ptr (b )) == wchartype ) {
86
+ else if (iscprim (b ) && cp_class ((cprim_t * )ptr (b )) == fl_ctx -> wchartype ) {
87
87
return fixnum (1 );
88
88
}
89
- c = numeric_compare (a , b , eq , 1 , NULL );
89
+ c = numeric_compare (fl_ctx , a , b , eq , 1 , NULL );
90
90
if (c != 2 )
91
91
return fixnum (c );
92
92
break ;
@@ -102,30 +102,30 @@ static value_t bounded_compare(value_t a, value_t b, int bound, int eq)
102
102
if (uintval (a ) > N_BUILTINS && uintval (b ) > N_BUILTINS ) {
103
103
function_t * fa = (function_t * )ptr (a );
104
104
function_t * fb = (function_t * )ptr (b );
105
- d = bounded_compare (fa -> bcode , fb -> bcode , bound - 1 , eq );
106
- if (d == NIL || numval (d ) != 0 ) return d ;
107
- d = bounded_compare (fa -> vals , fb -> vals , bound - 1 , eq );
108
- if (d == NIL || numval (d ) != 0 ) return d ;
109
- d = bounded_compare (fa -> env , fb -> env , bound - 1 , eq );
110
- if (d == NIL || numval (d ) != 0 ) return d ;
105
+ d = bounded_compare (fl_ctx , fa -> bcode , fb -> bcode , bound - 1 , eq );
106
+ if (d == fl_ctx -> NIL || numval (d ) != 0 ) return d ;
107
+ d = bounded_compare (fl_ctx , fa -> vals , fb -> vals , bound - 1 , eq );
108
+ if (d == fl_ctx -> NIL || numval (d ) != 0 ) return d ;
109
+ d = bounded_compare (fl_ctx , fa -> env , fb -> env , bound - 1 , eq );
110
+ if (d == fl_ctx -> NIL || numval (d ) != 0 ) return d ;
111
111
return fixnum (0 );
112
112
}
113
113
return (uintval (a ) < uintval (b )) ? fixnum (-1 ) : fixnum (1 );
114
114
}
115
115
break ;
116
116
case TAG_CONS :
117
117
if (tagb < TAG_CONS ) return fixnum (1 );
118
- d = bounded_compare (car_ (a ), car_ (b ), bound - 1 , eq );
119
- if (d == NIL || numval (d ) != 0 ) return d ;
118
+ d = bounded_compare (fl_ctx , car_ (a ), car_ (b ), bound - 1 , eq );
119
+ if (d == fl_ctx -> NIL || numval (d ) != 0 ) return d ;
120
120
a = cdr_ (a ); b = cdr_ (b );
121
121
bound -- ;
122
122
goto compare_top ;
123
123
}
124
124
return (taga < tagb ) ? fixnum (-1 ) : fixnum (1 );
125
125
}
126
126
127
- static value_t cyc_vector_compare (value_t a , value_t b , htable_t * table ,
128
- int eq )
127
+ static value_t cyc_vector_compare (fl_context_t * fl_ctx , value_t a ,
128
+ value_t b , htable_t * table , int eq )
129
129
{
130
130
size_t la = vector_size (a );
131
131
size_t lb = vector_size (b );
@@ -139,8 +139,8 @@ static value_t cyc_vector_compare(value_t a, value_t b, htable_t *table,
139
139
xa = vector_elt (a ,i );
140
140
xb = vector_elt (b ,i );
141
141
if (leafp (xa ) || leafp (xb )) {
142
- d = bounded_compare (xa , xb , 1 , eq );
143
- if (d != NIL && numval (d )!= 0 ) return d ;
142
+ d = bounded_compare (fl_ctx , xa , xb , 1 , eq );
143
+ if (d != fl_ctx -> NIL && numval (d )!= 0 ) return d ;
144
144
}
145
145
else if (tag (xa ) < tag (xb )) {
146
146
return fixnum (-1 );
@@ -150,18 +150,18 @@ static value_t cyc_vector_compare(value_t a, value_t b, htable_t *table,
150
150
}
151
151
}
152
152
153
- ca = eq_class (table , a );
154
- cb = eq_class (table , b );
155
- if (ca != NIL && ca == cb )
153
+ ca = eq_class (fl_ctx , table , a );
154
+ cb = eq_class (fl_ctx , table , b );
155
+ if (ca != fl_ctx -> NIL && ca == cb )
156
156
return fixnum (0 );
157
157
158
- eq_union (table , a , b , ca , cb );
158
+ eq_union (fl_ctx , table , a , b , ca , cb );
159
159
160
160
for (i = 0 ; i < m ; i ++ ) {
161
161
xa = vector_elt (a ,i );
162
162
xb = vector_elt (b ,i );
163
163
if (!leafp (xa ) || tag (xa )== TAG_FUNCTION ) {
164
- d = cyc_compare (xa , xb , table , eq );
164
+ d = cyc_compare (fl_ctx , xa , xb , table , eq );
165
165
if (numval (d )!= 0 )
166
166
return d ;
167
167
}
@@ -172,7 +172,7 @@ static value_t cyc_vector_compare(value_t a, value_t b, htable_t *table,
172
172
return fixnum (0 );
173
173
}
174
174
175
- static value_t cyc_compare (value_t a , value_t b , htable_t * table , int eq )
175
+ static value_t cyc_compare (fl_context_t * fl_ctx , value_t a , value_t b , htable_t * table , int eq )
176
176
{
177
177
value_t d , ca , cb ;
178
178
cyc_compare_top :
@@ -185,29 +185,29 @@ static value_t cyc_compare(value_t a, value_t b, htable_t *table, int eq)
185
185
int tagaa = tag (aa ); int tagda = tag (da );
186
186
int tagab = tag (ab ); int tagdb = tag (db );
187
187
if (leafp (aa ) || leafp (ab )) {
188
- d = bounded_compare (aa , ab , 1 , eq );
189
- if (d != NIL && numval (d )!= 0 ) return d ;
188
+ d = bounded_compare (fl_ctx , aa , ab , 1 , eq );
189
+ if (d != fl_ctx -> NIL && numval (d )!= 0 ) return d ;
190
190
}
191
191
else if (tagaa < tagab )
192
192
return fixnum (-1 );
193
193
else if (tagaa > tagab )
194
194
return fixnum (1 );
195
195
if (leafp (da ) || leafp (db )) {
196
- d = bounded_compare (da , db , 1 , eq );
197
- if (d != NIL && numval (d )!= 0 ) return d ;
196
+ d = bounded_compare (fl_ctx , da , db , 1 , eq );
197
+ if (d != fl_ctx -> NIL && numval (d )!= 0 ) return d ;
198
198
}
199
199
else if (tagda < tagdb )
200
200
return fixnum (-1 );
201
201
else if (tagda > tagdb )
202
202
return fixnum (1 );
203
203
204
- ca = eq_class (table , a );
205
- cb = eq_class (table , b );
206
- if (ca != NIL && ca == cb )
204
+ ca = eq_class (fl_ctx , table , a );
205
+ cb = eq_class (fl_ctx , table , b );
206
+ if (ca != fl_ctx -> NIL && ca == cb )
207
207
return fixnum (0 );
208
208
209
- eq_union (table , a , b , ca , cb );
210
- d = cyc_compare (aa , ab , table , eq );
209
+ eq_union (fl_ctx , table , a , b , ca , cb );
210
+ d = cyc_compare (fl_ctx , aa , ab , table , eq );
211
211
if (numval (d )!= 0 ) return d ;
212
212
a = da ;
213
213
b = db ;
@@ -218,56 +218,55 @@ static value_t cyc_compare(value_t a, value_t b, htable_t *table, int eq)
218
218
}
219
219
}
220
220
else if (isvector (a ) && isvector (b )) {
221
- return cyc_vector_compare (a , b , table , eq );
221
+ return cyc_vector_compare (fl_ctx , a , b , table , eq );
222
222
}
223
223
else if (isclosure (a ) && isclosure (b )) {
224
224
function_t * fa = (function_t * )ptr (a );
225
225
function_t * fb = (function_t * )ptr (b );
226
- d = bounded_compare (fa -> bcode , fb -> bcode , 1 , eq );
226
+ d = bounded_compare (fl_ctx , fa -> bcode , fb -> bcode , 1 , eq );
227
227
if (numval (d ) != 0 ) return d ;
228
228
229
- ca = eq_class (table , a );
230
- cb = eq_class (table , b );
231
- if (ca != NIL && ca == cb )
229
+ ca = eq_class (fl_ctx , table , a );
230
+ cb = eq_class (fl_ctx , table , b );
231
+ if (ca != fl_ctx -> NIL && ca == cb )
232
232
return fixnum (0 );
233
233
234
- eq_union (table , a , b , ca , cb );
235
- d = cyc_compare (fa -> vals , fb -> vals , table , eq );
234
+ eq_union (fl_ctx , table , a , b , ca , cb );
235
+ d = cyc_compare (fl_ctx , fa -> vals , fb -> vals , table , eq );
236
236
if (numval (d ) != 0 ) return d ;
237
237
a = fa -> env ;
238
238
b = fb -> env ;
239
239
goto cyc_compare_top ;
240
240
}
241
- return bounded_compare (a , b , 1 , eq );
241
+ return bounded_compare (fl_ctx , a , b , 1 , eq );
242
242
}
243
243
244
- static htable_t equal_eq_hashtable ;
245
- void comparehash_init (void )
244
+ void comparehash_init (fl_context_t * fl_ctx )
246
245
{
247
- htable_new (& equal_eq_hashtable , 512 );
246
+ htable_new (& fl_ctx -> equal_eq_hashtable , 512 );
248
247
}
249
248
250
249
// 'eq' means unordered comparison is sufficient
251
- static value_t compare_ (value_t a , value_t b , int eq )
250
+ static value_t compare_ (fl_context_t * fl_ctx , value_t a , value_t b , int eq )
252
251
{
253
- value_t guess = bounded_compare (a , b , BOUNDED_COMPARE_BOUND , eq );
254
- if (guess == NIL ) {
255
- guess = cyc_compare (a , b , & equal_eq_hashtable , eq );
256
- htable_reset (& equal_eq_hashtable , 512 );
252
+ value_t guess = bounded_compare (fl_ctx , a , b , BOUNDED_COMPARE_BOUND , eq );
253
+ if (guess == fl_ctx -> NIL ) {
254
+ guess = cyc_compare (fl_ctx , a , b , & fl_ctx -> equal_eq_hashtable , eq );
255
+ htable_reset (& fl_ctx -> equal_eq_hashtable , 512 );
257
256
}
258
257
return guess ;
259
258
}
260
259
261
- value_t fl_compare (value_t a , value_t b )
260
+ value_t fl_compare (fl_context_t * fl_ctx , value_t a , value_t b )
262
261
{
263
- return compare_ (a , b , 0 );
262
+ return compare_ (fl_ctx , a , b , 0 );
264
263
}
265
264
266
- value_t fl_equal (value_t a , value_t b )
265
+ value_t fl_equal (fl_context_t * fl_ctx , value_t a , value_t b )
267
266
{
268
267
if (eq_comparable (a , b ))
269
- return (a == b ) ? FL_T : FL_F ;
270
- return (numval (compare_ (a ,b ,1 ))== 0 ? FL_T : FL_F );
268
+ return (a == b ) ? fl_ctx -> T : fl_ctx -> F ;
269
+ return (numval (compare_ (fl_ctx , a ,b ,1 ))== 0 ? fl_ctx -> T : fl_ctx -> F );
271
270
}
272
271
273
272
/*
@@ -287,7 +286,7 @@ value_t fl_equal(value_t a, value_t b)
287
286
#endif
288
287
289
288
// *oob: output argument, means we hit the limit specified by 'bound'
290
- static uptrint_t bounded_hash (value_t a , int bound , int * oob )
289
+ static uptrint_t bounded_hash (fl_context_t * fl_ctx , value_t a , int bound , int * oob )
291
290
{
292
291
* oob = 0 ;
293
292
union {
@@ -308,14 +307,14 @@ static uptrint_t bounded_hash(value_t a, int bound, int *oob)
308
307
return doublehash (u .i64 );
309
308
case TAG_FUNCTION :
310
309
if (uintval (a ) > N_BUILTINS )
311
- return bounded_hash (((function_t * )ptr (a ))-> bcode , bound , oob );
310
+ return bounded_hash (fl_ctx , ((function_t * )ptr (a ))-> bcode , bound , oob );
312
311
return inthash (a );
313
312
case TAG_SYM :
314
313
return ((symbol_t * )ptr (a ))-> hash ;
315
314
case TAG_CPRIM :
316
315
cp = (cprim_t * )ptr (a );
317
316
data = cp_data (cp );
318
- if (cp_class (cp ) == wchartype )
317
+ if (cp_class (cp ) == fl_ctx -> wchartype )
319
318
return inthash (* (int32_t * )data );
320
319
nt = cp_numtype (cp );
321
320
u .d = conv_to_double (data , nt );
@@ -332,7 +331,7 @@ static uptrint_t bounded_hash(value_t a, int bound, int *oob)
332
331
}
333
332
len = vector_size (a );
334
333
for (i = 0 ; i < len ; i ++ ) {
335
- h = MIX (h , bounded_hash (vector_elt (a ,i ), bound /2 , & oob2 )^1 );
334
+ h = MIX (h , bounded_hash (fl_ctx , vector_elt (a ,i ), bound /2 , & oob2 )^1 );
336
335
if (oob2 )
337
336
bound /=2 ;
338
337
* oob = * oob || oob2 ;
@@ -345,7 +344,7 @@ static uptrint_t bounded_hash(value_t a, int bound, int *oob)
345
344
* oob = 1 ;
346
345
return h ;
347
346
}
348
- h = MIX (h , bounded_hash (car_ (a ), bound /2 , & oob2 ));
347
+ h = MIX (h , bounded_hash (fl_ctx , car_ (a ), bound /2 , & oob2 ));
349
348
// bounds balancing: try to share the bounds efficiently
350
349
// so we can hash better when a list is cdr-deep (a common case)
351
350
if (oob2 )
@@ -357,29 +356,29 @@ static uptrint_t bounded_hash(value_t a, int bound, int *oob)
357
356
* oob = * oob || oob2 ;
358
357
a = cdr_ (a );
359
358
} while (iscons (a ));
360
- h = MIX (h , bounded_hash (a , bound - 1 , & oob2 )^2 );
359
+ h = MIX (h , bounded_hash (fl_ctx , a , bound - 1 , & oob2 )^2 );
361
360
* oob = * oob || oob2 ;
362
361
return h ;
363
362
}
364
363
return 0 ;
365
364
}
366
365
367
- int equal_lispvalue (value_t a , value_t b )
366
+ int equal_lispvalue (fl_context_t * fl_ctx , value_t a , value_t b )
368
367
{
369
368
if (eq_comparable (a , b ))
370
369
return (a == b );
371
- return (numval (compare_ (a , b , 1 ))== 0 );
370
+ return (numval (compare_ (fl_ctx , a , b , 1 ))== 0 );
372
371
}
373
372
374
- uptrint_t hash_lispvalue (value_t a )
373
+ uptrint_t hash_lispvalue (fl_context_t * fl_ctx , value_t a )
375
374
{
376
- int oob = 0 ;
377
- uptrint_t n = bounded_hash (a , BOUNDED_HASH_BOUND , & oob );
375
+ int oob = 0 ;
376
+ uptrint_t n = bounded_hash (fl_ctx , a , BOUNDED_HASH_BOUND , & oob );
378
377
return n ;
379
378
}
380
379
381
- value_t fl_hash (value_t * args , u_int32_t nargs )
380
+ value_t fl_hash (fl_context_t * fl_ctx , value_t * args , u_int32_t nargs )
382
381
{
383
- argcount ("hash" , nargs , 1 );
384
- return fixnum (hash_lispvalue (args [0 ]));
382
+ argcount (fl_ctx , "hash" , nargs , 1 );
383
+ return fixnum (hash_lispvalue (fl_ctx , args [0 ]));
385
384
}
0 commit comments