01: // Copyright (c) 2002 Per M.A. Bothner.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.xquery.util;
05:
06: import gnu.mapping.*;
07: import gnu.lists.*;
08: import gnu.math.Numeric;
09: import gnu.math.IntNum;
10: import gnu.kawa.functions.AddOp;
11:
12: public class Reduce {
13: public static Object sum(Object arg) throws Throwable {
14: return sum(arg, IntNum.zero());
15: }
16:
17: public static Object sum(Object arg, Object zero) throws Throwable {
18: if (arg instanceof Values) {
19: TreeList tlist = (TreeList) arg;
20: int pos = 0;
21: Object next = tlist.getPosNext(pos);
22: if (next == Sequence.eofValue)
23: return zero;
24: Object result = MinMax.convert(next);
25: for (;;) {
26: pos = tlist.nextPos(pos);
27: next = tlist.getPosNext(pos);
28: if (next == Sequence.eofValue)
29: return result;
30: next = MinMax.convert(next);
31: result = AddOp.apply2(1, result, next);
32: }
33: } else
34: return (Number) MinMax.convert(arg);
35: }
36: }
|