class Quartz::List(T)

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

Defined in:

quartz/list.cr

Constructors

Instance Method Summary

Instance methods inherited from class Reference

==(other : Quartz::Any) ==

Instance methods inherited from class Object

===(other : Quartz::Any) ===

Constructor Detail

def self.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'}

[View source]
def self.new(array : Array(T)) #

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

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

[View source]
def self.new #

Creates a new empty List


[View source]
def self.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}

[View source]

Instance Method Detail

def +(other : Quartz::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.


[View source]
def <<(obj : T) #

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


[View source]
def <=>(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.


[View source]
def ==(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

[View source]
def [](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.


[View source]
def []=(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.


[View source]
def []?(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.


[View source]
def at(index : Int) #

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


[View source]
def at(index : Int, &) #

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


[View source]
def clear #

[View source]
def 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.


[View source]
def concat(other : Enumerable(T)) #

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


[View source]
def delete(node : Quartz::List::Node(T)) #

[View source]
def 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"}

[View source]
def 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}

[View source]
def dup #

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


[View source]
def each(&) #

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

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


[View source]
def each #

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


[View source]
def each_node(&) #

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


[View source]
def empty? #

Returns true if this deque has 0 items.


[View source]
def 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.


[View source]
def first : T #

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


[View source]
def first? : T? #

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


[View source]
def hash(hasher) #
Description copied from class Reference

See Object#hash(hasher)


[View source]
def 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}

[View source]
def inspect(io : IO) #
Description copied from class Reference

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>

[View source]
def last : T #

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


[View source]
def last? : T? #

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


[View source]
def pop(&) #

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


[View source]
def pop(n : Int) #

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


[View source]
def pop #

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

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

[View source]
def pop? #

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


[View source]
def 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}

[View source]
def reverse_each(&) #

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

Do not modify the list while using #reverse_each!


[View source]
def 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) }.


[View source]
def shift(&) #

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


[View source]
def shift #

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

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

[View source]
def shift(n : Int) #

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


[View source]
def shift? #

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


[View source]
def size #
Description copied from module Enumerable(T)

Returns the number of elements in the collection.

[1, 2, 3, 4].size # => 4

[View source]
def swap(i, j) #

Swaps the items at the indices i and j.


[View source]
def to_a #

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


[View source]
def to_s(io : IO) #
Description copied from class Reference

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>

[View source]
def unshift(obj : T) #

Prepends objects to the front of the list.


[View source]