extended Collection with asSet

This commit is contained in:
hyunghwan.chung 2019-02-21 08:23:43 +00:00
parent 04e28e979a
commit c875e41ce6
2 changed files with 24 additions and 1 deletions

View File

@ -727,6 +727,9 @@ class OrderedCollection(SequenceableCollection)
class Set(Collection)
{
## The set class stores unordered elemenents and disallows duplicates.
## It is implemented as an open-addressed hash table currently.
var tally, bucket.
method(#class) new
@ -933,6 +936,11 @@ class Set(Collection)
}
}
## TODO: implement IdentitySet
##class IdentitySet(Set)
##{
##}
class AssociativeCollection(Collection)
{
var tally, bucket.
@ -1555,6 +1563,14 @@ extend Collection
| coll |
coll := OrderedCollection new: self size.
self do: [:each | coll addLast: each ].
^coll
^coll.
}
method asSet
{
| coll |
coll := Set new: self size.
self do: [:each | coll add: each ].
^coll.
}
}

View File

@ -263,6 +263,13 @@ extend MyObject
k = '0=>3 1=>-6233041613697111534195832 2=>3.3333333333333333333 '.
],
[
| k |
k := #('str1' 'str2' 'str3' 'str4' 'str5' 'str1' 'str2') asSet.
##k do: [:each | each dump].
if ((k includes: 'str1') and (k includes: 'str3') and ((k includes: 'str9') not)) { true } else { false }
],
[
| b |
b := [:n | (n > 0) ifTrue: [ n * (b value: n - 1)] ifFalse: [1]].