1 4 package gnu.xquery.util; 5 import gnu.mapping.*; 6 import gnu.lists.*; 7 import gnu.math.Numeric; 8 import gnu.math.IntNum; 9 import gnu.kawa.functions.AddOp; 10 11 public class Reduce 12 { 13 public static Object sum (Object arg) 14 throws Throwable 15 { 16 return sum(arg, IntNum.zero()); 17 } 18 19 public static Object sum (Object arg, Object zero) 20 throws Throwable 21 { 22 if (arg instanceof Values) 23 { 24 TreeList tlist = (TreeList) arg; 25 int pos = 0; 26 Object next = tlist.getPosNext(pos); 27 if (next == Sequence.eofValue) 28 return zero; 29 Object result = MinMax.convert(next); 30 for (;;) 31 { 32 pos = tlist.nextPos(pos); 33 next = tlist.getPosNext(pos); 34 if (next == Sequence.eofValue) 35 return result; 36 next = MinMax.convert(next); 37 result = AddOp.apply2(1, result, next); 38 } 39 } 40 else 41 return (Number ) MinMax.convert(arg); 42 } 43 } 44 | Popular Tags |