/* // $Id: //guest/julian_hyde/saffron/src/main/saffron/runtime/Median.java#1 $ // Saffron preprocessor and data engine // Copyright (C) 2002 Julian Hyde <julian.hyde@mail.com> // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public // License as published by the Free Software Foundation; either // version 2 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Library General Public License for more details. // // You should have received a copy of the GNU Library General Public // License along with this library; if not, write to the // Free Software Foundation, Inc., 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. // // See the COPYING file located in the top-level-directory of // the archive of this library for complete text of license. */ package saffron.runtime; import saffron.AggregatorExtender; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; /** * <code>Median</code> is an aggregator which returns the Median of a set of * points. * * @author jhyde * @since 26 January, 2001 * @version $Id: //guest/julian_hyde/saffron/src/main/saffron/runtime/Median.java#1 $ **/ public class Median implements AggregatorExtender { public Object start(Comparable c) { return new ArrayList(); } public void addOne(Comparable c, Object accumulator) { ((ArrayList) accumulator).add(c); } public void merge(Comparable c, Object accumulator1, Object accumulator2) { ((ArrayList) accumulator1).addAll((ArrayList) accumulator2); } public Object result(Object accumulator) { Object[] a = ((ArrayList) accumulator).toArray(); Arrays.sort(a, new ArrayComparator()); if (a.length < 1) { return null; } int i = a.length / 2; return a[i]; } static class ArrayComparator implements Comparator { // implement Comparator public int compare(Object o, Object p) { Object[] a = (Object[]) o, b = (Object[]) p; for (int i = 0; i < a.length; i++) { int c = ((Comparable) a[i]).compareTo(b[i]); if (c != 0) { return c; } } return 0; } }; } // End Median.java
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 1475 | Julian Hyde |
Saffron. Re-integrate Median, etc. |
||
#1 | 1467 | Julian Hyde |
saffron: First saffron check-in; incorporate my changes to openjava. |