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
Creates a new List of the given size filled with the same value in each position.
List.new(3, 'a') # => List{'a', 'a', 'a'}
Creates a new List that copies its items from an Array.
List.new([1, 2, 3]) # => List{1, 2, 3}
Instance methods
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.
Pushes the given value on to the end of this list. Returns self instead
of the created node.
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.
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
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.
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.
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.
Returns the element at the given index, if in bounds, otherwise raises IndexError.
Returns the element at the given index, if in bounds, otherwise executes the given block and returns its value.
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.
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"}
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}
Returns a new List that has exactly this list's elements. That is, it returns a shallow copy of this list.
Yields each item in this list, from first to last.
Do not modify the list while using this variant of each!
Calls the given block once for each element in self, passing that element as a parameter.
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.
Insert a new item before the item at index.
l = List{0, 1, 2}
l.insert_at(1, 7) # => List{0, 7, 1, 2}
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>
Removes and returns the last item. Raises NoSuchElementError if empty.
l = List{1, 2, 3}
l.pop # => 3
# l == List{1, 2}
Removes and returns the last item, if not empty, otherwise executes the given block and returns its value.
Pushes the given value on to the end of this list.
l = List{1, 2}
l.push 3 # => List{1, 2, 3}
Yields each item in this list, from last to first.
Do not modify the list while using reverse_each!
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) }.
Removes and returns the first item. Raises NoSuchElementError if empty.
l = List{1, 2, 3}
l.shift # => 1
# l == List{2, 3} -> true
Removes the first element in the list, if not empty, otherwise executes the given block and returns its value.