WeakIdentityDictionary.st
author Claus Gittinger <cg@exept.de>
Tue, 23 Apr 1996 13:57:59 +0200
changeset 1263 92c1db6b0776
parent 1256 249fd57abee5
child 1290 15ba3221b89b
permissions -rw-r--r--
commentary
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
62211a9bc04d Initial revision
claus
parents:
diff changeset
     1
"
62211a9bc04d Initial revision
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1992 by Claus Gittinger
358
claus
parents: 159
diff changeset
     3
	      All Rights Reserved
6
62211a9bc04d Initial revision
claus
parents:
diff changeset
     4
62211a9bc04d Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
62211a9bc04d Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
62211a9bc04d Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
62211a9bc04d Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
62211a9bc04d Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
62211a9bc04d Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
62211a9bc04d Initial revision
claus
parents:
diff changeset
    11
"
62211a9bc04d Initial revision
claus
parents:
diff changeset
    12
62211a9bc04d Initial revision
claus
parents:
diff changeset
    13
IdentityDictionary subclass:#WeakIdentityDictionary
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    14
	instanceVariableNames:''
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    15
	classVariableNames:''
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    16
	poolDictionaries:''
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    17
	category:'Collections-Unordered'
6
62211a9bc04d Initial revision
claus
parents:
diff changeset
    18
!
62211a9bc04d Initial revision
claus
parents:
diff changeset
    19
10
claus
parents: 6
diff changeset
    20
!WeakIdentityDictionary class methodsFor:'documentation'!
claus
parents: 6
diff changeset
    21
88
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    22
copyright
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    23
"
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    24
 COPYRIGHT (c) 1992 by Claus Gittinger
358
claus
parents: 159
diff changeset
    25
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    26
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    27
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    28
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    29
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    30
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    31
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    32
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    33
"
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    34
!
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    35
10
claus
parents: 6
diff changeset
    36
documentation
claus
parents: 6
diff changeset
    37
"
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    38
    WeakIdentityDictionaries behave like IdentityDictionaries, 
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    39
    as long as the contained objects are still referenced by some 
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    40
    other (non-weak) object.
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    41
    However, once the last non-weak reference ceases to exist,
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    42
    the object will be automatically removed from the Weakcollection
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    43
    (with some delay: it will be removed after the next garbage collect).
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    44
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    45
    This class was added to support keeping track of dependents without
88
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    46
    keeping the values alive - values simply become nil when no one else
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    47
    references it. The original dependency mechanism used a regular Dictionary,
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    48
    which usually leads to a lot of garbage being kept due to a forgotten
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    49
    release. Using a WeakDictionary may be incompatible to ST-80 but is much
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    50
    more comfortable, since no manual release of dependents is needed.
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    51
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    52
    [See also:]
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    53
        WeakArray WeakIdentitySet
10
claus
parents: 6
diff changeset
    54
"
claus
parents: 6
diff changeset
    55
! !
claus
parents: 6
diff changeset
    56
359
claus
parents: 358
diff changeset
    57
!WeakIdentityDictionary methodsFor:'adding & removing'!
claus
parents: 358
diff changeset
    58
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    59
at:key put:anObject
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    60
    "add the argument anObject under key, aKey to the receiver.
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    61
     Return anObject (sigh).
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    62
     Redefined to block interrupts, to avoid triuble when dependencies
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    63
     are added within interrupting high prio processes."
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    64
359
claus
parents: 358
diff changeset
    65
    |wasBlocked|
claus
parents: 358
diff changeset
    66
claus
parents: 358
diff changeset
    67
    wasBlocked := OperatingSystem blockInterrupts.
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    68
    super at:key put:anObject.
1223
09d16bc18493 at:put: returns its arg
Claus Gittinger <cg@exept.de>
parents: 1054
diff changeset
    69
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    70
    ^ anObject
1223
09d16bc18493 at:put: returns its arg
Claus Gittinger <cg@exept.de>
parents: 1054
diff changeset
    71
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    72
    "Modified: 22.4.1996 / 17:39:09 / cg"
359
claus
parents: 358
diff changeset
    73
!
claus
parents: 358
diff changeset
    74
claus
parents: 358
diff changeset
    75
removeKey:aKey ifAbsent:aBlock
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    76
    "remove the association under aKey from the collection,
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    77
     return the value previously stored there..
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    78
     If it was not in the collection return the result 
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    79
     from evaluating aBlock.
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    80
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    81
    Redefined to avoid synchronization problems, in case
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    82
    of interrupts (otherwise, there could be some other operation 
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    83
    on the receiver done by another process, which garbles my contents)."
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    84
359
claus
parents: 358
diff changeset
    85
    |wasBlocked|
claus
parents: 358
diff changeset
    86
claus
parents: 358
diff changeset
    87
    wasBlocked := OperatingSystem blockInterrupts.
claus
parents: 358
diff changeset
    88
    [
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    89
        super removeKey:aKey ifAbsent:aBlock
359
claus
parents: 358
diff changeset
    90
    ] valueNowOrOnUnwindDo:[
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    91
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
359
claus
parents: 358
diff changeset
    92
    ]
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    93
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    94
    "Modified: 22.4.1996 / 17:39:57 / cg"
359
claus
parents: 358
diff changeset
    95
! !
claus
parents: 358
diff changeset
    96
10
claus
parents: 6
diff changeset
    97
!WeakIdentityDictionary methodsFor:'element disposal'!
claus
parents: 6
diff changeset
    98
claus
parents: 6
diff changeset
    99
informDispose
claus
parents: 6
diff changeset
   100
    "an element (either key or value) died - rehash"
6
62211a9bc04d Initial revision
claus
parents:
diff changeset
   101
10
claus
parents: 6
diff changeset
   102
    | sz "{ Class:SmallInteger }" 
358
claus
parents: 159
diff changeset
   103
      any wasBlocked |
claus
parents: 159
diff changeset
   104
claus
parents: 159
diff changeset
   105
    "
claus
parents: 159
diff changeset
   106
     have to block here - dispose may be done at a low priority
claus
parents: 159
diff changeset
   107
     from the background finalizer. If new views are opened in
claus
parents: 159
diff changeset
   108
     the foreground, the dependency dictionary may get corrupted
claus
parents: 159
diff changeset
   109
     otherwise
claus
parents: 159
diff changeset
   110
    "
claus
parents: 159
diff changeset
   111
    wasBlocked := OperatingSystem blockInterrupts.
10
claus
parents: 6
diff changeset
   112
claus
parents: 6
diff changeset
   113
    sz := keyArray size.
claus
parents: 6
diff changeset
   114
    any := false.
claus
parents: 6
diff changeset
   115
    1 to:sz do:[:index |
358
claus
parents: 159
diff changeset
   116
	(keyArray at:index) isNil ifTrue:[
claus
parents: 159
diff changeset
   117
	   (valueArray at:index) notNil ifTrue:[
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   118
"/ soon to come; can then avoid rehash
359
claus
parents: 358
diff changeset
   119
"/                "
claus
parents: 358
diff changeset
   120
"/                 if the next slot is not nil, it could be there due
claus
parents: 358
diff changeset
   121
"/                 to a hash collision. In this case we have to put a 
claus
parents: 358
diff changeset
   122
"/                 DeletedMark into the slot.
claus
parents: 358
diff changeset
   123
"/                "
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   124
"/
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   125
"/                index == sz ifTrue:[
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   126
"/                    next := 1
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   127
"/                ] ifFalse:[
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   128
"/                    next := index + 1.
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   129
"/                ].
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   130
"/
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   131
"/                (keyArray basicAt:next) notNil ifTrue:[
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   132
"/                    (valueArray basicAt:next) notNil ifTrue:[
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   133
"/                        keyArray basicAt:index put:DeletedEntry
358
claus
parents: 159
diff changeset
   134
"/                  ]
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   135
"/                ].
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   136
358
claus
parents: 159
diff changeset
   137
		valueArray at:index put:nil.
claus
parents: 159
diff changeset
   138
		tally := tally - 1.
claus
parents: 159
diff changeset
   139
		any := true
claus
parents: 159
diff changeset
   140
	    ]
claus
parents: 159
diff changeset
   141
	]
10
claus
parents: 6
diff changeset
   142
    ].
358
claus
parents: 159
diff changeset
   143
    any ifTrue:[self rehash].
claus
parents: 159
diff changeset
   144
359
claus
parents: 358
diff changeset
   145
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
6
62211a9bc04d Initial revision
claus
parents:
diff changeset
   146
! !
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   147
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   148
!WeakIdentityDictionary methodsFor:'private'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   149
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   150
keyContainerOfSize:n
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   151
    "return a container for keys of size n.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   152
     use WeakArrays here."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   153
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   154
    |w|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   155
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   156
    w := WeakArray new:n.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   157
    w watcher:self.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   158
    ^ w
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   159
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   160
635
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   161
!WeakIdentityDictionary class methodsFor:'documentation'!
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   162
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   163
version
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
   164
    ^ '$Header: /cvs/stx/stx/libbasic/WeakIdentityDictionary.st,v 1.17 1996-04-23 11:56:31 cg Exp $'
635
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   165
! !