class Quartz::List(T)
- Quartz::List(T)
- Reference
- Object
Overview
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)
Included Modules
- Comparable(Quartz::List(T))
- Enumerable(T)
- Iterable(T)
Defined in:
quartz/list.crConstructors
-
.new(size : Int, value : T)
Creates a new List of the given size filled with the same value in each position.
-
.new(array : Array(T))
Creates a new List that copies its items from an Array.
-
.new
Creates a new empty List
-
.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.
Instance Method Summary
-
#+(other : Quartz::List(U)) forall U
Concatenation.
-
#<<(obj : T)
Pushes the given value on to the end of this list.
-
#<=>(other : List)
Combined comparison operator.
-
#==(other : List)
Equality.
-
#[](index : Int)
Returns the element at the given
index
. -
#[]=(index : Int, value : T)
Sets the given value at the given index replacing the old value
-
#[]?(index : Int)
Returns the element at the given index.
-
#at(index : Int)
Returns the element at the given index, if in bounds, otherwise raises
IndexError
. -
#at(index : Int, &)
Returns the element at the given index, if in bounds, otherwise executes the given block and returns its value.
- #clear
-
#clone
Returns a new List that has this list's elements cloned.
-
#concat(other : Enumerable(T))
Appends the elements of other to
self
, and returnsself
. - #delete(node : Quartz::List::Node(T))
-
#delete(obj : T, all = true)
Removes all items or the first occurence that are equal to obj.
-
#delete_at(index : Int)
Delete the item that is present at the
index
. -
#dup
Returns a new List that has exactly this list's elements.
-
#each(&)
Yields each item in this list, from first to last.
-
#each
Gives an iterator over each item in this list, from first to last.
-
#each_node(&)
Calls the given block once for each element in self, passing that element as a parameter.
-
#empty?
Returns true if this deque has 0 items.
-
#equals?(other : List, &)
Determines if
self
equals other according to a comparison done by the given block. -
#first : T
Returns the first element of the list.
-
#first? : T?
Returns the first element of the list, or nil if the list is empty
-
#hash(hasher)
See
Object#hash(hasher)
-
#insert(index : Int, value : T)
Insert a new item before the item at
index
. -
#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.
-
#last : T
Returns the first element of the list.
-
#last? : T?
Returns the first element of the list, or nil if the list is empty
-
#pop(&)
Removes and returns the last item, if not empty, otherwise executes the given block and returns its value.
-
#pop(n : Int)
Removes the last
n
(at most) items in the list. -
#pop
Removes and returns the last item.
-
#pop?
Removes and returns the last item, if not empty, otherwise
nil
. -
#push(obj : T)
Pushes the given value on to the end of this list.
-
#reverse_each(&)
Yields each item in this list, from last to first.
-
#rotate!(n : Int = 1)
Rotates this list in place so that the element at
n
becomes first. -
#shift(&)
Removes the first element in the list, if not empty, otherwise executes the given block and returns its value.
-
#shift
Removes and returns the first item.
-
#shift(n : Int)
Removes the first
n
(at most) items in the list. -
#shift?
Removes and returns the first item, if not empty, otherwise
nil
. -
#size
Returns the number of elements in the collection.
-
#swap(i, j)
Swaps the items at the indices
i
andj
. -
#to_a
Returns an Array (shallow copy) that contains all the items of this list.
-
#to_s(io : IO)
Appends a short String representation of this object which includes its class name and its object address.
-
#unshift(obj : T)
Prepends objects to the front of the list.
Instance methods inherited from class Reference
==(other : Quartz::Any)
==
Instance methods inherited from class Object
===(other : Quartz::Any)
===
Constructor Detail
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}
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}
Instance Method Detail
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, if not empty, otherwise executes the given block and returns its value.
Removes and returns the last item. Raises NoSuchElementError
if empty.
l = List{1, 2, 3}
l.pop # => 3
# l == List{1, 2}
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 the first element in the list, if not empty, otherwise executes the given block and returns its value.
Removes and returns the first item. Raises NoSuchElementError
if empty.
l = List{1, 2, 3}
l.shift # => 1
# l == List{2, 3} -> true
Returns the number of elements in the collection.
[1, 2, 3, 4].size # => 4
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>