Skip to content
Permalink
Browse files

It implements WeakIdentitySet.

The tests and overrides are based on IdentitySet implementation
  • Loading branch information
dionisiydk committed Aug 31, 2019
1 parent 6fdd518 commit 555f842d0885861916d9653d36375a74aadc977e
Showing with 76 additions and 0 deletions.
  1. +41 −0 src/Collections-Tests/WeakIdentitySetTest.class.st
  2. +35 −0 src/Collections-Weak/WeakIdentitySet.class.st
@@ -0,0 +1,41 @@
Class {
#name : #WeakIdentitySetTest,
#superclass : #WeakSetTest,
#category : #'Collections-Tests-Weak'
}

{ #category : #requirements }
WeakIdentitySetTest >> classToBeTested [

^ WeakIdentitySet
]

{ #category : #tests }
WeakIdentitySetTest >> testGrowWithNil [
"This test covers that grow take into account that nil are wrapped elements of sets"
| set |
set := IdentitySet new.
set add: nil.
set grow.
self assert: (set includes: nil)
]

{ #category : #tests }
WeakIdentitySetTest >> testIdentity [
| identitySet aString anOtherString |

aString := 'hello'.
anOtherString := aString copy.

self assert: aString equals: anOtherString.
self deny: aString == anOtherString.

identitySet := self classToBeTested new.
identitySet add: aString.

self assert: (identitySet occurrencesOf: aString) equals: 1.
self assert: (identitySet occurrencesOf: anOtherString) equals: 0.

self assert: (identitySet includes: aString).
self deny: (identitySet includes: anOtherString) equals: 0.
]
@@ -0,0 +1,35 @@
"
I'm a IdentitySet with weak elements
"
Class {
#name : #WeakIdentitySet,
#superclass : #WeakSet,
#category : #'Collections-Weak-Base'
}

{ #category : #private }
WeakIdentitySet >> scanFor: anObject [
"Scan the key array for the first slot containing either flag (indicating an empty slot) or an element that matches anObject. Answer the index of that slot or raise an error if no slot is found. This method will be overridden in various subclasses that have different interpretations for matching elements."

| index start |
index := start := anObject identityHash \\ array size + 1.
[
| element |
((element := array at: index) == flag or: [ element enclosedElement == anObject ])
ifTrue: [ ^index ].
(index := index \\ array size + 1) = start ] whileFalse.
self errorNoFreeSpace
]

{ #category : #private }
WeakIdentitySet >> scanForEmptySlotFor: aKey [
"Scan the key array for the first slot containing an empty slot (indicated by flag or a nil). Answer the index of that slot. This method will be overridden in various subclasses that have different interpretations for matching elements."

| index start |
index := start := aKey identityHash \\ array size + 1.
[
| element |
((element := array at: index) == flag or: [ element == nil ]) ifTrue: [ ^index ].
(index := index \\ array size + 1) = start ] whileFalse.
self errorNoFreeSpace
]

0 comments on commit 555f842

Please sign in to comment.
You can’t perform that action at this time.