Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/Containers-UniqueOrdered-Tests/CTSmallOrderedSetTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,52 @@ CTSmallOrderedSetTest >> testSize [
collection addAll: #(2 1 1).
self assert: collection size equals: 2.
]

{ #category : #testing }
CTSmallOrderedSetTest >> testUnion[
"Tests the union operation of CTSmallOrderedSet to ensure it combines two sets, maintaining uniqueness and preserving the order of elements."
"scope: class-variables & instance-variables: collection"

| set1 set2 result |
set1 := CTSmallOrderedSet withAll: #(1 2 3).
set2 := CTSmallOrderedSet withAll: #(2 3 4).
result := set1 union: set2.

self assert: result size equals: 4.
self assert: (result includes: 1).
self assert: (result includes: 2).
self assert: (result includes: 3).
self assert: (result includes: 4).

"Test order preservation"
self assert: result asArray equals: #(1 2 3 4).
]

{ #category : #testing }
CTSmallOrderedSetTest >> testDifference [
| set1 set2 result |
set1 := CTSmallOrderedSet withAll: #(1 2 3 4).
set2 := CTSmallOrderedSet withAll: #(2 4).
result := set1 difference: set2.

self assert: result size equals: 2.
self assert: (result includes: 1).
self assert: (result includes: 3).
self deny: (result includes: 2).
self deny: (result includes: 4).
]

{ #category : #testing }
CTSmallOrderedSetTest >> testIntersection [
| set1 set2 result |
set1 := CTSmallOrderedSet withAll: #(1 2 3 4).
set2 := CTSmallOrderedSet withAll: #(2 4 5).
result := set1 intersection: set2.

self assert: result size equals: 2.
self assert: (result includes: 2).
self assert: (result includes: 4).
self deny: (result includes: 1).
self deny: (result includes: 3).
self deny: (result includes: 5).
]
32 changes: 32 additions & 0 deletions src/Containers-UniqueOrdered/CTSmallOrderedSet.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,35 @@ CTSmallOrderedSet >> removeIndex: index [
CTSmallOrderedSet >> size [
^ size
]

{ #category : #operations }
CTSmallOrderedSet >> union: aCollection [
"Return a new set containing all elements from self and aCollection"
^ self copy addAll: aCollection; yourself
]

{ #category : #operations }
CTSmallOrderedSet >> difference: aCollection [
"Return a new set containing elements in self that are not in aCollection"
| result |
result := self copy.
aCollection do: [ :each | result remove: each ifAbsent: [ ] ].
^ result
]

{ #category : #operations }
CTSmallOrderedSet >> intersection: aCollection [
"Return a new set containing elements present in both self and aCollection"
| result |
result := self class new.
self do: [ :each |
(aCollection includes: each) ifTrue: [
result add: each ] ].
^ result
]

{ #category : #converting }
CTSmallOrderedSet >> asArray[
"Return an array containing all elements in the order they are stored."
^ table copyFrom: 1 to: size
]
Loading