All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.stanford.nlp.util.TwoDimensionalSet Maven / Gradle / Ivy

Go to download

Stanford Parser processes raw text in English, Chinese, German, Arabic, and French, and extracts constituency parse trees.

There is a newer version: 3.9.2
Show newest version
package edu.stanford.nlp.util;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Set;

/**
 * Wrap a TwoDimensionalMap as a TwoDimensionalSet.
 *
 * @author John Bauer
 */
public class TwoDimensionalSet implements Serializable, Iterable> {
  private static final long serialVersionUID = 2L;

  private final TwoDimensionalMap backingMap;

  public TwoDimensionalSet() {
    this(new TwoDimensionalMap<>());
  }

  public TwoDimensionalSet(TwoDimensionalMap backingMap) {
    this.backingMap = backingMap;
  }

  public static  TwoDimensionalSet treeSet() { 
    return new TwoDimensionalSet<>(TwoDimensionalMap.treeMap());
  }

  public static  TwoDimensionalSet hashSet() { 
    return new TwoDimensionalSet<>(TwoDimensionalMap.hashMap());
  }

  public boolean add(K1 k1, K2 k2) {
    return (backingMap.put(k1, k2, true) != null);
  }

  public boolean addAll(TwoDimensionalSet set) {
    boolean result = false;
    for (Pair pair : set) {
      if (add(pair.first, pair.second)) {
        result = true;
      }
    }
    return result;
  }

  /**
   * Adds all the keys in the given TwoDimensionalMap.  Returns true iff at least one key is added.
   */
  public boolean addAllKeys(TwoDimensionalMap map) {
    boolean result = false;
    for (TwoDimensionalMap.Entry entry : map) {
      if (add(entry.getFirstKey(), entry.getSecondKey())) {
        result = true;
      }
    }
    return result;
  }

  public void clear() {
    backingMap.clear();
  }

  public boolean contains(K1 k1, K2 k2) {
    return backingMap.contains(k1, k2);
  }

  public boolean containsAll(TwoDimensionalSet set) {
    for (Pair pair : set) {
      if (!contains(pair.first, pair.second)) {
        return false;
      }
    }
    return true;
  }

  @Override
  public boolean equals(Object o) {
    if (o == this) {
      return true;
    }
    if (!(o instanceof TwoDimensionalSet)) {
      return false;
    }
    TwoDimensionalSet other = (TwoDimensionalSet) o;
    return backingMap.equals(other.backingMap);
  }

  @Override
  public int hashCode() {
    return backingMap.hashCode();
  }

  public boolean isEmpty() {
    return backingMap.isEmpty();
  }

  public boolean remove(K1 k1, K2 k2) {
    return backingMap.remove(k1, k2);
  }

  public boolean removeAll(TwoDimensionalSet set) {
    boolean removed = false;
    for (Pair pair : set) {
      if (remove(pair.first, pair.second)) {
        removed = true;
      }
    }
    return removed;
  }

  public int size() {
    return backingMap.size();
  }

  public Set firstKeySet() {
    return backingMap.firstKeySet();
  }

  public Set secondKeySet(K1 k1) {
    return backingMap.getMap(k1).keySet();
  }

  /**
   * Iterate over the map using the iterator and entry inner classes.
   */
  public Iterator> iterator() {
    return new TwoDimensionalSetIterator<>(this);
  }

  static class TwoDimensionalSetIterator implements Iterator> {
    Iterator> backingIterator;

    TwoDimensionalSetIterator(TwoDimensionalSet set) {
      backingIterator = set.backingMap.iterator();
    }

    public boolean hasNext() {
      return backingIterator.hasNext();
    }

    public Pair next() {
      TwoDimensionalMap.Entry entry = backingIterator.next();
      return Pair.makePair(entry.getFirstKey(), entry.getSecondKey());
    }

    public void remove() {
      backingIterator.remove();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy