forked from vermaseren/form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymmetr.c
2338 lines (2241 loc) · 60.9 KB
/
symmetr.c
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
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
340
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file symmetr.c
*
* The routines that deal with the pattern matching of functions with
* symmetric properties.
*/
/* #[ License : */
/*
* Copyright (C) 1984-2022 J.A.M. Vermaseren
* When using this file you are requested to refer to the publication
* J.A.M.Vermaseren "New features of FORM" math-ph/0010025
* This is considered a matter of courtesy as the development was paid
* for by FOM the Dutch physics granting agency and we would like to
* be able to track its scientific use to convince FOM of its value
* for the community.
*
* This file is part of FORM.
*
* FORM is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* FORM is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with FORM. If not, see <http://www.gnu.org/licenses/>.
*/
/* #] License : */
/*
#[ Includes : function.c
*/
#include "form3.h"
/*
#] Includes :
#[ MatchE : WORD MatchE(pattern,fun,inter,par)
Matches symmetric and antisymmetric tensors.
Pattern and fun point at a tensor.
Problem is the wildcarding and all its possible permutations.
This routine loops over all of them and calls for each
possible wildcarding the recursion in ScanFunctions.
Note that this can be very costly.
Originally this routine did only Levi Civita tensors and hence
it dealt only with commuting objects.
Because of the backtracking we cannot fall back to the calling
ScanFunctions routine and check the sequence of functions when
non-commuting objects are involved.
*/
WORD MatchE(PHEAD WORD *pattern, WORD *fun, WORD *inter, WORD par)
{
GETBIDENTITY
WORD *m, *t, *r, i, retval;
WORD *mstop, *tstop, j, newvalue, newfun;
WORD fixvec[MAXMATCH],wcvec[MAXMATCH],fixind[MAXMATCH],wcind[MAXMATCH];
WORD tfixvec[MAXMATCH],tfixind[MAXMATCH];
WORD vwc,vfix,ifix,iwc,tvfix,tifix,nv,ni;
WORD sign = 0, *rstop, first1, first2, first3, funwild;
WORD *OldWork, nwstore, oRepFunNum;
PERM perm1,perm2;
DISTRIBUTE distr;
WORD *newpat, /* *newter, *instart, */ offset;
/* instart = fun; */
offset = WORDDIF(fun,AN.terstart);
if ( pattern[1] != fun[1] ) return(0);
if ( *pattern >= FUNCTION+WILDOFFSET ) {
if ( CheckWild(BHEAD *pattern-WILDOFFSET,FUNTOFUN,*fun,&newfun) ) return(0);
funwild = 1;
}
else funwild = 0;
mstop = pattern + pattern[1];
tstop = fun + fun[1];
m = pattern + FUNHEAD;
t = fun + FUNHEAD;
while ( m < mstop ) {
if ( *m != *t ) break;
m++; t++;
}
if ( m >= mstop ) {
AN.RepFunList[AN.RepFunNum++] = offset;
AN.RepFunList[AN.RepFunNum++] = 0;
newpat = pattern + pattern[1];
if ( funwild ) {
m = AN.WildValue;
t = OldWork = AT.WorkPointer;
nwstore = i = (m[-SUBEXPSIZE+1]-SUBEXPSIZE)/4;
r = AT.WildMask;
if ( i > 0 ) {
do {
*t++ = *m++; *t++ = *m++; *t++ = *m++; *t++ = *m++; *t++ = *r++;
} while ( --i > 0 );
}
if ( t >= AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
AT.WorkPointer = t;
AddWild(BHEAD *pattern-WILDOFFSET,FUNTOFUN,newfun);
if ( newpat >= AN.patstop ) {
if ( AN.UseFindOnly == 0 ) {
if ( FindOnce(BHEAD AN.findTerm,AN.findPattern) ) {
AN.UsedOtherFind = 1;
return(1);
}
retval = 0;
}
else return(1);
}
else {
/* newter = instart; */
retval = ScanFunctions(BHEAD newpat,inter,par);
}
if ( retval == 0 ) {
m = AN.WildValue;
t = OldWork; r = AT.WildMask; i = nwstore;
if ( i > 0 ) {
do {
*m++ = *t++; *m++ = *t++; *m++ = *t++; *m++ = *t++; *r++ = *t++;
} while ( --i > 0 );
}
}
AT.WorkPointer = OldWork;
return(retval);
}
else {
if ( newpat >= AN.patstop ) {
if ( AN.UseFindOnly == 0 ) {
if ( FindOnce(BHEAD AN.findTerm,AN.findPattern) ) {
AN.UsedOtherFind = 1;
return(1);
}
else return(0);
}
else return(1);
}
/* newter = instart; */
i = ScanFunctions(BHEAD newpat,inter,par);
return(i);
}
/*
Now the recursion
*/
}
/*
Strategy:
1: match the fixed arguments
2: match, permuting the wildcards if needed.
3: keep track of sign.
*/
vwc = 0;
vfix = 0;
ifix = 0;
iwc = 0;
r = pattern+FUNHEAD;
while ( r < mstop ) {
if ( *r < (AM.OffsetVector+WILDOFFSET) ) {
fixvec[vfix++] = *r; /* Fixed vectors */
sign += vwc + ifix + iwc;
}
else if ( *r < MINSPEC ) {
wcvec[vwc++] = *r; /* Wildcard vectors */
sign += ifix + iwc;
}
else if ( *r < (AM.OffsetIndex+WILDOFFSET) ) {
fixind[ifix++] = *r; /* Fixed indices */
sign += iwc;
}
else if ( *r < (AM.OffsetIndex+(WILDOFFSET<<1)) ) {
wcind[iwc++] = *r; /* Wildcard indices */
}
else {
fixind[ifix++] = *r; /* Generated indices ~ fixed */
sign += iwc;
}
r++;
}
if ( iwc == 0 && vwc == 0 ) return(0);
tvfix = tifix = 0;
t = fun + FUNHEAD;
m = fixvec;
mstop = m + vfix;
r = fixind;
rstop = r + ifix;
nv = 0; ni = 0;
while ( t < tstop ) {
if ( *t < 0 ) {
nv++;
if ( m < mstop && *t == *m ) {
m++;
}
else {
sign += WORDDIF(mstop,m);
tfixvec[tvfix++] = *t;
}
}
else {
ni++;
if ( r < rstop && *r == *t ) {
r++;
}
else {
sign += WORDDIF(rstop,r);
tfixind[tifix++] = *t;
}
}
t++;
}
if ( m < mstop || r < rstop ) return(0);
if ( tvfix < vwc || (tvfix+tifix) < (vwc+iwc) ) return(0);
sign += ( nv - vfix - vwc ) & ni;
/*
Take now the wildcards that have an assignment already.
See whether they match.
*/
{
WORD *wv, *wm, n;
wm = AT.WildMask;
wv = AN.WildValue;
n = AN.NumWild;
do {
if ( *wm ) {
if ( *wv == VECTOVEC ) {
for ( ni = 0; ni < vwc; ni++ ) {
if ( wcvec[ni]-WILDOFFSET == wv[2] ) { /* Has been assigned */
sign += ni;
vwc--;
while ( ni < vwc ) {
wcvec[ni] = wcvec[ni+1];
ni++;
}
/* TryVect: */
for ( ni = 0; ni < tvfix; ni++ ) {
if ( tfixvec[ni] == wv[3] ) {
sign += ni;
tvfix--;
while ( ni < tvfix ) {
tfixvec[ni] = tfixvec[ni+1];
ni++;
}
goto NextWV;
}
}
return(0);
}
}
}
else if ( *wv == INDTOIND ) {
for ( ni = 0; ni < iwc; ni++ ) {
if ( wcind[ni]-WILDOFFSET == wv[2] ) { /* Has been assigned */
sign += ni;
iwc--;
while ( ni < iwc ) {
wcind[ni] = wcind[ni+1];
ni++;
}
for ( ni = 0; ni < tifix; ni++ ) {
if ( tfixind[ni] == wv[3] ) {
sign += ni;
tifix--;
while ( ni < tifix ) {
tfixind[ni] = tfixind[ni+1];
ni++;
}
goto NextWV;
}
}
/* goto TryVect; */
return(0);
}
}
}
else if ( *wv == VECTOSUB ) {
for ( ni = 0; ni < vwc; ni++ ) {
if ( wcvec[ni]-WILDOFFSET == wv[2] ) return(0);
}
}
else if ( *wv == INDTOSUB ) {
for ( ni = 0; ni < iwc; ni++ ) {
if ( wcind[ni]-WILDOFFSET == wv[2] ) return(0);
}
}
}
NextWV:
wm++;
wv += wv[1];
n--;
if ( n > 0 ) {
while ( n > 0 && ( *wv == FROMSET || *wv == SETTONUM
|| *wv == LOADDOLLAR ) ) { wv += wv[1]; wm++; n--; }
/*
Freak problem: doesn't test for n and ran into a reamining
code equal to SETTONUM followed by a big number and then
ran out of the memory.
while ( *wv == FROMSET || *wv == SETTONUM
|| ( *wv == LOADDOLLAR && n > 0 ) ) { wv += wv[1]; wm++; n--; }
*/
}
} while ( n > 0 );
}
/*
Now there are only free wildcards left.
Possibly the assigned values ate too many vectors.
The rest has to be done the 'hard way' via permutations.
This is too bad when there are 10 indices.
This could cause 10! tries.
We try to avoid the worst case by using a very special
(somewhat slow) permutation routine that has as its worst
cases some rather unlikely configurations, rather than some
common ones (as would have been the case with the conventional
permuation routine).
assume:
vvvvvvvvvvvviiiiiii (tvfix in tfixvec and tifix in tfixind)
VVVVVVVVVIIIIIIIIII (vwc in wcvec and iwc in wcind)
Note: all further assignments are possible at this point!
Strategy:
permute v
permute i
loop over the ordered distribution of the leftover v's
through the i's.
*/
if ( tvfix < vwc ) { return(0); }
perm1.n = tvfix;
perm1.sign = 0;
perm1.objects = tfixvec;
perm2.n = tifix;
perm2.sign = 0;
perm2.objects = tfixind;
distr.n1 = tvfix - vwc;
distr.n2 = tifix;
distr.obj1 = tfixvec + vwc;
distr.obj2 = tfixind;
distr.out = fixvec; /* For scratch */
first1 = 1;
/*
Store the current Wildcard assignments
*/
m = AN.WildValue;
t = OldWork = AT.WorkPointer;
nwstore = i = (m[-SUBEXPSIZE+1]-SUBEXPSIZE)/4;
r = AT.WildMask;
if ( i > 0 ) {
do {
*t++ = *m++; *t++ = *m++; *t++ = *m++; *t++ = *m++; *t++ = *r++;
} while ( --i > 0 );
}
if ( t >= AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
AT.WorkPointer = t;
while ( (first1 = Permute(&perm1,first1) ) == 0 ) {
first2 = 1;
while ( (first2 = Permute(&perm2,first2) ) == 0 ) {
first3 = 1;
while ( (first3 = Distribute(&distr,first3) ) == 0 ) {
/*
Make now the wildcard assignments
*/
for ( i = 0; i < vwc; i++ ) {
j = wcvec[i] - WILDOFFSET;
if ( CheckWild(BHEAD j,VECTOVEC,tfixvec[i],&newvalue) )
goto NoCaseB;
AddWild(BHEAD j,VECTOVEC,newvalue);
}
for ( i = 0; i < iwc; i++ ) {
j = wcind[i] - WILDOFFSET;
if ( CheckWild(BHEAD j,INDTOIND,fixvec[i],&newvalue) )
goto NoCaseB;
AddWild(BHEAD j,INDTOIND,newvalue);
}
/*
Go into the recursion
*/
oRepFunNum = AN.RepFunNum;
AN.RepFunList[AN.RepFunNum++] = offset;
AN.RepFunList[AN.RepFunNum++] =
( perm1.sign + perm2.sign + distr.sign + sign ) & 1;
newpat = pattern + pattern[1];
if ( funwild ) AddWild(BHEAD *pattern-WILDOFFSET,FUNTOFUN,newfun);
if ( newpat >= AN.patstop ) {
if ( AN.UseFindOnly == 0 ) {
if ( FindOnce(BHEAD AN.findTerm,AN.findPattern) ) {
AN.UsedOtherFind = 1;
return(1);
}
}
else return(1);
}
else {
/* newter = instart; */
if ( ScanFunctions(BHEAD newpat,inter,par) ) { return(1); }
}
/*
Restore the old Wildcard assignments
*/
AN.RepFunNum = oRepFunNum;
NoCaseB: m = AN.WildValue;
t = OldWork; r = AT.WildMask; i = nwstore;
if ( i > 0 ) {
do {
*m++ = *t++; *m++ = *t++; *m++ = *t++; *m++ = *t++; *r++ = *t++;
} while ( --i > 0 );
}
AT.WorkPointer = t;
}
}
}
AT.WorkPointer = OldWork;
return(0);
}
/*
#] MatchE :
#[ Permute : WORD Permute(perm,first)
Special permutation function.
Works recursively.
The aim is to cycle through in as fast a way as possible,
to take care that each object hits the various positions
already early in the game.
Start at two: -> cycle of two
then three -> cycle of three
etc;
The innermost cycle is the longest. This is the opposite
of the usual way of generating permutations and it is
certainly not the fastest one. It allows for the fastest
hit in the assignment of wildcards though.
*/
WORD Permute(PERM *perm, WORD first)
{
WORD *s, c, i, j;
if ( first ) {
perm->sign = ( perm->sign <= 1 ) ? 0: 1;
for ( i = 0; i < perm->n; i++ ) perm->cycle[i] = 0;
return(0);
}
i = perm->n;
while ( --i > 0 ) {
s = perm->objects;
c = s[0];
j = i;
while ( --j >= 0 ) { *s = s[1]; s++; }
*s = c;
if ( ( i & 1 ) != 0 ) perm->sign ^= 1;
if ( perm->cycle[i] < i ) {
(perm->cycle[i])++;
return(0);
}
else {
perm->cycle[i] = 0;
}
}
return(1);
}
/*
#] Permute :
#[ PermuteP : WORD PermuteP(perm,first)
Like Permute, but works on an array of pointers
*/
WORD PermuteP(PERMP *perm, WORD first)
{
WORD **s, *c, i, j;
if ( first ) {
perm->sign = ( perm->sign <= 1 ) ? 0: 1;
for ( i = 0; i < perm->n; i++ ) perm->cycle[i] = 0;
return(0);
}
i = perm->n;
while ( --i > 0 ) {
s = perm->objects;
c = s[0];
j = i;
while ( --j >= 0 ) { *s = s[1]; s++; }
*s = c;
if ( ( i & 1 ) != 0 ) perm->sign ^= 1;
if ( perm->cycle[i] < i ) {
(perm->cycle[i])++;
return(0);
}
else {
perm->cycle[i] = 0;
}
}
return(1);
}
/*
#] PermuteP :
#[ Distribute :
*/
WORD Distribute(DISTRIBUTE *d, WORD first)
{
WORD *to, *from, *inc, *from2, i, j;
if ( first ) {
d->n = d->n1 + d->n2;
to = d->out;
from = d->obj2;
for ( i = 0; i < d->n2; i++ ) {
d->cycle[i] = 0;
*to++ = *from++;
}
from = d->obj1;
while ( i < d->n ) {
d->cycle[i++] = 1;
*to++ = *from++;
}
d->sign = 0;
return(0);
}
if ( d->n1 == 0 || d->n2 == 0 ) return(1);
j = 0;
i = 0;
inc = d->cycle;
from = inc + d->n;
while ( *inc ) { j++; inc++; }
while ( !*inc && inc < from ) { i++; inc++; }
if ( inc >= from ) return(1);
d->sign ^= ((i&j)-j+1) & 1;
*inc = 0;
*--inc = 1;
while ( --j >= 0 ) *--inc = 1;
while ( --i > 0 ) *--inc = 0;
to = d->out;
from = d->obj1;
from2 = d->obj2;
for ( i = 0; i < d->n; i++ ) {
if ( *inc++ ) {
*to++ = *from++;
}
else {
*to++ = *from2++;
}
}
return(0);
}
/*
#] Distribute :
#[ MatchCy :
Matching of (r)cyclic tensors.
Parameters like in MatchE.
The structure of the routine is much simpler, because the number
of possibilities is much more limited.
The major complication is the ?a-type wildcards
We need a strategy for T(i1?,?a,i1?,?b). Which is the shorter
match: ?a or ?b ? (if possible of course)
This is also relevant in the case of the shortest match if there
is more than one choice for i1.
*/
int MatchCy(PHEAD WORD *pattern, WORD *fun, WORD *inter, WORD par)
{
GETBIDENTITY
WORD *t, *tstop, *p, *pstop, *m, *r, *oldworkpointer = AT.WorkPointer;
WORD *thewildcards, *multiplicity, *renum, wc, newvalue, oldwilval = 0;
WORD *params, *lowlevel = 0;
int argcount = 0, funnycount = 0, tcount = fun[1] - FUNHEAD;
int type = 0, pnum, i, j, k, nwstore, iraise, itop, sumeat;
CBUF *C = cbuf+AT.ebufnum;
int ntwa = 3*AN.NumTotWildArgs+1;
LONG oldcpointer = C->Pointer - C->Buffer;
WORD offset = fun-AN.terstart, *newpat;
if ( (functions[fun[0]-FUNCTION].symmetric & ~REVERSEORDER) == RCYCLESYMMETRIC ) type = 1;
pnum = pattern[0];
nwstore = (AN.WildValue[-SUBEXPSIZE+1]-SUBEXPSIZE)/4;
if ( pnum > FUNCTION + WILDOFFSET ) {
pnum -= WILDOFFSET;
if ( CheckWild(BHEAD pnum,FUNTOFUN,fun[0],&newvalue) ) return(0);
oldwilval = 1;
t = lowlevel = AT.WorkPointer;
m = AN.WildValue;
i = nwstore;
r = AT.WildMask;
if ( i > 0 ) {
do {
*t++ = *m++; *t++ = *m++; *t++ = *m++; *t++ = *m++; *t++ = *r++;
} while ( --i > 0 );
}
*t++ = C->numrhs;
if ( t >= AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
AT.WorkPointer = t;
AddWild(BHEAD pnum,FUNTOFUN,newvalue);
}
if ( (functions[pnum-FUNCTION].symmetric & ~REVERSEORDER) == RCYCLESYMMETRIC ) type = 1;
/* First we have to make an inventory. Are there FUNNYWILD pointers? */
p = pattern + FUNHEAD;
pstop = pattern + pattern[1];
while ( p < pstop ) {
if ( *p == FUNNYWILD ) { p += 2; funnycount++; }
else { p++; argcount++; }
}
if ( argcount > tcount ) goto NoSuccess;
if ( argcount < tcount && funnycount == 0 ) goto NoSuccess;
if ( argcount == 0 && tcount == 0 && funnycount == 0 ) {
AN.RepFunList[AN.RepFunNum++] = offset;
AN.RepFunList[AN.RepFunNum++] = 0;
newpat = pattern + pattern[1];
if ( newpat >= AN.patstop ) {
if ( AN.UseFindOnly == 0 ) {
if ( FindOnce(BHEAD AN.findTerm,AN.findPattern) ) {
AT.WorkPointer = oldworkpointer;
AN.UsedOtherFind = 1;
return(1);
}
j = 0;
}
else {
AT.WorkPointer = oldworkpointer;
return(1);
}
}
else j = ScanFunctions(BHEAD newpat,inter,par);
if ( j ) return(j);
goto NoSuccess;
}
tstop = fun + fun[1];
/* Store the wildcard assignments */
params = AT.WorkPointer;
thewildcards = t = params + tcount;
t += ntwa;
if ( oldwilval ) lowlevel = oldworkpointer;
else lowlevel = t;
m = AN.WildValue;
i = nwstore;
if ( i > 0 ) {
r = AT.WildMask;
do {
*t++ = *m++; *t++ = *m++; *t++ = *m++; *t++ = *m++; *t++ = *r++;
} while ( --i > 0 );
*t++ = C->numrhs;
}
if ( t >= AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
AT.WorkPointer = t;
/*
#[ Case 1: no funnies or all funnies must be empty. We just cycle through.
*/
if ( argcount == tcount ) {
if ( funnycount > 0 ) { /* Test all funnies first */
p = pattern + FUNHEAD;
t = fun + FUNHEAD;
while ( p < pstop ) {
if ( *p != FUNNYWILD ) { p++; continue; }
AN.argaddress = t;
if ( CheckWild(BHEAD p[1],ARGTOARG,0,t) ) goto nomatch;
AddWild(BHEAD p[1],ARGTOARG,0);
p += 2;
}
oldwilval = 1;
}
for ( k = 0; k <= type; k++ ) {
if ( k == 0 ) {
p = params; t = fun + FUNHEAD;
while ( t < tstop ) *p++ = *t++;
}
else {
p = params+tcount; t = fun + FUNHEAD;
while ( t < tstop ) *--p = *t++;
}
for ( i = 0; i < tcount; i++ ) { /* The various cycles */
p = pattern + FUNHEAD;
wc = 0;
for ( j = 0; j < tcount; j++, p++ ) { /* The arguments */
while ( *p == FUNNYWILD ) p += 2;
t = params + (i+j)%tcount;
if ( *t == *p ) continue;
if ( *p >= AM.OffsetIndex + WILDOFFSET
&& *p < AM.OffsetIndex + 2*WILDOFFSET ) {
/* Test wildcard index */
wc = *p - WILDOFFSET;
if ( CheckWild(BHEAD wc,INDTOIND,*t,&newvalue) ) break;
AddWild(BHEAD wc,INDTOIND,newvalue);
}
else if ( *t < MINSPEC && p[j] < MINSPEC
&& *p >= AM.OffsetVector + WILDOFFSET ) {
/* Test wildcard vector */
wc = *p - WILDOFFSET;
if ( CheckWild(BHEAD wc,VECTOVEC,*t,&newvalue) ) break;
AddWild(BHEAD wc,VECTOVEC,newvalue);
}
else break;
}
if ( j >= tcount ) { /* Match! */
/* Continue with other functions. Make sure of the funnies */
AN.RepFunList[AN.RepFunNum++] = offset;
AN.RepFunList[AN.RepFunNum++] = 0;
if ( funnycount > 0 ) {
p = pattern + FUNHEAD;
t = fun + FUNHEAD;
while ( p < pstop ) {
if ( *p != FUNNYWILD ) { p++; continue; }
AN.argaddress = t;
AddWild(BHEAD p[1],ARGTOARG,0);
p += 2;
}
}
newpat = pattern + pattern[1];
if ( newpat >= AN.patstop ) {
if ( AN.UseFindOnly == 0 ) {
if ( FindOnce(BHEAD AN.findTerm,AN.findPattern) ) {
AT.WorkPointer = oldworkpointer;
AN.UsedOtherFind = 1;
return(1);
}
j = 0;
}
else {
AT.WorkPointer = oldworkpointer;
return(1);
}
}
else j = ScanFunctions(BHEAD newpat,inter,par);
if ( j ) {
AT.WorkPointer = oldworkpointer;
return(j); /* Full match. Return our success */
}
AN.RepFunNum -= 2;
}
/* No (deeper) match. -> reset wildcards and continue */
if ( wc && nwstore > 0 ) {
j = nwstore;
m = AN.WildValue;
t = thewildcards + ntwa; r = AT.WildMask;
if ( j > 0 ) {
do {
*m++ = *t++; *m++ = *t++; *m++ = *t++; *m++ = *t++; *r++ = *t++;
} while ( --j > 0 );
}
C->numrhs = *t++;
C->Pointer = C->Buffer + oldcpointer;
}
}
}
goto NoSuccess;
}
/*
#] Case 1:
#[ Case 2: One FUNNYWILD. Fix its length.
*/
if ( funnycount == 1 ) {
funnycount = tcount - argcount; /* Number or arguments to be eaten */
for ( k = 0; k <= type; k++ ) {
if ( k == 0 ) {
p = params; t = fun + FUNHEAD;
while ( t < tstop ) *p++ = *t++;
}
else {
p = params+tcount; t = fun + FUNHEAD;
while ( t < tstop ) *--p = *t++;
}
for ( i = 0; i < tcount; i++ ) { /* The various cycles */
p = pattern + FUNHEAD;
t = params;
wc = 0;
for ( j = 0; j < tcount; j++, p++, t++ ) { /* The arguments */
if ( *t == *p ) continue;
if ( *p == FUNNYWILD ) {
p++; wc = 1;
AN.argaddress = t;
if ( CheckWild(BHEAD *p,ARGTOARG,funnycount|EATTENSOR,t) ) break;
AddWild(BHEAD *p,ARGTOARG,funnycount|EATTENSOR);
j += funnycount-1; t += funnycount-1;
}
else if ( *p >= AM.OffsetIndex + WILDOFFSET
&& *p < AM.OffsetIndex + 2*WILDOFFSET ) {
/* Test wildcard index */
wc = *p - WILDOFFSET;
if ( CheckWild(BHEAD wc,INDTOIND,*t,&newvalue) ) break;
AddWild(BHEAD wc,INDTOIND,newvalue);
}
else if ( *t < MINSPEC && *p < MINSPEC
&& *p >= AM.OffsetVector + WILDOFFSET ) {
/* Test wildcard vector */
wc = *p - WILDOFFSET;
if ( CheckWild(BHEAD wc,VECTOVEC,*t,&newvalue) ) break;
AddWild(BHEAD wc,VECTOVEC,newvalue);
}
else break;
}
if ( j >= tcount ) { /* Match! */
/* Continue with other functions. Make sure of the funnies */
AN.RepFunList[AN.RepFunNum++] = offset;
AN.RepFunList[AN.RepFunNum++] = 0;
newpat = pattern + pattern[1];
if ( newpat >= AN.patstop ) {
if ( AN.UseFindOnly == 0 ) {
if ( FindOnce(BHEAD AN.findTerm,AN.findPattern) ) {
AT.WorkPointer = oldworkpointer;
AN.UsedOtherFind = 1;
return(1);
}
j = 0;
}
else {
AT.WorkPointer = oldworkpointer;
return(1);
}
}
else j = ScanFunctions(BHEAD newpat,inter,par);
if ( j ) {
AT.WorkPointer = oldworkpointer;
return(j); /* Full match. Return our success */
}
AN.RepFunNum -= 2;
}
/* No (deeper) match. -> reset wildcards and continue */
if ( wc ) {
j = nwstore;
m = AN.WildValue;
t = thewildcards + ntwa; r = AT.WildMask;
if ( j > 0 ) {
do {
*m++ = *t++; *m++ = *t++; *m++ = *t++; *m++ = *t++; *r++ = *t++;
} while ( --j > 0 );
}
C->numrhs = *t++;
C->Pointer = C->Buffer + oldcpointer;
}
t = params;
wc = *t;
for ( j = 1; j < tcount; j++ ) { *t = t[1]; t++; }
*t = wc;
}
}
goto NoSuccess;
}
/*
#] Case 2:
#[ Case 3: More than one FUNNYWILD. Complicated.
*/
sumeat = tcount - argcount; /* Total number to be eaten by Funnies */
/*
In the first funnycount elements of 'thewildcards' we arrange
for the summing over the various possibilities.
The renumbering table is in thewildcards[2*funnycount]
The multiplicity table is in thewildcards[funnycount]
The number of arguments for each is in thewildcards[]
*/
p = pattern+FUNHEAD;
for ( i = funnycount; i < ntwa; i++ ) thewildcards[i] = -1;
multiplicity = thewildcards + funnycount;
renum = multiplicity + funnycount;
j = 0;
while ( p < pstop ) {
if ( *p != FUNNYWILD ) { p++; continue; }
p++;
if ( renum[*p] < 0 ) {
renum[*p] = j;
multiplicity[j] = 1;
j++;
}
else multiplicity[renum[*p]]++;
p++;
}
/*
Strategy: First 'declared' has a tendency to be smaller
*/
for ( i = 1; i < AN.NumTotWildArgs; i++ ) {
if ( renum[i] < 0 ) continue;
for ( j = i+1; j <= AN.NumTotWildArgs; j++ ) {
if ( renum[j] < 0 ) continue;
if ( renum[i] < renum[j] ) continue;
k = multiplicity[renum[i]];
multiplicity[renum[i]] = multiplicity[renum[j]];
multiplicity[renum[j]] = k;
k = renum[i]; renum[i] = renum[j]; renum[j] = k;
}
}
for ( i = 0; i < funnycount; i++ ) thewildcards[i] = 0;
iraise = funnycount-1;
for ( ;; ) {
for ( i = 0, j = sumeat; i < iraise; i++ )
j -= thewildcards[i]*multiplicity[i];
if ( j < 0 || j % multiplicity[iraise] != 0 ) {
if ( j > 0 ) {
thewildcards[iraise-1]++;
continue;
}
itop = iraise-1;
while ( itop > 0 && j < 0 ) {
j += thewildcards[itop]*multiplicity[itop];
thewildcards[itop] = 0;
itop--;
}
if ( itop <= 0 && j <= 0 ) break;
thewildcards[itop]++;
continue;
}
thewildcards[iraise] = j / multiplicity[iraise];
for ( k = 0; k <= type; k++ ) {
if ( k == 0 ) {
p = params; t = fun + FUNHEAD;
while ( t < tstop ) *p++ = *t++;
}
else {
p = params+tcount; t = fun + FUNHEAD;
while ( t < tstop ) *--p = *t++;
}
for ( i = 0; i < tcount; i++ ) { /* The various cycles */
p = pattern + FUNHEAD;
t = params;
wc = 0;
for ( j = 0; j < tcount; j++, p++, t++ ) { /* The arguments */
if ( *t == *p ) continue;
if ( *p == FUNNYWILD ) {
p++; wc = thewildcards[renum[*p]];
AN.argaddress = t;
if ( CheckWild(BHEAD *p,ARGTOARG,wc|EATTENSOR,t) ) break;
AddWild(BHEAD *p,ARGTOARG,wc|EATTENSOR);
j += wc-1; t += wc-1; wc = 1;
}
else if ( *p >= AM.OffsetIndex + WILDOFFSET
&& *p < AM.OffsetIndex + 2*WILDOFFSET ) {
/* Test wildcard index */
wc = *p - WILDOFFSET;
if ( CheckWild(BHEAD wc,INDTOIND,*t,&newvalue) ) break;
AddWild(BHEAD wc,INDTOIND,newvalue);
}
else if ( *t < MINSPEC && *p < MINSPEC
&& *p >= AM.OffsetVector + WILDOFFSET ) {
/* Test wildcard vector */
wc = *p - WILDOFFSET;
if ( CheckWild(BHEAD wc,VECTOVEC,*t,&newvalue) ) break;
AddWild(BHEAD wc,VECTOVEC,newvalue);
}
else break;
}
if ( j >= tcount ) { /* Match! */
/* Continue with other functions. Make sure of the funnies */
AN.RepFunList[AN.RepFunNum++] = offset;
AN.RepFunList[AN.RepFunNum++] = 0;
newpat = pattern + pattern[1];
if ( newpat >= AN.patstop ) {
if ( AN.UseFindOnly == 0 ) {
if ( FindOnce(BHEAD AN.findTerm,AN.findPattern) ) {
AT.WorkPointer = oldworkpointer;
AN.UsedOtherFind = 1;
return(1);
}
j = 0;
}
else {