Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Aug 11th, 2012  |  syntax: None  |  size: 2.76 KB  |  views: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. require 'uuid'
  2.  
  3. ObjectSpace; alias :each :each_object; include Enumerable; end
  4.  
  5. class AwesomeSet
  6.   include Enumerable
  7.  
  8.   def initialize ary = []
  9.     @uuid = UUID.create.to_s
  10.     @m = "is_in_awesomeset_#{@uuid}?".to_sym
  11.     ary.each {|o| add(o)}
  12.   end
  13.  
  14.   def & set
  15.     AwesomeSet.new select {|o| set.member? o}
  16.   end
  17.  
  18.   alias :intersection :&
  19.  
  20.   def | set
  21.     AwesomeSet.new(to_a + set.to_a)
  22.   end
  23.  
  24.   alias :+ :|
  25.  
  26.  
  27.   def - set
  28.     AwesomeSet.new select {|o| !set.member?(o)}
  29.   end
  30.  
  31.   alias :difference :-
  32.  
  33.   def == set
  34.     to_a == obj.to_a
  35.   end
  36.  
  37.   def ^ set
  38.     (self | set) - (self & set)
  39.   end
  40.  
  41.   def add obj
  42.     return if member? obj
  43.     obj.define_method @m { true }
  44.   end
  45.  
  46.   alias :<< :add
  47.  
  48.   def add? obj
  49.     r = member?(obj) ? self : nil
  50.     add obj
  51.     r
  52.   end
  53.  
  54.   def classify
  55.     h = {}
  56.     to_a.each do |o|
  57.       r = yield(o)
  58.       h[r] ||= []
  59.       h[r] << o
  60.     end
  61.     h
  62.   end
  63.  
  64.   def clear
  65.     each {|o| delete(o)}
  66.     self
  67.   end
  68.  
  69.   def collect!
  70.     items = map {|o| yield o}
  71.     clear
  72.     items.each {|o| add o}
  73.     self
  74.   end
  75.  
  76.   alias :map! :collect!
  77.  
  78.   def delete obj
  79.     obj.undef(@m)
  80.   end
  81.  
  82.   def delete? obj
  83.     r = member?(obj) ? self : nil
  84.     delete obj
  85.     r
  86.   end
  87.  
  88.   def delete_if? &b
  89.     reject! &b
  90.     self
  91.   end
  92.  
  93.   def divide &blk
  94.     Set.new( self).divide(&blk)
  95.   end
  96.  
  97.   def each &blk
  98.     to_a.each &blk
  99.   end
  100.  
  101.   def empty?
  102.     to_a.empty?
  103.   end
  104.  
  105.   def flatten
  106.     flat = AwesomeSet.new
  107.     each {|o| o.responds_to?(:flatten) ? flat.merge(o.flatten) : flat.add(o)}
  108.     flat
  109.   end
  110.  
  111.   def flatten!
  112.     c = nil
  113.     select {|o| o.responds_to?(:flatten) }.each do |o|
  114.       c = self
  115.       merge(o.flatten)
  116.       delete(o)
  117.     end
  118.     c
  119.   end
  120.  
  121.   def member? obj
  122.     obj.send @m
  123.   end
  124.  
  125.   alias :include? :member
  126.  
  127.  
  128.   def initialize_copy(orig)
  129.     clear
  130.     merge orig.to_a
  131.   end
  132.  
  133.   alias :merge :initialize_copy
  134.  
  135.   def inspect
  136.     "#<AwesomeSet: " + map(&:inspect).join(", ") + ">"
  137.   end
  138.  
  139.   def keep_if &b
  140.     reject! &b
  141.     self
  142.   end
  143.  
  144.   def reject!
  145.     r = nil
  146.     delete_if do |o|
  147.       r = self
  148.       ! yield(o)
  149.     end
  150.     r
  151.   end
  152.  
  153.   def size
  154.     to_a.size
  155.   end
  156.  
  157.   alias :size :length
  158.  
  159.   def merge set
  160.     set.each {|o| add o}
  161.     self
  162.   end
  163.  
  164.   def proper_subset? set
  165.     each {|o| return false unless set.member? o}
  166.   end
  167.  
  168.   alias :subset? :proper_subset?
  169.  
  170.   def proper_superset? set
  171.     set.each {|o| return false unless member? o}
  172.   end
  173.  
  174.   alias :superset? :proper_superset?
  175.  
  176.   def reject!
  177.     r = nil
  178.     each do |o|
  179.       r = self
  180.       delete(o) if yield(o)
  181.     end
  182.     r
  183.   end
  184.  
  185.   def to_a
  186.     ObjectSpace.select(&@m)
  187.   end
  188. end
clone this paste RAW Paste Data