PluggableSet.st
author Merge Script
Mon, 06 Jun 2016 06:56:04 +0200
branchjv
changeset 3882 a013a37e2c3d
parent 3179 3c3bf4781f2e
child 4022 bbe200de665c
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3176
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
 COPYRIGHT (c) 2014 by Claus Gittinger
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
              All Rights Reserved
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
 This software is furnished under a license and may be used
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
 hereby transferred.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
Set subclass:#PluggableSet
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
	instanceVariableNames:'hashFunction compareFunction'
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
	classVariableNames:''
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	poolDictionaries:''
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	category:'Collections-Unordered'
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
!
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
!PluggableSet class methodsFor:'documentation'!
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
copyright
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
 COPYRIGHT (c) 2014 by Claus Gittinger
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
              All Rights Reserved
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
 This software is furnished under a license and may be used
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
 only in accordance with the terms of that license and with the
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
 inclusion of the above copyright notice.   This software may not
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
 be provided or otherwise made available to, or used by, any
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
 other person.  No title to or ownership of the software is
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
 hereby transferred.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
!
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
documentation
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
    a set where the hash- and compare functions can be provided externally.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
    [author:]
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
        Claus Gittinger
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
!
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
examples
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
    |s|
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
    s := PluggableSet 
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
        hashWith:[:k | k size] 
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
        compareWith:[:a :b | a notNil and:[b notNil and:[a asLowercase = b asLowercase]]].
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
    s add:'hello'.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
    s add:'world'.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
    s add:'abc'.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
    s add:'Hello'.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
    s add:'heLLo'.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
    s add:'ABC'.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
    s add:'WORLD'.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
    s size.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
    s includes:'heLlo'.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
    s includes:'wOrLd'.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
    s includes:'wOrLds'.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
! !
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
!PluggableSet class methodsFor:'instance creation'!
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
hashWith:hashFunctionArg compareWith:compareFunctionArg
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
    ^ self new hashWith:hashFunctionArg compareWith:compareFunctionArg
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
! !
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
!PluggableSet methodsFor:'Compatibility-Squeak'!
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
copyWithout:anElement
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
    "return a new collection consisting of a copy of the receiver, with
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
     ALL elements equal to elementToSkip are left out.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
     No error is reported, if elementToSkip is not in the collection."
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
    ^ self reject:[:each | compareFunction value:each value:anElement]
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
    "
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
     #(1 2 3 4 5 6 7) asSet copyWithout:5
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
    "
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
! !
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
!PluggableSet methodsFor:'accessing'!
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
hashWith:hashFunctionArg compareWith:compareFunctionArg 
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
    hashFunction := hashFunctionArg.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
    compareFunction := compareFunctionArg.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
! !
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
!PluggableSet methodsFor:'converting'!
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
asIdentitySet 
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
    "return the receiver as an IdentitySet"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
    "could be an instance of a subclass..."
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
    self class == IdentitySet ifTrue:[
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
        ^ self
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
    ].
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
    ^ super asIdentitySet
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
!
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
asNewIdentitySet
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
    "make sure to return myself as a unique new IdentitySet"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
    "could be an instance of a subclass..."
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
    self class == IdentitySet ifTrue:[
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
        ^ self copy
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
    ].
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
    ^ super asIdentitySet
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
    "
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
        |s|
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
        s := #(1 2 3 4) asIdentitySet.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
        self assert:(s ~~ s asNewIdentitySet).
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
        self assert:(s = s asNewIdentitySet).
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
    "
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
! !
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
!PluggableSet methodsFor:'private'!
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
collisionsFor:key
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
    "Return the number of searches - 1 required for key"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
    |index  "{ Class:SmallInteger }"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
     length "{ Class:SmallInteger }" startIndex probe count|
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
    length := keyArray basicSize.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
    startIndex := index := self initialIndexForKey:key.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
    count := 0.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
    [true] whileTrue:[
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
        probe := keyArray basicAt:index.
3179
3c3bf4781f2e don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents: 3176
diff changeset
   137
        (probe notNil and:[compareFunction value:key value:probe]) ifTrue:[^ count].
3176
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
        (self slotIsEmpty:probe) ifTrue:[self error:'non existing key'].
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
        index == length ifTrue:[
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
            index := 1.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
        ] ifFalse:[
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
            index := index + 1.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
        ].
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
        count := count + 1.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
        index == startIndex ifTrue:[self error:'non existing key'].
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
    ]
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
!
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
find:key ifAbsent:aBlock
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
    "Look for the key in the receiver.  If it is found, return
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
     the index of the slot containing the key, otherwise
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
     return the value of evaluating aBlock.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
     Redefined to compare for identity instead of equality"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156
    |index  "{ Class:SmallInteger }"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157
     length "{ Class:SmallInteger }"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
     startIndex probe |
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
    length := keyArray basicSize.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
"/
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
"/  length < 10 ifTrue:[
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
"/      "assuming, that for small sets the overhead of hashing
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
"/       is large ..."
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
"/      ^ keyArray identityIndexOf:key ifAbsent:aBlock.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
"/  ].
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
"/
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
    startIndex := index := self initialIndexForKey:key.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
    [true] whileTrue:[
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
        probe := keyArray basicAt:index.
3179
3c3bf4781f2e don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents: 3176
diff changeset
   174
        probe notNil ifTrue:[
3c3bf4781f2e don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents: 3176
diff changeset
   175
            (compareFunction value:probe value:key) ifTrue:[^ index].        "<<<< == is different from inherited"
3c3bf4781f2e don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents: 3176
diff changeset
   176
        ].
3176
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
        (self slotIsEmpty:probe) ifTrue:[^ aBlock value].
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
        index == length ifTrue:[
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
            index := 1
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
        ] ifFalse:[
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
            index := index + 1
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
        ].
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
        index == startIndex ifTrue:[
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
            ^ aBlock value
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
        ]
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
    ]
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   188
!
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   189
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   190
findKeyOrNil:key
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   191
    "Look for the key in the receiver.  
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   192
     If it is found, return return the index of the first unused slot. 
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
     Grow the receiver, if key was not found, and no unused slots were present"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
    |index  "{ Class:SmallInteger }"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   196
     length "{ Class:SmallInteger }"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
     startIndex probe 
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   198
     delIndex "{ Class:SmallInteger }" |
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
    delIndex := 0.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
    length := keyArray basicSize.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
    startIndex := index := self initialIndexForKey:key.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
    [true] whileTrue:[
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
        probe := keyArray basicAt:index.
3179
3c3bf4781f2e don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents: 3176
diff changeset
   207
        probe notNil ifTrue:[
3c3bf4781f2e don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents: 3176
diff changeset
   208
            (compareFunction value:key value:probe) ifTrue:[^ index].
3c3bf4781f2e don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents: 3176
diff changeset
   209
        ].
3176
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
        (self slotIsEmpty:probe) ifTrue:[
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   211
            delIndex == 0 ifTrue:[^ index].
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   212
            keyArray basicAt:delIndex put:nil.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   213
            ^ delIndex
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   214
        ].
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   215
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
        probe == DeletedEntry ifTrue:[
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   217
            delIndex == 0 ifTrue:[
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
                delIndex := index
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
            ]
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
        ].
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   221
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222
        index == length ifTrue:[
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   223
            index := 1
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   224
        ] ifFalse:[
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
            index := index + 1
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   226
        ].
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
        index == startIndex ifTrue:[
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   228
            delIndex ~~ 0 ifTrue:[
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   229
                keyArray basicAt:delIndex put:nil.
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
                ^ delIndex
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   231
            ].
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   232
            ^ self grow findKeyOrNil:key
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   233
        ].
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   234
    ]
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
    "Modified: 26.3.1996 / 20:00:42 / cg"
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   237
!
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   238
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   239
hashFor:aKey
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   240
    "return an initial index given a key."
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   241
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   242
    ^ hashFunction value:aKey
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   243
! !
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   244
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   245
!PluggableSet class methodsFor:'documentation'!
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   246
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   247
version
3179
3c3bf4781f2e don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents: 3176
diff changeset
   248
    ^ '$Header: /cvs/stx/stx/libbasic2/PluggableSet.st,v 1.2 2014-02-18 21:21:18 cg Exp $'
3176
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   249
!
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   250
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   251
version_CVS
3179
3c3bf4781f2e don't call compareFunction on nil
Claus Gittinger <cg@exept.de>
parents: 3176
diff changeset
   252
    ^ '$Header: /cvs/stx/stx/libbasic2/PluggableSet.st,v 1.2 2014-02-18 21:21:18 cg Exp $'
3176
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   253
! !
fee31951025f initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   254