IdentitySet.st
author Stefan Vogel <sv@exept.de>
Tue, 15 Jan 2013 17:33:59 +0100
changeset 14655 1d2b0650e086
parent 14018 351ab0fb005d
child 14998 6bea48d14676
child 18011 deb0c3355881
permissions -rw-r--r--
class: IdentitySet added: #collisionsFor:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1993 by Claus Gittinger
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
     3
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
5559
426dee57de44 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4625
diff changeset
    12
"{ Package: 'stx:libbasic' }"
426dee57de44 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4625
diff changeset
    13
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    14
Set subclass:#IdentitySet
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1126
diff changeset
    15
	instanceVariableNames:''
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1126
diff changeset
    16
	classVariableNames:''
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1126
diff changeset
    17
	poolDictionaries:''
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1126
diff changeset
    18
	category:'Collections-Unordered'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    20
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    21
!IdentitySet class methodsFor:'documentation'!
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    22
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    23
copyright
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    24
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    25
 COPYRIGHT (c) 1993 by Claus Gittinger
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    26
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    27
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    28
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    29
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    30
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    31
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    32
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    33
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    34
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    35
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    36
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    37
documentation
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    38
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    39
    same as a Set but compares elements using == 
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    40
    (i.e. they must be identical - not just equal in structure).
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    41
    Since compare is on identity (using ==), hashing is also done via
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    42
    #identityHash instead of #hash.
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1126
diff changeset
    43
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1126
diff changeset
    44
    [author:]
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1126
diff changeset
    45
        Claus Gittinger
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    46
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    47
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    48
7310
8865e8d343cd +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 6953
diff changeset
    49
!IdentitySet methodsFor:'Compatibility-Squeak'!
8865e8d343cd +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 6953
diff changeset
    50
8865e8d343cd +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 6953
diff changeset
    51
copyWithout:anElement
11051
029e6b331d96 comment
Claus Gittinger <cg@exept.de>
parents: 10091
diff changeset
    52
    "return a new collection consisting of a copy of the receiver, with
029e6b331d96 comment
Claus Gittinger <cg@exept.de>
parents: 10091
diff changeset
    53
     ALL elements equal to elementToSkip are left out.
029e6b331d96 comment
Claus Gittinger <cg@exept.de>
parents: 10091
diff changeset
    54
     No error is reported, if elementToSkip is not in the collection."
029e6b331d96 comment
Claus Gittinger <cg@exept.de>
parents: 10091
diff changeset
    55
7310
8865e8d343cd +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 6953
diff changeset
    56
    ^ self select:[:each | each ~~ anElement]
8865e8d343cd +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 6953
diff changeset
    57
8865e8d343cd +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 6953
diff changeset
    58
    "
8865e8d343cd +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 6953
diff changeset
    59
     #(1 2 3 4 5 6 7) asSet copyWithout:5
8865e8d343cd +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 6953
diff changeset
    60
    "
8865e8d343cd +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 6953
diff changeset
    61
! !
8865e8d343cd +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 6953
diff changeset
    62
4625
1ebb8edfbd0d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3944
diff changeset
    63
!IdentitySet methodsFor:'adding & removing'!
1ebb8edfbd0d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3944
diff changeset
    64
1ebb8edfbd0d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3944
diff changeset
    65
removeIdentical:anObject ifAbsent:exceptionBlock
1ebb8edfbd0d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3944
diff changeset
    66
    ^ super remove:anObject ifAbsent:exceptionBlock
1ebb8edfbd0d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3944
diff changeset
    67
! !
1ebb8edfbd0d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3944
diff changeset
    68
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    69
!IdentitySet methodsFor:'private'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    70
14655
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    71
collisionsFor:key
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    72
    "Return the number of searches - 1 required for key"
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    73
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    74
    |index  "{ Class:SmallInteger }"
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    75
     length "{ Class:SmallInteger }" startIndex probe count|
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    76
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    77
    length := keyArray basicSize.
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    78
    startIndex := index := self initialIndexForKey:key.
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    79
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    80
    count := 0.
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    81
    [true] whileTrue:[
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    82
        probe := keyArray basicAt:index.
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    83
        (probe notNil and:[key == probe]) ifTrue:[^ count].
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    84
        (self slotIsEmpty:probe) ifTrue:[self error:'non existing key'].
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    85
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    86
        index == length ifTrue:[
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    87
            index := 1.
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    88
        ] ifFalse:[
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    89
            index := index + 1.
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    90
        ].
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    91
        count := count + 1.
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    92
        index == startIndex ifTrue:[self error:'non existing key'].
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    93
    ]
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    94
!
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
    95
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    96
find:key ifAbsent:aBlock
a27a279701f8 Initial revision
claus
parents:
diff changeset
    97
    "Look for the key in the receiver.  If it is found, return
a27a279701f8 Initial revision
claus
parents:
diff changeset
    98
     the index of the slot containing the key, otherwise
a27a279701f8 Initial revision
claus
parents:
diff changeset
    99
     return the value of evaluating aBlock.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   100
     Redefined to compare for identity instead of equality"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   101
6233
0a79cbd07543 *** empty log message ***
martin
parents: 5559
diff changeset
   102
    |index  "{ Class:SmallInteger }"
0a79cbd07543 *** empty log message ***
martin
parents: 5559
diff changeset
   103
     length "{ Class:SmallInteger }"
0a79cbd07543 *** empty log message ***
martin
parents: 5559
diff changeset
   104
     startIndex probe |
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   105
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   106
    length := keyArray basicSize.
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   107
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   108
"/
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   109
"/  length < 10 ifTrue:[
6233
0a79cbd07543 *** empty log message ***
martin
parents: 5559
diff changeset
   110
"/      "assuming, that for small sets the overhead of hashing
0a79cbd07543 *** empty log message ***
martin
parents: 5559
diff changeset
   111
"/       is large ..."
0a79cbd07543 *** empty log message ***
martin
parents: 5559
diff changeset
   112
"/      ^ keyArray identityIndexOf:key ifAbsent:aBlock.
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   113
"/  ].
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   114
"/
10
claus
parents: 3
diff changeset
   115
6953
6b3a197638f0 invoke hash/idHash only in one place (initialIndexFor..)
Claus Gittinger <cg@exept.de>
parents: 6346
diff changeset
   116
    startIndex := index := self initialIndexForKey:key.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   117
a27a279701f8 Initial revision
claus
parents:
diff changeset
   118
    [true] whileTrue:[
6233
0a79cbd07543 *** empty log message ***
martin
parents: 5559
diff changeset
   119
        probe := keyArray basicAt:index.
0a79cbd07543 *** empty log message ***
martin
parents: 5559
diff changeset
   120
        probe == key ifTrue:[^ index].        "<<<< == is different from inherited"
0a79cbd07543 *** empty log message ***
martin
parents: 5559
diff changeset
   121
        (self slotIsEmpty:probe) ifTrue:[^ aBlock value].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   122
6233
0a79cbd07543 *** empty log message ***
martin
parents: 5559
diff changeset
   123
        index == length ifTrue:[
0a79cbd07543 *** empty log message ***
martin
parents: 5559
diff changeset
   124
            index := 1
0a79cbd07543 *** empty log message ***
martin
parents: 5559
diff changeset
   125
        ] ifFalse:[
0a79cbd07543 *** empty log message ***
martin
parents: 5559
diff changeset
   126
            index := index + 1
0a79cbd07543 *** empty log message ***
martin
parents: 5559
diff changeset
   127
        ].
0a79cbd07543 *** empty log message ***
martin
parents: 5559
diff changeset
   128
        index == startIndex ifTrue:[
0a79cbd07543 *** empty log message ***
martin
parents: 5559
diff changeset
   129
            ^ aBlock value
0a79cbd07543 *** empty log message ***
martin
parents: 5559
diff changeset
   130
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   131
    ]
a27a279701f8 Initial revision
claus
parents:
diff changeset
   132
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   133
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   134
findKeyOrNil:key
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   135
    "Look for the key in the receiver.  
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   136
     If it is found, return return the index of the first unused slot. 
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   137
     Grow the receiver, if key was not found, and no unused slots were present"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   138
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   139
    |index  "{ Class:SmallInteger }"
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   140
     length "{ Class:SmallInteger }"
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   141
     startIndex probe 
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   142
     delIndex "{ Class:SmallInteger }" |
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   143
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   144
    delIndex := 0.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   145
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   146
    length := keyArray basicSize.
6953
6b3a197638f0 invoke hash/idHash only in one place (initialIndexFor..)
Claus Gittinger <cg@exept.de>
parents: 6346
diff changeset
   147
    startIndex := index := self initialIndexForKey:key.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   148
a27a279701f8 Initial revision
claus
parents:
diff changeset
   149
    [true] whileTrue:[
6346
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   150
        probe := keyArray basicAt:index.
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   151
        key == probe ifTrue:[^ index].
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   152
        (self slotIsEmpty:probe) ifTrue:[
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   153
            delIndex == 0 ifTrue:[^ index].
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   154
            keyArray basicAt:delIndex put:nil.
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   155
            ^ delIndex
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   156
        ].
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   157
6346
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   158
        probe == DeletedEntry ifTrue:[
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   159
            delIndex == 0 ifTrue:[
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   160
                delIndex := index
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   161
            ]
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   162
        ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   163
6346
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   164
        index == length ifTrue:[
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   165
            index := 1
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   166
        ] ifFalse:[
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   167
            index := index + 1
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   168
        ].
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   169
        index == startIndex ifTrue:[
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   170
            delIndex ~~ 0 ifTrue:[
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   171
                keyArray basicAt:delIndex put:nil.
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   172
                ^ delIndex
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   173
            ].
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   174
            ^ self grow findKeyOrNil:key
a69610ec89a0 slotIsEmpty
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   175
        ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   176
    ]
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   177
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   178
    "Modified: 26.3.1996 / 20:00:42 / cg"
10
claus
parents: 3
diff changeset
   179
!
claus
parents: 3
diff changeset
   180
2460
1d7caef5f5ed added #hashFor:
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   181
hashFor:aKey
1d7caef5f5ed added #hashFor:
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   182
    "return an initial index given a key."
1d7caef5f5ed added #hashFor:
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   183
1d7caef5f5ed added #hashFor:
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   184
    ^ aKey identityHash
1d7caef5f5ed added #hashFor:
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   185
1d7caef5f5ed added #hashFor:
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   186
    "Created: 19.3.1997 / 15:18:59 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   187
! !
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   188
14018
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   189
!IdentitySet methodsFor:'set operations'!
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   190
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   191
\ aCollection
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   192
    "return a new set containing all elements of the receiver, 
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   193
     which are NOT also contained in the aCollection
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   194
     For large collections you better use a Set for aCollection.
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   195
     Redefined here to do identity comparison."
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   196
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   197
    |newCollection|
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   198
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   199
    newCollection := self speciesForAdding new.
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   200
    self do:[:element |
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   201
        (aCollection includesIdentical:element) ifFalse:[
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   202
            newCollection add:element
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   203
        ]
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   204
    ].
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   205
    ^ newCollection
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   206
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   207
    "
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   208
     #(0 1 2 3 4 5 6 7 8 9) asIdentitySet \ #(1 2 3) asSet  
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   209
     #(0 1 2 3 4 5 6 7 8 9) asIdentitySet \ #(1 2 3)
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   210
    "
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   211
! !
351ab0fb005d added: #
Stefan Vogel <sv@exept.de>
parents: 14016
diff changeset
   212
3944
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   213
!IdentitySet methodsFor:'testing'!
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   214
5559
426dee57de44 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4625
diff changeset
   215
identicalContentsAs:aCollection
426dee57de44 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4625
diff changeset
   216
    "return true if the receiver and aCollection represent collections
426dee57de44 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4625
diff changeset
   217
     with identical contents (but not caring for order)."
426dee57de44 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4625
diff changeset
   218
426dee57de44 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4625
diff changeset
   219
    aCollection size == self size ifFalse:[^ false].
10091
ad0bf1206ce0 code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents: 7310
diff changeset
   220
    ^ aCollection conform:[:eachElement | (self includesIdentical:eachElement)].
5559
426dee57de44 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4625
diff changeset
   221
426dee57de44 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4625
diff changeset
   222
    "
426dee57de44 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4625
diff changeset
   223
     |col|
426dee57de44 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4625
diff changeset
   224
426dee57de44 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4625
diff changeset
   225
     col := #('aaa' 'bbb' 'ccc' 'ddd').
426dee57de44 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4625
diff changeset
   226
     col identicalContentsAs:(col asIdentitySet).  
426dee57de44 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4625
diff changeset
   227
     col identicalContentsAs:(col copy asIdentitySet).  
426dee57de44 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4625
diff changeset
   228
     col identicalContentsAs:(col deepCopy asIdentitySet).  
426dee57de44 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4625
diff changeset
   229
   "
10091
ad0bf1206ce0 code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents: 7310
diff changeset
   230
ad0bf1206ce0 code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents: 7310
diff changeset
   231
    "Modified: / 13-10-2006 / 12:55:43 / cg"
5559
426dee57de44 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4625
diff changeset
   232
!
426dee57de44 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4625
diff changeset
   233
3944
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   234
includesIdentical:anObject
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   235
    "for identitySet, the #includes: test already tests for identity"
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   236
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   237
    ^ self includes:anObject
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   238
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   239
    "Created: / 11.12.1998 / 20:01:12 / cg"
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   240
! !
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   241
634
e15218d09420 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   242
!IdentitySet class methodsFor:'documentation'!
e15218d09420 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   243
e15218d09420 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   244
version
14655
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
   245
    ^ '$Header: /cvs/stx/stx/libbasic/IdentitySet.st,v 1.34 2013-01-15 16:33:59 stefan Exp $'
634
e15218d09420 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   246
! !
14655
1d2b0650e086 class: IdentitySet
Stefan Vogel <sv@exept.de>
parents: 14018
diff changeset
   247