Skip to content

Commit

Permalink
apply formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
vreuter committed Nov 13, 2024
1 parent 0e6efd5 commit ec52bb9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 41 deletions.
2 changes: 1 addition & 1 deletion modules/cell/src/main/scala/NuclearDesignation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ object NucleusNumber:
given SimpleShow[NucleusNumber] = SimpleShow.instance(_.get.show)

/** Try to read the given string as a nucleus number. */
def parse(s: String): Either[String, NucleusNumber] =
def parse(s: String): Either[String, NucleusNumber] =
readAsInt(s)
.flatMap(PositiveInt.either)
.bimap(
Expand Down
41 changes: 21 additions & 20 deletions modules/graph/src/main/scala/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@ import scalax.collection.edges.UnDiEdge
import scalax.collection.edges.UnDiEdgeImplicits // for syntax, like ~
import scalax.collection.immutable


/** Graph-related operations */
package object graph:
private[graph] type SimplestGraph[N] = immutable.Graph[N, UnDiEdge[N]]
private[graph] type SimplestGraph[N] = immutable.Graph[N, UnDiEdge[N]]

/** Start with an empty graph, and combine by taking union of node and edge sets. */
def monoidKForSimplestGraphByOuterElements: MonoidK[SimplestGraph] = new:
override def empty[N]: SimplestGraph[N] = immutable.Graph.empty
override def combineK[N](g1: SimplestGraph[N], g2: SimplestGraph[N]): SimplestGraph[N] =
immutable.Graph.from((g1.nodes.toOuter | g2.nodes.toOuter), g1.edges.toOuter | g2.edges.toOuter)
/** Start with an empty graph, and combine by taking union of node and edge sets. */
def monoidKForSimplestGraphByOuterElements: MonoidK[SimplestGraph] = new:
override def empty[N]: SimplestGraph[N] = immutable.Graph.empty
override def combineK[N](g1: SimplestGraph[N], g2: SimplestGraph[N]): SimplestGraph[N] =
immutable.Graph.from(
(g1.nodes.toOuter | g2.nodes.toOuter),
g1.edges.toOuter | g2.edges.toOuter
)

private def fromSingleNodeAndNeighbors[N](n: N, neighbors: Set[N]): SimplestGraph[N] =
immutable.Graph.from(neighbors.map(n ~ _))
private def fromSingleNodeAndNeighbors[N](n: N, neighbors: Set[N]): SimplestGraph[N] =
immutable.Graph.from(neighbors.map(n ~ _))

/**
* Construct a simple graph from an adjancency list/'matrix'.
*
* @tparam N The node type
* @param adjecency The adjacency list / 'matrix' which encodes the
* edge relationships / node adjacencies
*/
def buildSimpleGraph[N](adjacency: Map[N, Set[N]]): SimplestGraph[N] =
val singleGraphs = adjacency.toList.map(fromSingleNodeAndNeighbors[N].tupled)
monoidKForSimplestGraphByOuterElements.combineAllK(singleGraphs)
/** Construct a simple graph from an adjancency list/'matrix'.
*
* @tparam N
* The node type
* @param adjecency
* The adjacency list / 'matrix' which encodes the edge relationships / node adjacencies
*/
def buildSimpleGraph[N](adjacency: Map[N, Set[N]]): SimplestGraph[N] =
val singleGraphs = adjacency.toList.map(fromSingleNodeAndNeighbors[N].tupled)
monoidKForSimplestGraphByOuterElements.combineAllK(singleGraphs)
end graph

42 changes: 23 additions & 19 deletions modules/graph/src/test/scala/TestGraph.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,48 @@ import org.scalatest.prop.Configuration.PropertyCheckConfiguration
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks

/** Test the functionality of the graph module. */
class TestGraph extends AnyFunSuiteLike, FunSuiteDiscipline, ScalaCheckPropertyChecks, should.Matchers:

class TestGraph
extends AnyFunSuiteLike,
FunSuiteDiscipline,
ScalaCheckPropertyChecks,
should.Matchers:

private val maxOrder: Int = 20
given arbitraryForRandomGraphMetrics[N: Arbitrary]: Arbitrary[GraphGen.Metrics[N]] =
Arbitrary{
Gen.choose(1, maxOrder).map{ n => // RandomGraph throws exception for order-0.
new GraphGen.Metrics[N]{

given arbitraryForRandomGraphMetrics[N: Arbitrary]: Arbitrary[GraphGen.Metrics[N]] =
Arbitrary {
Gen.choose(1, maxOrder).map { n => // RandomGraph throws exception for order-0.
new GraphGen.Metrics[N]:
override def order: Int = n
override def nodeDegrees: NodeDegreeRange =
// Don't let graph order approach vertex degreer, else
override def nodeDegrees: NodeDegreeRange =
// Don't let graph order approach vertex degreer, else
// too many edge add tries will fail and the generator will stop.
NodeDegreeRange(0, n / 2)
override def nodeGen: Gen[N] = Arbitrary.arbitrary[N]
override def connected: Boolean = false
}
}
}

given arbitrarySimplestGraph[N: Arbitrary: ClassTag]: Arbitrary[SimplestGraph[N]] =
given arbitrarySimplestGraph[N: Arbitrary: ClassTag]: Arbitrary[SimplestGraph[N]] =
def genEmpty: Gen[SimplestGraph[N]] = Graph.empty
def genNonEmpty: Gen[SimplestGraph[N]] = Arbitrary
.arbitrary[GraphGen.Metrics[N]]
.flatMap{ metrics =>
GraphGen.fromMetrics[N, UnDiEdge[N], Graph](Graph, metrics, Set(UnDiEdge)).apply
.flatMap { metrics =>
GraphGen.fromMetrics[N, UnDiEdge[N], Graph](Graph, metrics, Set(UnDiEdge)).apply
}
Arbitrary{ Gen.frequency(1 -> genEmpty, (maxOrder - 1) -> genNonEmpty) }
Arbitrary { Gen.frequency(1 -> genEmpty, (maxOrder - 1) -> genNonEmpty) }

given eqSimplestGraphByOuter[N: Eq]: Eq[SimplestGraph[N]] =
Eq.by{ g => (g.nodes.toOuter, g.edges.toOuter) }
given eqSimplestGraphByOuter[N: Eq]: Eq[SimplestGraph[N]] =
Eq.by: g =>
(g.nodes.toOuter, g.edges.toOuter)

// needed since we're in AnyFunSuiteLike land
override implicit val generatorDrivenConfig: PropertyCheckConfiguration =
PropertyCheckConfiguration(minSuccessful = 100)

// Check the SemigroupK laws for the at-least-2-element refinement of Set.
checkAll(
"graph.SimplestGraph.MonoidKLaws",
MonoidKTests[SimplestGraph](using monoidKForSimplestGraphByOuterElements
).monoidK[Int])
"graph.SimplestGraph.MonoidKLaws",
MonoidKTests[SimplestGraph](using monoidKForSimplestGraphByOuterElements).monoidK[Int]
)
end TestGraph
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ trait JsonInstancesForGeometry:
): JsonValueWriter[C, O] = new:
override def apply(c: C): O = writeRaw(c.value)


end JsonInstancesForGeometry

0 comments on commit ec52bb9

Please sign in to comment.