IdDict.st
author Claus Gittinger <cg@exept.de>
Sat, 09 Dec 1995 22:34:58 +0100
changeset 726 997e30ef8423
parent 634 e15218d09420
child 1126 497de696dff0
permissions -rw-r--r--
careful with empty revision strings
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) 1992 by Claus Gittinger
175
82ba8d2e3569 *** 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
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
Dictionary subclass:#IdentityDictionary
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    14
	 instanceVariableNames:''
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    15
	 classVariableNames:''
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    16
	 poolDictionaries:''
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    17
	 category:'Collections-Unordered'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    18
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    20
!IdentityDictionary class methodsFor:'documentation'!
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    21
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    22
copyright
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    23
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    24
 COPYRIGHT (c) 1992 by Claus Gittinger
175
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
    25
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    26
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    27
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    28
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    29
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    30
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    31
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    32
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    33
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    34
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    35
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    36
documentation
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    37
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    38
    same as a Dictionary but key must be identical - not just equal.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    39
    Since compare is on identical keys (using ==), hashing is also done via
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    40
    #identityHash instead of #hash.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    41
    IdentityDictionaries are especially useful, when symbols are used as keys.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    42
"
345
claus
parents: 175
diff changeset
    43
! !
claus
parents: 175
diff changeset
    44
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    45
!IdentityDictionary methodsFor:'private'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    46
345
claus
parents: 175
diff changeset
    47
compareSame:element1 with:element2
claus
parents: 175
diff changeset
    48
    ^ element1 == element2
claus
parents: 175
diff changeset
    49
!
claus
parents: 175
diff changeset
    50
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    51
emptyCollectionForKeys
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    52
    "return an empty collection used for keys.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    53
     Made a separate method to allow redefinition for different kind of
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    54
     containers in subclasses"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    55
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    56
    ^ IdentitySet new:(self size)
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    57
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    58
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    59
find:key ifAbsent:aBlock
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    60
    "Look for the key in the receiver.  If it is found, return
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    61
     the index of the association containing the key, otherwise
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    62
     return the value of evaluating aBlock."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    63
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    64
    |index  "{ Class:SmallInteger }"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    65
     length "{ Class:SmallInteger }"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    66
     startIndex
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    67
     probe|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    68
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    69
    length := keyArray basicSize.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    70
    length < 10 ifTrue:[
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    71
	"assuming, that for small dictionaries the overhead of hashing
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    72
	 is large ... maybe that proves wrong (if overhead of comparing
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    73
	 is high)"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    74
	^ keyArray identityIndexOf:key ifAbsent:aBlock.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    75
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    76
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    77
    index := key identityHash.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    78
    index := index \\ length + 1.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    79
    startIndex := index.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    80
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    81
    [true] whileTrue:[
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    82
	probe := keyArray basicAt:index.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    83
	probe == key ifTrue:[^ index].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    84
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    85
	index == length ifTrue:[
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    86
	    index := 1
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    87
	] ifFalse:[
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    88
	    index := index + 1
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    89
	].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    90
	(probe isNil or:[index == startIndex]) ifTrue:[^ aBlock value]
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    91
    ]
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    92
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    93
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    94
findKeyOrNil:key
a27a279701f8 Initial revision
claus
parents:
diff changeset
    95
    "Look for the key in the receiver.  If it is found, return
a27a279701f8 Initial revision
claus
parents:
diff changeset
    96
     the index of the association containing the key, otherwise
a27a279701f8 Initial revision
claus
parents:
diff changeset
    97
     return the index of the first unused slot. Grow the receiver,
a27a279701f8 Initial revision
claus
parents:
diff changeset
    98
     if key was not found, and no unused slots where present"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    99
a27a279701f8 Initial revision
claus
parents:
diff changeset
   100
    |index  "{ Class:SmallInteger }"
375
claus
parents: 362
diff changeset
   101
     length "{ Class:SmallInteger }"
claus
parents: 362
diff changeset
   102
     startIndex probe |
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   103
a27a279701f8 Initial revision
claus
parents:
diff changeset
   104
    length := keyArray basicSize.
362
claus
parents: 345
diff changeset
   105
    index := key identityHash.
claus
parents: 345
diff changeset
   106
    index := index \\ length + 1.
claus
parents: 345
diff changeset
   107
    startIndex := index.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   108
a27a279701f8 Initial revision
claus
parents:
diff changeset
   109
    [true] whileTrue:[
175
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   110
	probe := keyArray basicAt:index.
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   111
	(probe isNil or: [probe == key]) ifTrue:[^ index].
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   112
	probe == DeletedEntry ifTrue:[
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   113
	    keyArray basicAt:index put:nil.
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   114
	    ^ index
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   115
	].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   116
175
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   117
	index == length ifTrue:[
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   118
	    index := 1
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   119
	] ifFalse:[
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   120
	    index := index + 1
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   121
	].
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   122
	index == startIndex ifTrue:[^ self grow findKeyOrNil:key].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   123
    ]
a27a279701f8 Initial revision
claus
parents:
diff changeset
   124
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   125
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   126
findNil:key
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   127
    "Look for the next slot usable for key.  This method assumes that
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   128
     key is not already in the receiver - used only while growing/rehashing"
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   129
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   130
    |index  "{ Class:SmallInteger }"
375
claus
parents: 362
diff changeset
   131
     length "{ Class:SmallInteger }"|
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   132
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   133
    length := keyArray basicSize.
362
claus
parents: 345
diff changeset
   134
    index := key identityHash.
claus
parents: 345
diff changeset
   135
    index := index \\ length + 1.
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   136
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   137
    [(keyArray basicAt:index) notNil] whileTrue:[
175
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   138
	index == length ifTrue:[
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   139
	    index := 1
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   140
	] ifFalse:[
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   141
	    index := index + 1
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   142
	].
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   143
	"notice: no check for no nil found - we must find one since
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   144
	 this is only called after growing"
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   145
    ].
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   146
    ^ index
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   147
! !
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   148
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   149
!IdentityDictionary methodsFor:'testing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   150
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   151
includesValue:aValue
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   152
    "return true, if the argument, aValue is stored in the dictionary,
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   153
     Redefined to use identity compare, NOT equality compare"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   154
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   155
    ^ (valueArray identityIndexOf:aValue) ~~ 0
77
6c38ca59927f *** empty log message ***
claus
parents: 39
diff changeset
   156
!
6c38ca59927f *** empty log message ***
claus
parents: 39
diff changeset
   157
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   158
occurrencesOf:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   159
    "count & return how often anObject is stored in the dictionary.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   160
     This counts values - not keys.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   161
     Redefined to use identity compare, NOT equality compare."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   162
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   163
    |cnt|
175
82ba8d2e3569 *** empty log message ***
claus
parents: 92
diff changeset
   164
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   165
    cnt := 0.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   166
    valueArray do:[:element |
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   167
       element == anObject ifTrue:[cnt := cnt + 1]
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   168
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   169
    ^ cnt
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   170
! !
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   171
634
e15218d09420 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   172
!IdentityDictionary class methodsFor:'documentation'!
e15218d09420 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   173
e15218d09420 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   174
version
e15218d09420 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   175
    ^ '$Header: /cvs/stx/stx/libbasic/Attic/IdDict.st,v 1.18 1995-11-23 17:31:22 cg Exp $'
e15218d09420 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   176
! !