3636import org .projectnessie .cel .common .types .UintT ;
3737import org .projectnessie .cel .common .types .ref .Val ;
3838import org .projectnessie .cel .common .types .traits .Lister ;
39+ import org .projectnessie .cel .interpreter .functions .FunctionOp ;
3940import org .projectnessie .cel .interpreter .functions .Overload ;
41+ import org .projectnessie .cel .interpreter .functions .QuaternaryOp ;
42+ import org .projectnessie .cel .interpreter .functions .QuinaryOp ;
43+ import org .projectnessie .cel .interpreter .functions .TernaryOp ;
4044
4145/** MathLib provides CEL helper functions from the standard math extension library. */
4246public final class MathLib implements Library {
@@ -91,10 +95,34 @@ public List<EnvOption> getCompileOptions() {
9195 public List <ProgramOption > getProgramOptions () {
9296 List <Overload > overloads = new ArrayList <>();
9397 overloads .add (
94- Overload .overload (GREATEST , null , MathLib ::greatest , MathLib ::greatest , MathLib ::greatest ));
95- overloads .add (Overload .overload (LEAST , null , MathLib ::least , MathLib ::least , MathLib ::least ));
96- addArityOverloads (overloads , GREATEST , MathLib ::greatest );
97- addArityOverloads (overloads , LEAST , MathLib ::least );
98+ Overload .overload (
99+ GREATEST ,
100+ null ,
101+ MathLib ::greatest ,
102+ MathLib ::greatest ,
103+ MathLib ::greatest ,
104+ MathLib ::greatest ,
105+ MathLib ::greatest ,
106+ MathLib ::greatest ));
107+ overloads .add (
108+ Overload .overload (
109+ LEAST ,
110+ null ,
111+ MathLib ::least ,
112+ MathLib ::least ,
113+ MathLib ::least ,
114+ MathLib ::least ,
115+ MathLib ::least ,
116+ MathLib ::least ));
117+ addArityOverloads (
118+ overloads ,
119+ GREATEST ,
120+ MathLib ::greatest ,
121+ MathLib ::greatest ,
122+ MathLib ::greatest ,
123+ MathLib ::greatest );
124+ addArityOverloads (
125+ overloads , LEAST , MathLib ::least , MathLib ::least , MathLib ::least , MathLib ::least );
98126 overloads .add (Overload .unary (CEIL , MathLib ::ceil ));
99127 overloads .add (Overload .unary (overloadId (CEIL , 1 ), MathLib ::ceil ));
100128 overloads .add (Overload .unary (FLOOR , MathLib ::floor ));
@@ -164,15 +192,18 @@ private static List<Type> dynArgs(int count) {
164192 private static void addArityOverloads (
165193 List <Overload > overloads ,
166194 String function ,
167- org .projectnessie .cel .interpreter .functions .FunctionOp op ) {
195+ FunctionOp op ,
196+ TernaryOp ternaryOp ,
197+ QuaternaryOp quaternaryOp ,
198+ QuinaryOp quinaryOp ) {
168199 overloads .add (Overload .unary (overloadId (function , "int" ), op ::invoke ));
169200 overloads .add (Overload .unary (overloadId (function , "uint" ), op ::invoke ));
170201 overloads .add (Overload .unary (overloadId (function , "double" ), op ::invoke ));
171202 overloads .add (
172203 Overload .binary (overloadId (function , 2 ), (left , right ) -> op .invoke (left , right )));
173- for ( int arity = 3 ; arity <= 5 ; arity ++) {
174- overloads .add (Overload .function (overloadId (function , arity ), op ));
175- }
204+ overloads . add ( Overload . ternary ( overloadId ( function , 3 ), ternaryOp ));
205+ overloads .add (Overload .quaternary (overloadId (function , 4 ), quaternaryOp ));
206+ overloads . add ( Overload . quinary ( overloadId ( function , 5 ), quinaryOp ));
176207 overloads .add (Overload .unary (overloadId (function , "list" ), op ::invoke ));
177208 }
178209
@@ -188,10 +219,87 @@ private static Val greatest(Val... values) {
188219 return minMax (values , true );
189220 }
190221
222+ private static Val greatest (Val first , Val second , Val third ) {
223+ return minMax (first , second , third , true );
224+ }
225+
226+ private static Val greatest (Val first , Val second , Val third , Val fourth ) {
227+ return minMax (first , second , third , fourth , true );
228+ }
229+
230+ private static Val greatest (Val first , Val second , Val third , Val fourth , Val fifth ) {
231+ return minMax (first , second , third , fourth , fifth , true );
232+ }
233+
191234 private static Val least (Val ... values ) {
192235 return minMax (values , false );
193236 }
194237
238+ private static Val least (Val first , Val second , Val third ) {
239+ return minMax (first , second , third , false );
240+ }
241+
242+ private static Val least (Val first , Val second , Val third , Val fourth ) {
243+ return minMax (first , second , third , fourth , false );
244+ }
245+
246+ private static Val least (Val first , Val second , Val third , Val fourth , Val fifth ) {
247+ return minMax (first , second , third , fourth , fifth , false );
248+ }
249+
250+ private static Val minMax (
251+ Val first , Val second , Val third , Val fourth , Val fifth , boolean greatest ) {
252+ Val result = minMax (first , second , third , fourth , greatest );
253+ if (!isNumber (result )) {
254+ return result ;
255+ }
256+ if (!isNumber (fifth )) {
257+ return noSuchOverload ();
258+ }
259+ int cmp = compareNumbers (fifth , result );
260+ if ((greatest && cmp > 0 ) || (!greatest && cmp < 0 )) {
261+ result = fifth ;
262+ }
263+ return result ;
264+ }
265+
266+ private static Val minMax (Val first , Val second , Val third , Val fourth , boolean greatest ) {
267+ Val result = minMax (first , second , third , greatest );
268+ if (!isNumber (result )) {
269+ return result ;
270+ }
271+ if (!isNumber (fourth )) {
272+ return noSuchOverload ();
273+ }
274+ int cmp = compareNumbers (fourth , result );
275+ if ((greatest && cmp > 0 ) || (!greatest && cmp < 0 )) {
276+ result = fourth ;
277+ }
278+ return result ;
279+ }
280+
281+ private static Val minMax (Val first , Val second , Val third , boolean greatest ) {
282+ if (!isNumber (first )) {
283+ return noSuchOverload ();
284+ }
285+ Val result = first ;
286+ if (!isNumber (second )) {
287+ return noSuchOverload ();
288+ }
289+ int cmp = compareNumbers (second , result );
290+ if ((greatest && cmp > 0 ) || (!greatest && cmp < 0 )) {
291+ result = second ;
292+ }
293+ if (!isNumber (third )) {
294+ return noSuchOverload ();
295+ }
296+ cmp = compareNumbers (third , result );
297+ if ((greatest && cmp > 0 ) || (!greatest && cmp < 0 )) {
298+ result = third ;
299+ }
300+ return result ;
301+ }
302+
195303 private static Val minMax (Val [] values , boolean greatest ) {
196304 List <Val > candidates = candidates (values );
197305 if (candidates .isEmpty ()) {
0 commit comments