2424
2525import static com .oracle .truffle .r .nodes .builtin .CastBuilder .Predef .logicalFalse ;
2626import static com .oracle .truffle .r .nodes .builtin .CastBuilder .Predef .logicalValue ;
27- import static com .oracle .truffle .r .runtime .RError .Message .MATCH_VECTOR_ARGS ;
2827import static com .oracle .truffle .r .runtime .builtins .RBehavior .PURE ;
2928import static com .oracle .truffle .r .runtime .builtins .RBuiltinKind .INTERNAL ;
3029
31- import java .util .Arrays ;
32-
3330import com .oracle .truffle .api .dsl .Cached ;
34- import com .oracle .truffle .api .dsl .Fallback ;
3531import com .oracle .truffle .api .dsl .Specialization ;
36- import com .oracle .truffle .api .profiles .ConditionProfile ;
37- import com .oracle .truffle .r .nodes .builtin .MatchInternalNode ;
32+ import com .oracle .truffle .r .nodes .builtin .Match5Node ;
3833import com .oracle .truffle .r .nodes .builtin .RBuiltinNode ;
39- import com .oracle .truffle .r .nodes .helpers .InheritsCheckNode ;
40- import com .oracle .truffle .r .nodes .helpers .RFactorNodes ;
41- import com .oracle .truffle .r .nodes .unary .CastStringNode ;
4234import com .oracle .truffle .r .runtime .RError .Message ;
43- import com .oracle .truffle .r .runtime .RRuntime ;
44- import com .oracle .truffle .r .runtime .RType ;
4535import com .oracle .truffle .r .runtime .builtins .RBuiltin ;
46- import com .oracle .truffle .r .runtime .data .RDataFactory ;
47- import com .oracle .truffle .r .runtime .data .RNull ;
48- import com .oracle .truffle .r .runtime .data .closures .RClosures ;
49- import com .oracle .truffle .r .runtime .data .RIntVector ;
50- import com .oracle .truffle .r .runtime .data .model .RAbstractListVector ;
51- import com .oracle .truffle .r .runtime .data .model .RAbstractVector ;
5236
5337/*
5438 * TODO: handle "incomparables" parameter.
@@ -60,97 +44,14 @@ public abstract class Match extends RBuiltinNode.Arg4 {
6044
6145 static {
6246 Casts casts = new Casts (Match .class );
63- // TODO initially commented out because of use of scalars, the commented out version
64- // converted to new cast pipelines API
65-
66- // casts.arg("x").allowNull().mustBe(abstractVectorValue(), SHOW_CALLER,
67- // MATCH_VECTOR_ARGS).asVectorPreserveAttrs(true);
68- // casts.arg("table").allowNull().mustBe(abstractVectorValue()).asVectorPreserveAttrs(true);
6947 casts .arg ("nomatch" ).asIntegerVector ().findFirst ();
7048 casts .arg ("incomparables" ).defaultError (Message .GENERIC , "usage of 'incomparables' in match not implemented" ).allowNull ().mustBe (logicalValue ()).asLogicalVector ().findFirst ().mustBe (
7149 logicalFalse ());
7250 }
7351
74- protected boolean isCharSXP (RAbstractListVector list ) {
75- for (int i = 0 ; i < list .getLength (); i ++) {
76- if (!(RType .getRType (list .getDataAt (i )).equals (RType .Char ))) {
77- return false ;
78- }
79- }
80- return true ;
81- }
82-
83- @ Specialization
84- @ SuppressWarnings ("unused" )
85- protected RIntVector match (RNull x , RNull table , int nomatch , Object incomparables ) {
86- return RDataFactory .createIntVector (0 );
87- }
88-
8952 @ Specialization
90- @ SuppressWarnings ("unused" )
91- protected RIntVector match (RNull x , RAbstractVector table , int nomatch , Object incomparables ) {
92- return RDataFactory .createIntVector (0 );
93- }
94-
95- @ Specialization
96- @ SuppressWarnings ("unused" )
97- protected RIntVector match (RAbstractVector x , RNull table , int nomatch , Object incomparables ,
98- @ Cached ("createBinaryProfile()" ) ConditionProfile na ) {
99- int [] data = new int [x .getLength ()];
100- Arrays .fill (data , nomatch );
101- return RDataFactory .createIntVector (data , na .profile (!RRuntime .isNA (nomatch )));
102- }
103-
104- @ Child private InheritsCheckNode factorInheritsCheck = InheritsCheckNode .create (RRuntime .CLASS_FACTOR );
105-
106- protected boolean isFactor (Object o ) {
107- return factorInheritsCheck .execute (o );
108- }
109-
110- @ Specialization (guards = {"isFactor(x)" , "isFactor(table)" })
111- protected Object matchFactor (RIntVector x , RIntVector table , int nomatch , @ SuppressWarnings ("unused" ) Object incomparables ,
112- @ Cached ("create()" ) RFactorNodes .GetLevels getLevelsNode ,
113- @ Cached () MatchInternalNode match ) {
114- return match .execute (RClosures .createFactorToVector (x , true , getLevelsNode .execute (x )),
115- RClosures .createFactorToVector (table , true , getLevelsNode .execute (table )), nomatch );
116- }
117-
118- @ Specialization (guards = {"isFactor(x)" , "!isFactor(table)" })
119- protected Object matchFactor (RIntVector x , RAbstractVector table , int nomatch , @ SuppressWarnings ("unused" ) Object incomparables ,
120- @ Cached ("create()" ) RFactorNodes .GetLevels getLevelsNode ,
121- @ Cached () MatchInternalNode match ) {
122- return match .execute (RClosures .createFactorToVector (x , true , getLevelsNode .execute (x )), table , nomatch );
123- }
124-
125- @ Specialization (guards = {"!isFactor(x)" , "isFactor(table)" })
126- protected Object matchFactor (RAbstractVector x , RIntVector table , int nomatch , @ SuppressWarnings ("unused" ) Object incomparables ,
127- @ Cached ("create()" ) RFactorNodes .GetLevels getLevelsNode ,
128- @ Cached () MatchInternalNode match ) {
129- return match .execute (x , RClosures .createFactorToVector (table , true , getLevelsNode .execute (table )), nomatch );
130- }
131-
132- @ Specialization (guards = {"isCharSXP(x)" , "isCharSXP(table)" })
133- protected Object matchDoubleList (RAbstractListVector x , RAbstractListVector table , int nomatchObj , @ SuppressWarnings ("unused" ) Object incomparables ,
134- @ Cached () MatchInternalNode match ) {
135- return match .execute (x , table , nomatchObj );
136- }
137-
138- @ Specialization (guards = {"!isRIntVector(table) || !isFactor(table)" })
139- protected Object matchList (RAbstractListVector x , RAbstractVector table , int nomatchObj , @ SuppressWarnings ("unused" ) Object incomparables ,
140- @ Cached ("create()" ) CastStringNode cast ,
141- @ Cached () MatchInternalNode match ) {
142- return match .execute ((RAbstractVector ) cast .doCast (x ), table , nomatchObj );
143- }
144-
145- @ Specialization (guards = {"!isRAbstractListVector(x)" , "!isRIntVector(x) || !isFactor(x)" , "!isRIntVector(table) || !isFactor(table)" })
146- protected Object match (RAbstractVector x , RAbstractVector table , int noMatch , @ SuppressWarnings ("unused" ) Object incomparables ,
147- @ Cached () MatchInternalNode match ) {
148- return match .execute (x , table , noMatch );
149- }
150-
151- @ Fallback
152- @ SuppressWarnings ("unused" )
153- protected RIntVector match (Object x , Object table , Object nomatch , Object incomparables ) {
154- throw error (MATCH_VECTOR_ARGS );
53+ Object doIt (Object x , Object table , int nomatch , Object incomparables ,
54+ @ Cached Match5Node match5Node ) {
55+ return match5Node .execute (x , table , nomatch , incomparables );
15556 }
15657}
0 commit comments