core :: Collection
Collections modelize finite groups of objects, called elements.
The specific behavior and representation of collections is determined by the subclasses of the hierarchy.
The main service of Collection is to provide a stable iterator
method usable to retrieve all the elements of the collection.
Additional services are provided.
For an implementation point of view, Collection provide a basic
implementation of these services using the iterator
method.
Subclasses often provide a more efficient implementation.
Because of the iterator
method, Collections instances can use
the for
control structure.
var x: Collection[U]
# ...
for u in x do
# u is a U
# ...
end
that is equivalent with the following:
var x: Collection[U]
# ...
var i = x.iterator
while i.is_ok do
var u = i.item # u is a U
# ...
i.next
end
core :: Collection :: CONCURRENT
Type of the concurrent variant of this collectioncore :: Collection :: combinations
Allr
-length combinations on self (in same order) without repeated elements.
core :: Collection :: combinations_with_replacement
Allr
-length combination on self (in same order) with repeated elements.
core :: Collection :: defaultinit
core :: Collection :: has_all
Does the collection contain at least each element ofother
?
core :: Collection :: has_any
Does the collection contain at least one element ofother
?
core :: Collection :: has_exactly
Does the collection contain exactly all the elements ofother
?
core :: Collection :: permutations
Allr
-length permutations on self (all possible ordering) without repeated elements.
core :: Collection :: product
Cartesian product, overr
times self
.
core :: Collection :: to_concurrent
Wrapsself
in a thread-safe collection
core :: Collection :: to_counter
Create and fill up a counter with the elements of `self.core :: Collection :: to_curlslist
Convert Collection[String] to CURLSListcore :: Collection :: to_shuffle
Return a new array made of elements in a random order.core $ Collection :: SELF
Type of this instance, automatically specialized in every classcore :: abstract_text $ Collection :: to_s
String representation of the content of the collection.core :: Collection :: CONCURRENT
Type of the concurrent variant of this collectioncore :: Object :: class_factory
Implementation used byget_class
to create the specific class.
core :: Collection :: combinations
Allr
-length combinations on self (in same order) without repeated elements.
core :: Collection :: combinations_with_replacement
Allr
-length combination on self (in same order) with repeated elements.
core :: Object :: defaultinit
core :: Collection :: defaultinit
core :: Collection :: has_all
Does the collection contain at least each element ofother
?
core :: Collection :: has_any
Does the collection contain at least one element ofother
?
core :: Collection :: has_exactly
Does the collection contain exactly all the elements ofother
?
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: Object :: output_class_name
Display class name on stdout (debug only).core :: Collection :: permutations
Allr
-length permutations on self (all possible ordering) without repeated elements.
core :: Collection :: product
Cartesian product, overr
times self
.
core :: Collection :: to_concurrent
Wrapsself
in a thread-safe collection
core :: Collection :: to_counter
Create and fill up a counter with the elements of `self.core :: Collection :: to_curlslist
Convert Collection[String] to CURLSListcore :: Collection :: to_shuffle
Return a new array made of elements in a random order.combinations :: CartesianCollection
A view of a Cartesian-product collection over homogeneous collections.combinations :: CombinationCollection
A view of some combinations over a base collections.pthreads :: ConcurrentCollection
A concurrent variant to the standardCollection
ConcurrentList
c :: CByteArray
Wrapper around an array ofunsigned char
in C (unsigned char*
) with length and destroy state
c :: CCStringArray
Wrapper around an array ofCString
in C (char**
) with length and destroy state.
c :: CUInt16Array
Wrapper of a C array of typeuint16_t*
with length and destroy state
Array
List
pthreads :: ConcurrentSequence
A concurrent variant to the standardSequence
pthreads :: ConcurrentSequenceRead
A concurrent variant to the standardSequenceRead
core :: DisjointSet
Data structure to keep track of elements partitioned into disjoint subsetspthreads :: ReverseBlockingQueue
A collection whichis_empty
method blocks until it's empty
neo4j :: SequentialNodeCollection
A Neo4j node collection using a sequential identification scheme.
# The root of the collection hierarchy.
#
# Collections modelize finite groups of objects, called elements.
#
# The specific behavior and representation of collections is determined
# by the subclasses of the hierarchy.
#
# The main service of Collection is to provide a stable `iterator`
# method usable to retrieve all the elements of the collection.
#
# Additional services are provided.
# For an implementation point of view, Collection provide a basic
# implementation of these services using the `iterator` method.
# Subclasses often provide a more efficient implementation.
#
# Because of the `iterator` method, Collections instances can use
# the `for` control structure.
#
# ~~~nitish
# var x: Collection[U]
# # ...
# for u in x do
# # u is a U
# # ...
# end
# ~~~
#
# that is equivalent with the following:
#
# ~~~nitish
# var x: Collection[U]
# # ...
# var i = x.iterator
# while i.is_ok do
# var u = i.item # u is a U
# # ...
# i.next
# end
# ~~~
interface Collection[E]
# Get a new iterator on the collection.
fun iterator: Iterator[E] is abstract
# Is there no item in the collection?
#
# assert [1,2,3].is_empty == false
# 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
# assert [20..30[.length == 10
fun length: Int
do
var nb = 0
for i in self do nb += 1
return nb
end
# Is `item` in the collection ?
# Comparisons are done with ==
#
# assert [1,2,3].has(2) == true
# assert [1,2,3].has(9) == false
# assert [1..5[.has(2) == true
# assert [1..5[.has(9) == false
fun has(item: nullable Object): Bool
do
for i in self do if i == item then return true
return false
end
# Is the collection contain only `item`?
# Comparisons are done with ==
# Return true if the collection is empty.
#
# assert [1,1,1].has_only(1) == true
# assert [1,2,3].has_only(1) == false
# assert [1..1].has_only(1) == true
# assert [1..3].has_only(1) == false
# assert [3..3[.has_only(1) == true # empty collection
#
# ENSURE `is_empty implies result == true`
fun has_only(item: nullable Object): Bool
do
for i in self do if i != item then return false
return true
end
# How many occurrences of `item` are in the collection?
# Comparisons are done with ==
#
# assert [10,20,10].count(10) == 2
fun count(item: nullable Object): Int
do
var nb = 0
for i in self do if i == item then nb += 1
return nb
end
# Return the first item of the collection
#
# assert [1,2,3].first == 1
fun first: E
do
assert length > 0
return iterator.item
end
# Does the collection contain at least each element of `other`?
#
# assert [1,3,4,2].has_all([1..2]) == true
# assert [1,3,4,2].has_all([1..5]) == false
#
# Repeated elements in the collections are not considered.
#
# assert [1,1,1].has_all([1]) == true
# assert [1..5].has_all([1,1,1]) == true
#
# Note that the default implementation is general and correct for any lawful Collections.
# It is memory-efficient but relies on `has` so may be CPU-inefficient for some kind of collections.
fun has_all(other: Collection[nullable Object]): Bool
do
if is_same_instance(other) then return true
var ol = other.length
var l = length
if ol == 0 then return true
if l == 0 then return false
if ol == 1 then return has(other.first)
for x in other do if not has(x) then return false
return true
end
# Does the collection contain exactly all the elements of `other`?
#
# The same elements must be present in both `self` and `other`,
# but the order of the elements in the collections are not considered.
#
# assert [1..3].has_exactly([3,1,2]) == true # the same elements
# assert [1..3].has_exactly([3,1]) == false # 2 is not in the array
# assert [1..2].has_exactly([3,1,2]) == false # 3 is not in the range
#
# Repeated elements must be present in both collections in the same amount.
# So basically it is a multi-set comparison.
#
# assert [1,2,3,2].has_exactly([1,2,2,3]) == true # the same elements
# assert [1,2,3,2].has_exactly([1,2,3]) == false # more 2 in the first array
# assert [1,2,3].has_exactly([1,2,2,3]) == false # more 2 in the second array
#
# Note that the default implementation is general and correct for any lawful Collections.
# It is memory-efficient but relies on `count` so may be CPU-inefficient for some kind of collections.
fun has_exactly(other: Collection[nullable Object]): Bool
do
if length != other.length then return false
for e in self do if self.count(e) != other.count(e) then return false
return true
end
# Does the collection contain at least one element of `other`?
#
# assert [1,3,4,2].has_any([1..10]) == true
# assert [1,3,4,2].has_any([5..10]) == false
#
# Note that the default implementation is general and correct for any lawful Collections.
# It is memory-efficient but relies on `has` so may be CPU-inefficient for some kind of collections.
fun has_any(other: Collection[nullable Object]): Bool
do
for o in other do
if has(o) then return true
end
return false
end
end
lib/core/collection/abstract_collection.nit:20,1--201,3
redef class Collection[ E ]
# Return a random element form the collection
# There must be at least one element in the collection
#
# ~~~
# var x = [1,2,3].rand
# assert x == 1 or x == 2 or x == 3
# ~~~
fun rand: E
do
if is_empty then abort
var rand_index = length.rand
for e in self do
if rand_index == 0 then return e
rand_index -= 1
end
abort
end
# Return a new array made of elements in a random order.
#
# ~~~
# var a = [1,2,1].to_shuffle
# assert a == [1,1,2] or a == [1,2,1] or a == [2,1,1]
# ~~~
fun to_shuffle: Array[E]
do
var res = self.to_a
res.shuffle
return res
end
# Return a new array made of (at most) `length` elements randomly chosen.
#
# ~~~
# var a = [1,2,1].sample(2)
# assert a == [1,1] or a == [1,2] or a == [2,1]
# ~~~
#
# If there is not enough elements, then the result only contains them in a random order.
# See `to_shuffle`.
#
# ENSURE `result.length == self.length.min(length)`
#
# Note: the default implementation uses the Reservoir Algorithm
fun sample(length: Int): Array[E]
do
if length >= self.length then return to_shuffle
var res = new Array[E].with_capacity(length)
var it = iterator
for i in [0..length[ do
res[i] = it.item
it.next
end
res.shuffle
for i in [length+1..self.length] do
var j = i.rand
if j < length then
res[j] = it.item
end
it.next
end
return res
end
end
lib/core/math.nit:434,1--500,3
redef class Collection[E]
# String representation of the content of the collection.
#
# The standard representation is the list of elements separated with commas.
#
# ~~~
# assert [1,2,3].to_s == "[1,2,3]"
# assert [1..3].to_s == "[1,2,3]"
# assert (new Array[Int]).to_s == "[]" # empty collection
# ~~~
#
# Subclasses may return a more specific string representation.
redef fun to_s
do
return "[" + join(",") + "]"
end
# Concatenate elements without separators
#
# ~~~
# assert [1,2,3].plain_to_s == "123"
# assert [11..13].plain_to_s == "111213"
# assert (new Array[Int]).plain_to_s == "" # empty collection
# ~~~
fun plain_to_s: String
do
var s = new Buffer
for e in self do if e != null then s.append(e.to_s)
return s.to_s
end
# Concatenate and separate each elements with `separator`.
#
# Only concatenate if `separator == null`.
#
# ~~~
# assert [1, 2, 3].join(":") == "1:2:3"
# assert [1..3].join(":") == "1:2:3"
# assert [1..3].join == "123"
# ~~~
#
# if `last_separator` is given, then it is used to separate the last element.
#
# ~~~
# assert [1, 2, 3, 4].join(", ", " and ") == "1, 2, 3 and 4"
# ~~~
fun join(separator: nullable Text, last_separator: nullable Text): String
do
if is_empty then return ""
var s = new Buffer # Result
# Concat first item
var i = iterator
var e = i.item
if e != null then s.append(e.to_s)
if last_separator == null then last_separator = separator
# Concat other items
i.next
while i.is_ok do
e = i.item
i.next
if i.is_ok then
if separator != null then s.append(separator)
else
if last_separator != null then s.append(last_separator)
end
if e != null then s.append(e.to_s)
end
return s.to_s
end
end
lib/core/text/abstract_text.nit:2339,1--2412,3
redef class Collection[E]
# Cartesian product, over `r` times `self`.
#
# See `CartesianCollection` for details.
#
# FIXME: Cannot be used if RTA is enabled. So `niti` or `--erasure` only.
fun product(r: Int): Collection[SequenceRead[E]]
do
return new CartesianCollection[E]([self]*r)
end
# All `r`-length permutations on self (all possible ordering) without repeated elements.
#
# See `CartesianCollection` for details.
#
# FIXME: Cannot be used if RTA is enabled. So `niti` or `--erasure` only.
fun permutations(r: Int): Collection[SequenceRead[E]]
do
var res = new CombinationCollection[E](self, r)
res.are_sorted = false
res.are_unique = true
return res
end
# All `r`-length combinations on self (in same order) without repeated elements.
#
# See `CartesianCollection` for details.
#
# FIXME: Cannot be used if RTA is enabled. So `niti` or `--erasure` only.
fun combinations(r: Int): Collection[SequenceRead[E]]
do
var res = new CombinationCollection[E](self, r)
res.are_sorted = true
res.are_unique = true
return res
end
# All `r`-length combination on self (in same order) with repeated elements.
#
# See `CartesianCollection` for details.
#
# FIXME: Cannot be used if RTA is enabled. So `niti` or `--erasure` only.
fun combinations_with_replacement(r: Int): Collection[SequenceRead[E]]
do
var res = new CombinationCollection[E](self, r)
res.are_sorted = true
res.are_unique = false
return res
end
end
lib/combinations/combinations.nit:25,1--74,3
redef class Collection[E]
# Convert Collection[String] to CURLSList
fun to_curlslist: CURLSList
do
assert collectionItemType: self isa Collection[String] else
print "Collection item must be strings."
end
if is_empty then return new CURLSList
var primList = new CURLSList.with_str(self.first)
var is_first = true
for s in self do
if not is_first then primList.append(s)
is_first = false
end
return primList
end
end
lib/curl/native_curl.nit:337,1--353,3
redef class Collection[E]
# Type of the concurrent variant of this collection
type CONCURRENT: ConcurrentCollection[E]
# Wraps `self` in a thread-safe collection
fun to_concurrent: CONCURRENT is abstract
end
lib/pthreads/concurrent_collections.nit:40,1--46,3
redef class Collection[E]
# Create and fill up a counter with the elements of `self.
#
# ~~~
# var cpt = "abaa".chars.to_counter
# assert cpt['a'] == 3
# assert cpt['b'] == 1
# assert cpt.length == 2
# assert cpt.sum == 4
# ~~~
fun to_counter: Counter[E]
do
var res = new Counter[E]
res.inc_all(self)
return res
end
end
lib/counter/counter.nit:323,1--339,3
redef class Collection[E]
# Utility to serialize a normal Json array
private fun serialize_to_pure_json(v: JsonSerializer)
do
v.stream.write "["
var is_first = true
for e in self do
if is_first then
is_first = false
else
v.stream.write ","
if v.pretty_json then v.stream.write " "
end
if not v.try_to_serialize(e) then
assert e != null # null would have been serialized
v.warn("element of type {e.class_name} is not serializable.")
v.stream.write "null"
end
end
v.stream.write "]"
end
end
lib/json/serialization_write.nit:306,1--328,3
redef class Collection[E]
private fun serialize_as_inspect(v: InspectSerializer)
do
v.stream.write "["
var is_first = true
for e in self do
if is_first then
is_first = false
else
v.stream.write ", "
end
if not v.try_to_serialize(e) then
assert e != null
v.stream.write e.inspect
end
end
v.stream.write "]"
end
end
lib/serialization/inspect.nit:239,1--258,3