Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CartesianProduct |
|
| 0.0;0 | ||||
CartesianProduct$Mapper |
|
| 0.0;0 |
1 | /* | |
2 | * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. | |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 | * | |
5 | * This code is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License version 2 only, as | |
7 | * published by the Free Software Foundation. Sun designates this | |
8 | * particular file as subject to the "Classpath" exception as provided | |
9 | * by Sun in the LICENSE file that accompanied this code. | |
10 | * | |
11 | * This code is distributed in the hope that it will be useful, but WITHOUT | |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | * version 2 for more details (a copy is included in the LICENSE file that | |
15 | * accompanied this code). | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License version | |
18 | * 2 along with this work; if not, write to the Free Software Foundation, | |
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 | * | |
21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
22 | * CA 95054 USA or visit www.sun.com if you need additional information or | |
23 | * have any questions. | |
24 | */ | |
25 | ||
26 | package com.sun.javafx.runtime.sequence; | |
27 | ||
28 | /** | |
29 | * Special case for n-dimensional foreach comprehension when there are no where clauses on any list and | |
30 | * the foreach body always returns a single instance. The results are computed as needed rather than | |
31 | * precomputed, to save space. | |
32 | * | |
33 | * @author Brian Goetz | |
34 | */ | |
35 | public class CartesianProduct<T> extends AbstractSequence<T> implements Sequence<T> { | |
36 | ||
37 | public interface Mapper<T> { | |
38 | public T map(int[] indexes, Object[] values); | |
39 | } | |
40 | ||
41 | private final Sequence<?>[] sequences; | |
42 | private final Mapper<T> mapper; | |
43 | private final int size; | |
44 | private final int[] sizes; | |
45 | ||
46 | public CartesianProduct(Class<T> clazz, Mapper<T> mapper, Sequence<?>... sequences) { | |
47 | 3 | super(clazz); |
48 | 3 | this.sequences = sequences; |
49 | 3 | this.mapper = mapper; |
50 | 3 | if (sequences.length == 0) |
51 | 0 | size = 0; |
52 | else { | |
53 | 3 | int depth = 1; |
54 | 10 | for (Sequence<?> seq : sequences) |
55 | 7 | depth = depth * seq.size(); |
56 | 3 | size = depth; |
57 | } | |
58 | 3 | sizes = new int[sequences.length]; |
59 | 10 | for (int i=0; i<sequences.length; i++) { |
60 | 7 | int cur = 1; |
61 | 12 | for (int j=i+1; j<sequences.length; j++) |
62 | 5 | cur *= sequences[j].size(); |
63 | 7 | sizes[i] = cur; |
64 | } | |
65 | 3 | } |
66 | ||
67 | public int getDepth() { | |
68 | 0 | int depth = 0; |
69 | 0 | for (Sequence<?> seq : sequences) |
70 | 0 | depth = Math.max(depth, seq.getDepth()); |
71 | 0 | return depth + 1; |
72 | } | |
73 | ||
74 | public int size() { | |
75 | 39 | return size; |
76 | } | |
77 | ||
78 | public T get(int position) { | |
79 | 36 | int[] indices = new int[sequences.length]; |
80 | 36 | Object[] values = new Object[sequences.length]; |
81 | 36 | int last = sequences.length-1; |
82 | 96 | for (int i=0; i<last; i++) { |
83 | 60 | indices[i] = position / sizes[i]; |
84 | 60 | values[i] = sequences[i].get(indices[i]); |
85 | 60 | position -= indices[i]*sizes[i]; |
86 | } | |
87 | 36 | indices[last] = position; |
88 | 36 | values[last] = sequences[last].get(indices[last]); |
89 | 36 | return mapper.map(indices, values); |
90 | } | |
91 | } |