001package conexp.fx.core.context;
002
003/*
004 * #%L
005 * Concept Explorer FX
006 * %%
007 * Copyright (C) 2010 - 2023 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.HashSet;
026import java.util.Set;
027
028import conexp.fx.core.collections.Pair;
029import conexp.fx.core.collections.setlist.HashSetArrayList;
030import conexp.fx.core.collections.setlist.SetList;
031
032public class SparseContext<G, M> extends AbstractContext<G, M> {
033
034  private final Set<Pair<G, M>> incidences = new HashSet<Pair<G, M>>();
035
036  public SparseContext(SetList<G> objects, SetList<M> attributes, boolean homogen) {
037    super(objects, attributes, homogen);
038  }
039
040  @Override
041  public boolean contains(Object o1, Object o2) {
042    return incidences.contains(new Pair<Object, Object>(o1, o2));
043  }
044
045  @Override
046  public boolean add(G row, M col) {
047    return incidences.add(Pair.of(row, col));
048  }
049
050  @SuppressWarnings("unchecked")
051  @Override
052  public boolean addFast(Object o1, Object o2) {
053    return incidences.add(Pair.of((G) o1, (M) o2));
054  }
055
056  public final SparseContext<Set<G>, M> cleanDomain() {
057    final SetList<Set<G>> eqClasses = new HashSetArrayList<Set<G>>(this.objectQuasiOrder().equivalenceClasses());
058    final SparseContext<Set<G>, M> cleanedContext = new SparseContext<Set<G>, M>(eqClasses, this.colHeads(), false);
059    for (Set<G> g : cleanedContext.rowHeads())
060      for (M m : cleanedContext.colHeads())
061        if (this.contains(g.iterator().next(), m))
062          cleanedContext.add(g, m);
063    return cleanedContext;
064  }
065
066}