]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.history/src/org/simantics/history/util/WeightedMedianBinding.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.history / src / org / simantics / history / util / WeightedMedianBinding.java
diff --git a/bundles/org.simantics.history/src/org/simantics/history/util/WeightedMedianBinding.java b/bundles/org.simantics.history/src/org/simantics/history/util/WeightedMedianBinding.java
new file mode 100644 (file)
index 0000000..f91e780
--- /dev/null
@@ -0,0 +1,163 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.history.util;\r
+\r
+import java.util.Iterator;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.ArrayBinding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.type.ArrayType;\r
+\r
+/**\r
+ * This class binds median class to as an array(double) where every second value\r
+ * represents value and odd weight.\r
+ */\r
+public class WeightedMedianBinding extends ArrayBinding {\r
+\r
+       public WeightedMedianBinding() {\r
+               super(new ArrayType(Bindings.DOUBLE.type()), Bindings.DOUBLE);\r
+       }\r
+       \r
+       @Override\r
+       public Object create() {\r
+               return new WeightedMedian();\r
+       }\r
+\r
+       @Override\r
+       public Object create(int length, Iterator<Object> it) throws BindingException {         \r
+               WeightedMedian result = new WeightedMedian( length );\r
+               int count = length/2;\r
+               WeightedMedian.Item[] items = new WeightedMedian.Item[count];           \r
+               int index = 0;\r
+               double value=0.0, weight=0.0;\r
+               while (it.hasNext()) {\r
+                       double d = (Double) it.next();\r
+                       if ( index%2==0 ) {\r
+                               value = d;\r
+                       } else {\r
+                               weight = d;\r
+                               WeightedMedian.Item i = new WeightedMedian.Item(weight, value);\r
+                               items[ index/2 ] = i;\r
+                       }\r
+                       index++;\r
+               }\r
+               if ( count>0 ) {\r
+                       items[0].next = items[1];\r
+                       items[count-1].prev = items[count-2];\r
+               }               \r
+               for (index=1; index<count-1; index++) {\r
+                       items[index].next = items[index+1];\r
+                       items[index].prev = items[index-1];\r
+               }\r
+               result.median = items[count/2];\r
+               result.itemCount = count;\r
+               return result;\r
+       }\r
+\r
+       @Override\r
+       public Object create(Object[] array) throws BindingException {\r
+               int count = array.length/2;\r
+               WeightedMedian result = new WeightedMedian();\r
+               WeightedMedian.Item[] items = new WeightedMedian.Item[count];           \r
+               double value=0.0, weight=0.0;\r
+               for (int index=0; index<array.length; index++) {\r
+                       double d = (Double) array[index];\r
+                       if ( index%2==0 ) {\r
+                               value = d;\r
+                       } else {\r
+                               weight = d;\r
+                               WeightedMedian.Item i = new WeightedMedian.Item(weight, value);\r
+                               items[ index/2 ] = i;\r
+                       }\r
+                       index++;\r
+               }\r
+               if ( count>0 ) {\r
+                       items[0].next = items[1];\r
+                       items[count-1].prev = items[count-2];\r
+               }               \r
+               for (int index=1; index<count-1; index++) {\r
+                       items[index].next = items[index+1];\r
+                       items[index].prev = items[index-1];\r
+               }\r
+               result.median = items[count/2];\r
+               result.itemCount = count;\r
+               return result;\r
+       }\r
+\r
+       @Override\r
+       public void add(Object array, int index, Object element) throws BindingException, IndexOutOfBoundsException {\r
+               throw new BindingException("not implemented");\r
+       }\r
+\r
+       @Override\r
+       public void remove(Object array, int index, int count) throws BindingException {\r
+               throw new BindingException("not implemented");\r
+       }\r
+\r
+       @Override\r
+       public Object get(Object array, int index) throws BindingException {\r
+               int pos = index/2;\r
+               WeightedMedian median = (WeightedMedian) array;\r
+               int c = median.itemCount*2;\r
+               if ( index<0 || index>=c ) throw new BindingException("Index "+index+" out of bounds "+c);\r
+               WeightedMedian.Item i = median.head();\r
+               for (int ix=1; ix<pos; ix++) i = i.next;\r
+               return index%2==0?i.value:i.weight;\r
+       }\r
+\r
+       @Override\r
+       public void getAll(Object array, Object[] result) throws BindingException {\r
+               int c = size( array );\r
+               if (result.length<c) throw new BindingException("Array too small");\r
+               WeightedMedian median = (WeightedMedian) array;\r
+               int ix = 0;\r
+               WeightedMedian.Item i = median.head();\r
+               while (i != null) {\r
+                       result[ix] = i.value; ix++;\r
+                       result[ix] = i.weight; ix++;\r
+                       i = i.next;\r
+               }\r
+       }\r
+\r
+       @Override\r
+       public void set(Object array, int index, Object value) throws BindingException {\r
+               remove(array, index);\r
+               add(array, value);\r
+       }\r
+\r
+       @Override\r
+       public void setSize(Object array, int newSize) throws BindingException {\r
+               int size = size(array);\r
+               if (size<newSize) newSize = size;\r
+               remove( array, size-newSize, newSize);\r
+       }\r
+\r
+       @Override\r
+       public int size(Object array) throws BindingException {\r
+               WeightedMedian median = (WeightedMedian) array;\r
+               return median.itemCount*2;\r
+       }\r
+\r
+       @Override\r
+       public boolean isInstance(Object obj) {\r
+               return obj instanceof WeightedMedian;\r
+       }       \r
+\r
+       @Override\r
+       public boolean isResizable() {\r
+               return true;\r
+       }\r
+       \r
+}\r
+\r
+\r