]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.history/src/org/simantics/history/util/WeightedMedianBinding.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.history / src / org / simantics / history / util / WeightedMedianBinding.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.history.util;
13
14 import java.util.Iterator;
15
16 import org.simantics.databoard.Bindings;
17 import org.simantics.databoard.binding.ArrayBinding;
18 import org.simantics.databoard.binding.error.BindingException;
19 import org.simantics.databoard.type.ArrayType;
20
21 /**
22  * This class binds median class to as an array(double) where every second value
23  * represents value and odd weight.
24  */
25 public class WeightedMedianBinding extends ArrayBinding {
26
27         public WeightedMedianBinding() {
28                 super(new ArrayType(Bindings.DOUBLE.type()), Bindings.DOUBLE);
29         }
30         
31         @Override
32         public Object create() {
33                 return new WeightedMedian();
34         }
35
36         @Override
37         public Object create(int length, Iterator<Object> it) throws BindingException {         
38                 WeightedMedian result = new WeightedMedian( length );
39                 int count = length/2;
40                 WeightedMedian.Item[] items = new WeightedMedian.Item[count];           
41                 int index = 0;
42                 double value=0.0, weight=0.0;
43                 while (it.hasNext()) {
44                         double d = (Double) it.next();
45                         if ( index%2==0 ) {
46                                 value = d;
47                         } else {
48                                 weight = d;
49                                 WeightedMedian.Item i = new WeightedMedian.Item(weight, value);
50                                 items[ index/2 ] = i;
51                         }
52                         index++;
53                 }
54                 if ( count>0 ) {
55                         items[0].next = items[1];
56                         items[count-1].prev = items[count-2];
57                 }               
58                 for (index=1; index<count-1; index++) {
59                         items[index].next = items[index+1];
60                         items[index].prev = items[index-1];
61                 }
62                 result.median = items[count/2];
63                 result.itemCount = count;
64                 return result;
65         }
66
67         @Override
68         public Object create(Object[] array) throws BindingException {
69                 int count = array.length/2;
70                 WeightedMedian result = new WeightedMedian();
71                 WeightedMedian.Item[] items = new WeightedMedian.Item[count];           
72                 double value=0.0, weight=0.0;
73                 for (int index=0; index<array.length; index++) {
74                         double d = (Double) array[index];
75                         if ( index%2==0 ) {
76                                 value = d;
77                         } else {
78                                 weight = d;
79                                 WeightedMedian.Item i = new WeightedMedian.Item(weight, value);
80                                 items[ index/2 ] = i;
81                         }
82                         index++;
83                 }
84                 if ( count>0 ) {
85                         items[0].next = items[1];
86                         items[count-1].prev = items[count-2];
87                 }               
88                 for (int index=1; index<count-1; index++) {
89                         items[index].next = items[index+1];
90                         items[index].prev = items[index-1];
91                 }
92                 result.median = items[count/2];
93                 result.itemCount = count;
94                 return result;
95         }
96
97         @Override
98         public void add(Object array, int index, Object element) throws BindingException, IndexOutOfBoundsException {
99                 throw new BindingException("not implemented");
100         }
101
102         @Override
103         public void remove(Object array, int index, int count) throws BindingException {
104                 throw new BindingException("not implemented");
105         }
106
107         @Override
108         public Object get(Object array, int index) throws BindingException {
109                 int pos = index/2;
110                 WeightedMedian median = (WeightedMedian) array;
111                 int c = median.itemCount*2;
112                 if ( index<0 || index>=c ) throw new BindingException("Index "+index+" out of bounds "+c);
113                 WeightedMedian.Item i = median.head();
114                 for (int ix=1; ix<pos; ix++) i = i.next;
115                 return index%2==0?i.value:i.weight;
116         }
117
118         @Override
119         public void getAll(Object array, Object[] result) throws BindingException {
120                 int c = size( array );
121                 if (result.length<c) throw new BindingException("Array too small");
122                 WeightedMedian median = (WeightedMedian) array;
123                 int ix = 0;
124                 WeightedMedian.Item i = median.head();
125                 while (i != null) {
126                         result[ix] = i.value; ix++;
127                         result[ix] = i.weight; ix++;
128                         i = i.next;
129                 }
130         }
131
132         @Override
133         public void set(Object array, int index, Object value) throws BindingException {
134                 remove(array, index);
135                 add(array, value);
136         }
137
138         @Override
139         public void setSize(Object array, int newSize) throws BindingException {
140                 int size = size(array);
141                 if (size<newSize) newSize = size;
142                 remove( array, size-newSize, newSize);
143         }
144
145         @Override
146         public int size(Object array) throws BindingException {
147                 WeightedMedian median = (WeightedMedian) array;
148                 return median.itemCount*2;
149         }
150
151         @Override
152         public boolean isInstance(Object obj) {
153                 return obj instanceof WeightedMedian;
154         }       
155
156         @Override
157         public boolean isResizable() {
158                 return true;
159         }
160         
161 }
162
163