From c875e41ce642b43dbcbbff52d4f2f4b4d76c55c6 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Thu, 21 Feb 2019 08:23:43 +0000 Subject: [PATCH] extended Collection with asSet --- moo/kernel/Collect.moo | 18 +++++++++++++++++- moo/kernel/test-001.moo | 7 +++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/moo/kernel/Collect.moo b/moo/kernel/Collect.moo index eb203f0..9ba8c2d 100644 --- a/moo/kernel/Collect.moo +++ b/moo/kernel/Collect.moo @@ -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. } } diff --git a/moo/kernel/test-001.moo b/moo/kernel/test-001.moo index 8687c98..2acde35 100644 --- a/moo/kernel/test-001.moo +++ b/moo/kernel/test-001.moo @@ -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]].