Class: Sass::Util::SubsetMap
 Inherits:

Object
 Object
 Sass::Util::SubsetMap
 Defined in:
 lib/sass/util/subset_map.rb
Overview
A map from sets to values. A value is set by providing a set (the "setset") and a value, which is then recorded as corresponding to that set. Values are accessed by providing a set (the "getset") and returning all values that correspond to setsets that are subsets of the getset.
SubsetMap preserves the order of values as they're inserted.
Instance Method Summary collapse

#[](set) ⇒ Array
Same as #get, but doesn't return the subsets of the argument for which values were found.

#[]=(set, value)
Associates a value with a set.

#each_value {Object ... }
Iterates over each value in the subset map.

#empty? ⇒ Boolean
Whether or not this SubsetMap has any keyvalue pairs.

#get(set) ⇒ Array<(Object, #to_set)>
Returns all values associated with subsets of
set
. 
#initialize ⇒ SubsetMap
constructor
Creates a new, empty SubsetMap.
Constructor Details
#initialize ⇒ SubsetMap
Creates a new, empty SubsetMap.
23 24 25 26 
# File 'lib/sass/util/subset_map.rb', line 23
def initialize
@hash = {}
@vals = []
end

Instance Method Details
#[](set) ⇒ Array
Same as #get, but doesn't return the subsets of the argument for which values were found.
97 98 99 
# File 'lib/sass/util/subset_map.rb', line 97
def [](set)
get(set).map {v, _ v}
end

#[]=(set, value)
Associates a value with a set.
When set
or any of its supersets is accessed,
value
will be among the values returned.
Note that if the same set
is passed to this method multiple times,
all given value
s will be associated with that set
.
This runs in O(n)
time, where n
is the size of set
.
47 48 49 50 51 52 53 54 55 56 
# File 'lib/sass/util/subset_map.rb', line 47
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

#each_value {Object ... }
Iterates over each value in the subset map. Ignores keys completely. If multiple keys have the same value, this will return them multiple times.
105 106 107 
# File 'lib/sass/util/subset_map.rb', line 105
def each_value
@vals.each {v yield v}
end

#empty? ⇒ Boolean
Whether or not this SubsetMap has any keyvalue pairs.
31 32 33 
# File 'lib/sass/util/subset_map.rb', line 31
def empty?
@hash.empty?
end

#get(set) ⇒ Array<(Object, #to_set)>
Returns all values associated with subsets of set
.
In the worst case, this runs in O(m*max(n, log m))
time,
where n
is the size of set
and m
is the number of associations in the map.
However, unless many keys in the map overlap with set
,
m
will typically be much smaller.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 
# File 'lib/sass/util/subset_map.rb', line 73
def get(set)
res = set.map do k
subsets = @hash[k]
next unless subsets
subsets.map do subenum, subset, index
next unless subset.subset?(set)
[index, subenum]
end
end
res = Sass::Util.flatten(res, 1)
res.compact!
res.uniq!
res.sort!
res.map! {i, s [@vals[i], s]}
res
end
