class Haml::Util::SubsetMap
ssm[Set[1, 2, 3]] #=> [“Foo”, “Bar”, “Baz”]
ssm[Set[1, 2, 3]] = “Baz”
ssm[Set[2, 3]] = “Bar”
ssm[Set[1, 2]] = “Foo”
ssm = SubsetMap.new
@example
SubsetMap preserves the order of values as they’re inserted.
that are subsets of the get-set.
and returning all values that correspond to set-sets
Values are {#[] accessed} by providing a set (the “get-set”)
which is then recorded as corresponding to that set.
A value is {#[]= set} by providing a set (the “set-set”) and a value,
A map from sets to values.
def [](set)
- See: #get -
Returns:
-
(Array)
- The array of all values
Parameters:
-
set
(Set
) -- The set to use as the map key.
def [](set) get(set).map {|v, _| v} end
def []=(set, value)
-
(ArgumentError)
- If `set` is empty.
Parameters:
-
value
(Object
) -- The value to associate with `set`. -
set
(#to_set
) -- The set to use as the map key. May not be empty.
def []=(set, value) raise ArgumentError.new("SubsetMap keys may not be empty.") if set.empty? index = @vals.size @vals << value set.each do |k| @hash[k] ||= [] @hash[k] << [set, set.to_set, index] end end
def empty?
-
(Boolean)
-
def empty? @hash.empty? end
def get(set)
- See: #[] -
Returns:
-
(Array<(Object, #to_set)>)
- An array of pairs,
Parameters:
-
set
(Set
) -- The set to use as the map key.
def get(set) res = set.map do |k| next unless subsets = @hash[k] subsets.map do |subenum, subset, index| next unless subset.subset?(set) [index, subenum] end end res = Haml::Util.flatten(res, 1) res.compact! res.uniq! res.sort! res.map! {|i, s| [@vals[i], s]} return res end
def initialize
def initialize @hash = {} @vals = [] end