A map that stores ranges and their associated values for efficient lookup.
Definitionsdef initialize
Initialize a new RangeMap.
Implementationdef initialize
@ranges = []
end
def add(range, value)
Add a range-value pair to the map.
Signaturerange
Range
The range to map.
value
Object
The value to associate with the range.
Object
The added value.
def add(range, value)
@ranges << [range, value]
return value
end
def find(key)
Find the value associated with a key within any range.
Signaturekey
Object
The key to find.
{...}
Block called if no range contains the key.
Object
The value if found, result of block if given, or nil.
def find(key)
@ranges.each do |range, value|
return value if range.include?(key)
end
if block_given?
return yield
end
return nil
end
def each
Iterate over all values in the map.
Signature{|value| ...}
Block called for each value.
value
Object
The value from the range-value pair.
def each
@ranges.each do |range, value|
yield value
end
end
def clear
Clear all ranges from the map.
Implementationdef clear
@ranges.clear
end
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4