Range of discrete objects.

Introduced properties

fun after: E

core :: Range :: after

Get the element after the last one.
protected fun after=(after: E)

core :: Range :: after=

Get the element after the last one.
init defaultinit(from: E, to: E)

core :: Range :: defaultinit

Create a range [from, to].
protected fun first=(first: E)

core :: Range :: first=

fun last: E

core :: Range :: last

Get the last element.
protected fun last=(last: E)

core :: Range :: last=

Get the last element.
fun reverse_iterator: Iterator[E]

core :: Range :: reverse_iterator

Gets an iterator starting at the end and going backwards
fun step(step: Int): Iterator[E]

core :: Range :: step

Gets an iterator that progress with a given step.
init without_last(from: E, to: E)

core :: Range :: without_last

Create a range [from, to[.

Redefined properties

redef fun ==(o: nullable Object): Bool

core $ Range :: ==

Two ranges are equals if they have the same first and last elements.
redef type SELF: Range[E]

core $ Range :: SELF

Type of this instance, automatically specialized in every class
redef fun count(item: nullable Object): Int

core $ Range :: count

assert [1..10].count(1) == 1
redef fun first: E

core $ Range :: first

Return the first item of the collection
redef fun has(item: nullable Object): Bool

core $ Range :: has

assert [1..10].has(5)
redef fun has_only(item: nullable Object): Bool

core $ Range :: has_only

assert [1..1].has_only(1)
redef fun hash: Int

core $ Range :: hash

var a = new Range[Int](10, 15)
redef fun is_empty: Bool

core $ Range :: is_empty

assert not [1..10[.is_empty
redef fun iterator: Iterator[E]

core $ Range :: iterator

Get a new iterator on the collection.
redef fun length: Int

core $ Range :: length

assert [1..10].length == 10

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type CONCURRENT: ConcurrentCollection[E]

core :: Collection :: CONCURRENT

Type of the concurrent variant of this collection
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun after: E

core :: Range :: after

Get the element after the last one.
protected fun after=(after: E)

core :: Range :: after=

Get the element after the last one.
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun combinations(r: Int): Collection[SequenceRead[E]]

core :: Collection :: combinations

All r-length combinations on self (in same order) without repeated elements.
fun combinations_with_replacement(r: Int): Collection[SequenceRead[E]]

core :: Collection :: combinations_with_replacement

All r-length combination on self (in same order) with repeated elements.
fun count(item: nullable Object): Int

core :: Collection :: count

How many occurrences of item are in the collection?
init defaultinit(from: E, to: E)

core :: Range :: defaultinit

Create a range [from, to].
fun first: E

core :: Collection :: first

Return the first item of the collection
protected fun first=(first: E)

core :: Range :: first=

fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun has(item: nullable Object): Bool

core :: Collection :: has

Is item in the collection ?
fun has_all(other: Collection[nullable Object]): Bool

core :: Collection :: has_all

Does the collection contain at least each element of other?
fun has_any(other: Collection[nullable Object]): Bool

core :: Collection :: has_any

Does the collection contain at least one element of other?
fun has_exactly(other: Collection[nullable Object]): Bool

core :: Collection :: has_exactly

Does the collection contain exactly all the elements of other?
fun has_only(item: nullable Object): Bool

core :: Collection :: has_only

Is the collection contain only item?
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
fun is_empty: Bool

core :: Collection :: is_empty

Is there no item in the collection?
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
abstract fun iterator: Iterator[E]

core :: Collection :: iterator

Get a new iterator on the collection.
fun join(separator: nullable Text, last_separator: nullable Text): String

core :: Collection :: join

Concatenate and separate each elements with separator.
fun last: E

core :: Range :: last

Get the last element.
protected fun last=(last: E)

core :: Range :: last=

Get the last element.
fun length: Int

core :: Collection :: length

Number of items in the collection.
fun not_empty: Bool

core :: Collection :: not_empty

Alias for not is_empty.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun permutations(r: Int): Collection[SequenceRead[E]]

core :: Collection :: permutations

All r-length permutations on self (all possible ordering) without repeated elements.
fun plain_to_s: String

core :: Collection :: plain_to_s

Concatenate elements without separators
fun product(r: Int): Collection[SequenceRead[E]]

core :: Collection :: product

Cartesian product, over r times self.
fun rand: E

core :: Collection :: rand

Return a random element form the collection
fun reverse_iterator: Iterator[E]

core :: Range :: reverse_iterator

Gets an iterator starting at the end and going backwards
fun sample(length: Int): Array[E]

core :: Collection :: sample

Return a new array made of (at most) length elements randomly chosen.
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun step(step: Int): Iterator[E]

core :: Range :: step

Gets an iterator that progress with a given step.
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
fun to_a: Array[E]

core :: Collection :: to_a

Build a new array from a collection
abstract fun to_concurrent: CONCURRENT

core :: Collection :: to_concurrent

Wraps self in a thread-safe collection
fun to_counter: Counter[E]

core :: Collection :: to_counter

Create and fill up a counter with the elements of `self.
fun to_curlslist: CURLSList

core :: Collection :: to_curlslist

Convert Collection[String] to CURLSList
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun to_shuffle: Array[E]

core :: Collection :: to_shuffle

Return a new array made of elements in a random order.
init without_last(from: E, to: E)

core :: Range :: without_last

Create a range [from, to[.
package_diagram core::Range Range core::Collection Collection core::Range->core::Collection core::Object Object core::Collection->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

interface Collection[E: nullable Object]

core :: Collection

The root of the collection hierarchy.

Class definitions

core $ Range
# Range of discrete objects.
class Range[E: Discrete]
	super Collection[E]

	redef var first

	# Get the last element.
	var last: E

	# Get the element after the last one.
	var after: E

	#     assert [1..10].has(5)
	#     assert [1..10].has(10)
	#     assert not [1..10[.has(10)
	redef fun has(item) do return item isa Comparable and item >= first and item <= last

	#     assert [1..1].has_only(1)
	#     assert not [1..10].has_only(1)
	redef fun has_only(item) do return first == item and item == last or is_empty

	#     assert [1..10].count(1)	== 1
	#     assert [1..10].count(0)	== 0
	redef fun count(item)
	do
		if has(item) then
			return 1
		else
			return 0
		end
	end

	redef fun iterator do return new IteratorRange[E](self)

	# Gets an iterator starting at the end and going backwards
	#
	#     var reviter = [1..4].reverse_iterator
	#     assert reviter.to_a == [4,3,2,1]
	#
	#     reviter = [1..4[.reverse_iterator
	#     assert reviter.to_a == [3,2,1]
	fun reverse_iterator: Iterator[E] do return new ReverseIteratorRange[E](self)

	#     assert [1..10].length		== 10
	#     assert [1..10[.length		== 9
	#     assert [1..1].length		== 1
	#     assert [1..-10].length	== 0
	redef fun length
	do
		if is_empty then return 0
		var nb = first.distance(after)
		if nb > 0 then
			return nb
		else
			return 0
		end
	end

	#     assert not [1..10[.is_empty
	#     assert not [1..1].is_empty
	#     assert [1..-10].is_empty
	redef fun is_empty do return first >= after

	# Create a range [`from`, `to`].
	# The syntax `[from..to]` is equivalent.
	#
	#     var a = [10..15]
	#     var b = new Range[Int] (10,15)
	#     assert a == b
	#     assert a.to_a == [10, 11, 12, 13, 14, 15]
	init(from: E, to: E) is old_style_init do
		first = from
		last = to
		after = to.successor(1)
	end

	# Create a range [`from`, `to`[.
	# The syntax `[from..to[` is equivalent.
	#
	#     var a = [10..15[
	#     var b = new Range[Int].without_last(10,15)
	#     assert a == b
	#     assert a.to_a == [10, 11, 12, 13, 14]
	init without_last(from: E, to: E)
	do
		first = from
		if from <= to then
			last = to.predecessor(1)
			after = to
		else
			last = to.successor(1)
			after = to
		end
	end

	# Two ranges are equals if they have the same first and last elements.
	#
	#     var a = new Range[Int](10, 15)
	#     var b = new Range[Int].without_last(10, 15)
	#     assert a == [10..15]
	#     assert a == [10..16[
	#     assert not a == [10..15[
	#     assert b == [10..15[
	#     assert b == [10..14]
	#     assert not b == [10..15]
	redef fun ==(o) do
		return o isa Range[E] and self.first == o.first and self.last == o.last
	end

	#     var a = new Range[Int](10, 15)
	#     assert a.hash == 455
	#     var b = new Range[Int].without_last(10, 15)
	#     assert b.hash == 432
	redef fun hash do
		# 11 and 23 are magic numbers empirically determined to be not so bad.
		return first.hash * 11 + last.hash * 23
	end

	# Gets an iterator that progress with a given step.
	#
	# The main usage is in `for` construction.
	#
	# ~~~
	# for i in [10..25].step(10) do assert i == 10 or i == 20
	# ~~~
	#
	# But `step` is usable as any kind of iterator.
	#
	# ~~~
	# assert [10..27].step(5).to_a == [10,15,20,25]
	# ~~~
	#
	# If `step == 1`, then it is equivalent to the default `iterator`.
	#
	# ~~~
	# assert [1..5].step(1).to_a == [1..5].to_a
	# ~~~
	#
	# If `step` is negative, then the iterator will iterate on ranges whose `first` > `last`.
	#
	# ~~~
	# assert [25..12].step(-5).to_a == [25,20,15]
	# ~~~
	#
	# On such ranges, the default `iterator` will be empty
	#
	# ~~~
	# assert [5..1].step(1).to_a.is_empty
	# assert [5..1].iterator.to_a.is_empty
	# assert [5..1].to_a.is_empty
	# assert [5..1].is_empty
	# ~~~
	#
	# Note that on non-empty range, iterating with a negative step will be empty
	#
	# ~~~
	# assert [1..5].step(-1).to_a.is_empty
	# ~~~
	fun step(step: Int): Iterator[E]
	do
		var i
		if step >= 0 then
			i = iterator
		else
			i = new DowntoIteratorRange[E](self)
			step = -step
		end

		if step == 1 then return i
		return i.to_step(step)
	end
end
lib/core/collection/range.nit:18,1--189,3