Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.00 KB | None | 0 0
  1. lass Point { /* some reference to the DCEL or points in general */ }
  2. class Face  { /* some reference to the DCEL or faces in general */ }
  3.  
  4. class ConflictGraph{
  5.     private var setOfArcs = Set[(Point, Face)]()
  6.  
  7.     /* adds an arc to the set, with two possible options for the parameters */
  8.     def    addArc(p: Point, f: Face): Unit = setOfArcs += ((p,f))
  9.     def    addArc(a: (Point, Face)):  Unit = setOfArcs += a
  10.  
  11.     /* removes an arc from the set, with two possible options for the parameters */
  12.     def removeArc(p: Point, f: Face): Unit = setOfArcs -= ((p,f))
  13.     def removeArc(a: (Point, Face)):  Unit = setOfArcs -= a
  14.  
  15.     /* returns the list of points/faces visible from that face/point */
  16.     def P_conflict(f: Face): List[Point] = setOfArcs.filter(_._2 == f).toList.map(_._1)
  17.     def F_conflict(p: Point): List[Face] = setOfArcs.filter(_._1 == p).toList.map(_._2)
  18.  
  19.     /* removes all the arcs with that point */
  20.     def removePoint(p: Point): Unit = setOfArcs --= setOfArcs.filter(_._1 == p)
  21.     /* removes all the arcs with that face */
  22.     def removeFace (f: Face):  Unit = setOfArcs --= setOfArcs.filter(_._2 == f)
  23.  
  24.     /* adds new arcs with that point and each face */
  25.     def addPointWithItsVisibleFaces(p: Point, l: List[Face]): Unit = l.foreach(f => setOfArcs += ((p,f)))
  26.     /* adds new arcs with that face and each point */
  27.     def addFaceWithItsVisiblePoints(f: Face, l: List[Point]): Unit = l.foreach(p => setOfArcs += ((p,f)))
  28.  
  29.     override def toString: String = setOfArcs.toString
  30. }
  31.  
  32. class Test{
  33.     private var setOfArcs = Set[(Int, String)]()
  34.  
  35.     def    addArc(p: Int, f: String): Unit = setOfArcs += ((p,f))
  36.     def    addArc(a: (Int, String)): Unit = setOfArcs += a
  37.  
  38.  
  39.     def removeArc(p: Int, f: String): Unit = setOfArcs -= ((p,f))
  40.     def removeArc(a: (Int, String)):  Unit = setOfArcs -= a
  41.  
  42.     def P_conflict(f: String): List[Int] = setOfArcs.filter(_._2 == f).toList.map(_._1)
  43.     def F_conflict(p: Int): List[String] = setOfArcs.filter(_._1 == p).toList.map(_._2)
  44.  
  45.     def removeInt(p: Int): Unit = setOfArcs --= setOfArcs.filter(_._1 == p)
  46.     def removeString (f: String):  Unit = setOfArcs --= setOfArcs.filter(_._2 == f)
  47.    
  48.     def addIntWithItsVisibleStrings(p: Int, l: List[String]): Unit = l.foreach(f => setOfArcs += ((p,f)))
  49.     def addStringWithItsVisibleInts(f: String, l: List[Int]): Unit = l.foreach(p => setOfArcs += ((p,f)))
  50.  
  51.     override def toString: String = setOfArcs.toString
  52. }
  53.  
  54. object Main {
  55.  
  56.     def main(args: Array[String]) {
  57.         val test = new Test
  58.  
  59.         test.addArc(1, "a")
  60.         test.addArc(1, "a") // added only once
  61.         test.addArc(2, "b")
  62.         test.addArc((2, "c"))
  63.         println(test)
  64.  
  65.         test.removeArc(999, "zzz") // no problem if I try to remove an inexistent item
  66.         println(test)
  67.  
  68.         test.removeInt(2)
  69.         println(test)
  70.  
  71.         test.addIntWithItsVisibleStrings(3, List("d","e"))
  72.         println(test)
  73.  
  74.         test.addStringWithItsVisibleInts("f", List(4,5))
  75.         println(test)
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement