[svn] r6424: nemerle/trunk/lib/narray.n

VladD2 svnadmin at nemerle.org
Mon Jul 3 08:18:53 CEST 2006


Log:
Has add some extension methods to NArray.

Author: VladD2
Date: Mon Jul  3 08:18:51 2006
New Revision: 6424

Modified:
   nemerle/trunk/lib/narray.n

Modified: nemerle/trunk/lib/narray.n
==============================================================================
--- nemerle/trunk/lib/narray.n	(original)
+++ nemerle/trunk/lib/narray.n	Mon Jul  3 08:18:51 2006
@@ -27,6 +27,7 @@
  */
 
 using Nemerle.Collections;
+using SCG = System.Collections.Generic;
 
 namespace Nemerle.Utility 
 {
@@ -372,9 +373,100 @@
       dest;
     }
     
+    /** Convert collection to array. */
+    public ToArray[T] (this sourse : SCG.ICollection[T]) : array [T]
+    {
+      if (sourse is null)
+        array (0)
+      else
+      {
+        def tmp = array (sourse.Count);
+        sourse.CopyTo (tmp, 0);
+        tmp
+      }
+    }
+
+    /** Convert sequence to array. */
+    public ToArray[T] (this sourse : SCG.IEnumerable [T]) : array [T]
+    {
+      match (sourse)
+      {
+        | coll is SCG.ICollection[T] => coll.ToArray();
+        | null => array (0);
+        | _ =>
+          def dest = SCG.List();
+
+          foreach (elem in sourse)
+            dest.Add(elem);
+            
+          dest.ToArray()
+      }
+    }
+
+    /**
+     * Convert collection of one type to array of another type.
+     */
+    public ToArray[From, To] (this sourse : SCG.ICollection[From], 
+      f : From -> To) : array [To]
+    {
+      if (sourse is null)
+        array (0)
+      else
+      {
+        def tmp = array (sourse.Count);
+        sourse.CopyTo (tmp, 0);
+        tmp.ConvertTo (f)
+      }
+    }
+
     /**
-     * Convert array to string.
+     * Convert sequence of one type to array of another type.
      */
+    public ToArray[From, To] (this sourse : SCG.IEnumerable [From], 
+      f : From -> To) : array [To]
+    {
+      match (sourse)
+      {
+        | coll is SCG.ICollection[From] => coll.ToArray (f);
+        | null => array (0);
+        | _ =>
+          def dest = SCG.List();
+
+          foreach (elem in sourse)
+            dest.Add(f (elem));
+            
+          dest.ToArray()
+      }
+    }
+
+    /** Attention! It's inplace sort. */
+    public SortInplace[T] (this sourse : array [T], comparison : System.Comparison[T]) : array [T]
+    {
+      System.Array.Sort(sourse, comparison);
+      sourse
+    }
+
+    //FixMe: narray.n(457,37,457,58): error : Internal compiler error, please report 
+    // a bug to bugs.nemerle.org. Yon ``False'' failed in file generation/Typer4.n, line 186:
+    ///** Attention! It's inplace sort. */
+    //public Sort[T, Val] (this sourse : array [T], getComparableValue : T -> Val) : array [T]
+    //  where Val: System.IComparable[Val]
+    //{
+    //  def Cmp(x : T, y : T) : int
+    //  {
+    //    getComparableValue(x).CompareTo(getComparableValue(y))
+    //  }
+    //  System.Array.Sort.[T](sourse, System.Comparison.[T](Cmp));
+    //  sourse
+    //}
+
+    public Clone[T](this sourse : T) : T
+      where T: System.ICloneable
+    {
+      (sourse.Clone() :> T)
+    }
+
+    /** Convert array to string. */
     public ToString['a] (this sourse : array ['a], separator : string) : string
     {
       string.Join(separator, sourse.ConvertTo(value => value.ToString()));



More information about the svn mailing list