Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix toShortSafely: yes we can safely convert -1 to short :) #168

Merged
merged 6 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions core/src/main/scala/flatgraph/DiffGraphApplier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ private[flatgraph] class DiffGraphApplier(graph: Graph, diff: DiffGraphBuilder)
already
case None =>
val nodeKind = detached.nodeKind
assert(
nodeKind < graph.schema.getNumberOfNodeKinds,
s"given nodeKind=$nodeKind does not fit into the graph schema, which only supports node kinds [0..${graph.schema.getNumberOfNodeKinds - 1}] (inclusive range)"
)
graph.schema.verifyNodeKindIsValid(nodeKind)

val seqId = graph.nodeCountByKind(nodeKind) + Option(newNodes(nodeKind)).map { _.size }.getOrElse(0)
val res = graph.schema.makeNode(graph, nodeKind, seqId)
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/scala/flatgraph/Graph.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ class Graph(val schema: Schema, val storagePathMaybe: Option[Path] = None) exten
private[flatgraph] val neighbors: Array[AnyRef] = makeNeighbors()

/** Note: this included `deleted` nodes! You might want to use `livingNodeCountByKind` instead. */
private[flatgraph] def nodeCountByKind(kind: Int): Int =
if (nodesArray.length <= kind) 0
else nodesArray(kind).length
private[flatgraph] def nodeCountByKind(kind: Int): Int = {
schema.verifyNodeKindIsValid(kind)
nodesArray(kind).length
}

def _nodes(nodeKind: Int): InitNodeIterator[GNode] = {
if (nodeKind < 0 || schema.getNumberOfNodeKinds <= nodeKind) InitNodeIteratorArray(Array.empty[GNode])
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/scala/flatgraph/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ abstract class Schema {
def allocateEdgeProperty(nodeKind: Int, direction: Direction, edgeKind: Int, size: Int): Array[_]
def getNodePropertyFormalType(nodeKind: Int, propertyKind: Int): FormalQtyType.FormalType
def getNodePropertyFormalQuantity(nodeKind: Int, propertyKind: Int): FormalQtyType.FormalQuantity

def verifyNodeKindIsValid(kind: Int): Unit = {
assert(
0 <= kind && kind < getNumberOfNodeKinds,
s"given nodeKind=$kind does not belong to (this) graph schema, which only supports node kinds [0..${getNumberOfNodeKinds - 1}] (inclusive range)"
)
}
}

class FreeSchema(
Expand Down
7 changes: 5 additions & 2 deletions core/src/main/scala/flatgraph/misc/Conversions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package flatgraph.misc
object Conversions {
extension (i: Int) {
def toShortSafely: Short = {
assert(0 <= i && i <= Short.MaxValue, throw new ConversionException(s"cannot safely downcast int with value=$i to short"))
i.toShort
val shortValue = i.toShort
if (shortValue.toInt == i)
shortValue
else
throw new ConversionException(s"cannot safely downcast int with value=$i to short")
}
}
}
Expand Down
Loading