001: // ============================================================================
002: // $Id: Summarize.java,v 1.5 2006/12/05 04:45:44 davidahall Exp $
003: // Copyright (c) 2006 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032:
033: package net.sf.jga.algorithms;
034:
035: import java.util.Collection;
036: import java.util.Comparator;
037: import java.util.Iterator;
038: import net.sf.jga.fn.BinaryFunctor;
039: import net.sf.jga.fn.UnaryFunctor;
040: import net.sf.jga.fn.algorithm.Accumulate;
041: import net.sf.jga.fn.arithmetic.Arithmetic;
042: import net.sf.jga.fn.arithmetic.ArithmeticFactory;
043: import net.sf.jga.fn.arithmetic.Average;
044: import net.sf.jga.fn.arithmetic.Plus;
045: import net.sf.jga.fn.comparison.EqualTo;
046: import net.sf.jga.fn.comparison.Equality;
047: import net.sf.jga.fn.comparison.Max;
048: import net.sf.jga.fn.comparison.Min;
049: import net.sf.jga.util.ArrayUtils;
050:
051: /**
052: * Algorithms that consume input and produce a single value. The input may be
053: * an array, collection, or iteration.
054: * <p>
055: * Copyright © 2006 David A. Hall
056: */
057:
058: public class Summarize {
059:
060: // -------------------
061: // counting algorithms
062: // -------------------
063:
064: /**
065: * Returns the number of times that the given value appears in the array
066: */
067: static public <T> long count(T[] ts, T value) {
068: return count(ArrayUtils.iterate(ts), new EqualTo<T>()
069: .bind2nd(value));
070: }
071:
072: /**
073: * Returns the number of times that the given value appears in the array, using
074: * the given equality operator
075: */
076: static public <T> long count(T[] ts, Equality<T> eq, T value) {
077: return count(ArrayUtils.iterate(ts), eq.bind2nd(value));
078: }
079:
080: /**
081: * Returns the number of elements in the array for which the predicate is true
082: */
083: static public <T> long count(T[] ts, UnaryFunctor<T, Boolean> pred) {
084: return count(ArrayUtils.iterate(ts), pred);
085: }
086:
087: /**
088: * Returns the number of elements in the input.
089: */
090: static public <T> long count(Iterable<T> input) {
091: return count(input.iterator());
092: }
093:
094: /**
095: * Returns the number of times that the given value appears in the input.
096: */
097: static public <T> long count(Iterable<? extends T> input, T value) {
098: return count(input.iterator(), value);
099: }
100:
101: /**
102: * Returns the number of times that the given value appears in the input, using
103: * the given equality operator.
104: */
105: static public <T> long count(Iterable<? extends T> input,
106: Equality<T> eq, T value) {
107: return count(input.iterator(), eq, value);
108: }
109:
110: /**
111: * Returns the number of elements in the input for which the predicate is true.
112: */
113: static public <T> long count(Iterable<? extends T> input,
114: UnaryFunctor<T, Boolean> pred) {
115: return count(input.iterator(), pred);
116: }
117:
118: /**
119: * Returns the number of elements in the iterator. The iterator is consumed entirely
120: * by this function.
121: */
122: static public <T> long count(Iterator<? extends T> iter) {
123: long ct = 0;
124: while (iter.hasNext()) {
125: iter.next();
126: ++ct;
127: }
128:
129: return ct;
130: }
131:
132: /**
133: * Returns the number of times that the given value appears in the iterator. The
134: * iterator is consumed entirely by this function.
135: */
136: static public <T> long count(Iterator<? extends T> iter, T value) {
137: return count(Filter.filter(iter, new EqualTo<T>()
138: .bind2nd(value)));
139: }
140:
141: /**
142: * Returns the number of times that the given value appears in the iterator, using
143: * the given equality operator. The iterator is consumed entirely by this function.
144: */
145: static public <T> long count(Iterator<? extends T> iter,
146: Equality<T> eq, T value) {
147: return count(Filter.filter(iter, eq.bind2nd(value)));
148: }
149:
150: /**
151: * Returns the number of elements in the iterator for which the predicate is true. The
152: * iterator is consumed entirely by this function.
153: */
154: static public <T> long count(Iterator<? extends T> iter,
155: UnaryFunctor<T, Boolean> pred) {
156: return count(Filter.filter(iter, pred));
157: }
158:
159: // -------------------
160: // minimum value
161: // -------------------
162:
163: /**
164: * Returns the smallest T value in the input, as determined by the given comparator.
165: */
166: static public <T> T min(T[] ts, Comparator<? super T> comp) {
167: return min(ArrayUtils.iterate(ts), new Min<T>(comp));
168: }
169:
170: /**
171: * Returns the smallest T value in the input.
172: */
173: static public <T extends Comparable<? super T>> T min(T[] ts) {
174: return min(ArrayUtils.iterate(ts), new Min.Comparable<T>());
175: }
176:
177: /**
178: * Returns the smallest T value in the input, as determined by the given functor.
179: * The functor is expected to return the smaller of its two arguments.
180: */
181: static public <T> T min(T[] ts, BinaryFunctor<T, T, T> fn) {
182: return min(ArrayUtils.iterate(ts), fn);
183: }
184:
185: /**
186: * Returns the smallest T value in the input, as determined by the given comparator.
187: */
188: static public <T> T min(Iterable<? extends T> ts,
189: Comparator<? super T> comp) {
190: return min(ts.iterator(), new Min<T>(comp));
191: }
192:
193: /**
194: * Returns the smallest T value in the input.
195: */
196: static public <T extends Comparable<? super T>> T min(
197: Iterable<? extends T> ts) {
198: return min(ts.iterator(), new Min.Comparable<T>());
199: }
200:
201: /**
202: * Returns the smallest T value in the input, as determined by the given functor.
203: * The functor is expected to return the smaller of its two arguments.
204: */
205: static public <T> T min(Iterable<? extends T> ts,
206: BinaryFunctor<T, T, T> fn) {
207: return min(ts.iterator(), fn);
208: }
209:
210: /**
211: * Returns the smallest T value in the input, as determined by the given comparator.
212: * The input iterator is consumed.
213: */
214: static public <T> T min(Iterator<? extends T> ts,
215: Comparator<? super T> comp) {
216: return min(ts, new Min<T>(comp));
217: }
218:
219: /**
220: * Returns the smallest T value in the input. The input iterator is consumed.
221: */
222: static public <T extends Comparable<? super T>> T min(
223: Iterator<? extends T> ts) {
224: return min(ts, new Min.Comparable<T>());
225: }
226:
227: /**
228: * Returns the smallest T value in the input, as determined by the given functor.
229: * The functor is expected to return the smaller of its two arguments.
230: * The input iterator is consumed.
231: */
232: static public <T> T min(Iterator<? extends T> ts,
233: BinaryFunctor<T, T, T> fn) {
234: return new Accumulate<T>(fn).fn(ts);
235: }
236:
237: // -------------------
238: // maximum value
239: // -------------------
240:
241: /**
242: * Returns the largest T value in the input, as determined by the given comparator.
243: */
244: static public <T> T max(T[] ts, Comparator<? super T> comp) {
245: return max(ArrayUtils.iterate(ts), new Max<T>(comp));
246: }
247:
248: /**
249: * Returns the largest T value in the input.
250: */
251: static public <T extends Comparable<? super T>> T max(T[] ts) {
252: return max(ArrayUtils.iterate(ts), new Max.Comparable<T>());
253: }
254:
255: /**
256: * Returns the largest T value in the input, as determined by the given functor.
257: * The functor is expected to return the smaller of its two arguments.
258: */
259: static public <T> T max(T[] ts, BinaryFunctor<T, T, T> fn) {
260: return max(ArrayUtils.iterate(ts), fn);
261: }
262:
263: /**
264: * Returns the largest T value in the input, as determined by the given comparator.
265: */
266: static public <T> T max(Iterable<? extends T> ts,
267: Comparator<? super T> comp) {
268: return max(ts.iterator(), new Max<T>(comp));
269: }
270:
271: /**
272: * Returns the largest T value in the input.
273: */
274: static public <T extends Comparable<? super T>> T max(
275: Iterable<? extends T> ts) {
276: return max(ts.iterator(), new Max.Comparable<T>());
277: }
278:
279: /**
280: * Returns the largest T value in the input, as determined by the given functor.
281: * The functor is expected to return the smaller of its two arguments.
282: */
283: static public <T> T max(Iterable<? extends T> ts,
284: BinaryFunctor<T, T, T> fn) {
285: return max(ts.iterator(), fn);
286: }
287:
288: /**
289: * Returns the largest T value in the input, as determined by the given comparator.
290: * The input iterator is consumed.
291: */
292: static public <T> T max(Iterator<? extends T> ts,
293: Comparator<? super T> comp) {
294: return max(ts, new Max<T>(comp));
295: }
296:
297: /**
298: * Returns the largest T value in the input. The input iterator is consumed.
299: */
300: static public <T extends Comparable<? super T>> T max(
301: Iterator<? extends T> ts) {
302: return max(ts, new Max.Comparable<T>());
303: }
304:
305: /**
306: * Returns the largest T value in the input, as determined by the given functor.
307: * The functor is expected to return the smaller of its two arguments.
308: * The input iterator is consumed.
309: */
310: static public <T> T max(Iterator<? extends T> ts,
311: BinaryFunctor<T, T, T> fn) {
312: return new Accumulate<T>(fn).fn(ts);
313: }
314:
315: // -------------------
316: // total value
317: // -------------------
318:
319: /**
320: * Returns the sum of the values
321: */
322: static public <T extends Number> T sum(T[] ts) {
323: return sum((Class<T>) ts.getClass().getComponentType(),
324: ArrayUtils.iterate(ts));
325: }
326:
327: /**
328: * Returns the sum of the values
329: */
330: static public <T extends Number> T sum(Class<T> type, T[] ts) {
331: return sum(type, ArrayUtils.iterate(ts));
332: }
333:
334: /**
335: * Returns the sum of the values
336: */
337: static public <T extends Number> T sum(Class<T> type,
338: Iterable<? extends T> ts) {
339: return sum(type, ts.iterator());
340: }
341:
342: /**
343: * Returns the sum of the values. The iterator is entirely consumed.
344: */
345: static public <T extends Number> T sum(Class<T> type,
346: Iterator<? extends T> ts) {
347: return accumulate(ts, new Plus<T>(type));
348: }
349:
350: // -------------------
351: // accumulated value
352: // -------------------
353:
354: static public <T> T accumulate(T[] ts, BinaryFunctor<T, T, T> fn) {
355: return accumulate(ArrayUtils.iterate(ts), fn);
356: }
357:
358: static public <T> T accumulate(T[] ts, T initial,
359: BinaryFunctor<T, T, T> fn) {
360: return accumulate(ArrayUtils.iterate(ts), initial, fn);
361:
362: }
363:
364: static public <T> T accumulate(Iterable<? extends T> values,
365: BinaryFunctor<T, T, T> fn) {
366: return accumulate(values.iterator(), fn);
367: }
368:
369: static public <T> T accumulate(Iterable<? extends T> values,
370: T initial, BinaryFunctor<T, T, T> fn) {
371: return accumulate(values.iterator(), initial, fn);
372: }
373:
374: static public <T> T accumulate(Iterator<? extends T> iterator,
375: BinaryFunctor<T, T, T> fn) {
376: return new Accumulate<T>(fn).fn(iterator);
377: }
378:
379: static public <T> T accumulate(Iterator<? extends T> iterator,
380: T initial, BinaryFunctor<T, T, T> fn) {
381: return new Accumulate<T>(initial, fn).fn(iterator);
382: }
383:
384: // --------------
385: // Average value
386: // --------------
387:
388: /**
389: * Returns the average of the values
390: */
391: static public <T extends Number> T average(T[] ts) {
392: return average((Class<T>) ts.getClass().getComponentType(),
393: ArrayUtils.iterate(ts));
394: }
395:
396: /**
397: * Returns the average of the values
398: */
399: static public <T extends Number> T average(Class<T> type, T[] ts) {
400: return average(type, ArrayUtils.iterate(ts));
401: }
402:
403: /**
404: * Returns the average of the values
405: */
406: static public <T extends Number> T average(Class<T> type,
407: Iterable<T> ts) {
408: return average(type, ts.iterator());
409: }
410:
411: /**
412: * Returns the average of the values. The iterator is entirely consumed.
413: */
414: static public <T extends Number> T average(Class<T> type,
415: Iterator<T> ts) {
416: return new Average<T>(type).fn(ts);
417: }
418: }
|