MthdDict.st
author claus
Thu, 10 Aug 1995 14:32:31 +0200
changeset 379 5b5a130ccd09
parent 328 7b542c0bf1dd
child 384 cc3d110ea879
permissions -rw-r--r--
revision added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
328
claus
parents:
diff changeset
     1
"
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1995 by Claus Gittinger
claus
parents:
diff changeset
     3
	      All Rights Reserved
claus
parents:
diff changeset
     4
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
claus
parents:
diff changeset
    10
 hereby transferred.
claus
parents:
diff changeset
    11
"
claus
parents:
diff changeset
    12
claus
parents:
diff changeset
    13
Array subclass:#MethodDictionary
claus
parents:
diff changeset
    14
       instanceVariableNames:''
claus
parents:
diff changeset
    15
       classVariableNames:''
claus
parents:
diff changeset
    16
       poolDictionaries:''
claus
parents:
diff changeset
    17
       category:'Kernel-Methods'
claus
parents:
diff changeset
    18
!
claus
parents:
diff changeset
    19
claus
parents:
diff changeset
    20
MethodDictionary comment:'
claus
parents:
diff changeset
    21
COPYRIGHT (c) 1995 by Claus Gittinger
claus
parents:
diff changeset
    22
	      All Rights Reserved
claus
parents:
diff changeset
    23
379
5b5a130ccd09 revision added
claus
parents: 328
diff changeset
    24
$Header: /cvs/stx/stx/libbasic/Attic/MthdDict.st,v 1.2 1995-08-10 12:29:28 claus Exp $
328
claus
parents:
diff changeset
    25
'!
claus
parents:
diff changeset
    26
claus
parents:
diff changeset
    27
!MethodDictionary class methodsFor:'documentation'!
claus
parents:
diff changeset
    28
claus
parents:
diff changeset
    29
copyright
claus
parents:
diff changeset
    30
"
claus
parents:
diff changeset
    31
 COPYRIGHT (c) 1995 by Claus Gittinger
claus
parents:
diff changeset
    32
	      All Rights Reserved
claus
parents:
diff changeset
    33
claus
parents:
diff changeset
    34
 This software is furnished under a license and may be used
claus
parents:
diff changeset
    35
 only in accordance with the terms of that license and with the
claus
parents:
diff changeset
    36
 inclusion of the above copyright notice.   This software may not
claus
parents:
diff changeset
    37
 be provided or otherwise made available to, or used by, any
claus
parents:
diff changeset
    38
 other person.  No title to or ownership of the software is
claus
parents:
diff changeset
    39
 hereby transferred.
claus
parents:
diff changeset
    40
"
claus
parents:
diff changeset
    41
!
claus
parents:
diff changeset
    42
claus
parents:
diff changeset
    43
version
claus
parents:
diff changeset
    44
"
379
5b5a130ccd09 revision added
claus
parents: 328
diff changeset
    45
$Header: /cvs/stx/stx/libbasic/Attic/MthdDict.st,v 1.2 1995-08-10 12:29:28 claus Exp $
5b5a130ccd09 revision added
claus
parents: 328
diff changeset
    46
$Revision: 1.2 $
328
claus
parents:
diff changeset
    47
"
claus
parents:
diff changeset
    48
!
claus
parents:
diff changeset
    49
claus
parents:
diff changeset
    50
documentation
claus
parents:
diff changeset
    51
"
claus
parents:
diff changeset
    52
    Instances of MethodDictionary store selector/method associations
claus
parents:
diff changeset
    53
    in classes. Conceptionally, they behave like Dictionaries, but are
claus
parents:
diff changeset
    54
    implemented using a single array (instead of dictionary, which uses
claus
parents:
diff changeset
    55
    two arrays to store keys and values separately).
claus
parents:
diff changeset
    56
    Also, they do not use hashing, since due to caching in the VM, hashing
claus
parents:
diff changeset
    57
    does not make too much of a difference in speed, but complicates the 
claus
parents:
diff changeset
    58
    VM implementations.
claus
parents:
diff changeset
    59
"
claus
parents:
diff changeset
    60
! !
claus
parents:
diff changeset
    61
claus
parents:
diff changeset
    62
!MethodDictionary class methodsFor:'queries'!
claus
parents:
diff changeset
    63
claus
parents:
diff changeset
    64
isBuiltInClass
claus
parents:
diff changeset
    65
    "this class is known by the run-time-system"
claus
parents:
diff changeset
    66
claus
parents:
diff changeset
    67
    ^ self == MethodDictionary
claus
parents:
diff changeset
    68
! !
claus
parents:
diff changeset
    69
claus
parents:
diff changeset
    70
!MethodDictionary class methodsFor:'instance creation'!
claus
parents:
diff changeset
    71
claus
parents:
diff changeset
    72
new:sz 
claus
parents:
diff changeset
    73
    ^ self basicNew:(sz * 2)
claus
parents:
diff changeset
    74
! !
claus
parents:
diff changeset
    75
claus
parents:
diff changeset
    76
!MethodDictionary methodsFor:'accessing'!
claus
parents:
diff changeset
    77
claus
parents:
diff changeset
    78
size
claus
parents:
diff changeset
    79
    "return the number of elements (associations) in the receiver"
claus
parents:
diff changeset
    80
claus
parents:
diff changeset
    81
    ^ self basicSize // 2
claus
parents:
diff changeset
    82
!
claus
parents:
diff changeset
    83
claus
parents:
diff changeset
    84
at:key 
claus
parents:
diff changeset
    85
    "return the value for a given key, which is supposed to be a symbol"
claus
parents:
diff changeset
    86
claus
parents:
diff changeset
    87
    |sz "{ Class: SmallInteger }"|
claus
parents:
diff changeset
    88
claus
parents:
diff changeset
    89
    sz := self basicSize.
claus
parents:
diff changeset
    90
    1 to:sz by:2 do:[:i |
claus
parents:
diff changeset
    91
	(self basicAt:i) == key ifTrue:[
claus
parents:
diff changeset
    92
	    ^ self basicAt:(i + 1)
claus
parents:
diff changeset
    93
	]
claus
parents:
diff changeset
    94
    ].
claus
parents:
diff changeset
    95
    ^ self errorKeyNotFound
claus
parents:
diff changeset
    96
!
claus
parents:
diff changeset
    97
claus
parents:
diff changeset
    98
at:key ifAbsent:exceptionBlock
claus
parents:
diff changeset
    99
    "return the element indexed by aKey - 
claus
parents:
diff changeset
   100
     return result of exceptionBlock if no element is stored under aKey"
claus
parents:
diff changeset
   101
claus
parents:
diff changeset
   102
    |sz "{ Class: SmallInteger }"|
claus
parents:
diff changeset
   103
claus
parents:
diff changeset
   104
    sz := self basicSize.
claus
parents:
diff changeset
   105
    1 to:sz by:2 do:[:i |
claus
parents:
diff changeset
   106
	(self basicAt:i) == key ifTrue:[
claus
parents:
diff changeset
   107
	    ^ self basicAt:(i + 1)
claus
parents:
diff changeset
   108
	]
claus
parents:
diff changeset
   109
    ].
claus
parents:
diff changeset
   110
    ^ exceptionBlock value
claus
parents:
diff changeset
   111
!
claus
parents:
diff changeset
   112
claus
parents:
diff changeset
   113
associationAt:key 
claus
parents:
diff changeset
   114
    "return an association consisting of aKey and the element indexed 
claus
parents:
diff changeset
   115
     by aKey - 
claus
parents:
diff changeset
   116
     report an error, if no element is stored under aKey."
claus
parents:
diff changeset
   117
claus
parents:
diff changeset
   118
    |sz "{ Class: SmallInteger }"|
claus
parents:
diff changeset
   119
claus
parents:
diff changeset
   120
    sz := self basicSize.
claus
parents:
diff changeset
   121
    1 to:sz by:2 do:[:i |
claus
parents:
diff changeset
   122
	(self basicAt:i) == key ifTrue:[
claus
parents:
diff changeset
   123
	    ^ key -> (self basicAt:(i + 1))
claus
parents:
diff changeset
   124
	]
claus
parents:
diff changeset
   125
    ].
claus
parents:
diff changeset
   126
    ^ self errorKeyNotFound
claus
parents:
diff changeset
   127
!
claus
parents:
diff changeset
   128
claus
parents:
diff changeset
   129
at:key put:value 
claus
parents:
diff changeset
   130
    "set the value for a given key, which is supposed to be a symbol.
claus
parents:
diff changeset
   131
     In contrast to dictionaries, we do not allow adding elements here."
claus
parents:
diff changeset
   132
claus
parents:
diff changeset
   133
    |sz "{ Class: SmallInteger }"|
claus
parents:
diff changeset
   134
claus
parents:
diff changeset
   135
    sz := self basicSize.
claus
parents:
diff changeset
   136
    1 to:sz by:2 do:[:i |
claus
parents:
diff changeset
   137
	(self basicAt:i) == key ifTrue:[
claus
parents:
diff changeset
   138
	    ^ self basicAt:(i + 1) put:value
claus
parents:
diff changeset
   139
	]
claus
parents:
diff changeset
   140
    ].
claus
parents:
diff changeset
   141
    ^ self errorKeyNotFound
claus
parents:
diff changeset
   142
! !
claus
parents:
diff changeset
   143
claus
parents:
diff changeset
   144
!MethodDictionary methodsFor:'queries'!
claus
parents:
diff changeset
   145
claus
parents:
diff changeset
   146
keys
claus
parents:
diff changeset
   147
    "return a collection containing all keys of the receiver"
claus
parents:
diff changeset
   148
claus
parents:
diff changeset
   149
    ^ (1 to:self basicSize by:2) collect:[:index | self basicAt:index].
claus
parents:
diff changeset
   150
!
claus
parents:
diff changeset
   151
claus
parents:
diff changeset
   152
claus
parents:
diff changeset
   153
!MethodDictionary methodsFor:'enumeration'!
claus
parents:
diff changeset
   154
claus
parents:
diff changeset
   155
do:aBlock
claus
parents:
diff changeset
   156
    "evaluate the argument, aBlock for each element in the collection."
claus
parents:
diff changeset
   157
claus
parents:
diff changeset
   158
    |sz "{ Class: SmallInteger }"|
claus
parents:
diff changeset
   159
claus
parents:
diff changeset
   160
    sz := self basicSize.
claus
parents:
diff changeset
   161
    1 to:sz by:2 do:[:i |
claus
parents:
diff changeset
   162
	aBlock value:(self basicAt:i)
claus
parents:
diff changeset
   163
    ].
claus
parents:
diff changeset
   164
!
claus
parents:
diff changeset
   165
claus
parents:
diff changeset
   166
keysAndValuesDo:aBlock
claus
parents:
diff changeset
   167
    "evaluate the argument, aBlock for each element in the collection.
claus
parents:
diff changeset
   168
     Pass both index and element to the block."
claus
parents:
diff changeset
   169
claus
parents:
diff changeset
   170
    |sz "{ Class: SmallInteger }"|
claus
parents:
diff changeset
   171
claus
parents:
diff changeset
   172
    sz := self basicSize.
claus
parents:
diff changeset
   173
    1 to:sz by:2 do:[:i |
claus
parents:
diff changeset
   174
	aBlock value:(self basicAt:i) value:(self basicAt:(i + 1))
claus
parents:
diff changeset
   175
    ].
claus
parents:
diff changeset
   176
! !