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
20 changes: 15 additions & 5 deletions src/Containers-OrderedSet-Tests/CTOrderedSetTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,31 @@ CTOrderedSetTest >> testAllLargestSubsets [
self assert: actual equals: expected.
]

{ #category : #'performance tests' }
CTOrderedSetTest >> testAllSubsetsPerformance [
| set time |
set := CTOrderedSet withAll: (1 to: 10) asArray. "Set of size 10"

time := [ set allSubsets ] timeToRun.
self assert: set allSubsets size equals: (1 bitShift: 10) - 2. "1022 subsets"
self assert: time < 1 second description: 'Should complete within 1 second for size 10'.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this fail used to fail before?

]

{ #category : #tests }
CTOrderedSetTest >> testAllSubsets [
| set expected actual |
set := CTOrderedSet withAll: #(a c b).

expected := {
CTOrderedSet withAll: #(a) .
CTOrderedSet withAll: #(c) .
CTOrderedSet withAll: #(b) .
CTOrderedSet withAll: #(a c) .
CTOrderedSet withAll: #(a c) .
CTOrderedSet withAll: #(b) .
CTOrderedSet withAll: #(a b) .
CTOrderedSet withAll: #(c b) }.

actual := set allSubsets.
self assert: actual equals: expected.
self assert: actual equals: expected.
]

{ #category : #'ordered collection tests' }
Expand Down
21 changes: 15 additions & 6 deletions src/Containers-OrderedSet/CTOrderedSet.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,22 @@ CTOrderedSet >> allLargestSubsets [

{ #category : #'as yet unclassified' }
CTOrderedSet >> allSubsets [
"Generate all possible subsets of this self without self and an empty set.
Example:
{1, 2, 3} => {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}"
"Generate all possible subsets of the receiver, excluding the receiver itself and the empty set.
Uses a bitmask approach for efficiency.
Example: #(a b c) => #(a), #(b), #(c), #(a b), #(a c), #(b c)"

| subsets size |
size := self size.
subsets := OrderedCollection new: (1 bitShift: size) - 2. "Reserve space for 2^n - 2 subsets"

1 to: (1 bitShift: size) - 2 do: [ :mask |
| subset |
subset := self species new.
1 to: size do: [ :index |
(mask bitAt: index) = 1 ifTrue: [ subset addLast: (self at: index) ] ].
subsets add: subset ].

"This implementation might not be very fast and has to be improved"
^ (self combinations copyWithout: self asArray) collect: [ :eachArray |
self species withAll: eachArray ]
^ subsets asArray
]

{ #category : #accessing }
Expand Down
Loading