001package conexp.fx.core.dl.deprecated; 002 003/* 004 * #%L 005 * Concept Explorer FX 006 * %% 007 * Copyright (C) 2010 - 2019 Francesco Kriegel 008 * %% 009 * This program is free software: you can redistribute it and/or modify 010 * it under the terms of the GNU General Public License as 011 * published by the Free Software Foundation, either version 3 of the 012 * License, or (at your option) any later version. 013 * 014 * This program is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public 020 * License along with this program. If not, see 021 * <http://www.gnu.org/licenses/gpl-3.0.html>. 022 * #L% 023 */ 024 025import java.util.HashMap; 026import java.util.HashSet; 027import java.util.Map; 028import java.util.Set; 029import java.util.stream.Collectors; 030import java.util.stream.Stream; 031 032import org.ujmp.core.Matrix; 033import org.ujmp.core.enums.ValueType; 034 035import conexp.fx.core.collections.Pair; 036 037@Deprecated 038public class Matrix3D<G, M, W> { 039 040 protected final Matrix mat; 041 private int xs = 0; 042 private int ys = 0; 043 private int zs = 0; 044 private final Map<G, Integer> x1; 045 private final Map<M, Integer> y1; 046 private final Map<W, Integer> z1; 047 private final Map<Integer, G> x2; 048 private final Map<Integer, M> y2; 049 private final Map<Integer, W> z2; 050 051 public Matrix3D(final int x, final int y, final int z) { 052 super(); 053 mat = Matrix.Factory.zeros(ValueType.BOOLEAN, x, y, z); 054 x1 = new HashMap<G, Integer>(x); 055 y1 = new HashMap<M, Integer>(y); 056 z1 = new HashMap<W, Integer>(z); 057 x2 = new HashMap<Integer, G>(x); 058 y2 = new HashMap<Integer, M>(y); 059 z2 = new HashMap<Integer, W>(z); 060 } 061 062 public final void addG(final G g) { 063 if (x1.keySet().contains(g)) 064 return; 065 x1.put(g, xs); 066 x2.put(xs, g); 067 xs++; 068 } 069 070 public final void addM(final M m) { 071 if (y1.keySet().contains(m)) 072 return; 073 y1.put(m, ys); 074 y2.put(ys, m); 075 ys++; 076 } 077 078 public final void addW(final W w) { 079 if (z1.keySet().contains(w)) 080 return; 081 z1.put(w, zs); 082 z2.put(zs, w); 083 zs++; 084 } 085 086 public final Set<G> getGs() { 087 return x1.keySet(); 088 } 089 090 public final Set<M> getMs() { 091 return y1.keySet(); 092 } 093 094 public final Set<W> getWs() { 095 return z1.keySet(); 096 } 097 098 public final void add(final G g, final M m, final W w) throws NullPointerException { 099 mat.setAsBoolean(true, x1.get(g), y1.get(m), z1.get(w)); 100 } 101 102 public final boolean get(final G g, final M m, final W w) throws NullPointerException { 103 return mat.getAsBoolean(x1.get(g), y1.get(m), z1.get(w)); 104 } 105 106 public final Stream<G> row(final M m, final W w) { 107 final int y = y1.get(m); 108 final int z = z1.get(w); 109 return x1.keySet().stream().filter(g -> mat.getAsBoolean(x1.get(g), y, z)); 110 } 111 112 public final Stream<M> col(final G g, final W w) { 113 final int x = x1.get(g); 114 final int z = z1.get(w); 115 return y1.keySet().stream().filter(m -> mat.getAsBoolean(x, y1.get(m), z)); 116 } 117 118 public final Stream<W> cut(final G g, final M m) { 119 final int x = x1.get(g); 120 final int y = y1.get(m); 121 return z1.keySet().stream().filter(w -> mat.getAsBoolean(x, y, z1.get(w))); 122 } 123 124 public final Stream<W> cut(final Stream<G> gs, final M m) { 125 final Set<G> _gs = gs.collect(Collectors.toSet()); 126 final int y = y1.get(m); 127 return z1.keySet().stream().filter(w -> _gs.stream().allMatch(g -> mat.getAsBoolean(x1.get(g), y, z1.get(w)))); 128 } 129 130 public final Set<Pair<G, W>> getPairsGW(final M m) { 131 final Set<Pair<G, W>> pairs = new HashSet<Pair<G, W>>(); 132 for (G g : getGs()) 133 for (W w : getWs()) 134 if (get(g, m, w)) 135 pairs.add(new Pair<G, W>(g, w)); 136 return pairs; 137 } 138 139}