in cassovary-core/src/main/scala/com/twitter/cassovary/graph/ArrayBasedDynamicDirectedGraph.scala [57:100]
private def outboundListOption(id: Int): Option[IntArrayList] =
if (id < 0 || id >= outboundLists.size)
None
else
Option(outboundLists(id)) // If list is null, convert to None
private def inboundListOption(id: Int): Option[IntArrayList] =
if (id < 0 || id >= inboundLists.size)
None
else
Option(inboundLists(id)) // If list is null, convert to None
/** Wraps outboundList and inboundList in a node structure. This is an inner class
* because mutations will affect other nodes of the graph if both inbound
* and outbound neighbor lists are stored.
*
* For efficiency, we don't store Nodes, but create them on the fly as needed.
*/
class IntListNode(override val id: Int,
val outboundList: Option[CSeq[Int]],
val inboundList: Option[CSeq[Int]])
extends DynamicNode {
override def outboundNodes(): CSeq[Int] = outboundList.getOrElse(CSeq.empty[Int])
override def inboundNodes(): CSeq[Int] = storedGraphDir match {
case Mutual => outboundNodes()
case _ => inboundList.getOrElse(CSeq.empty[Int])
}
def addOutBoundNodes(nodeIds: Seq[Int]): Unit = {
//For future optimization, we could check if we are only storing outbound
// nodes and then do:
// outboundList.get.addAll(IntArrayList.wrap(nodeIds.toArray))
nodeIds map { addEdge(id, _) }
}
def addInBoundNodes(nodeIds: Seq[Int]): Unit = {
nodeIds map { addEdge(_, id) }
}
def removeOutBoundNode(nodeId: Int): Unit = removeEdge(id, nodeId)
def removeInBoundNode(nodeId: Int): Unit = removeEdge(nodeId, id)
}