Fixed allSelectorsSent because a simpler implementation was overriding the original.
[cslatevm.git] / src / core / collection-extensible.slate
blob62c7f69c01cebdff9908a01077e6aa0fab481cc0
1 collections define: #ExtensibleCollection &parents: {Collection}.
2 "Defines the protocol for inserting into and removing from Collections."
4 e@(ExtensibleCollection traits) add: obj
5 "Insert the object into the Collection, returning the object."
6 [overrideThis].
8 c@(ExtensibleCollection traits) newWith: obj
9 "Answer a new collection with the one element added."
10 [c new `>> [add: obj. ]].
12 c@(ExtensibleCollection traits) newWithAll: d@(Collection traits)
13 "Answer a new collection of kind c and stuff in all of d's elements."
14 [(c newSizeOf: d) `>> [addAll: d. ]].
16 c@(ExtensibleCollection traits) new* [| *rest | c newWithAll: rest].
18 e@(ExtensibleCollection traits) add: obj ifPresent: block
19 "Add the object only if it doesn't already occur in c. If it already does
20 occur, perform the alternative given."
22   (e includes: obj)
23     ifTrue: [block do]
24     ifFalse: [e add: obj]
27 e@(ExtensibleCollection traits) include: obj
28 "Add the object only if it doesn't already occur in c."
30   e add: obj ifPresent: [obj]
33 e@(ExtensibleCollection traits) add: obj withOccurrences: n@(Integer traits)
34 "Add n occurrences of an object."
36   n timesRepeat: [e add: obj].
37   obj
40 e@(ExtensibleCollection traits) addAll: c
41 "Add each element of c to e."
43   c do: #(e add: _) `er.
44   c
47 e@(ExtensibleCollection traits) removeAll: c
48 "Remove all elements of d from c. Removing all from itself has to be
49 separately handled."
51   (e == c ifTrue: [c copy] ifFalse: [c])
52    `>> [do: #(e remove: _) `er. ]
55 e@(ExtensibleCollection traits) removeAll
56 "Remove every element from the collection, leaving it empty."
57 [e removeAll: c].
59 e@(ExtensibleCollection traits) clear
60 "Make the collection empty. By default, use remove calls.
61 Override this for efficiency."
62 [e removeAll].
64 e@(ExtensibleCollection traits) removeAllSuchThat: test
65 "Remove all elements that satisfy the test.
66 Copy the collection since some subclasses re-order their elements on removal."
68   e copy do: [| :each | (test applyWith: each) ifTrue: [e remove: each]].
69   e
72 e@(ExtensibleCollection traits) remove: obj ifAbsent: block
73 [overrideThis].
75 e@(ExtensibleCollection traits) remove: obj
76 [e remove: obj ifAbsent: []].
78 e@(ExtensibleCollection traits) copyWith: obj
80   e copy `>> [add: obj. ]
83 c@(Collection traits) collect: block into: result@(ExtensibleCollection traits)
84 "Similar to collect:, but answers the given collection with the results of
85 applying the block to each element added."
87   c do: [| :each | result add: (block applyWith: each)].
88   result
91 c@(Collection traits) select: test into: result@(ExtensibleCollection traits)
92 "Answer a subset of c containing those elements causing the input block to
93 return True."
95   c do: [| :each | (test applyWith: each) ifTrue: [result add: each]].
96   result