Class: Sequence::WeakRefSet

Inherits:
Set
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sequence/weakrefset.rb

Direct Known Subclasses

NotReallyWeakRefSet

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items = nil, &block) ⇒ WeakRefSet Also known as: initialize_copy

create a new WeakRefSet from an optional Enumerable (of objects) which is optionally processed through a block

Raises:

  • (ArgumentError)


21
22
23
24
25
26
# File 'lib/sequence/weakrefset.rb', line 21

def initialize(items=nil,&block) # :yield: obj
  items=[] if items.nil?
  raise ArgumentError unless items.respond_to? :each
  items=items.map(&block) if block
  replace(items)
end

Class Method Details

.[](*items) ⇒ Object



29
30
31
# File 'lib/sequence/weakrefset.rb', line 29

def [] *items
  new(items)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



109
110
111
112
113
# File 'lib/sequence/weakrefset.rb', line 109

def == other
  return true if self.equal? other
  other.is_a? Set and other.size==self.size and
    all?{|x| other.include?(x) }
end

#^(enum) ⇒ Object

Returns a new set containing elements exclusive between the set and the given enumerable object. (set ^ enum) is equivalent to ((set | enum) - (set & enum)).



229
230
231
232
233
234
# File 'lib/sequence/weakrefset.rb', line 229

def ^(enum)
  enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
  n = self.class.new(enum)
  each { |o| if n.include?(o) then n.delete(o) else n.add(o) end }
  n
end

#add(obj) ⇒ Object Also known as: <<

add a weak reference to the set



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sequence/weakrefset.rb', line 67

def add(obj)
#    return self if include? obj
#        Symbol===obj || Fixnum===obj || nil==obj || true==obj || false==obj and 
#          raise ArgumentError, "no immediates in weakrefset"
      id=ref obj
      case (o2=unref id) #test id for validity
      when Fixnum; 
        obj.equal? o2 or raise
      when Symbol,true,false,nil;          id=obj #hopefully rare
      else         
        obj.equal? o2 or raise
        ObjectSpace.define_finalizer(obj,method(:finalizer))
      end
      @ids[id] = true
      self
end

#clearObject

clear the set (return self)



131
132
133
134
# File 'lib/sequence/weakrefset.rb', line 131

def clear
      @ids = {}
      self
end

#delete(obj) ⇒ Object

delete an object in the set (return self)



150
151
152
153
# File 'lib/sequence/weakrefset.rb', line 150

def delete(obj)
      delete?(obj)
      self
end

#delete?(obj) ⇒ Boolean

delete an object in the set (return self if obj was found, else nil if nothing deleted)

Returns:

  • (Boolean)


156
157
158
159
160
161
162
# File 'lib/sequence/weakrefset.rb', line 156

def delete?(obj)
  x=include?(obj)
  if x
    fail unless  @ids.delete(ref( obj ))||@ids.delete(obj)
    return self
  end
end

#delete_ifObject

Deletes every element of the set for which block evaluates to true, and returns self.



166
167
168
169
# File 'lib/sequence/weakrefset.rb', line 166

def delete_if
  to_a.each { |o| delete(o) if yield(o) }
  self
end

#eachObject

iterate over remaining valid objects in the set



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sequence/weakrefset.rb', line 86

def each
      @ids.each_key { |id|
        case id
        when Integer
          @ids.include?(id) or next
          o = unref(id) or next
          #i don't know where the random symbols come from, but at least they're always symbols...
        else
          o=id
        end
#          case o
#            when Symbol,Fixnum,true,false,nil: warn "immediate value #{o.inspect} found in weakrefset"
#            else 
            yield(o) 
#          end
      }
      self
end

#empty?Boolean

any objects in the set still valid?

Returns:

  • (Boolean)


237
238
239
# File 'lib/sequence/weakrefset.rb', line 237

def empty?
  @ids.empty?
end

#hashObject



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/sequence/weakrefset.rb', line 116

def hash
  hashing=Thread.current[:$WeakRefSet_hashing]||=[]
  return 0 if hashing.include? self
  hashing<<self
  
  result=0
  each{|x| result^=x.hash }
  result

ensure
  hashing.delete(self)
  Thread.current[:$WeakRefSet_hashing]=nil if hashing.empty?
end

#include?(obj) ⇒ Boolean Also known as: member?

is this object in the set?

Returns:

  • (Boolean)


172
173
174
# File 'lib/sequence/weakrefset.rb', line 172

def include?(obj)
  any?{|x| obj==x}
end

#inspectObject

return a human-readable string showing the set



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/sequence/weakrefset.rb', line 179

def inspect
  #unless $weakrefset_verbose_inspect 
  #  return sprintf('#<%s:0x%x {...}>', self.class.name, object_id)
  #end
  ids = (Thread.current[:__weakrefset__inspect_key__] ||= [])

  if ids.include?(object_id)
    return sprintf('#<%s: {...}>', self.class.name)
  end

  begin
    ids << object_id
    return sprintf('#<%s: {%s}>', self.class.name, to_a.inspect[1..-2])
  ensure
    ids.pop
    Thread.current[:__weakrefset__inspect_key__].empty? and
      Thread.current[:__weakrefset__inspect_key__]=nil
  end
end

#merge(enum) ⇒ Object

merge some more objects into the set (return self)



137
138
139
140
# File 'lib/sequence/weakrefset.rb', line 137

def merge(enum)
      enum.each { |obj| add(obj) }
      self
end

#replace(enum) ⇒ Object

replace the objects in the set (return self)



143
144
145
146
147
# File 'lib/sequence/weakrefset.rb', line 143

def replace(enum)
      clear
      merge(enum)
      self
end

#sizeObject Also known as: length

number of objects in the set still valid



242
243
244
# File 'lib/sequence/weakrefset.rb', line 242

def size
  @ids.size
end

#subtract(enum) ⇒ Object

remove some objects from the set (return self)



221
222
223
224
# File 'lib/sequence/weakrefset.rb', line 221

def subtract(enum)
      enum.each { |obj| delete(obj) }
      self
end

#to_aObject



105
106
107
# File 'lib/sequence/weakrefset.rb', line 105

def to_a
  map{|x| x}
end