Parent

Class Index [+]

Quicksearch

Haml::Util::SubsetMap

A map from sets to values. A value is {#[]= set} by providing a set (the “set-set”) and a value, which is then recorded as corresponding to that set. Values are {#[] accessed} by providing a set (the “get-set”) and returning all values that correspond to set-sets that are subsets of the get-set.

SubsetMap preserves the order of values as they’re inserted.

@example ssm = SubsetMap.new ssm[Set[1, 2]] = “Foo” ssm[Set[2, 3]] = “Bar” ssm[Set[1, 2, 3]] = “Baz“

ssm[Set[1, 2, 3]] #=> [“Foo”, “Bar”, “Baz”]

Public Class Methods

new() click to toggle source

Creates a new, empty SubsetMap.

    # File lib/haml/util/subset_map.rb, line 23
23:       def initialize
24:         @hash = {}
25:         @vals = []
26:       end

Public Instance Methods

[](set) click to toggle source

Same as {#get}, but doesn’t return the subsets of the argument for which values were found.

@param set [Set] The set to use as the map key. @return [Array] The array of all values

  associated with subsets of `set`, in insertion order.

@see #

    # File lib/haml/util/subset_map.rb, line 96
96:       def [](set)
97:         get(set).map {|v, _| v}
98:       end
[]=(set, value) click to toggle source

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`.

@param set [#] The set to use as the map key. May not be empty. @param value [Object] The value to associate with `set`. @raise [ArgumentError] If `set` is empty.

    # File lib/haml/util/subset_map.rb, line 47
47:       def []=(set, value)
48:         raise ArgumentError.new("SubsetMap keys may not be empty.") if set.empty?
49: 
50:         index = @vals.size
51:         @vals << value
52:         set.each do |k|
53:           @hash[k] ||= []
54:           @hash[k] << [set, set.to_set, index]
55:         end
56:       end
empty?() click to toggle source

Whether or not this SubsetMap has any key-value pairs.

@return [Boolean]

    # File lib/haml/util/subset_map.rb, line 31
31:       def empty?
32:         @hash.empty?
33:       end
get(set) click to toggle source

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 assocations in the map. However, unless many keys in the map overlap with `set`, `m` will typically be much smaller.

@param set [Set] The set to use as the map key. @return [Array<(Object, #)>] An array of pairs,

  where the first value is the value associated with a subset of `set`,
  and the second value is that subset of `set`
  (or whatever `#to_set` object was used to set the value)
  This array is in insertion order.

@see #[]

    # File lib/haml/util/subset_map.rb, line 73
73:       def get(set)
74:         res = set.map do |k|
75:           next unless subsets = @hash[k]
76:           subsets.map do |subenum, subset, index|
77:             next unless subset.subset?(set)
78:             [index, subenum]
79:           end
80:         end
81:         res = Haml::Util.flatten(res, 1)
82:         res.compact!
83:         res.uniq!
84:         res.sort!
85:         res.map! {|i, s| [@vals[i], s]}
86:         return res
87:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.