-
Notifications
You must be signed in to change notification settings - Fork 2
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
Sneharaj7645653
wants to merge
14
commits into
pharo-containers:master
Choose a base branch
from
Sneharaj7645653:testDoSeparatedBy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
07b5c7c
grow improved
Sneharaj7645653 29551b7
grow improved
Sneharaj7645653 6c9b653
removed improved
Sneharaj7645653 ddbaea1
removeIndex improved
Sneharaj7645653 471933a
test for withAll added
Sneharaj7645653 e506493
test for removeIndex
Sneharaj7645653 410fd8e
test for privateAdd
Sneharaj7645653 ef0ccb8
test for postCopt
Sneharaj7645653 4225ee3
test for isCollection
Sneharaj7645653 2ab34df
test for initialize
Sneharaj7645653 c9b91e8
test for grow
Sneharaj7645653 20cf03c
test for findIndexFor
Sneharaj7645653 ff41d88
test for Error Not Found
Sneharaj7645653 4aa35b9
test for doSeparatedBy
Sneharaj7645653 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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 ' } | ||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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 | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.