_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
_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]
# 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]
--- /dev/null
+1
+2
+3
+4
+5
--- /dev/null
+10
+10
+10
+10
+10
+10
+10
+10
+10
+10
--- /dev/null
+# 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
--- /dev/null
+# 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