Class FastWeightedRelation<W>

java.lang.Object
es.uam.eps.ir.relison.index.fast.FastWeightedRelation<W>
Type Parameters:
W - type of the weights.
All Implemented Interfaces:
Relation<W>
Direct Known Subclasses:
FastWeightedAutoRelation, FastWeightedPairwiseRelation

public abstract class FastWeightedRelation<W>
extends java.lang.Object
implements Relation<W>
Fast implementation of a weighted relation.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected java.util.List<java.util.List<IdxValue<W>>> firstIdxList
    Links from the first kind of objects to the second.
    protected java.util.List<java.util.List<IdxValue<W>>> secondIdxList
    Links from the second kind of objects to the first.
  • Constructor Summary

    Constructors 
    Constructor Description
    FastWeightedRelation()
    Constructor.
    FastWeightedRelation​(java.util.List<java.util.List<IdxValue<W>>> firstIdxList, java.util.List<java.util.List<IdxValue<W>>> secondIdxList)
    Constructor.
  • Method Summary

    Modifier and Type Method Description
    boolean addFirstItem​(int firstIdx)
    Adds a new item to the relation (if it does not previously exist).
    boolean addRelation​(int firstIdx, int secondIdx, W weight)
    Adds a new relation (if it does not previously exist)
    private java.lang.Integer binarySearch​(int firstIdx, int secondIdx, W weight, boolean firstList)
    Given a pair (firstIdx, secondIdx), finds it in the graph using binary search.
    boolean containsPair​(int firstIdx, int secondIdx)
    Returns whether there is a relation between two objects.
    java.util.stream.IntStream firstsWithSeconds()
    Obtains the elements in first set with relations to the ones in the second.
    java.util.stream.Stream<java.lang.Integer> getAllFirst()
    Gets all the elements of the first type.
    java.util.stream.Stream<IdxValue<W>> getIdsFirst​(int secondIdx)
    Gets all the identifiers of items related to a second identifier.
    java.util.stream.Stream<IdxValue<W>> getIdsSecond​(int firstIdx)
    Gets all the identifiers of items related to a first identifier.
    java.util.stream.IntStream getIsolatedFirsts()
    Get the set of elements in the first set without relations with elements.
    java.util.stream.IntStream getIsolatedSeconds()
    Get the set of elements in the second set without relations with elements.
    W getValue​(int firstIdx, int secondIdx)
    Obtains the weight of a relation.
    boolean hasFirsts​(int secondIdx)
    Checks whether the second element has relations with some first element.
    boolean hasSeconds​(int firstIdx)
    Checks whether the first element has relations with some second element.
    int numFirst()
    Gets the total number of elements of the first item in the relation.
    int numFirst​(int secondIdx)
    Gets the total number of elements of the first item related to a second item.
    int numSecond​(int firstIdx)
    Gets the total number of elements of the second item related to a first item.
    boolean removePair​(int firstIdx, int secondIdx)
    Removes a pair.
    java.util.stream.IntStream secondsWithFirsts()
    Obtains the elements in second set with relations to the ones in the first.
    boolean updatePair​(int firstIdx, int secondIdx, W weight, boolean createRelation)
    Updates the weight of a pair.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface es.uam.eps.ir.relison.index.Relation

    addSecondItem, getAllSecond, numSecond, updatePair
  • Field Details

    • firstIdxList

      protected final java.util.List<java.util.List<IdxValue<W>>> firstIdxList
      Links from the first kind of objects to the second. Indexed by the second. E.g. incident edges.
    • secondIdxList

      protected final java.util.List<java.util.List<IdxValue<W>>> secondIdxList
      Links from the second kind of objects to the first. Indexed by the first. E.g. outgoing edges.
  • Constructor Details

    • FastWeightedRelation

      public FastWeightedRelation()
      Constructor. Builds an empty weighted relation.
    • FastWeightedRelation

      public FastWeightedRelation​(java.util.List<java.util.List<IdxValue<W>>> firstIdxList, java.util.List<java.util.List<IdxValue<W>>> secondIdxList)
      Constructor.
      Parameters:
      firstIdxList - Links from the first kind of objects to the second. Indexed by the second. E.g. incident edges.
      secondIdxList - Links from the second kind of objects to the first. Indexed by the first. E.g. outgoing edges.
  • Method Details

    • numFirst

      public int numFirst()
      Description copied from interface: Relation
      Gets the total number of elements of the first item in the relation.
      Specified by:
      numFirst in interface Relation<W>
      Returns:
      the number of different elements of the first item in the relation.
    • numFirst

      public int numFirst​(int secondIdx)
      Description copied from interface: Relation
      Gets the total number of elements of the first item related to a second item.
      Specified by:
      numFirst in interface Relation<W>
      Parameters:
      secondIdx - Identifier of the second item.
      Returns:
      the first item count.
    • numSecond

      public int numSecond​(int firstIdx)
      Description copied from interface: Relation
      Gets the total number of elements of the second item related to a first item.
      Specified by:
      numSecond in interface Relation<W>
      Parameters:
      firstIdx - Identifier of the first item.
      Returns:
      the second item count.
    • getAllFirst

      public java.util.stream.Stream<java.lang.Integer> getAllFirst()
      Description copied from interface: Relation
      Gets all the elements of the first type.
      Specified by:
      getAllFirst in interface Relation<W>
      Returns:
      A stream containing all the elements.
    • getIdsFirst

      public java.util.stream.Stream<IdxValue<W>> getIdsFirst​(int secondIdx)
      Description copied from interface: Relation
      Gets all the identifiers of items related to a second identifier.
      Specified by:
      getIdsFirst in interface Relation<W>
      Parameters:
      secondIdx - Identifier of the second object.
      Returns:
      A stream containing all the weights of the first items.
    • getIdsSecond

      public java.util.stream.Stream<IdxValue<W>> getIdsSecond​(int firstIdx)
      Description copied from interface: Relation
      Gets all the identifiers of items related to a first identifier.
      Specified by:
      getIdsSecond in interface Relation<W>
      Parameters:
      firstIdx - Identifier of the first object.
      Returns:
      A stream containing all the weights of the second items.
    • addFirstItem

      public boolean addFirstItem​(int firstIdx)
      Description copied from interface: Relation
      Adds a new item to the relation (if it does not previously exist).
      Specified by:
      addFirstItem in interface Relation<W>
      Parameters:
      firstIdx - Identifier of the first item.
      Returns:
      true if everything went OK, false if it did previously exist, or something went wrong.
    • addRelation

      public boolean addRelation​(int firstIdx, int secondIdx, W weight)
      Description copied from interface: Relation
      Adds a new relation (if it does not previously exist)
      Specified by:
      addRelation in interface Relation<W>
      Parameters:
      firstIdx - Identifier of the first object.
      secondIdx - Identifier of the second object.
      weight - Weight of the relation.
      Returns:
      true if everything went OK, false if it did previously exist, or something went wrong.
    • getValue

      public W getValue​(int firstIdx, int secondIdx)
      Description copied from interface: Relation
      Obtains the weight of a relation.
      Specified by:
      getValue in interface Relation<W>
      Parameters:
      firstIdx - Identifier of the first object.
      secondIdx - Identifier of the second object.
      Returns:
      the weight if it exists, NaN if not.
    • containsPair

      public boolean containsPair​(int firstIdx, int secondIdx)
      Description copied from interface: Relation
      Returns whether there is a relation between two objects.
      Specified by:
      containsPair in interface Relation<W>
      Parameters:
      firstIdx - Identifier of the first object.
      secondIdx - Identifier of the second object
      Returns:
      true if it exists, false otherwise.
    • updatePair

      public boolean updatePair​(int firstIdx, int secondIdx, W weight, boolean createRelation)
      Description copied from interface: Relation
      Updates the weight of a pair.
      Specified by:
      updatePair in interface Relation<W>
      Parameters:
      firstIdx - Index of the first element.
      secondIdx - Index of the second element.
      weight - New weight for the element.
      createRelation - If true, adds the new relation.
      Returns:
      true if everything went ok, false if not.
    • removePair

      public boolean removePair​(int firstIdx, int secondIdx)
      Description copied from interface: Relation
      Removes a pair.
      Specified by:
      removePair in interface Relation<W>
      Parameters:
      firstIdx - Index of the first element.
      secondIdx - Index of the second element.
      Returns:
      true if everything went ok, false if not.
    • binarySearch

      private java.lang.Integer binarySearch​(int firstIdx, int secondIdx, W weight, boolean firstList)
      Given a pair (firstIdx, secondIdx), finds it in the graph using binary search.
      Parameters:
      firstIdx - The first element.
      secondIdx - The second element.
      weight - The weight value.
      firstList - True if the element has to be found on the list of first elements, false if it has to be found on the list of second elements.
      Returns:
      the index of the element if it exists, - (insertpoint - 1) if it does not, where insertpoint is the corresponding point where the element should be added.
    • firstsWithSeconds

      public java.util.stream.IntStream firstsWithSeconds()
      Description copied from interface: Relation
      Obtains the elements in first set with relations to the ones in the second.
      Specified by:
      firstsWithSeconds in interface Relation<W>
      Returns:
      the elements in first set with relations to the ones in the second.
    • secondsWithFirsts

      public java.util.stream.IntStream secondsWithFirsts()
      Description copied from interface: Relation
      Obtains the elements in second set with relations to the ones in the first.
      Specified by:
      secondsWithFirsts in interface Relation<W>
      Returns:
      the elements in second set with relations to the ones in the first.
    • hasSeconds

      public boolean hasSeconds​(int firstIdx)
      Description copied from interface: Relation
      Checks whether the first element has relations with some second element.
      Specified by:
      hasSeconds in interface Relation<W>
      Parameters:
      firstIdx - the index of the first element.
      Returns:
      true if it has, false otherwise.
    • hasFirsts

      public boolean hasFirsts​(int secondIdx)
      Description copied from interface: Relation
      Checks whether the second element has relations with some first element.
      Specified by:
      hasFirsts in interface Relation<W>
      Parameters:
      secondIdx - The index of the second element.
      Returns:
      true if it has, false otherwise.
    • getIsolatedFirsts

      public java.util.stream.IntStream getIsolatedFirsts()
      Description copied from interface: Relation
      Get the set of elements in the first set without relations with elements. in the second.
      Specified by:
      getIsolatedFirsts in interface Relation<W>
      Returns:
      the elements in the first set without relations with elements. in the second.
    • getIsolatedSeconds

      public java.util.stream.IntStream getIsolatedSeconds()
      Description copied from interface: Relation
      Get the set of elements in the second set without relations with elements. in the first.
      Specified by:
      getIsolatedSeconds in interface Relation<W>
      Returns:
      the elements in the second set without relations with elements. in the first.