lib: added init.from in array and hashset
authorAlexandre Terrasa <alexandre@moz-code.org>
Sat, 22 Jun 2013 02:58:48 +0000 (22:58 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Sat, 22 Jun 2013 02:59:37 +0000 (22:59 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

lib/standard/collection/array.nit
lib/standard/collection/hash_collection.nit
src/abstract_compiler.nit
tests/sav/test_array_init.res [new file with mode: 0644]
tests/sav/test_hashset_init.res [new file with mode: 0644]
tests/test_array_init.nit [new file with mode: 0644]
tests/test_hashset_init.nit [new file with mode: 0644]

index 47dcfea..fab39a9 100644 (file)
@@ -321,6 +321,12 @@ class Array[E]
                _length = 0
        end
 
+       # Create an array from a collection.
+       init from(items: Collection[E]) do
+               with_capacity(items.length)
+               self.add_all(items)
+       end
+
        # Create an array with some `items'.
        init with_items(objects: E...)
        do
index 0c772b8..0e0638a 100644 (file)
@@ -445,6 +445,12 @@ class HashSet[E: Object]
                _length = 0
                enlarge(0)
        end
+
+       # Build a list filled with the items of `coll'.
+       init from(coll: Collection[E]) do
+               init
+               add_all(coll)
+       end
 end
 
 private class HashSetNode[E: Object]
index 36edf73..8230b82 100644 (file)
@@ -2240,19 +2240,7 @@ end
 
 # Utils
 
-redef class HashSet[E]
-       init from(elements: Collection[E]) do
-               init
-               self.add_all(elements)
-       end
-end
-
 redef class Array[E]
-       init from(elements: Collection[E]) do
-               init
-               self.add_all(elements)
-       end
-
        # Return a new Array with the elements only contened in 'self' and not in 'o'
        fun -(o: Array[E]): Array[E] do
                var res = new Array[E]
diff --git a/tests/sav/test_array_init.res b/tests/sav/test_array_init.res
new file mode 100644 (file)
index 0000000..8a1218a
--- /dev/null
@@ -0,0 +1,5 @@
+1
+2
+3
+4
+5
diff --git a/tests/sav/test_hashset_init.res b/tests/sav/test_hashset_init.res
new file mode 100644 (file)
index 0000000..d77b1af
--- /dev/null
@@ -0,0 +1,10 @@
+10
+10
+10
+10
+10
+10
+10
+10
+10
+10
diff --git a/tests/test_array_init.nit b/tests/test_array_init.nit
new file mode 100644 (file)
index 0000000..a1e9fb6
--- /dev/null
@@ -0,0 +1,22 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2004-2008 Jean Privat <jean@pryen.org>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+var a1 = [1, 2, 3, 4, 5]
+var a2 = new Array[Int].from(a1)
+
+for i in a2 do
+       print i
+end
\ No newline at end of file
diff --git a/tests/test_hashset_init.nit b/tests/test_hashset_init.nit
new file mode 100644 (file)
index 0000000..1903672
--- /dev/null
@@ -0,0 +1,25 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2004-2008 Jean Privat <jean@pryen.org>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+var set = new HashSet[Int]
+
+for i in [0..10[ do set.add(i)
+for i in [0..10[ do set.add(i)
+
+var test = new HashSet[Int].from(set)
+
+for i in test do print test.length