Skip to content

Test for doSeparatedBy #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
74 changes: 39 additions & 35 deletions src/Containers-SmallOrderedSet/CTSmallOrderedSet.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,48 @@
I am an implementation of an ordered set. Compared to other sets I am very efficient for small sizes, speed- and space-wise. I also mantain the order in which elements are added when iterating.
"
Class {
#name : #CTSmallOrderedSet,
#superclass : #Object,
#name : 'CTSmallOrderedSet',
#superclass : 'Object',
#instVars : [
'size',
'table'
],
#category : #'Containers-SmallOrderedSet'
#category : 'Containers-SmallOrderedSet',
#package : 'Containers-SmallOrderedSet'
}

{ #category : #'instance creation' }
{ #category : 'instance creation' }
CTSmallOrderedSet class >> new [
^ self new: 3
]

{ #category : #'instance creation' }
{ #category : 'instance creation' }
CTSmallOrderedSet class >> new: anInteger [
^ self basicNew initialize: anInteger; yourself
]

{ #category : #'instance creation' }
{ #category : 'instance creation' }
CTSmallOrderedSet class >> withAll: aDictionary [
^ (self new: aDictionary size)
addAll: aDictionary;
yourself
]

{ #category : #adding }
{ #category : 'adding' }
CTSmallOrderedSet >> add: newObject [
(self findIndexFor: newObject) = 0
ifTrue: [ self privateAdd: newObject ].
^ newObject
]

{ #category : #adding }
{ #category : 'adding' }
CTSmallOrderedSet >> addAll: aCollection [
aCollection do: [ :each |
self add: each ].
^ aCollection
]

{ #category : #converting }
{ #category : 'converting' }
CTSmallOrderedSet >> asArray [
| array index |
array := Array new: self size.
Expand All @@ -54,82 +55,82 @@ CTSmallOrderedSet >> asArray [
^ array
]

{ #category : #enumerating }
{ #category : 'enumerating' }
CTSmallOrderedSet >> do: aOneArgumentBlock [
1 to: size do: [ :i |
aOneArgumentBlock value: (table at: i) ]
]

{ #category : #enumerating }
{ #category : 'enumerating' }
CTSmallOrderedSet >> do: aOneArgumentBlock separatedBy: aNiladicBlock [
1 to: size do: [ :i |
i > 1 ifTrue: [ aNiladicBlock value ].
aOneArgumentBlock value: (table at: i) ]
]

{ #category : #'private ' }
{ #category : 'private ' }
CTSmallOrderedSet >> errorNotFound [
self error: 'Not found'
]

{ #category : #'private ' }
{ #category : 'private ' }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add a comment stating that the 0 is returned to indicate that the element is not found.

CTSmallOrderedSet >> findIndexFor: aKey [
1 to: size do: [ :index |
(table at: index) = aKey
ifTrue: [ ^ index ] ].
^ 0
]

{ #category : #'private ' }
{ #category : 'private' }
CTSmallOrderedSet >> grow [
| newTable |
"#replaceFrom:to:with:startingAt: would be better but not portable"
newTable := Array new: 2 * size.
1 to: size do: [ :index |
newTable at: index put: (table at: index) ].
table := newTable
| newTable newSize |
newSize := (table isEmpty) ifTrue: [1] ifFalse: [2 * table size]. "Ensure it grows from 0"
newTable := Array new: newSize.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I do not understand why the new size is not double because it can have an impact on the grow efficiency

1 to: size do: [ :index |
newTable at: index put: (table at: index) ].
table := newTable
]

{ #category : #testing }
{ #category : 'testing' }
CTSmallOrderedSet >> includes: anObject [
^ (self findIndexFor: anObject) ~= 0
]

{ #category : #initialization }
{ #category : 'initialization' }
CTSmallOrderedSet >> initialize: anInteger [
self initialize.
size := 0.
table := Array new: anInteger
]

{ #category : #testing }
{ #category : 'testing' }
CTSmallOrderedSet >> isCollection [
^ true
]

{ #category : #testing }
{ #category : 'testing' }
CTSmallOrderedSet >> isEmpty [
^ size = 0
]

{ #category : #copying }
{ #category : 'copying' }
CTSmallOrderedSet >> postCopy [
super postCopy.
table := table copy
]

{ #category : #'private ' }
{ #category : 'private ' }
CTSmallOrderedSet >> privateAdd: newObject [
size = table size ifTrue: [ self grow ].
table at: (size := size + 1) put: newObject.
]

{ #category : #removing }
{ #category : 'removing' }
CTSmallOrderedSet >> remove: anObject [
^ self remove: anObject ifAbsent: [ self errorNotFound ]
]

{ #category : #removing }
{ #category : 'removing' }
CTSmallOrderedSet >> remove: anObject ifAbsent: aNiladicBlock [
| index |
index := self findIndexFor: anObject.
Expand All @@ -139,15 +140,18 @@ CTSmallOrderedSet >> remove: anObject ifAbsent: aNiladicBlock [
^ anObject
]

{ #category : #'private ' }
CTSmallOrderedSet >> removeIndex: index [
table at: index put: nil.
index to: size - 1 do: [ :i |
table at: i put: (table at: i + 1) ].
size := size - 1
{ #category : 'private' }
CTSmallOrderedSet >> removeIndex: index [
(index < 1 or: [ index > size ])
ifTrue: [ self error: 'Index out of bounds' ].
table at: index put: nil.
index to: size - 1 do: [ :i |
table at: i put: (table at: i + 1) ].
table at: size put: nil.
size := size - 1.
]

{ #category : #accessing }
{ #category : 'accessing' }
CTSmallOrderedSet >> size [
^ size
]
Loading