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: SplitByNumber.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.List;
36
37 /**
38 * Strategy who split the elements in 2 groups of same cardinal.
39 *
40 * @author Laurent DUTHEIL
41 */
42 public class SplitByNumber extends SplitStrategy {
43
44 @Override
45 public void splitElements(List<TreeMapNode> v, List<TreeMapNode> v1, List<TreeMapNode> v2) {
46 int size = v.size();
47 int middle = size / 2;
48 int index = 0;
49 //we add first elements to v1
50 for (; index < middle; index++) {
51 v1.add(v.get(index));
52 }
53 //we add last elements to v2
54 for (; index < size; index++) {
55 v2.add(v.get(index));
56 }
57 }
58
59 @Override
60 public double sumWeight(List<TreeMapNode> v) {
61 //all the elements must have the same weight
62 double weight = 0.0;
63 for (TreeMapNode node : v) {
64 if (node.isLeaf()) {
65 weight += 1;
66 } else {
67 weight += this.sumWeight(node.getChildren());
68 }
69 }
70 return weight;
71 }
72 }
73 /*
74 * ObjectLab is supporing JTreeMap
75 *
76 * Based in London, we are world leaders in the design and development
77 * of bespoke applications for the securities financing markets.
78 *
79 * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
80 * ___ _ _ _ _ _
81 * / _ \| |__ (_) ___ ___| |_| | __ _| |__
82 * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \
83 * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) |
84 * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
85 * |__/
86 *
87 * www.ObjectLab.co.uk
88 */