Merge: Misc for lib
authorJean Privat <jean@pryen.org>
Tue, 31 Mar 2015 00:44:48 +0000 (07:44 +0700)
committerJean Privat <jean@pryen.org>
Tue, 31 Mar 2015 00:44:48 +0000 (07:44 +0700)
Litttle features and warning-fixes for the libs

Pull-Request: #1228
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>
Reviewed-by: Romain Chanoir <chanoir.romain@courrier.uqam.ca>
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/geometry/points_and_lines.nit
lib/privileges/privileges.nit [moved from lib/privileges.nit with 100% similarity]
lib/standard/collection/abstract_collection.nit
lib/standard/collection/sorter.nit
lib/standard/file.nit
lib/template/examples/tmpl_composer.nit

index 14a67fb..303aae1 100644 (file)
@@ -55,9 +55,13 @@ end
 
 # An abstract 2d line segment
 interface ILine[N: Numeric]
+       # The type of points that ends the segment
        type P: IPoint[N]
 
+       # The point that is the left-end of the segment
        fun point_left: P is abstract
+
+       # The point that is the right-end of the segment
        fun point_right: P is abstract
 
        redef fun to_s do return "{point_left}--{point_right}"
index d51e710..1b1e71e 100644 (file)
@@ -66,6 +66,14 @@ interface Collection[E]
        #     assert [1..1[.is_empty   == true
        fun is_empty: Bool do return length == 0
 
+       # Alias for `not is_empty`.
+       #
+       # Some people prefer to have conditions grammatically easier to read.
+       #
+       #     assert [1,2,3].not_empty  == true
+       #     assert [1..1[.not_empty   == false
+       fun not_empty: Bool do return not self.is_empty
+
        # Number of items in the collection.
        #
        #     assert [10,20,30].length == 3
index cfcc376..00c45a3 100644 (file)
@@ -250,6 +250,81 @@ interface Comparator
 
 end
 
+redef class MapRead[K,V]
+       # Return an array of all values sorted with their keys using `comparator`.
+       #
+       # ~~~
+       # var map = new HashMap[Int, String]
+       # map[10] = "ten"
+       # map[2]  = "two"
+       # map[1]  = "one"
+       # assert map.values_sorted_by_key(default_comparator) == ["one", "two", "ten"]
+       # assert map.values_sorted_by_key(alpha_comparator) == ["one", "ten", "two"]
+       # ~~~
+       fun values_sorted_by_key(comparator: Comparator): Array[V]
+       do
+               var keys = self.keys.to_a
+               comparator.sort(keys)
+               return [for k in keys do self[k]]
+       end
+
+       # Return an array of all keys sorted with their values using `comparator`.
+       #
+       # ~~~
+       # var map = new HashMap[String, Int]
+       # map["ten"] = 10
+       # map["two"] = 2
+       # map["one"] = 1
+       # assert map.keys_sorted_by_values(default_comparator) == ["one", "two", "ten"]
+       # assert map.keys_sorted_by_values(alpha_comparator) == ["one", "ten", "two"]
+       # ~~~
+       #
+       # See: `to_map_comparator` to get the comparator used internally.
+       fun keys_sorted_by_values(comparator: Comparator): Array[K]
+       do
+               var keys = self.keys.to_a
+               var map_cmp = to_map_comparator(comparator)
+               map_cmp.sort(keys)
+               return keys
+       end
+
+       # A comparator that compares things with their values in self.
+       #
+       # See `MapComparator` for details.
+       fun to_map_comparator(comparator: Comparator): MapComparator[K, V] do return new MapComparator[K,V](self, comparator)
+end
+
+# A comparator that compares things with their values in a map.
+#
+# ~~~
+# var map = new HashMap[String, Int]
+# map["ten"] = 10
+# map["two"] = 2
+# map["one"] = 1
+#
+# var map_cmp = map.to_map_comparator(default_comparator)
+# var a = ["ten", "one", "two"]
+# map_cmp.sort(a)
+# assert a == ["one", "two", "ten"]
+# map_cmp = map.to_map_comparator(alpha_comparator)
+# map_cmp.sort(a)
+# assert a == ["one", "ten", "two"]
+# ~~~
+class MapComparator[K,V]
+       super Comparator
+
+       # What is compared are the keys of the values
+       redef type COMPARED: K
+
+       # The map that associates compared elements to the value used to compare them
+       var map: MapRead[K,V]
+
+       # The comparator used to compare values
+       var comparator: Comparator
+
+       redef fun compare(a,b) do return comparator.compare(map[a], map[b])
+end
+
 # This comparator uses the operator `<=>` to compare objects.
 # see `default_comparator` for an easy-to-use general stateless default comparator.
 class DefaultComparator
index a2856ae..81341ad 100644 (file)
@@ -47,6 +47,21 @@ abstract class FileStream
        # File descriptor of this file
        fun fd: Int do return _file.fileno
 
+       redef fun close
+       do
+               if _file == null then return
+               if _file.address_is_null then
+                       if last_error != null then return
+                       last_error = new IOError("Cannot close unopened file")
+                       return
+               end
+               var i = _file.io_close
+               if i != 0 then
+                       last_error = new IOError("Close failed due to error {sys.errno.strerror}")
+               end
+               _file = null
+       end
+
        # Sets the buffering mode for the current FileStream
        #
        # If the buf_size is <= 0, its value will be 512 by default
@@ -89,11 +104,9 @@ class FileReader
 
        redef fun close
        do
-               if _file == null or _file.address_is_null then return
-               var i = _file.io_close
+               super
                _buffer.clear
                end_reached = true
-               _file = null
        end
 
        redef fun fill_buffer
@@ -122,6 +135,7 @@ class FileReader
                end
        end
 
+       # Creates a new File stream from a file descriptor
        init from_fd(fd: Int) do
                self.path = ""
                prepare_buffer(1)
@@ -154,19 +168,8 @@ class FileWriter
 
        redef fun close
        do
-               if _file == null then return
-               if _file.address_is_null then
-                       if last_error != null then return
-                       last_error = new IOError("Cannot close unopened write stream")
-                       _is_writable = false
-                       return
-               end
-               var i = _file.io_close
-               if i != 0 then
-                       last_error = new IOError("Close failed due to error {sys.errno.strerror}")
-               end
+               super
                _is_writable = false
-               _file = null
        end
        redef var is_writable = false
 
index 6160b1a..3150c5f 100644 (file)
@@ -55,8 +55,6 @@ class TmplComposer
        # Short name
        var name: String
 
-       init(name: String) do self.name = name
-
        redef fun rendering do add "- {name}\n"
 end
 
@@ -69,13 +67,6 @@ class TmplComposerDetail
        var birth: Int
        var death: Int
 
-       init(firstname, lastname: String, birth, death: Int) do
-               self.firstname = firstname
-               self.lastname = lastname
-               self.birth = birth
-               self.death = death
-       end
-
        redef fun rendering do add """
 
 COMPOSER: {{{firstname}}} {{{lastname}}}