class FDS::UnorderedSet
Public Class Methods
new()
click to toggle source
# File lib/fds/unordered_set.rb, line 2 def initialize @index = 0 @data = Hash.new do |h, e| h[e] = @index @index += 1 end end
Public Instance Methods
&(other)
click to toggle source
# File lib/fds/unordered_set.rb, line 64 def &(other) if self.size < other.size result = self.dup bigger_set = other else result = other.dup bigger_set = self end result.keep_if do |e| bigger_set.include?(e) end result end
Also aliased as: intersection
<<(e)
[:to_s, :first, :last, :to_a].each do |function|
class_eval(<<-EOF, __FILE__, __LINE__ + 1) def #{function} @data.keys.#{function} end EOF
end
Alias for: add
add(e)
click to toggle source
# File lib/fds/unordered_set.rb, line 10 def add(e) @data[e] self end
Also aliased as: <<
delete(e)
click to toggle source
# File lib/fds/unordered_set.rb, line 21 def delete(e) @data.delete(e) self end
delete_if() { |k| ... }
click to toggle source
# File lib/fds/unordered_set.rb, line 38 def delete_if @data.delete_if { |k, _| yield k } self end
each()
click to toggle source
# File lib/fds/unordered_set.rb, line 30 def each @data.each_key(&proc) end
find_index(e)
click to toggle source
# File lib/fds/unordered_set.rb, line 15 def find_index(e) # @data.has_key? is needed otherwise @data will create a new element, # see @data definition in #initialize. @data[e] if @data.has_key?(e) end
first()
click to toggle source
# File lib/fds/unordered_set.rb, line 80 def first @data.each_key { |k| return k } nil end
include?(e)
click to toggle source
# File lib/fds/unordered_set.rb, line 34 def include?(e) find_index(e) != nil end
keep_if() { |k| ... }
click to toggle source
# File lib/fds/unordered_set.rb, line 43 def keep_if @data.keep_if { |k, _| yield k } self end
last()
click to toggle source
# File lib/fds/unordered_set.rb, line 85 def last # All keys are read through, but no intermediate array is created as # whereas "keys.last" accumulates all keys for nothing. # We can't use @data.each_key.last, because there's no Enumerable#last. :/ @data.reverse_each { |k, _| return k } nil end
size()
click to toggle source
# File lib/fds/unordered_set.rb, line 26 def size @data.size end
to_a()
click to toggle source
# File lib/fds/unordered_set.rb, line 93 def to_a @data.keys end
to_s()
click to toggle source
# File lib/fds/unordered_set.rb, line 97 def to_s to_a.to_s end
|(other)
click to toggle source
# File lib/fds/unordered_set.rb, line 48 def |(other) if self.size > other.size result = self.dup smaller_set = other else result = other.dup smaller_set = self end smaller_set.each do |e| result << e end result end
Also aliased as: union