class AVLTree::SortedMap(K, V)

Overview

SortedMap implements a associative array that guarantees that its keys are yielded in sorted order (according to the return keys of their #<=> methods) when iterating over them.

SortedMap is implemented using an AVL tree.

While it often has slower computational speed compared to a Hash implemented using a hash-based approach, it offers potential optimizations for operations related to order. For example, retrieving the maximum and minimum keys of the map can be performed in logarithmic time.

SortedMap does not allow duplicates and only stores unique keys.

Example

require "avltree"

map = AVLTree::SortedMap(String, Int32).new({"bob" => 3, "alice" => 1, "carol" => -2})
map.to_a == [{"alice", 1}, {"bob", 3}, {"carol", -2}] # => true
map.to_a == [{"bob", 3}, {"alice", 1}, {"carol", -2}] # => false

map["dave"] = 4
map["oscar"] = 3

map # => {alice => 1, bob => 3, carol => -2, dave => 4, oscar => 3}

map.min # => {"alice", 1}  (O(logN))
map.max # => {"oscar", 3}  (O(logN))

map.lower_bound("a")     # => 0  (O(logN))
map.lower_bound("bryan") # => 2  (O(logN))
map.lower_bound("zoe")   # => 5  (O(logN))

Included Modules

Defined in:

avltree/sorted_map.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Enumerable({K, V})

to_sorted_multiset to_sorted_multiset, to_sorted_set to_sorted_set

Constructor Detail

def self.new(hash : Hash(K, V)) #

[View source]
def self.new(defalut_value : V) #

[View source]
def self.new #

[View source]
def self.new(&block : self, K -> V) #

[View source]

Class Method Detail

def self.zip(ary1 : Array(K), ary2 : Array(V)) #

[View source]

Instance Method Detail

def ==(other : SortedMap) : Bool #

[View source]
def [](key : K) : V #

[View source]
def []=(key : K, value : V) : V #

[View source]
def []?(key : K) : V | Nil #

[View source]
def at(index : Int) : Tuple(K, V) #

Returns the key-value at the index-th.


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

Returns the key-value at the index-th.


[View source]
def at?(index : Int) : Tuple(K, V) | Nil #

Like #at, but returns nil if trying to access an key-value outside the set's range.


[View source]
def clear : self #

[View source]
def clone #

[View source]
def compact #

[View source]
def compact! : self #

[View source]
def delete(key : K) : V | Nil #

[View source]
def delete(key : K, &) #

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

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

[View source]
def delete_at?(index : Int) #

[View source]
def dig(key : K) #

[View source]
def dig(key : K, *subkeys) #

[View source]
def dig?(key : K) #

[View source]
def dig?(key : K, *subkeys) #

[View source]
def dup #
Description copied from class Reference

Returns a shallow copy of this object.

This allocates a new object and copies the contents of self into it.


[View source]
def each(& : Tuple(K, V) -> ) : Nil #
Description copied from module Enumerable({K, V})

Must yield this collection's elements to the block.


[View source]
def each #
Description copied from module Iterable({K, V})

Must return an Iterator over the elements in this collection.


[View source]
def each_key(& : K -> ) : Nil #

[View source]
def each_key #

[View source]
def each_value(& : V -> ) : Nil #

[View source]
def each_value #

[View source]
def empty? #
Description copied from module Enumerable({K, V})

Returns true if self is empty, false otherwise.

([] of Int32).empty? # => true
([1]).empty?         # => false

[View source]
def fetch(key : K, default) #

[View source]
def fetch(key : K, &) #

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

[View source]
def fetch_at(index : Int, default) #

[View source]
def first_key : K #

[View source]
def first_key? : K | Nil #

[View source]
def first_value : K #

[View source]
def first_value? : K | Nil #

[View source]
def greater_equal_index(key : K) : Int32 | Nil #

[View source]
def greater_equal_item(key : K) : Tuple(K, V) | Nil #

[View source]
def greater_equal_item_with_index(key : K) : Tuple(Tuple(K, V) | Nil, Int32 | Nil) #

[View source]
def greater_index(key : K) : Int32 | Nil #

[View source]
def greater_item(key : K) : Tuple(K, V) | Nil #

[View source]
def greater_item_with_index(key : K) : Tuple(Tuple(K, V) | Nil, Int32 | Nil) #

[View source]
def has_key?(key : K) : Bool #

[View source]
def has_value?(value : V) : Bool #

[View source]
def index(key : K) : Int32 | Nil #

[View source]
def index!(key : K) : Int32 #

[View source]
def inspect(io : IO) : Nil #
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 invert : SortedMap(V, K) #

[View source]
def key_at(index : Int) : K #

Returns the key at the index-th.


[View source]
def key_at?(index : Int) : K | Nil #

Like #at, but returns nil if trying to access an key outside the set's range.


[View source]
def key_for(value) : K #

[View source]
def key_for(value, &) #

[View source]
def key_for?(value) : K | Nil #

[View source]
def keys : Array(K) #

[View source]
def last_key : K #

[View source]
def last_key? : K | Nil #

[View source]
def last_value : K #

[View source]
def last_value? : K | Nil #

[View source]
def less_equal_index(key : K) : Int32 | Nil #

[View source]
def less_equal_item(key : K) : Tuple(K, V) | Nil #

[View source]
def less_equal_item_with_index(key : K) : Tuple(Tuple(K, V) | Nil, Int32 | Nil) #

[View source]
def less_index(key : K) : Int32 | Nil #

[View source]
def less_item(key : K) : Tuple(K, V) | Nil #

[View source]
def less_item_with_index(key : K) : Tuple(Tuple(K, V) | Nil, Int32 | Nil) #

[View source]
def lower_bound(key : K) : Int32 #

[View source]
def max #
Description copied from module Enumerable({K, V})

Returns the element with the maximum value in the collection.

It compares using > so it will work for any type that supports that method.

[1, 2, 3].max        # => 3
["Alice", "Bob"].max # => "Bob"

Raises Enumerable::EmptyError if the collection is empty.


[View source]
def min #
Description copied from module Enumerable({K, V})

Returns the element with the minimum value in the collection.

It compares using < so it will work for any type that supports that method.

[1, 2, 3].min        # => 1
["Alice", "Bob"].min # => "Alice"

Raises Enumerable::EmptyError if the collection is empty.


[View source]
def pop : Tuple(K, V) #

[View source]
def pop(&) #

[View source]
def pop? : Tuple(K, V) | Nil #

[View source]
def proper_superset_of?(other : Hash) : Bool #

[View source]
def put(key : K, value : V, &) #

[View source]
def reject(& : K, V -> ) : SortedMap(K, V) #
Description copied from module Enumerable({K, V})

Returns an Array with all the elements in the collection for which the passed block is falsey.

[1, 2, 3, 4, 5, 6].reject { |i| i % 2 == 0 } # => [1, 3, 5]

[View source]
def reject(*keys) : SortedMap(K, V) #

[View source]
def reject!(& : K, V -> ) : SortedMap(K, V) #

[View source]
def reject!(keys : Enumerable) : SortedMap(K, V) #

[View source]
def reject!(*keys) : SortedMap(K, V) #

[View source]
def reverse_each(& : Tuple(K, V) -> ) : Nil #

[View source]
def reverse_each #

[View source]
def reverse_each_key(& : K -> ) : Nil #

[View source]
def reverse_each_key #

[View source]
def reverse_each_value(& : V -> ) : Nil #

[View source]
def reverse_each_value #

[View source]
def select(keys : Enumerable) : SortedMap(K, V) #

[View source]
def select(*keys) : SortedMap(K, V) #

[View source]
def select!(keys : Indexable) : self #

[View source]
def select!(keys : Enumerable) : self #

[View source]
def select!(*keys) : self #

[View source]
def shift : Tuple(K, V) #

[View source]
def shift(&) #

[View source]
def shift? : Tuple(K, V) | Nil #

[View source]
def size : Int32 #
Description copied from module Enumerable({K, V})

Returns the number of elements in the collection.

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

[View source]
def subset_of?(other : SortedMap(K, V)) : Bool #

[View source]
def superset_of?(other : SortedMap(K, V)) : Bool #

[View source]
def to_a : Array(Tuple(K, V)) #
Description copied from module Enumerable({K, V})

Returns an Array with all the elements in the collection.

(1..5).to_a # => [1, 2, 3, 4, 5]

[View source]
def to_hash #

[View source]
def to_s(io : IO) : Nil #
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 unordered_each(node = @root, & : Tuple(K, V) -> ) : Nil #

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

[View source]
def update(key : K, & : V -> V) : V #

[View source]
def upper_bound(key : K) : Int32 #

[View source]
def value_at(index : Int) : V #

Returns the value at the index-th.


[View source]
def value_at?(index : Int) : V | Nil #

Like #at, but returns nil if trying to access an value outside the set's range.


[View source]
def values : Array(V) #

[View source]
def values_at(*indices : Int) #

[View source]
def values_by_key(*keys : K) #

[View source]