6.29. kink/container/FLAT_SET¶
Provides an `order_set` implementation which stores all the elements in a sorted `vec`.
Flat set is a good choice when elements are rarely inserted or deleted, or the size of the set is small.
6.29.1. FLAT_SET.new(...[$config={}])¶
`new` returns a new empty flat `order_set`.
Config method
C.order($up_to?): default = {(:X :Y) X <= Y }
Total order relation
$up_to? must be a function which takes two args X and Y. `up_to?(X Y)` must return the bool value of `X up-to Y`, where `up-to` is the total order relation of elements.
Precondition
$up_to? must be a function which takes two values and returns a `bool`.
Example: default order
:FLAT_SET.require_from('kink/container/')
:Map <- FLAT_SET.new
Map.push('foo')
Map.push('bar')
Map.push('baz')
stdout.print_line(Map.have?('foo').repr) # => true
stdout.print_line(Map.have?('FOO').repr) # => false
Map.each{(:Elem)
stdout.print_line(Elem.repr)
}
# Output:
# "bar"
# "baz"
# "foo"
Example: reversed order
:FLAT_SET.require_from('kink/container/')
:Map <- FLAT_SET.new{(:C)
C.order{(:X :Y) X >= Y }
}
Map.push('foo')
Map.push('bar')
Map.push('baz')
Map.each{(:Elem)
stdout.print_line(Elem.repr)
}
# Output:
# "foo"
# "baz"
# "bar"
6.29.2. FLAT_SET.of(...Elems)¶
`of` makes a new flat `order_set` which contains the values of `Elems`. The elements are ordered by `<=` operator, or `op_le` method.
Precondition
The elements of `Elems` must be in the domain of the elements of the set.
Example
:FLAT_SET.require_from('kink/container/')
:Map <- FLAT_SET.of('foo' 'bar' 'baz')
stdout.print_line(Map.have?('foo').repr) # => true
stdout.print_line(Map.have?('FOO').repr) # => false
Map.each{(:Elem)
stdout.print_line(Elem.repr)
}
# Output:
# "bar"
# "baz"
# "foo"
6.29.3. FLAT_SET.from_each(Eacher)¶
`from_each` makes a flat `order_set` containing the elements of `Eacher`. The elements are ordered by `<=` operator, or `op_le` method.
Precondition
`Eacher` must implement each($consume) method, which calls $consume for each element of `Eacher`.
The elements of `Eacher` must be in the domain of the elements of the set.
Example
:FLAT_SET.require_from('kink/container/')
:Iter <- ['foo' 'bar' 'baz'].iter
:Map <- FLAT_SET.from_each(Iter)
stdout.print_line(Map.have?('foo').repr) # => true
stdout.print_line(Map.have?('FOO').repr) # => false
Map.each{(:Elem)
stdout.print_line(Elem.repr)
}
# Output:
# "bar"
# "baz"
# "foo"