added more methods to Dictionary.

made a simple optimization in dic.c
This commit is contained in:
hyunghwan.chung
2017-01-06 14:49:42 +00:00
parent 23d80e77aa
commit 6e335cd4fb
3 changed files with 93 additions and 28 deletions

View File

@ -127,7 +127,7 @@ class Set(Collection)
method initialize
{
self.tally := 0.
self.bucket := Array new: 100.
self.bucket := Array new: 128. (* TODO: initial size *)
}
method size
@ -135,28 +135,6 @@ class Set(Collection)
^self.tally
}
method at: key
{
| ass |
ass := self __find: key or_upsert: false with: nil.
(ass notNil) ifTrue: [^ass value].
^ErrorCode.NOENT
}
method at: key ifAbsent: error_block
{
| ass |
ass := self __find: key or_upsert: false with: nil.
(ass notNil) ifTrue: [^ass value].
^error_block value.
}
method at: key put: value
{
self __find: key or_upsert: true with: value.
^value
}
method __find: key or_upsert: upsert with: value
{
| hv ass bs index ntally |
@ -179,8 +157,23 @@ class Set(Collection)
ntally := self.tally + 1.
(ntally >= bs) ifTrue: [
| newbuc newsz |
(* expand the bucket *)
newsz := bs + 123. (* TODO: keep this growth policy in sync with VM(dic.c) *)
newbuc := Array new: newsz.
0 priorTo: bs do: [:i |
ass := self.bucket at: i.
(ass notNil) ifTrue: [
index := (ass key hash) rem: newsz.
[(newbuc at: index) notNil] whileTrue: [index := (index + 1) rem: newsz].
newbuc at: index put: ass
]
].
self.bucket := newbuc.
bs := self.bucket size.
index := hv rem: bs.
[(self.bucket at: index) notNil] whileTrue: [index := (index + 1) rem: bs ].
].
ass := Association key: key value: value.
@ -190,6 +183,71 @@ class Set(Collection)
^ass
}
method at: key
{
| ass |
ass := self __find: key or_upsert: false with: nil.
(ass notNil) ifTrue: [^ass value].
^ErrorCode.NOENT
}
method at: key ifAbsent: error_block
{
| ass |
ass := self __find: key or_upsert: false with: nil.
(ass notNil) ifTrue: [^ass value].
^error_block value.
}
method at: key put: value
{
self __find: key or_upsert: true with: value.
^value
}
method includesKey: key
{
| ass |
ass := self __find: key or_upsert: false with: nil.
^ass notNil
}
method includesAssociation: assoc
{
| ass |
ass := self __find: (assoc key) or_upsert: false with: nil.
^ass = assoc.
}
method includesKey: key value: value
{
| ass |
ass := self __find: key or_upsert: false with: nil.
^ass key = key and: [ass value = value]
}
method removeKey: key
{
(* TODO: *)
self subclassResponsbility: #removeKey
}
method removeKey: key ifAbsent: error_block
{
(* TODO: *)
self subclassResponsbility: #removeKey
}
method remove: assoc
{
^self removeKey: (assoc key)
}
method remove: assoc ifAbsent: error_block
{
^self removeKey: (assoc key) ifAbsent: error_block
}
method do: block
{
| bs |
@ -351,4 +409,4 @@ extend Apex
{
^Association new key: self value: object
}
}
}

View File

@ -10,11 +10,17 @@ class Association(Magnitude)
{
^self new key: key value: value
}
method key: key value: value
{
self.key := key.
self.value := value.
}
method value: value
{
self.value := value
}
method key
{