Merge: Extends chars, string and stream with some whitespace-related function
[nit.git] / lib / poset.nit
index c6fd1d2..93afcf1 100644 (file)
@@ -71,7 +71,7 @@ module poset
 #
 # Thanks to the `[]` method, elements can be considered relatively to the poset.
 # SEE `POSetElement`
-class POSet[E: Object]
+class POSet[E]
        super Collection[E]
        super Comparator
 
@@ -226,21 +226,26 @@ class POSet[E: Object]
 
        # Write the POSet as a graphviz digraph.
        #
-       # Nodes are identified with their `to_s`.
+       # Nodes are labeled with their `to_s` so homonymous nodes may appear.
        # Edges are unlabeled.
-       fun write_dot(f: OStream)
+       fun write_dot(f: Writer)
        do
                f.write "digraph \{\n"
+               var ids = new HashMap[E, Int]
+               for x in elements.keys do
+                       ids[x] = ids.length
+               end
                for x in elements.keys do
                        var xstr = x.to_s.escape_to_dot
-                       f.write "\"{xstr}\";\n"
+                       var nx = "n{ids[x]}"
+                       f.write "{nx}[label=\"{xstr}\"];\n"
                        var xe = self.elements[x]
                        for y in xe.dtos do
-                               var ystr = y.to_s.escape_to_dot
+                               var ny = "n{ids[y]}"
                                if self.has_edge(y,x) then
-                                       f.write "\"{xstr}\" -> \"{ystr}\"[dir=both];\n"
+                                       f.write "{nx} -> {ny}[dir=both];\n"
                                else
-                                       f.write "\"{xstr}\" -> \"{ystr}\";\n"
+                                       f.write "{nx} -> {ny};\n"
                                end
                        end
                end
@@ -253,8 +258,7 @@ class POSet[E: Object]
        # See `write_dot` for details.
        fun show_dot
        do
-               var f = new OProcess("dot", "-Txlib")
-               f.write "\}\n"
+               var f = new ProcessWriter("dot", "-Txlib")
                write_dot(f)
                f.close
                f.wait
@@ -366,14 +370,16 @@ end
 #
 # For instance, one common usage is to add a specific attribute for each poset a class belong.
 #
-#     class Thing
-#         var in_some_relation: POSetElement[Thing]
-#         var in_other_relation: POSetElement[Thing]
-#     end
-#     var t: Thing # ...
-#     t.in_some_relation.greaters
-#
-class POSetElement[E: Object]
+# ~~~nitish
+# class Thing
+#     var in_some_relation: POSetElement[Thing]
+#     var in_other_relation: POSetElement[Thing]
+# end
+# var t: Thing
+# # ...
+# t.in_some_relation.greaters
+# ~~~
+class POSetElement[E]
        # The poset self belong to
        var poset: POSet[E]