Java Code Examples for it.unimi.dsi.fastutil.longs.LongOpenHashSet#contains()

The following examples show how to use it.unimi.dsi.fastutil.longs.LongOpenHashSet#contains() . 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: KnowledgeBase.java    From fasten with Apache License 2.0 6 votes vote down vote up
/**
 * The set of all node signatures that are reachable from the signature <code>startSig</code>.
 *
 * @param start the starting node.
 * @return the set of all node signatures for which there is a directed path from
 *         <code>startSig</code> to that node.
 */
public synchronized LongSet reaches(final long startSig) {
	final LongOpenHashSet result = new LongOpenHashSet();
	// Visit queue
	final LongArrayFIFOQueue queue = new LongArrayFIFOQueue();
	queue.enqueue(startSig);
	result.add(startSig);

	while (!queue.isEmpty()) {
		final long nodeSig = queue.dequeueLong();
		for (final long s : successors(nodeSig)) if (!result.contains(s)) {
			queue.enqueue(s);
			result.add(s);
		}
	}

	return result;
}
 
Example 2
Source File: KnowledgeBase.java    From fasten with Apache License 2.0 6 votes vote down vote up
/**
 * The set of all nodes signatures that are coreachable from <code>startSig</code>.
 *
 * @param start the starting node signature.
 * @return the set of all node signatures for which there is a directed path from that node to
 *         <code>startSig</code>.
 */
public synchronized LongSet coreaches(final long startSig) {
	final LongOpenHashSet result = new LongOpenHashSet();
	// Visit queue
	final LongArrayFIFOQueue queue = new LongArrayFIFOQueue();
	queue.enqueue(startSig);
	result.add(startSig);

	while (!queue.isEmpty()) {
		final long nodeSig = queue.dequeueLong();
		for (final long s : predecessors(nodeSig)) if (!result.contains(s)) {
			queue.enqueue(s);
			result.add(s);
		}
	}

	return result;
}
 
Example 3
Source File: GlobalVisitStats.java    From fasten with Apache License 2.0 4 votes vote down vote up
public static Result reaches(final KnowledgeBase kb, final long startSig, final int maxRevs, final ProgressLogger pl) {
	final LongOpenHashSet result = new LongOpenHashSet();
	final Object2ObjectOpenHashMap<String, IntOpenHashSet> product2Revs = new Object2ObjectOpenHashMap<>();
	final MutableLong totRevs = new MutableLong();

	// Visit queue
	final LongArrayFIFOQueue queue = new LongArrayFIFOQueue();
	queue.enqueue(startSig);
	result.add(startSig);

	String p = kb.callGraphs.get(index(startSig)).product;
	IntOpenHashSet revs = new IntOpenHashSet();
	revs.add(index(startSig));
	product2Revs.put(p, revs);
	totRevs.increment();


	pl.itemsName = "nodes";
	pl.info = new Object() {
		@Override
		public String toString() {
			return "[nodes: " + result.size() + " products: " + product2Revs.size() + " revisions: " + totRevs.getValue() + "]";
		}
	};

	pl.start("Visiting reachable nodes...");

	while (!queue.isEmpty()) {
		final long node = queue.dequeueLong();

		for (final long s : kb.successors(node)) if (!result.contains(s)) {
			p = kb.callGraphs.get(index(s)).product;
			final long gid = gid(s);
			if (badGIDs.contains(gid)) continue;
			final String targetNameSpace = kb.new Node(gid, index(s)).toFastenURI().getRawNamespace();
			if (targetNameSpace.startsWith("java.") || targetNameSpace.startsWith("javax.") || targetNameSpace.startsWith("jdk.")) {
				badGIDs.add(gid);
				continue;
			}
			revs = product2Revs.get(p);
			if (revs == null) product2Revs.put(p, revs = new IntOpenHashSet());
			if (revs.contains(index(s)) || revs.size() < maxRevs) {
				queue.enqueue(s);
				result.add(s);
				//System.out.println(kb.new Node(gid(node), index(node)).toFastenURI() + " -> " + kb.new Node(gid(s), index(s)).toFastenURI());
				if (revs.add(index(s))) totRevs.increment();
			}
		}
		pl.lightUpdate();
	}

	pl.done();
	return new Result(result, product2Revs.size(), totRevs.getValue().longValue());
}
 
Example 4
Source File: GlobalVisitStats.java    From fasten with Apache License 2.0 4 votes vote down vote up
public static Result coreaches(final KnowledgeBase kb, final long startSig, final int maxRevs, final ProgressLogger pl) {
	final LongOpenHashSet result = new LongOpenHashSet();
	final Object2ObjectOpenHashMap<String, IntOpenHashSet> product2Revs = new Object2ObjectOpenHashMap<>();
	final MutableLong totRevs = new MutableLong();

	// Visit queue
	final LongArrayFIFOQueue queue = new LongArrayFIFOQueue();
	queue.enqueue(startSig);
	result.add(startSig);

	String p = kb.callGraphs.get(index(startSig)).product;
	IntOpenHashSet revs = new IntOpenHashSet();
	revs.add(index(startSig));
	product2Revs.put(p, revs);
	totRevs.increment();


	pl.itemsName = "nodes";
	pl.info = new Object() {
		@Override
		public String toString() {
			return "[nodes: " + result.size() + " products: " + product2Revs.size() + " revisions: " + totRevs.getValue() + "]";
		}
	};
	pl.start("Visiting coreachable nodes...");
	while (!queue.isEmpty()) {
		final long node = queue.dequeueLong();

		for (final long s : kb.predecessors(node)) if (!result.contains(s)) {
			p = kb.callGraphs.get(index(s)).product;
			final String targetNameSpace = kb.new Node(gid(s), index(s)).toFastenURI().getRawNamespace();
			if (targetNameSpace.startsWith("java.") || targetNameSpace.startsWith("javax.") || targetNameSpace.startsWith("jdk.")) continue;
			revs = product2Revs.get(p);
			if (revs == null) product2Revs.put(p, revs = new IntOpenHashSet());
			if (revs.contains(index(s)) || revs.size() < maxRevs) {
				queue.enqueue(s);
				result.add(s);
				//System.out.println(kb.new Node(gid(node), index(node)).toFastenURI() + " -> " + kb.new Node(gid(s), index(s)).toFastenURI());
				if (revs.add(index(s))) totRevs.increment();
			}
		}
		pl.lightUpdate();
	}

	pl.done();
	return new Result(result, product2Revs.size(), totRevs.getValue().longValue());
}
 
Example 5
Source File: PageRankTest.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
@Test
public void testLesMisGraph() throws Exception {
  OutIndexedPowerLawMultiSegmentDirectedGraph graph =
      new OutIndexedPowerLawMultiSegmentDirectedGraph(1, 1000, 100, 10, 2,
          new IdentityEdgeTypeMask(), new NullStatsReceiver());

  for (int i=0; i<LES_MIS_GRAPH.length; i++) {
    graph.addEdge(LES_MIS_GRAPH[i][0], LES_MIS_GRAPH[i][1], (byte) 0);
  }

  // Spot check the graph to make sure it's been loaded correctly.
  assertEquals(7, graph.getOutDegree(76));
  assertEquals(new LongArrayList(new long[]{64, 65, 66, 63, 62, 48, 58}), new LongArrayList(graph.getOutEdges(76)));

  assertEquals(1, graph.getOutDegree(30));
  assertEquals(new LongArrayList(new long[]{23}), new LongArrayList(graph.getOutEdges(30)));

  assertEquals(4, graph.getOutDegree(11));
  assertEquals(new LongArrayList(new long[]{10, 3, 2, 0}), new LongArrayList(graph.getOutEdges(11)));

  LongOpenHashSet nodes = new LongOpenHashSet();
  long maxNodeId = 0;
  for (int i=0; i<LES_MIS_GRAPH.length; i++) {
    if ( !nodes.contains(LES_MIS_GRAPH[i][0])) nodes.add(LES_MIS_GRAPH[i][0]);
    if ( !nodes.contains(LES_MIS_GRAPH[i][1])) nodes.add(LES_MIS_GRAPH[i][1]);
    if ( LES_MIS_GRAPH[i][0] > maxNodeId ) maxNodeId = LES_MIS_GRAPH[i][0];
    if ( LES_MIS_GRAPH[i][1] > maxNodeId ) maxNodeId = LES_MIS_GRAPH[i][1];
  }

  assertEquals(76, maxNodeId);
  PageRank pr = new PageRank(graph, nodes, maxNodeId, 0.85, 10, 1e-15);
  int numIterations = pr.run();
  double normL1 = pr.getL1Norm();
  double[] pagerank = pr.getPageRankVector();
  assertEquals(10, numIterations);
  assertEquals(0.00108, normL1, 10e-4);

  List<Map.Entry<Long, Double>> scores = new ArrayList<>();
  for (int i=0; i<maxNodeId+1; i++) {
    scores.add(new AbstractMap.SimpleEntry<>((long) i, pagerank[i]));
  }

  // Sort by score.
  scores.sort((e1, e2) -> e2.getValue() > e1.getValue() ? 1 : e2.getKey().compareTo(e1.getKey()));

  // We're going to verify that the ranking and score are both correct. These rankings have been verified against an
  // external implementation (JUNG).
  assertEquals(11, (long) scores.get(0).getKey());
  assertEquals(0.1088995, scores.get(0).getValue(), 10e-4);
  assertEquals(0, (long) scores.get(1).getKey());
  assertEquals(0.09538347, scores.get(1).getValue(), 10e-4);
  assertEquals(16, (long) scores.get(2).getKey());
  assertEquals(0.05104386, scores.get(2).getValue(), 10e-4);
  assertEquals(23, (long) scores.get(3).getKey());
  assertEquals(0.04389916, scores.get(3).getValue(), 10e-4);
  assertEquals(25, (long) scores.get(4).getKey());
  assertEquals(0.04095956, scores.get(4).getValue(), 10e-4);
  assertEquals(2, (long) scores.get(5).getKey());
  assertEquals(0.03868165, scores.get(5).getValue(), 10e-4);
  assertEquals(24, (long) scores.get(6).getKey());
  assertEquals(0.03617344, scores.get(6).getValue(), 10e-4);
  assertEquals(48, (long) scores.get(7).getKey());
  assertEquals(0.0290502, scores.get(7).getValue(), 10e-4);
  assertEquals(10, (long) scores.get(8).getKey());
  assertEquals(0.02714507, scores.get(8).getValue(), 10e-4);
  assertEquals(3, (long) scores.get(9).getKey());
  assertEquals(0.02714507, scores.get(9).getValue(), 10e-4);

  double totalMass = 0.0;
  for (int i=0; i<maxNodeId+1; i++) {
    totalMass += scores.get(i).getValue();
  }
  // Total mass should still be 1.0.
  assertEquals(1.0, totalMass, 10e-10);
}
 
Example 6
Source File: MultiThreadedPageRankTest.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
@Test
public void testLesMisGraph() throws Exception {
  OutIndexedPowerLawMultiSegmentDirectedGraph graph =
      new OutIndexedPowerLawMultiSegmentDirectedGraph(1, 1000, 100, 10, 2,
          new IdentityEdgeTypeMask(), new NullStatsReceiver());

  for (int i=0; i<LES_MIS_GRAPH.length; i++) {
    graph.addEdge(LES_MIS_GRAPH[i][0], LES_MIS_GRAPH[i][1], (byte) 0);
  }

  // Spot check the graph to make sure it's been loaded correctly.
  assertEquals(7, graph.getOutDegree(76));
  assertEquals(new LongArrayList(new long[]{64, 65, 66, 63, 62, 48, 58}), new LongArrayList(graph.getOutEdges(76)));

  assertEquals(1, graph.getOutDegree(30));
  assertEquals(new LongArrayList(new long[]{23}), new LongArrayList(graph.getOutEdges(30)));

  assertEquals(4, graph.getOutDegree(11));
  assertEquals(new LongArrayList(new long[]{10, 3, 2, 0}), new LongArrayList(graph.getOutEdges(11)));

  LongOpenHashSet nodes = new LongOpenHashSet();
  long maxNodeId = 0;
  for (int i=0; i<LES_MIS_GRAPH.length; i++) {
    if ( !nodes.contains(LES_MIS_GRAPH[i][0])) nodes.add(LES_MIS_GRAPH[i][0]);
    if ( !nodes.contains(LES_MIS_GRAPH[i][1])) nodes.add(LES_MIS_GRAPH[i][1]);
    if ( LES_MIS_GRAPH[i][0] > maxNodeId ) maxNodeId = LES_MIS_GRAPH[i][0];
    if ( LES_MIS_GRAPH[i][1] > maxNodeId ) maxNodeId = LES_MIS_GRAPH[i][1];
  }

  assertEquals(76, maxNodeId);
  MultiThreadedPageRank pr = new MultiThreadedPageRank(graph, new LongArrayList(nodes), maxNodeId, 0.85, 10, 1e-15, 3);
  int numIterations = pr.run();
  double normL1 = pr.getL1Norm();
  AtomicDoubleArray pagerank = pr.getPageRankVector();
  assertEquals(10, numIterations);
  assertEquals(0.00108, normL1, 10e-4);

  List<Map.Entry<Long, Double>> scores = new ArrayList<>();
  for (int i=0; i<maxNodeId+1; i++) {
    scores.add(new AbstractMap.SimpleEntry<>((long) i, pagerank.get(i)));
  }

  // Sort by score.
  scores.sort((e1, e2) -> e2.getValue() > e1.getValue() ? 1 : e2.getKey().compareTo(e1.getKey()));

  // We're going to verify that the ranking and score are both correct. These rankings have been verified against an
  // external implementation (JUNG).
  assertEquals(11, (long) scores.get(0).getKey());
  assertEquals(0.1088995, scores.get(0).getValue(), 10e-4);
  assertEquals(0, (long) scores.get(1).getKey());
  assertEquals(0.09538347, scores.get(1).getValue(), 10e-4);
  assertEquals(16, (long) scores.get(2).getKey());
  assertEquals(0.05104386, scores.get(2).getValue(), 10e-4);
  assertEquals(23, (long) scores.get(3).getKey());
  assertEquals(0.04389916, scores.get(3).getValue(), 10e-4);
  assertEquals(25, (long) scores.get(4).getKey());
  assertEquals(0.04095956, scores.get(4).getValue(), 10e-4);
  assertEquals(2, (long) scores.get(5).getKey());
  assertEquals(0.03868165, scores.get(5).getValue(), 10e-4);
  assertEquals(24, (long) scores.get(6).getKey());
  assertEquals(0.03617344, scores.get(6).getValue(), 10e-4);
  assertEquals(48, (long) scores.get(7).getKey());
  assertEquals(0.0290502, scores.get(7).getValue(), 10e-4);
  assertEquals(10, (long) scores.get(8).getKey());
  assertEquals(0.02714507, scores.get(8).getValue(), 10e-4);
  assertEquals(3, (long) scores.get(9).getKey());
  assertEquals(0.02714507, scores.get(9).getValue(), 10e-4);

  double totalMass = 0.0;
  for (int i=0; i<maxNodeId+1; i++) {
    totalMass += scores.get(i).getValue();
  }
  // Total mass should still be 1.0.
  assertEquals(1.0, totalMass, 10e-10);
}