Use Containers namespace shorthand
masterTo avoid prefixing every class with Containers::, you can include the Containers module in your scope.
include Containers
max_heap = MaxHeap.newrepository·master·Indexed 25 days ago
https://github.com/kanwei/algorithmsA Ruby library providing standard data structures and algorithms not present in the Ruby standard library. It includes containers such as Heaps, Priority Queues, Stacks, Queues, Deques, Red-Black Trees, Splay Trees, Tries, Suffix Arrays, and KD Trees. It also provides search algorithms (binary search, KMP), sorting algorithms (bubble, comb, selection, heapsort, insertion, shell, quicksort, mergesort, and dualpivotquicksort), and string operations like Levenshtein distance.
To avoid prefixing every class with Containers::, you can include the Containers module in your scope.
include Containers
max_heap = MaxHeap.newTo use the algorithms library, require rubygems and algorithms. The library provides various data structures under the Containers namespace and algorithms under the Algorithms namespace. For better performance, it is highly recommended to install the C extensions.
require 'rubygems'
require 'algorithms'
# Usage example
max_heap = Containers::MaxHeap.newA Containers::Trie is a Ternary Search Tree implementation that stores key-value pairs. It provides $O(m)$ lookup speed (where $m$ is the length of the key) and avoids collisions. It is suitable for longest prefix matching and wildcard searches.
t = Containers::Trie.new
t["hello"] = "world"
puts t["hello"] #=> "world"A SplayTreeMap is a map that stores items in ascending order of their keys (using the <=> operator). It is self-optimizing: recently accessed nodes are moved near the root for faster subsequent access.
Key characteristics:
A Containers::RBTreeMap is a map that stores items in sorted order based on their keys (using the <=> operator). Unlike a standard Hash, keys can be iterated over in order. Duplicate keys are not allowed; inserting a duplicate key will overwrite the existing value.
Containers::RBTreeMap automatically selects the faster C implementation (Containers::CRBTreeMap) if available, otherwise it falls back to the Ruby implementation (Containers::RubyRBTreeMap).
By default, all container classes are namespaced under the Containers module. To avoid prefixing every initialization with Containers::, you can include Containers in your scope.
require 'algorithms'
include Containers
tree = RBTreeMap.newThe library provides the following implementations:
Containers::Heap, Containers::MaxHeap, Containers::MinHeapContainers::PriorityQueueContainers::Deque, Containers::CDeque (C extension)Containers::StackContainers::QueueContainers::RBTreeMap, Containers::CRBTreeMap (C extension)Containers::SplayTreeMap, Containers::CSplayTreeMap (C extension)Containers::TrieContainers::SuffixArrayAlgorithms::Search.binary_search, Algorithms::Search.kmp_searchAlgorithms::Sort.bubble_sort, Algorithms::Sort.comb_sort, Algorithms::Sort.selection_sort, Algorithms::Sort.heapsort, Algorithms::Sort.insertion_sort, Algorithms::Sort.shell_sort, Algorithms::Sort.quicksort, Algorithms::Sort.mergesort, Algorithms::Sort.dualpivotquicksortUse has_substring?(substring) to determine if a specific substring exists within the original string. This method uses binary search for efficiency.
Complexity: O(m + log n) where m is the length of the substring and n is the total number of suffixes.
Returns: true if the substring is found, false otherwise.
s_array = Containers::SuffixArray.new("abracadabra")
s_array.has_substring?("abra") #=> true
s_array.has_substring?("nope") #=> falseUse push_front(obj) to add an object to the beginning of the Deque, or push_back(obj) to add an object to the end. Both methods return the object being added.
d = Containers::Deque.new([1, 2, 3])
d.push_front(0)
d.push_back(4)Use Algorithms::Search.kmp_search(string, substring) to efficiently find the starting position of a substring within a string.
nil if no match is found.n is the length of the string and k is the length of the substring.Algorithms::Search in a class (like String) to call kmp_search as an instance method.# As a module method
Algorithms::Search.kmp_search("ABC ABCDAB ABCDABCDABDE", "ABCDABD") #=> 15
Algorithms::Search.kmp_search("ABC ABCDAB ABCDABCDABDE", "ABCDEF") #=> nil
# As an instance method via inclusion
class String; include Algorithms::Search; end
"ABC ABCDAB ABCDABCDABDE".kmp_search("ABCDABD") #=> 15pop_front to remove and return the object at the front of the Deque, or pop_back to remove and return the object at the back. If the Deque is empty, these methods return nil.Use delete(key) to remove the item associated with the key. It returns the value of the deleted item, or nil if the key was not found. This operation has amortized $O(\log n)$ complexity.
map = Containers::SplayTreeMap.new
map["MA"] = "Massachusetts"
map.delete("MA") #=> "Massachusetts"
map.delete("MA") #=> nilmap = Containers::SplayTreeMap.new
map["MA"] = "Massachusetts"
map.delete("MA") #=> "Massachusetts"