|
41 | 41 | import com.oracle.graal.python.builtins.objects.function.PKeyword;
|
42 | 42 | import com.oracle.graal.python.builtins.objects.itertools.PAccumulate;
|
43 | 43 | import com.oracle.graal.python.builtins.objects.itertools.PChain;
|
| 44 | +import com.oracle.graal.python.builtins.objects.itertools.PCombinations; |
| 45 | +import com.oracle.graal.python.builtins.objects.itertools.PCombinationsWithReplacement; |
44 | 46 | import com.oracle.graal.python.builtins.objects.itertools.PCompress;
|
45 | 47 | import com.oracle.graal.python.builtins.objects.itertools.PCount;
|
46 | 48 | import com.oracle.graal.python.builtins.objects.itertools.PDropwhile;
|
|
61 | 63 | import com.oracle.graal.python.nodes.call.special.CallVarargsMethodNode;
|
62 | 64 | import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
|
63 | 65 | import com.oracle.graal.python.nodes.function.builtins.PythonBinaryClinicBuiltinNode;
|
| 66 | +import com.oracle.graal.python.nodes.function.builtins.PythonTernaryClinicBuiltinNode; |
64 | 67 | import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
|
65 | 68 | import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
|
66 | 69 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
|
@@ -99,6 +102,64 @@ protected Object notype(Object cls, Object[] arguments, PKeyword[] keywords,
|
99 | 102 | }
|
100 | 103 | }
|
101 | 104 |
|
| 105 | + @Builtin(name = "combinations", minNumOfPositionalArgs = 3, constructsClass = PythonBuiltinClassType.PCombinations, parameterNames = {"$self", "iterable", |
| 106 | + "r"}, doc = "combinations(iterable, r) --> combinations object\n\n" + |
| 107 | + "Return successive r-length combinations of elements in the iterable.\n\n" + |
| 108 | + "combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)") |
| 109 | + @ArgumentClinic(name = "r", conversion = ArgumentClinic.ClinicConversion.Int) |
| 110 | + @GenerateNodeFactory |
| 111 | + public abstract static class CombinationsNode extends PythonTernaryClinicBuiltinNode { |
| 112 | + |
| 113 | + @Override |
| 114 | + protected ArgumentClinicProvider getArgumentClinic() { |
| 115 | + return ItertoolsModuleBuiltinsClinicProviders.CombinationsNodeClinicProviderGen.INSTANCE; |
| 116 | + } |
| 117 | + |
| 118 | + @SuppressWarnings("unused") |
| 119 | + @Specialization(guards = "isTypeNode.execute(cls)", limit = "1") |
| 120 | + protected PCombinations construct(Object cls, Object iterable, int r, |
| 121 | + @Cached TypeNodes.IsTypeNode isTypeNode) { |
| 122 | + return factory().createCombinations(); |
| 123 | + } |
| 124 | + |
| 125 | + @SuppressWarnings("unused") |
| 126 | + @Specialization(guards = "!isTypeNode.execute(cls)") |
| 127 | + protected Object notype(Object cls, Object iterable, Object r, |
| 128 | + @SuppressWarnings("unused") @Cached TypeNodes.IsTypeNode isTypeNode) { |
| 129 | + throw raise(TypeError, ErrorMessages.IS_NOT_TYPE_OBJ, "'cls'", cls); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // XXX this vs. init? |
| 134 | + @Builtin(name = "combinations_with_replacement", minNumOfPositionalArgs = 3, constructsClass = PythonBuiltinClassType.PCombinationsWithReplacement, parameterNames = {"$self", "iterable", |
| 135 | + "r"}, doc = "combinations_with_replacement(iterable, r) --> combinations_with_replacement object\n\n" + |
| 136 | + "Return successive r-length combinations of elements in the iterable\n" + |
| 137 | + "allowing individual elements to have successive repeats.\n" + |
| 138 | + " combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC") |
| 139 | + @ArgumentClinic(name = "r", conversion = ArgumentClinic.ClinicConversion.Int) |
| 140 | + @GenerateNodeFactory |
| 141 | + public abstract static class CombinationsWithReplacementNode extends PythonTernaryClinicBuiltinNode { |
| 142 | + |
| 143 | + @Override |
| 144 | + protected ArgumentClinicProvider getArgumentClinic() { |
| 145 | + return ItertoolsModuleBuiltinsClinicProviders.CombinationsNodeClinicProviderGen.INSTANCE; |
| 146 | + } |
| 147 | + |
| 148 | + @SuppressWarnings("unused") |
| 149 | + @Specialization(guards = "isTypeNode.execute(cls)", limit = "1") |
| 150 | + protected PCombinationsWithReplacement construct(Object cls, Object iterable, int r, |
| 151 | + @Cached TypeNodes.IsTypeNode isTypeNode) { |
| 152 | + return factory().createCombinationsWithReplacement(); |
| 153 | + } |
| 154 | + |
| 155 | + @SuppressWarnings("unused") |
| 156 | + @Specialization(guards = "!isTypeNode.execute(cls)") |
| 157 | + protected Object notype(Object cls, Object iterable, Object r, |
| 158 | + @SuppressWarnings("unused") @Cached TypeNodes.IsTypeNode isTypeNode) { |
| 159 | + throw raise(TypeError, ErrorMessages.IS_NOT_TYPE_OBJ, "'cls'", cls); |
| 160 | + } |
| 161 | + } |
| 162 | + |
102 | 163 | @Builtin(name = "compress", minNumOfPositionalArgs = 1, takesVarArgs = true, takesVarKeywordArgs = true, constructsClass = PythonBuiltinClassType.PCompress, doc = "Make an iterator that filters elements from *data* returning\n" +
|
103 | 164 | "only those that have a corresponding element in *selectors* that evaluates to\n" +
|
104 | 165 | "``True``. Stops when either the *data* or *selectors* iterables has been\n" +
|
@@ -345,6 +406,9 @@ protected Object construct(Object cls, Object[] arguments, PKeyword[] keywords)
|
345 | 406 | }
|
346 | 407 | }
|
347 | 408 |
|
| 409 | + // XXX all c-tors should implement what is done in init and remove init, it is a noop in python |
| 410 | + // too |
| 411 | + // XXX add tests for it |
348 | 412 | @Builtin(name = "repeat", minNumOfPositionalArgs = 1, takesVarArgs = true, takesVarKeywordArgs = true, constructsClass = PythonBuiltinClassType.PRepeat, doc = "repeat(object [,times]) -> create an iterator which returns the object\n" +
|
349 | 413 | "for the specified number of times. If not specified, returns the object\nendlessly.")
|
350 | 414 | @GenerateNodeFactory
|
|
0 commit comments