/******************************************************************************* * Copyright (c) 2007, 2011 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.history.util; import java.util.Iterator; import org.simantics.databoard.Bindings; import org.simantics.databoard.binding.ArrayBinding; import org.simantics.databoard.binding.error.BindingException; import org.simantics.databoard.type.ArrayType; /** * This class binds median class to as an array(double) where every second value * represents value and odd weight. */ public class WeightedMedianBinding extends ArrayBinding { public WeightedMedianBinding() { super(new ArrayType(Bindings.DOUBLE.type()), Bindings.DOUBLE); } @Override public Object create() { return new WeightedMedian(); } @Override public Object create(int length, Iterator it) throws BindingException { WeightedMedian result = new WeightedMedian( length ); int count = length/2; WeightedMedian.Item[] items = new WeightedMedian.Item[count]; int index = 0; double value=0.0, weight=0.0; while (it.hasNext()) { double d = (Double) it.next(); if ( index%2==0 ) { value = d; } else { weight = d; WeightedMedian.Item i = new WeightedMedian.Item(weight, value); items[ index/2 ] = i; } index++; } if ( count>0 ) { items[0].next = items[1]; items[count-1].prev = items[count-2]; } for (index=1; index0 ) { items[0].next = items[1]; items[count-1].prev = items[count-2]; } for (int index=1; index=c ) throw new BindingException("Index "+index+" out of bounds "+c); WeightedMedian.Item i = median.head(); for (int ix=1; ix