Class: Immutable::Map::Fork

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

Overview

:nodoc:

Direct Known Subclasses

BlackFork, RedFork

Constant Summary

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

- (Fork) initialize(left, key, value, right)

A new instance of Fork



138
139
140
141
142
143
# File 'lib/immutable/map.rb', line 138

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

Instance Attribute Details

- (Object) key (readonly)

Returns the value of attribute key



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

def key
  @key
end

- (Object) left (readonly)

Returns the value of attribute left



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

def left
  @left
end

- (Object) right (readonly)

Returns the value of attribute right



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

def right
  @right
end

- (Object) value (readonly)

Returns the value of attribute value



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

def value
  @value
end

Instance Method Details

- (Object) [](key)



153
154
155
156
157
158
159
160
161
162
# File 'lib/immutable/map.rb', line 153

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

- (Object) del(key)



164
165
166
167
168
169
170
171
172
# File 'lib/immutable/map.rb', line 164

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

- (Boolean) empty?

Returns:

  • (Boolean)


149
150
151
# File 'lib/immutable/map.rb', line 149

def empty?
  false
end

- (Object) foldl_with_key(e, &block)



187
188
189
190
# File 'lib/immutable/map.rb', line 187

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

- (Object) foldr_with_key(e, &block)



178
179
180
181
# File 'lib/immutable/map.rb', line 178

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