Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Use immutable SortedSet in DependencyGraph.

  • Loading branch information...
commit 6e15c7e4ccfe9341b3176be5e0a1527a2e4267d9 1 parent ff448df
Michael Schmitz authored October 25, 2012
38  core/src/main/scala/edu/washington/cs/knowitall/tool/parse/graph/DependencyGraph.scala
@@ -3,7 +3,7 @@ package tool
3 3
 package parse
4 4
 package graph
5 5
 
6  
-import scala.collection._
  6
+import scala.collection.immutable
7 7
 import org.slf4j.LoggerFactory
8 8
 import collection.immutable.Interval
9 9
 import collection.immutable.graph.Graph
@@ -19,9 +19,9 @@ class DependencyGraph (
19 19
     /** the text of the source sentence */
20 20
     val text: String,
21 21
     /** the `DependencyNode`s from the parser */
22  
-    val nodes: SortedSet[DependencyNode],
  22
+    val nodes: immutable.SortedSet[DependencyNode],
23 23
     /** the `Dependency`s from the parser */
24  
-    val dependencies: SortedSet[Dependency],
  24
+    val dependencies: immutable.SortedSet[Dependency],
25 25
     /** a graph representation dependencies */
26 26
     val graph: Graph[DependencyNode]
27 27
   ) {
@@ -44,30 +44,30 @@ class DependencyGraph (
44 44
   // constructors
45 45
 
46 46
   def this(text: String,
47  
-      nodes: SortedSet[DependencyNode],
48  
-      dependencies: SortedSet[Dependency]) =
  47
+      nodes: immutable.SortedSet[DependencyNode],
  48
+      dependencies: immutable.SortedSet[Dependency]) =
49 49
     this(nodes.iterator.map(_.text).mkString(" "),
50  
-        SortedSet[DependencyNode]() ++ nodes,
51  
-        SortedSet[Dependency]() ++ dependencies,
  50
+        immutable.SortedSet[DependencyNode]() ++ nodes,
  51
+        immutable.SortedSet[Dependency]() ++ dependencies,
52 52
         new Graph[DependencyNode](dependencies.flatMap(dep => Set(dep.source, dep.dest)).toSet, dependencies))
53 53
 
54 54
   def this(text: String,
55 55
       nodes: Iterable[DependencyNode],
56 56
       dependencies: Iterable[Dependency]) =
57 57
     this(text,
58  
-      SortedSet[DependencyNode]() ++ nodes,
59  
-      SortedSet[Dependency]() ++ dependencies)
  58
+      immutable.SortedSet[DependencyNode]() ++ nodes,
  59
+      immutable.SortedSet[Dependency]() ++ dependencies)
60 60
 
61  
-  def this(nodes: SortedSet[DependencyNode],
62  
-      dependencies: SortedSet[Dependency]) =
  61
+  def this(nodes: immutable.SortedSet[DependencyNode],
  62
+      dependencies: immutable.SortedSet[Dependency]) =
63 63
     this(nodes.iterator.map(_.text).mkString(" "),
64  
-        SortedSet[DependencyNode]() ++ nodes,
65  
-        SortedSet[Dependency]() ++ dependencies)
  64
+        immutable.SortedSet[DependencyNode]() ++ nodes,
  65
+        immutable.SortedSet[Dependency]() ++ dependencies)
66 66
 
67 67
   def this(nodes: Iterable[DependencyNode],
68 68
       dependencies: Iterable[Dependency]) =
69  
-    this(SortedSet[DependencyNode]() ++ nodes,
70  
-        SortedSet[Dependency]() ++ dependencies)
  69
+    this(immutable.SortedSet[DependencyNode]() ++ nodes,
  70
+        immutable.SortedSet[Dependency]() ++ dependencies)
71 71
 
72 72
   def canEqual(that: Any) = that.isInstanceOf[DependencyGraph]
73 73
   override def equals(that: Any) = that match {
@@ -340,7 +340,7 @@ class DependencyGraph (
340 340
     }
341 341
 
342 342
     val groups: Set[Set[DependencyNode]] = (for (dep <- graph.edges; if pred(dep)) yield {
343  
-      graph.connected(dep.source, dedge=>pred(dedge.edge))
  343
+      graph.connected(dep.source, dedge=>pred(dedge.edge)).toSet
344 344
     })(scala.collection.breakOut)
345 345
 
346 346
 
@@ -556,9 +556,9 @@ object DependencyGraph {
556 556
   val logger = LoggerFactory.getLogger(classOf[DependencyGraph])
557 557
 
558 558
   def deserialize(string: String) = {
559  
-    def rec(string: String, nodes: SortedSet[DependencyNode]): (SortedSet[DependencyNode], SortedSet[Dependency]) = {
  559
+    def rec(string: String, nodes: immutable.SortedSet[DependencyNode]): (immutable.SortedSet[DependencyNode], immutable.SortedSet[Dependency]) = {
560 560
       // we're done, return the extra nodes and dependencies
561  
-      if (string.isEmpty) (nodes, SortedSet.empty[Dependency])
  561
+      if (string.isEmpty) (nodes, immutable.SortedSet.empty[Dependency])
562 562
       // we found an extra node that needs to be deserialized
563 563
       else if (string.charAt(0) == '(') {
564 564
         val pickled = string.drop(1).takeWhile(_ != ')')
@@ -572,7 +572,7 @@ object DependencyGraph {
572 572
     }
573 573
 
574 574
     try {
575  
-      val (nodes, deps) = rec(string, SortedSet[DependencyNode]())
  575
+      val (nodes, deps) = rec(string, immutable.SortedSet[DependencyNode]())
576 576
       val depNodes = deps.flatMap(dep => List(dep.source, dep.dest)).toSet
577 577
       new DependencyGraph((depNodes ++ nodes).iterator.map(_.text).mkString(" "), nodes ++ depNodes, deps, new Graph[DependencyNode](depNodes, deps))
578 578
     }

0 notes on commit 6e15c7e

Please sign in to comment.
Something went wrong with that request. Please try again.