Skip to content

Commit

Permalink
Misnomer.
Browse files Browse the repository at this point in the history
  • Loading branch information
PhillHenry committed Apr 9, 2016
1 parent b5761c5 commit 50b806f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/main/java/uk/co/odinconsultants/bitset/AtomicBitSet.java
Expand Up @@ -43,6 +43,10 @@ public boolean set(long n) {
}
}

public boolean apply(long n) {
return set(n);
}

public boolean get(long n) {
int bit = 1 << n;
int idx = (int) (n >>> 5);
Expand Down
18 changes: 9 additions & 9 deletions src/main/scala/uk/co/odinconsultants/graph/search/BFS.scala
Expand Up @@ -17,22 +17,22 @@ object BFS {

def search(g: Graph, start: VertexId, alreadySeen: AtomicBitSet): Unit = {
@tailrec
def bfs(toExpore: Seq[VertexId]): Unit = {
if (toExpore.nonEmpty) {
val next = toExpore.head
if (alreadySeen.set(next.toLong)) {
bfs(toExpore.tail ++ g.neighboursOf(next))
def bfs(toExplore: Seq[VertexId]): Unit = {
if (toExplore.nonEmpty) {
val next = toExplore.head
if (alreadySeen(next.toLong)) {
bfs(toExplore.tail ++ g.neighboursOf(next))
} else {
bfs(toExpore.tail)
bfs(toExplore.tail)
}
}
}
if (alreadySeen.set(start)) {
if (alreadySeen(start)) {
bfs(g.neighboursOf(start))
}
}

def parallelTopologicalSort(g: Graph, start: VertexId)(implicit xc: ExecutionContext): Seq[VertexId] = {
def parallelPath(g: Graph, start: VertexId)(implicit xc: ExecutionContext): Seq[VertexId] = {

val alreadySeen = new AtomicBitSet(g.numberOfVertices.toInt)

Expand All @@ -59,7 +59,7 @@ object BFS {
bfs(g.neighboursOf(start), Seq(start))
}

def topologicalSort(g: Graph, start: VertexId): Seq[VertexId] = {
def path(g: Graph, start: VertexId): Seq[VertexId] = {

val alreadySeen = new AtomicBitSet(g.numberOfVertices.toInt)

Expand Down
Expand Up @@ -24,13 +24,13 @@ public class BFSPerformanceMain {
@Benchmark
@Fork(1)
public void bfsTopologicalOrderingMultiThreaded() {
BFS.parallelTopologicalSort(graph, 1L, xc);
BFS.parallelPath(graph, 1L, xc);
}

@Benchmark
@Fork(1)
public void bfsTopologicalOrdering() {
BFS.topologicalSort(graph, 1L);
BFS.path(graph, 1L);
}

public static void main(String[] args) throws RunnerException {
Expand Down
Expand Up @@ -16,7 +16,7 @@ class AtomicBitSetSpec extends WordSpec with Matchers {
val propSmallInteger = forAll(smallInteger) { n =>
val toTest = new AtomicBitSet(n)
for (i <- 0 to n) toTest.set(i.toLong)
toTest.isEverythingSet()
toTest.isEverythingSet
}
}
}
Expand Down
Expand Up @@ -6,7 +6,7 @@ import org.scalatest.{Matchers, WordSpec}
import uk.co.odinconsultants.bitset.AtomicBitSet
import uk.co.odinconsultants.graph.impl.AdjacencyListGraph.asString
import uk.co.odinconsultants.graph.impl.GraphGenerator._
import uk.co.odinconsultants.graph.search.BFS.{search, parallelTopologicalSort}
import uk.co.odinconsultants.graph.search.BFS.{search, parallelPath}

import scala.concurrent.ExecutionContext

Expand All @@ -29,7 +29,7 @@ class BFSSpec extends WordSpec with Matchers {
"Strongly-connected component graph" should {
"have topological sort corresponding to its leaders" in new GraphFixture {
implicit val xc = ExecutionContext.global
val sortedVertices = parallelTopologicalSort(graph, 1)
val sortedVertices = parallelPath(graph, 1)

withClue(asString(graph) + sortedVertices.groupBy(x => x).filter(_._2.size > 1)) {
sortedVertices.toSet should have size sortedVertices.size
Expand Down

0 comments on commit 50b806f

Please sign in to comment.