This GitLab instance reached the end of its service life. It won't be possible to create new users or projects.

Please read the deprecation notice for more information concerning the deprecation timeline

Visit migration.git.tu-berlin.de (internal network only) to import your old projects to the new GitLab platform 📥

NodeID.scala 901 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
package de.bbisping.coupledsim.tool.model

import de.bbisping.coupledsim.ts.TransitionSystem

class NodeID private (val name: String) extends Ordered[NodeID] {
  
  val hash = name.hashCode()
  
  override def hashCode() = hash
  
  override def equals(other: Any) = this eq other.asInstanceOf[AnyRef]
  
  def compare(that: NodeID): Int = this.hash - that.hash
  
  override def toString() = name
}

object NodeID {
  private val cache = collection.mutable.Map.empty[String, NodeID]
  
  // assumes single-threaded-ness for now
  def apply(name: String) = {
    cache getOrElseUpdate(name, new NodeID(name))
  }
  
  def freshNodeID(ts: TransitionSystem[NodeID, _, _], oldID: NodeID, suffixNum: Int = 0): NodeID = {
    val suggestedID = NodeID(oldID.name + "_" + suffixNum)
    if (ts.isFreshNode(suggestedID)) {
      suggestedID
    } else {
      freshNodeID(ts, oldID, suffixNum + 1)
    }
  }
}