From 05258f86f503493e98615913fe4e2d511360fa6b Mon Sep 17 00:00:00 2001 From: Pat Garrity Date: Thu, 8 Jan 2026 04:26:52 +0000 Subject: [PATCH] (patch) add incoming edges (#4) Reviewed-on: https://git.garrity.co/garrity-software/gs-graph/pulls/4 --- build.sbt | 1 + .../main/scala/gs/graph/v0/Adjacency.scala | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/build.sbt b/build.sbt index d477c9f..520ffd6 100644 --- a/build.sbt +++ b/build.sbt @@ -51,6 +51,7 @@ lazy val testSettings = Seq( lazy val `gs-graph` = project .in(file(".")) .aggregate(core, cats, fs2) + .settings(noPublishSettings) .settings(sharedSettings) .settings(testSettings) .settings(name := s"${gsProjectName.value}-v${semVerMajor.value}") diff --git a/modules/core/src/main/scala/gs/graph/v0/Adjacency.scala b/modules/core/src/main/scala/gs/graph/v0/Adjacency.scala index 7bb4fd6..bc4966a 100644 --- a/modules/core/src/main/scala/gs/graph/v0/Adjacency.scala +++ b/modules/core/src/main/scala/gs/graph/v0/Adjacency.scala @@ -20,6 +20,26 @@ final class Adjacency(val neighbors: Vector[Vector[Vertex]]): */ def at(vertex: Vertex): Vector[Vertex] = neighbors(vertex.ordinal) + /** Get the vector of _incoming_ [[Vertex]] that point _to_ some input + * [[Vertex]]. + * + * @param vertex + * The [[Vertex]] for which to retrieve the incoming vertices. + * @return + * The list of [[Vertex]] that have an edge _to_ the input [[Vertex]]. + */ + def incoming(vertex: Vertex): Vector[Vertex] = + if vertex.ordinal >= neighbors.length then Vector.empty + else + neighbors.zipWithIndex + .filterNot { + // ignore the neighbors of the input vertex + case (_, index) => index != vertex.ordinal + } + .filter(_._1.contains(vertex)) + .map(_._2) + .map(Vertex(_)) + /** Express this [[Adjacency]] as a vector of [[Edge]]. * * @return