class

Quartz::List(T)

Inherits Iterable / Comparable / Enumerable / Reference / Object

A List (implementation of a doubly linked list) is a collection of objects of type T that behaves much like an Array.

All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.

This structure allows for efficient insertion or removal of elements from any position since it returns a List::Node from all insert operations (#push, #insert, #unshift) in order to be reused in #delete.

TODO : #insert_before(node)

Constructors

new(size : Int, value : T)

Creates a new List of the given size filled with the same value in each position.

List.new(3, 'a') # => List{'a', 'a', 'a'}
Source
new(array : Array(T))

Creates a new List that copies its items from an Array.

List.new([1, 2, 3]) # => List{1, 2, 3}
Source
new

Creates a new empty List

Source
new(size : Int, &)

Creates a new List of the given size and invokes the block once for each index of the list, assigning the block's value in that index.

List.new(3) { |i| (i + 1) ** 2 } # => List{1, 4, 9}
Source

Instance methods

+(other : List(U)) forall U

Concatenation. Returns a new List built by concatenating two lists together to create a third. The type of the new list is the union of the types of both the other lists.

Source
<<(obj : T)

Pushes the given value on to the end of this list. Returns self instead of the created node.

Source
<=>(other : List)

Combined comparison operator. Returns 0 if self equals other, 1 if self is greater than other and -1 if self is smaller than other.

It compares the elements of both lists in the same position using the <=> operator. As soon as one of such comparisons returns a non-zero value, that result is the return value of the comparison.

If all elements are equal, the comparison is based on the size of the lists.

Source
==(other : List)

Equality. Returns true if each element in self is equal to each corresponding element in other.

list = List{2, 3}
list.unshift
list == List{1, 2, 3} # => true
list == List{2, 3}    # => false
Source
[](index : Int)

Returns the element at the given index.

Negative indices can be used to start counting from the end of the list. Raises IndexError if trying to access an element outside the list's range.

Source
[]=(index : Int, value : T)

Sets the given value at the given index replacing the old value

Negative indices can be used to start counting from the end of the list. Raises IndexError if trying to access an element outside the list's range.

Source
[]?(index : Int)

Returns the element at the given index.

Negative indices can be used to start counting from the end of the list. Returns nil if trying to access an element outside the list's range.

Source
at(index : Int)

Returns the element at the given index, if in bounds, otherwise raises IndexError.

Source
at(index : Int, &)

Returns the element at the given index, if in bounds, otherwise executes the given block and returns its value.

Source
clear
Source
clone

Returns a new List that has this list's elements cloned. That is, it returns a deep copy of this list.

Use #dup if you want a shallow copy.

Source
concat(other : Enumerable(T))

Appends the elements of other to self, and returns self.

Source
delete(node : Node(T))
Source
delete(obj : T, all = true)

Removes all items or the first occurence that are equal to obj.

l = List{"a", "b", "b", "b", "c", "c"}
l.delete("b")
l # => List{"a", "c", "c"}
l.delete("c", all: false)
l # => List{"a", "c"}
Source
delete_at(index : Int)

Delete the item that is present at the index. Raises IndexError if trying to delete an element outside the list's range.

a = List{1, 2, 3}
a.delete_at(1) # => List{1, 3}
Source
dup

Returns a new List that has exactly this list's elements. That is, it returns a shallow copy of this list.

Source
each

Yields each item in this list, from first to last.

Do not modify the list while using this variant of each!

Source
each

Gives an iterator over each item in this list, from first to last.

Source
each_node

Calls the given block once for each element in self, passing that element as a parameter.

Source
empty?

Returns true if this deque has 0 items.

Source
equals?(other : List, &)

Determines if self equals other according to a comparison done by the given block.

If self's size is the same as other's size, this method yields elements from self and other in tandem: if the block returns true for all of them, this method returns true. Otherwise it returns false.

Source
first

Returns the first element of the list. Raises if empty.

Source
first?

Returns the first element of the list, or nil if the list is empty

Source
hash(hasher)

See Object#hash(hasher)

Source
insert(index : Int, value : T)

Insert a new item before the item at index.

l = List{0, 1, 2}
l.insert_at(1, 7) # => List{0, 7, 1, 2}
Source
inspect(io : IO)

Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.

class Person
  def initialize(@name : String, @age : Int32)
  end
end

Person.new("John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32>
Source
last

Returns the first element of the list. Raises if empty.

Source
last?

Returns the first element of the list, or nil if the list is empty

Source
pop(n : Int)

Removes the last n (at most) items in the list.

Source
pop

Removes and returns the last item. Raises NoSuchElementError if empty.

l = List{1, 2, 3}
l.pop # => 3
# l == List{1, 2}
Source
pop

Removes and returns the last item, if not empty, otherwise executes the given block and returns its value.

Source
pop?

Removes and returns the last item, if not empty, otherwise nil.

Source
push(obj : T)

Pushes the given value on to the end of this list.

l = List{1, 2}
l.push 3 # => List{1, 2, 3}
Source
reverse_each

Yields each item in this list, from last to first.

Do not modify the list while using reverse_each!

Source
rotate!(n : Int = 1)

Rotates this list in place so that the element at n becomes first.

For positive n, equivalent to n.times { push(shift) }. For negative n, equivalent to (-n).times { unshift(pop) }.

Source
shift(n : Int)

Removes the first n (at most) items in the list.

Source
shift

Removes and returns the first item. Raises NoSuchElementError if empty.

l = List{1, 2, 3}
l.shift # => 1
# l == List{2, 3} -> true
Source
shift

Removes the first element in the list, if not empty, otherwise executes the given block and returns its value.

Source
shift?

Removes and returns the first item, if not empty, otherwise nil.

Source
size

Returns the number of elements in the collection.

[1, 2, 3, 4].size # => 4
Source
swap(i, j)

Swaps the items at the indices i and j.

Source
to_a

Returns an Array (shallow copy) that contains all the items of this list.

Source
to_s(io : IO)

Appends a short String representation of this object which includes its class name and its object address.

class Person
  def initialize(@name : String, @age : Int32)
  end
end

Person.new("John", 32).to_s # => #<Person:0x10a199f20>
Source
unshift(obj : T)

Prepends objects to the front of the list.

Source

Nested types