gnu.trove.set.TIntSet Java Examples

The following examples show how to use gnu.trove.set.TIntSet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractAttributeClustering.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
private void executeDirtyErComparisons(int attributeId, TIntSet coOccurringAttrs) {
    for (TIntIterator sigIterator = coOccurringAttrs.iterator(); sigIterator.hasNext();) {
        int neighborId = sigIterator.next();
        if (neighborId <= attributeId) { // avoid repeated comparisons & comparison with attributeId
            continue;
        }

        float similarity = attributeModels[DATASET_1][attributeId].getSimilarity(attributeModels[DATASET_1][neighborId]);
        if (globalMaxSimilarities[attributeId] < similarity) {
            globalMaxSimilarities[attributeId] = similarity;
        }

        if (globalMaxSimilarities[neighborId] < similarity) {
            globalMaxSimilarities[neighborId] = similarity;
        }
    }
}
 
Example #2
Source File: GraphTracer.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
private TIntObjectHashMap<EntityMetaData> loadEntitiesMetaData(String docId) throws EntityLinkingDataAccessException {
  List<String> mentions = new LinkedList<String>(docMentionCandidatesOriginalMap.get(docId).keySet());
  Map<String, List<TracingEntity>> mentionCandidatesOriginalMap = docMentionCandidatesOriginalMap.get(docId);

  TIntSet entities = new TIntHashSet();

  for (String mention : mentions) {
    List<TracingEntity> allCandidites = mentionCandidatesOriginalMap.get(mention);
    for (TracingEntity te : allCandidites) {
      entities.add(te.entityId);
      for (int e : te.connectedEntities.keySet()) {
        entities.add(e);
      }
    }
  }
  return DataAccess.getEntitiesMetaData(entities.toArray());
}
 
Example #3
Source File: GraphConfidenceEstimator.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
/**
 * Will return numElements integers from the input elements. If numElements
 * is larger than elements.size(), everything will be returned.
 *
 * @param elements    Elements to choose from.
 * @param numElements Number of elements to choose.
 * @return numElement random integers from elements.
 */
private TIntSet getRandomElements(TIntSet elements, int numElements) {
  TIntList source = new TIntArrayList(elements.toArray());
  TIntSet randomElements = new TIntHashSet();
  for (int i = 0; i < numElements; ++i) {
    if (source.size() == 0) {
      break;
    }
    // TODO: this is not efficient, as deleting from the ArrayList
    // will copy ... make this more efficient when necessary.
    int elementPosition = random_.nextInt(source.size());
    int element = source.get(elementPosition);
    source.remove(element);
    randomElements.add(element);
  }
  return randomElements;
}
 
Example #4
Source File: GraphUtil.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Remove from the {@param graph} vertices which are not connected to any edge,
 * and which have no associated object.
 */
public static <V, E> void removeIsolatedVertices(UndirectedGraph<V, E> graph) {
    Objects.requireNonNull(graph, "Graph is null.");

    TIntSet connectedVertices = new TIntHashSet();
    for (int e : graph.getEdges()) {
        connectedVertices.add(graph.getEdgeVertex1(e));
        connectedVertices.add(graph.getEdgeVertex2(e));
    }

    for (int v : graph.getVertices()) {
        if (!connectedVertices.contains(v) && graph.getVertexObject(v) == null) {
            graph.removeVertex(v);
        }
    }
}
 
Example #5
Source File: AbstractNodeBreakerInternalConnectionsTest.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
private InternalConnections findInternalConnectionsTraverseStoppingAtTerminals(VoltageLevel vl) {
    InternalConnections cs = new InternalConnections();

    VoltageLevel.NodeBreakerView topo = vl.getNodeBreakerView();
    int[] nodes = topo.getNodes();
    final TIntSet explored = new TIntHashSet();
    for (int n : nodes) {
        if (explored.contains(n) || topo.getTerminal(n) == null) {
            continue;
        }
        explored.add(n);
        topo.traverse(n, (n1, sw, n2) -> {
            explored.add(n2);
            if (sw == null) {
                cs.add(n1, n2);
            }
            return topo.getTerminal(n2) == null;
        });
    }
    return cs;
}
 
Example #6
Source File: AbstractNodeBreakerInternalConnectionsTest.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
private InternalConnections findInternalConnections(VoltageLevel vl) {
    InternalConnections cs = new InternalConnections();

    VoltageLevel.NodeBreakerView topo = vl.getNodeBreakerView();
    int[] nodes = topo.getNodes();
    final TIntSet explored = new TIntHashSet();
    for (int n : nodes) {
        if (explored.contains(n)) {
            continue;
        }
        explored.add(n);
        topo.traverse(n, (n1, sw, n2) -> {
            explored.add(n2);
            if (sw == null) {
                cs.add(n1, n2);
            }
            return true;
        });
    }
    return cs;
}
 
Example #7
Source File: SortedNeighborhoodBlocking.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
@Override
protected void parseIndex() {
    final Set<String> blockingKeysSet = invertedIndexD1.keySet();
    final String[] sortedTerms = blockingKeysSet.toArray(new String[0]);
    Arrays.sort(sortedTerms);

    final int[] allEntityIds = getSortedEntities(sortedTerms);

    //slide window over the sorted list of entity ids
    int upperLimit = allEntityIds.length - windowSize;
    for (int i = 0; i <= upperLimit; i++) {
        final TIntSet entityIds = new TIntHashSet();
        for (int j = 0; j < windowSize; j++) {
            entityIds.add(allEntityIds[i + j]);
        }

        if (1 < entityIds.size()) {
            blocks.add(new UnilateralBlock(entityIds.toArray()));
        }
    }
}
 
Example #8
Source File: SparseArrayOfInts.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
@Override
public final Array<Integer> distinct(int limit) {
    final int capacity = limit < Integer.MAX_VALUE ? limit : 100;
    final TIntSet set = new TIntHashSet(capacity);
    final ArrayBuilder<Integer> builder = ArrayBuilder.of(capacity, Integer.class);
    for (int i=0; i<length(); ++i) {
        final int value = getInt(i);
        if (set.add(value)) {
            builder.addInt(value);
            if (set.size() >= limit) {
                break;
            }
        }
    }
    return builder.toArray();
}
 
Example #9
Source File: SparseArrayWithIntCoding.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
@Override
public Array<T> distinct(int limit) {
    final int capacity = limit < Integer.MAX_VALUE ? limit : 100;
    final TIntSet set = new TIntHashSet(capacity);
    final ArrayBuilder<T> builder = ArrayBuilder.of(capacity, type());
    for (int i=0; i<length(); ++i) {
        final int code = getInt(i);
        if (set.add(code)) {
            final T value = getValue(i);
            builder.add(value);
            if (set.size() >= limit) {
                break;
            }
        }
    }
    return builder.toArray();
}
 
Example #10
Source File: MappedArrayWithIntCoding.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
@Override
public Array<T> distinct(int limit) {
    final int capacity = limit < Integer.MAX_VALUE ? limit : 100;
    final TIntSet set = new TIntHashSet(capacity);
    final ArrayBuilder<T> builder = ArrayBuilder.of(capacity, type());
    for (int i=0; i<length(); ++i) {
        final int code = getInt(i);
        if (set.add(code)) {
            final T value = getValue(i);
            builder.add(value);
            if (set.size() >= limit) {
                break;
            }
        }
    }
    return builder.toArray();
}
 
Example #11
Source File: DenseArrayOfInts.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
@Override
public final Array<Integer> distinct(int limit) {
    final int capacity = limit < Integer.MAX_VALUE ? limit : 100;
    final TIntSet set = new TIntHashSet(capacity);
    final ArrayBuilder<Integer> builder = ArrayBuilder.of(capacity, Integer.class);
    for (int i=0; i<length(); ++i) {
        final int value = getInt(i);
        if (set.add(value)) {
            builder.addInt(value);
            if (set.size() >= limit) {
                break;
            }
        }
    }
    return builder.toArray();
}
 
Example #12
Source File: DenseArrayWithIntCoding.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
@Override
public Array<T> distinct(int limit) {
    final int capacity = limit < Integer.MAX_VALUE ? limit : 100;
    final TIntSet set = new TIntHashSet(capacity);
    final ArrayBuilder<T> builder = ArrayBuilder.of(capacity, type());
    for (int i=0; i<length(); ++i) {
        final int code = getInt(i);
        if (set.add(code)) {
            final T value = getValue(i);
            builder.add(value);
            if (set.size() >= limit) {
                break;
            }
        }
    }
    return builder.toArray();
}
 
Example #13
Source File: FuzzySetSimJoin.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
/**
 * Find matches for a given set
 */
private TIntFloatHashMap search(int[][] querySet, int[][][] collection, float simThreshold,
        TIntObjectMap<TIntList>[] idx) {

    /* SIGNATURE GENERATION */
    TIntSet[] unflattenedSignature = computeUnflattenedSignature(querySet, simThreshold, idx);

    /* CANDIDATE SELECTION AND CHECK FILTER */
    TIntObjectMap<TIntFloatMap> checkFilterCandidates = applyCheckFilter(querySet, collection,
            unflattenedSignature, idx, simThreshold);

    /* NEAREST NEIGHBOR FILTER */
    TIntSet nnFilterCandidates = applyNNFilter(querySet, collection, checkFilterCandidates, simThreshold);

    /* VERIFICATION */
    TIntFloatHashMap matches = verifyCandidates(querySet, collection, nnFilterCandidates, simThreshold);

    return matches;
}
 
Example #14
Source File: MappedArrayOfInts.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
@Override
public final Array<Integer> distinct(int limit) {
    final int capacity = limit < Integer.MAX_VALUE ? limit : 100;
    final TIntSet set = new TIntHashSet(capacity);
    final ArrayBuilder<Integer> builder = ArrayBuilder.of(capacity, Integer.class);
    for (int i=0; i<length(); ++i) {
        final int value = getInt(i);
        if (set.add(value)) {
            builder.addInt(value);
            if (set.size() >= limit) {
                break;
            }
        }
    }
    return builder.toArray();
}
 
Example #15
Source File: AbstractAttributeClustering.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
protected void compareAttributes() {
    globalMaxSimilarities = new float[noOfAttributes];
    final TIntSet coOccurringAttrs = new TIntHashSet();
    int lastId = 0 < attributesDelimiter ? attributesDelimiter : noOfAttributes;
    for (int i = 0; i < lastId; i++) {
        coOccurringAttrs.clear();

        final Set<String> signatures = attributeModels[DATASET_1][i].getSignatures();
        for (String signature : signatures) {
            final TIntList attrIds = invertedIndex.get(signature);
            if (attrIds == null) {
                continue;
            }
            coOccurringAttrs.addAll(attrIds);
        }

        if (0 < attributesDelimiter) { // Clean-Clean ER
            executeCleanCleanErComparisons(i, coOccurringAttrs);
        } else { // Dirty ER
            executeDirtyErComparisons(i, coOccurringAttrs);
        }
    }
}
 
Example #16
Source File: ExtendedSortedNeighborhoodBlocking.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
@Override
protected void parseIndex() {
    final Set<String> blockingKeysSet = invertedIndexD1.keySet();
    final String[] sortedTerms = blockingKeysSet.toArray(new String[0]);
    Arrays.sort(sortedTerms);

    //slide window over the sorted list of blocking keys
    int upperLimit = sortedTerms.length - windowSize;
    for (int i = 0; i <= upperLimit; i++) {
        final TIntSet entityIds = new TIntHashSet();
        for (int j = 0; j < windowSize; j++) {
            entityIds.addAll(invertedIndexD1.get(sortedTerms[i + j]));
        }

        if (1 < entityIds.size()) {
            blocks.add(new UnilateralBlock(entityIds.toArray()));
        }
    }
}
 
Example #17
Source File: AbstractMetablocking.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
protected void setStatistics() {
    distinctComparisons = 0;
    comparisonsPerEntity = new float[noOfEntities];
    final TIntSet distinctNeighbors = new TIntHashSet();
    for (int i = 0; i < noOfEntities; i++) {
        final int[] associatedBlocks = entityIndex.getEntityBlocks(i, 0);
        if (associatedBlocks.length != 0) {
            distinctNeighbors.clear();
            for (int blockIndex : associatedBlocks) {
                for (int neighborId : getNeighborEntities(blockIndex, i)) {
                    distinctNeighbors.add(neighborId);
                }
            }
            comparisonsPerEntity[i] = distinctNeighbors.size();
            if (!cleanCleanER) {
                comparisonsPerEntity[i]--;
            }
            distinctComparisons += comparisonsPerEntity[i];
        }
    }
    distinctComparisons /= 2;
}
 
Example #18
Source File: ToXdr.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static bitset createPresenceBitset(TIntSet have, int timestampsSize) {
    boolean bits[] = new boolean[timestampsSize];

    for (int i = 0; i < timestampsSize; ++i)
        bits[i] = have.contains(i);
    return bitset(bits);
}
 
Example #19
Source File: DefaultShardManager.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Override
public void restart()
{
    TIntSet map = this.shards.keySet();

    Arrays.stream(map.toArray())
          .sorted() // this ensures shards are started in natural order
          .forEach(this::restart);
}
 
Example #20
Source File: AbstractAttributeClustering.java    From JedAIToolkit with Apache License 2.0 5 votes vote down vote up
private void connectDirtyErComparisons(int attributeId, TIntSet coOccurringAttrs, UndirectedGraph similarityGraph) {
    for (TIntIterator sigIterator = coOccurringAttrs.iterator(); sigIterator.hasNext();) {
        int neighborId = sigIterator.next();
        if (neighborId <= attributeId) { // avoid repeated comparisons & comparison with attributeId
            continue;
        }

        float similarity = attributeModels[DATASET_1][attributeId].getSimilarity(attributeModels[DATASET_1][neighborId]);
        if (a * globalMaxSimilarities[attributeId] < similarity
                || a * globalMaxSimilarities[neighborId] < similarity) {
            similarityGraph.addEdge(attributeId, neighborId);
        }
    }
}
 
Example #21
Source File: SortedNeighborhoodBlocking.java    From JedAIToolkit with Apache License 2.0 5 votes vote down vote up
@Override
protected void parseIndices() {
    final Set<String> blockingKeysSet = new HashSet<>();
    blockingKeysSet.addAll(invertedIndexD1.keySet());
    blockingKeysSet.addAll(invertedIndexD2.keySet());
    
    final String[] sortedTerms = blockingKeysSet.toArray(new String[0]);
    Arrays.sort(sortedTerms);

    final int[] allEntityIds = getMixedSortedEntities(sortedTerms);

    int datasetLimit = entityProfilesD1.size();
    //slide window over the sorted list of entity ids
    int upperLimit = allEntityIds.length - windowSize;
    for (int i = 0; i <= upperLimit; i++) {
        final TIntSet entityIds1 = new TIntHashSet();
        final TIntSet entityIds2 = new TIntHashSet();
        for (int j = 0; j < windowSize; j++) {
            if (allEntityIds[i + j] < datasetLimit) {
                entityIds1.add(allEntityIds[i + j]);
            } else {
                entityIds2.add(allEntityIds[i + j] - datasetLimit);
            }
        }

        if (!entityIds1.isEmpty() && !entityIds2.isEmpty()) {
            blocks.add(new BilateralBlock(entityIds1.toArray(), entityIds2.toArray()));
        }
    }
}
 
Example #22
Source File: ExtendedSortedNeighborhoodBlocking.java    From JedAIToolkit with Apache License 2.0 5 votes vote down vote up
@Override
protected void parseIndices() {
    final Set<String> blockingKeysSet = new HashSet<>();
    blockingKeysSet.addAll(invertedIndexD1.keySet());
    blockingKeysSet.addAll(invertedIndexD2.keySet());
    final String[] sortedTerms = blockingKeysSet.toArray(new String[0]);
    Arrays.sort(sortedTerms);

    //slide window over the sorted list of blocking keys
    int upperLimit = sortedTerms.length - windowSize;
    for (int i = 0; i <= upperLimit; i++) {
        final TIntSet entityIds1 = new TIntHashSet();
        final TIntSet entityIds2 = new TIntHashSet();
        for (int j = 0; j < windowSize; j++) {
            final TIntList d1Entities = invertedIndexD1.get(sortedTerms[i + j]);
            if (d1Entities != null) {
                entityIds1.addAll(d1Entities);
            }

            final TIntList d2Entities = invertedIndexD2.get(sortedTerms[i + j]);
            if (d2Entities != null) {
                entityIds2.addAll(d2Entities);
            }
        }

        if (!entityIds1.isEmpty() && !entityIds2.isEmpty()) {
            blocks.add(new BilateralBlock(entityIds1.toArray(), entityIds2.toArray()));
        }
    }
}
 
Example #23
Source File: UndirectedGraph.java    From JedAIToolkit with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an empty graph with {@code V} vertices and 0 edges. param V
 * the number of vertices
 *
 * @param V number of vertices
 * @throws IllegalArgumentException if {@code V < 0}
 */
public UndirectedGraph(int V) {
    if (V < 0) {
        throw new IllegalArgumentException("Number of vertices must be nonnegative");
    }
    this.V = V;
    this.E = 0;
    adj = new TIntSet[V];
    for (int v = 0; v < V; v++) {
        adj[v] = new TIntHashSet();
    }

    Log.info("Created graph with " + V + " nodes");
}
 
Example #24
Source File: FuzzySetSimJoin.java    From JedAIToolkit with Apache License 2.0 5 votes vote down vote up
private static float jaccard(int[] r, int[] s) {
    TIntSet nr = new TIntHashSet(r);
    TIntSet ns = new TIntHashSet(s);
    TIntSet intersection = new TIntHashSet(nr);
    intersection.retainAll(ns);
    TIntSet union = new TIntHashSet(nr);
    union.addAll(ns);
    return ((float) intersection.size()) / ((float) union.size());
}
 
Example #25
Source File: AbstractSparseBinaryMatrix.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Returns true if any of the on bit indexes of the specified matrix are
 * matched by the on bits of this matrix. It is allowed that 
 * this matrix have more on bits than the specified matrix.
 * 
 * @param matrix
 * @return
 */
public boolean any(int[] onBits) {
    TIntSet keySet = getSparseSet();
    
    for(int i : onBits) {
        if(keySet.contains(i)) return true;
    }
    return false;
}
 
Example #26
Source File: WeightedOverlap.java    From ADW with GNU General Public License v3.0 5 votes vote down vote up
public double compare(SemSig v1, SemSig v2, boolean sortedNormalized) 
{
           TIntSet overlap = new TIntHashSet(v1.getVector().keySet());
           overlap.retainAll(v2.getVector().keySet());
           return compare(overlap,
                          v1.getSortedIndices(),
                          v2.getSortedIndices());
}
 
Example #27
Source File: SizeBasedBlockPurging.java    From JedAIToolkit with Apache License 2.0 5 votes vote down vote up
private int getMaxInnerBlockSize(List<AbstractBlock> blocks) {
    final TIntSet d1Entities = new TIntHashSet();
    final TIntSet d2Entities = new TIntHashSet();
    blocks.stream().map((aBlock) -> (BilateralBlock) aBlock).map((bBlock) -> {
        d1Entities.addAll(bBlock.getIndex1Entities());
        return bBlock;
    }).forEachOrdered((bBlock) -> {
        d2Entities.addAll(bBlock.getIndex2Entities());
    });
    
    return (int) Math.round(Math.min(d1Entities.size(), d2Entities.size())*purgingFactor);
}
 
Example #28
Source File: SizeBasedBlockPurging.java    From JedAIToolkit with Apache License 2.0 5 votes vote down vote up
private int getMaxBlockSize(List<AbstractBlock> blocks) {
    final TIntSet entities = new TIntHashSet();
    blocks.stream().map((aBlock) -> (UnilateralBlock) aBlock).forEachOrdered((uBlock) -> {
        entities.addAll(uBlock.getEntities());
    });
    
    return (int) Math.round(entities.size()*purgingFactor);
}
 
Example #29
Source File: WeightedOverlap.java    From ADW with GNU General Public License v3.0 5 votes vote down vote up
public double compare(
		TIntFloatMap v1,
		TIntFloatMap v2,
		boolean sorted) 
{
           TIntSet overlap = new TIntHashSet(v1.keySet());
           overlap.retainAll(v2.keySet());

           return compare(overlap,
                          SemSigUtils.getSortedIndices(v1),
                          SemSigUtils.getSortedIndices(v2));
}
 
Example #30
Source File: WeightedOverlap.java    From ADW with GNU General Public License v3.0 5 votes vote down vote up
public static double compare(TIntSet overlaps, int[] v1, int[] v2)
{
	//in order to normalize by the smaller vector
	if(v1.length > v2.length)
                       return compareSmallerWithBigger(overlaps, v2, v1);
	else
                       return compareSmallerWithBigger(overlaps, v1, v2);
}