Class: Immutable::Map::Fork

Inherits:
Immutable::Map show all
Defined in:
lib/immutable/map.rb

Overview

:nodoc:

Direct Known Subclasses

BlackFork, RedFork

Constant Summary

Constants inherited from Immutable::Map

Leaf

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Immutable::Map

#delete, #each, empty, #foldl, #foldr, #insert, #inspect, #map, #map_with_key, singleton

Methods included from Foldable

#foldl, #length, #product, #sum

Constructor Details

#initialize(left, key, value, right) ⇒ Fork

Returns a new instance of Fork.



135
136
137
138
139
140
# File 'lib/immutable/map.rb', line 135

def initialize(left, key, value, right)
  @left = left
  @key = key
  @value = value
  @right = right
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



133
134
135
# File 'lib/immutable/map.rb', line 133

def key
  @key
end

#leftObject (readonly)

Returns the value of attribute left.



133
134
135
# File 'lib/immutable/map.rb', line 133

def left
  @left
end

#rightObject (readonly)

Returns the value of attribute right.



133
134
135
# File 'lib/immutable/map.rb', line 133

def right
  @right
end

#valueObject (readonly)

Returns the value of attribute value.



133
134
135
# File 'lib/immutable/map.rb', line 133

def value
  @value
end

Instance Method Details

#[](key) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/immutable/map.rb', line 150

def [](key)
  x = key <=> @key
  if x < 0
    @left[key]
  elsif x > 0
    @right[key]
  else
    @value
  end
end

#del(key) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/immutable/map.rb', line 161

def del(key)
  if key < self.key
    del_left(left, self.key, self.value, right, key)
  elsif key > self.key
    del_right(left, self.key, self.value, right, key)
  else
    app(left, right)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/immutable/map.rb', line 146

def empty?
  false
end

#foldl_with_key(e, &block) ⇒ Object



184
185
186
187
# File 'lib/immutable/map.rb', line 184

def foldl_with_key(e, &block)
  l = @left.foldl_with_key(e, &block)
  @right.foldl_with_key(yield(l, @key, @value), &block)
end

#foldr_with_key(e, &block) ⇒ Object



175
176
177
178
# File 'lib/immutable/map.rb', line 175

def foldr_with_key(e, &block)
  r = @right.foldr_with_key(e, &block)
  @left.foldr_with_key(yield(@key, @value, r), &block)
end