WeakIdentityDictionary.st
author Claus Gittinger <cg@exept.de>
Thu, 25 Apr 1996 18:20:46 +0200
changeset 1290 15ba3221b89b
parent 1263 92c1db6b0776
child 1343 b0ee705c63e0
permissions -rw-r--r--
documentation
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
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
    52
    [author:]
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
    53
        Claus Gittinger
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
    54
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    55
    [See also:]
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    56
        WeakArray WeakIdentitySet
10
claus
parents: 6
diff changeset
    57
"
claus
parents: 6
diff changeset
    58
! !
claus
parents: 6
diff changeset
    59
359
claus
parents: 358
diff changeset
    60
!WeakIdentityDictionary methodsFor:'adding & removing'!
claus
parents: 358
diff changeset
    61
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    62
at:key put:anObject
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    63
    "add the argument anObject under key, aKey to the receiver.
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    64
     Return anObject (sigh).
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    65
     Redefined to block interrupts, to avoid triuble when dependencies
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    66
     are added within interrupting high prio processes."
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    67
359
claus
parents: 358
diff changeset
    68
    |wasBlocked|
claus
parents: 358
diff changeset
    69
claus
parents: 358
diff changeset
    70
    wasBlocked := OperatingSystem blockInterrupts.
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    71
    super at:key put:anObject.
1223
09d16bc18493 at:put: returns its arg
Claus Gittinger <cg@exept.de>
parents: 1054
diff changeset
    72
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    73
    ^ anObject
1223
09d16bc18493 at:put: returns its arg
Claus Gittinger <cg@exept.de>
parents: 1054
diff changeset
    74
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    75
    "Modified: 22.4.1996 / 17:39:09 / cg"
359
claus
parents: 358
diff changeset
    76
!
claus
parents: 358
diff changeset
    77
claus
parents: 358
diff changeset
    78
removeKey:aKey ifAbsent:aBlock
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    79
    "remove the association under aKey from the collection,
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    80
     return the value previously stored there..
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    81
     If it was not in the collection return the result 
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    82
     from evaluating aBlock.
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    83
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    84
    Redefined to avoid synchronization problems, in case
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    85
    of interrupts (otherwise, there could be some other operation 
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    86
    on the receiver done by another process, which garbles my contents)."
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    87
359
claus
parents: 358
diff changeset
    88
    |wasBlocked|
claus
parents: 358
diff changeset
    89
claus
parents: 358
diff changeset
    90
    wasBlocked := OperatingSystem blockInterrupts.
claus
parents: 358
diff changeset
    91
    [
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    92
        super removeKey:aKey ifAbsent:aBlock
359
claus
parents: 358
diff changeset
    93
    ] valueNowOrOnUnwindDo:[
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    94
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
359
claus
parents: 358
diff changeset
    95
    ]
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    96
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    97
    "Modified: 22.4.1996 / 17:39:57 / cg"
359
claus
parents: 358
diff changeset
    98
! !
claus
parents: 358
diff changeset
    99
10
claus
parents: 6
diff changeset
   100
!WeakIdentityDictionary methodsFor:'element disposal'!
claus
parents: 6
diff changeset
   101
claus
parents: 6
diff changeset
   102
informDispose
claus
parents: 6
diff changeset
   103
    "an element (either key or value) died - rehash"
6
62211a9bc04d Initial revision
claus
parents:
diff changeset
   104
10
claus
parents: 6
diff changeset
   105
    | sz "{ Class:SmallInteger }" 
358
claus
parents: 159
diff changeset
   106
      any wasBlocked |
claus
parents: 159
diff changeset
   107
claus
parents: 159
diff changeset
   108
    "
claus
parents: 159
diff changeset
   109
     have to block here - dispose may be done at a low priority
claus
parents: 159
diff changeset
   110
     from the background finalizer. If new views are opened in
claus
parents: 159
diff changeset
   111
     the foreground, the dependency dictionary may get corrupted
claus
parents: 159
diff changeset
   112
     otherwise
claus
parents: 159
diff changeset
   113
    "
claus
parents: 159
diff changeset
   114
    wasBlocked := OperatingSystem blockInterrupts.
10
claus
parents: 6
diff changeset
   115
claus
parents: 6
diff changeset
   116
    sz := keyArray size.
claus
parents: 6
diff changeset
   117
    any := false.
claus
parents: 6
diff changeset
   118
    1 to:sz do:[:index |
358
claus
parents: 159
diff changeset
   119
	(keyArray at:index) isNil ifTrue:[
claus
parents: 159
diff changeset
   120
	   (valueArray at:index) notNil ifTrue:[
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   121
"/ soon to come; can then avoid rehash
359
claus
parents: 358
diff changeset
   122
"/                "
claus
parents: 358
diff changeset
   123
"/                 if the next slot is not nil, it could be there due
claus
parents: 358
diff changeset
   124
"/                 to a hash collision. In this case we have to put a 
claus
parents: 358
diff changeset
   125
"/                 DeletedMark into the slot.
claus
parents: 358
diff changeset
   126
"/                "
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   127
"/
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   128
"/                index == sz ifTrue:[
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   129
"/                    next := 1
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   130
"/                ] ifFalse:[
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   131
"/                    next := index + 1.
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   132
"/                ].
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   133
"/
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   134
"/                (keyArray basicAt:next) notNil ifTrue:[
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   135
"/                    (valueArray basicAt:next) notNil ifTrue:[
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   136
"/                        keyArray basicAt:index put:DeletedEntry
358
claus
parents: 159
diff changeset
   137
"/                  ]
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   138
"/                ].
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   139
358
claus
parents: 159
diff changeset
   140
		valueArray at:index put:nil.
claus
parents: 159
diff changeset
   141
		tally := tally - 1.
claus
parents: 159
diff changeset
   142
		any := true
claus
parents: 159
diff changeset
   143
	    ]
claus
parents: 159
diff changeset
   144
	]
10
claus
parents: 6
diff changeset
   145
    ].
358
claus
parents: 159
diff changeset
   146
    any ifTrue:[self rehash].
claus
parents: 159
diff changeset
   147
359
claus
parents: 358
diff changeset
   148
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
6
62211a9bc04d Initial revision
claus
parents:
diff changeset
   149
! !
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   150
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   151
!WeakIdentityDictionary methodsFor:'private'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   152
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   153
keyContainerOfSize:n
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   154
    "return a container for keys of size n.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   155
     use WeakArrays here."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   156
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   157
    |w|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   158
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   159
    w := WeakArray new:n.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   160
    w watcher:self.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   161
    ^ w
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
635
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   164
!WeakIdentityDictionary class methodsFor:'documentation'!
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   165
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   166
version
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
   167
    ^ '$Header: /cvs/stx/stx/libbasic/WeakIdentityDictionary.st,v 1.18 1996-04-25 16:18:46 cg Exp $'
635
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   168
! !