1 /*
2 * ObjectLab, http://www.objectlab.co.uk/open is supporting JTreeMap.
3 *
4 * Based in London, we are world leaders in the design and development
5 * of bespoke applications for the securities financing markets.
6 *
7 * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
8 * ___ _ _ _ _ _
9 * / _ \| |__ (_) ___ ___| |_| | __ _| |__
10 * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \
11 * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) |
12 * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
13 * |__/
14 *
15 * www.ObjectLab.co.uk
16 *
17 * $Id: SplitByWeight.java 75 2006-10-24 23:00:51Z benoitx $
18 *
19 * Copyright 2006 the original author or authors.
20 *
21 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
22 * use this file except in compliance with the License. You may obtain a copy of
23 * the License at
24 *
25 * http://www.apache.org/licenses/LICENSE-2.0
26 *
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
30 * License for the specific language governing permissions and limitations under
31 * the License.
32 */
33 package net.sf.jtreemap.ktreemap;
34
35 import java.util.Iterator;
36 import java.util.List;
37
38 /**
39 * Strategy who split the elements in 2 groups of equivalent weight.
40 *
41 * @author Laurent Dutheil
42 */
43
44 public class SplitByWeight extends SplitStrategy {
45
46 @Override
47 public void splitElements(List<TreeMapNode> list, List<TreeMapNode> group1,
48 List<TreeMapNode> group2) {
49 double memWeight = 0.0;
50 double sumWeight = sumWeight(list);
51 double elemWeight = 0.0;
52
53 for (Iterator<TreeMapNode> i = list.iterator(); i.hasNext();) {
54 TreeMapNode tmn = i.next();
55 elemWeight = tmn.getWeight();
56 // if adding the current element pass the middle of total weight
57 if (memWeight + elemWeight >= sumWeight / 2) {
58 // we look at the finest split (the nearest of the middle of weight)
59 if (((sumWeight / 2) - memWeight) > ((memWeight + elemWeight) - (sumWeight / 2))) {
60 // if it is after the add, we add the element to the first Vector
61 memWeight += elemWeight;
62 group1.add(tmn);
63 } else {
64 // we must have at least 1 element in the first vector
65 if (group1.isEmpty()) {
66 group1.add(tmn);
67 } else {
68 // if it is before the add, we add the element to the second Vector
69 group2.add(tmn);
70 }
71 }
72 // then we fill the second Vector qith the rest of elements
73 while (i.hasNext()) {
74 tmn = i.next();
75 group2.add(tmn);
76 }
77 } else {
78 // we add in the first vector while we don't reach the middle of weight
79 memWeight += elemWeight;
80 group1.add(tmn);
81 }
82 }
83 }
84 }
85 /*
86 * ObjectLab is supporing JTreeMap
87 *
88 * Based in London, we are world leaders in the design and development
89 * of bespoke applications for the securities financing markets.
90 *
91 * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
92 * ___ _ _ _ _ _
93 * / _ \| |__ (_) ___ ___| |_| | __ _| |__
94 * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \
95 * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) |
96 * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
97 * |__/
98 *
99 * www.ObjectLab.co.uk
100 */