added stix_deletedic() and Dictionary>>removeKey: and its derivatives

This commit is contained in:
hyunghwan.chung
2017-01-09 09:11:36 +00:00
parent 6e335cd4fb
commit 064d02d031
5 changed files with 232 additions and 37 deletions

View File

@ -259,6 +259,16 @@ class Apex(nil)
^false
}
method notError
{
^true
}
method(#class) notError
{
^true
}
## -------------------------------------------------------
## -------------------------------------------------------
@ -320,32 +330,37 @@ class Apex(nil)
* ------------------------------------------------------------------ *)
method primitiveFailed
{
self class primitiveFailed.
}
method doesNotUnderstand: messageSymbol
{
self class doesNotUnderstand: messageSymbol
}
method index: index outOfRange: ubound
{
self class index: index outOfRange: ubound.
^self class primitiveFailed.
}
method cannotInstantiate
{
self class cannotInstantiate
^self class cannotInstantiate
}
method subclassResponsibility: message_name
method doesNotUnderstand: messageSymbol
{
self class subclassResponsibility: message_name
^self class doesNotUnderstand: messageSymbol
}
method index: index outOfRange: ubound
{
^self class index: index outOfRange: ubound.
}
method subclassResponsibility: method_name
{
^self class subclassResponsibility: method_name
}
method notImplemented: method_name
{
^self class notImplemented: method_name
}
method cannotExceptionizeError
{
self class cannotExceptionizeError
^self class cannotExceptionizeError
}
method(#class) error: msgText
@ -413,6 +428,11 @@ class Error(Apex)
^true
}
method notError
{
^false
}
method asInteger
{
<primitive: #_error_as_integer>

View File

@ -145,7 +145,7 @@ class Set(Collection)
[(ass := self.bucket at: index) notNil]
whileTrue: [
[key == ass key] ifTrue: [ (* TODO: change it to equality??? *)
(key = ass key) ifTrue: [
(* found *)
upsert ifTrue: [ass value: value].
^ass
@ -153,7 +153,7 @@ class Set(Collection)
index := (index + 1) rem: bs.
].
upsert ifFalse: [^nil].
upsert ifFalse: [^ErrorCode.NOENT].
ntally := self.tally + 1.
(ntally >= bs) ifTrue: [
@ -187,29 +187,42 @@ class Set(Collection)
{
| ass |
ass := self __find: key or_upsert: false with: nil.
(ass notNil) ifTrue: [^ass value].
^ErrorCode.NOENT
(ass isError) ifTrue: [^ass].
^ass value
}
method at: key ifAbsent: error_block
{
| ass |
ass := self __find: key or_upsert: false with: nil.
(ass notNil) ifTrue: [^ass value].
^error_block value.
(ass isError) ifTrue: [^error_block value].
^ass value
}
method associationAt: key
{
^self __find: key or_upsert: false with: nil.
}
method associationAt: key ifAbsent: error_block
{
| ass |
ass := self __find: key or_upsert: false with: nil.
(ass isError) ifTrue: [^error_block value].
^ass
}
method at: key put: value
{
self __find: key or_upsert: true with: value.
^value
(* returns the affected/inserted association *)
^self __find: key or_upsert: true with: value.
}
method includesKey: key
{
| ass |
ass := self __find: key or_upsert: false with: nil.
^ass notNil
^ass notError
}
method includesAssociation: assoc
@ -225,19 +238,99 @@ class Set(Collection)
ass := self __find: key or_upsert: false with: nil.
^ass key = key and: [ass value = value]
}
method __find_index: key
{
| bs ass index |
bs := self.bucket size.
index := (key hash) rem: bs.
[(ass := self.bucket at: index) notNil]
whileTrue: [
(key = ass key) ifTrue: [^index].
index := (index + 1) rem: bs.
].
^ErrorCode.NOENT.
}
method __remove_at: index
{
| bs x y i v |
bs := self.bucket size.
v := self.bucket basicAt: index.
x := index.
y := index.
i := 0.
[i < self.tally] whileTrue: [
| ass z |
y := (y + 1) rem: bs.
ass := self.bucket at: i.
(ass isNil)
ifTrue: [
(* done. the slot at the current index is nil *)
i := self.tally
]
ifFalse: [
(* get the natural hash index *)
z := (ass key hash) rem: bs.
(* move an element if necessary *)
((y > x and: [(z <= x) or: [z > y]]) or:
[(y < x) and: [(z <= x) and: [z > y]]]) ifTrue: [
self.bucket at: x put: (self.bucket at: y).
x := y.
].
i := i + 1
].
].
self.bucket at: x put: nil.
self.tally := self.tally - 1.
(* return the affected association *)
^v
}
method removeKey: key
{
(* TODO: *)
self subclassResponsbility: #removeKey
| index |
index := self __find_index: key.
(index isError) ifTrue: [ ^index ].
^self __remove_at: index.
}
method removeKey: key ifAbsent: error_block
{
(* TODO: *)
self subclassResponsbility: #removeKey
| index |
index := self __find_index: key.
(index isError) ifTrue: [ ^error_block value ].
^self __remove_at: index.
}
method removeAllKeys
{
(* remove all items from a dictionary *)
| bs |
bs := self.bucket size.
0 priorTo: bs do: [:i | self.bucket at: i put: nil ].
self.tally := 0
}
(* TODO: ... keys is an array of keys.
method removeAllKeys: keys
{
self notImplemented: #removeAllKeys:
}
*)
method remove: assoc
{
^self removeKey: (assoc key)
@ -248,6 +341,7 @@ self subclassResponsbility: #removeKey
^self removeKey: (assoc key) ifAbsent: error_block
}
method do: block
{
| bs |

View File

@ -351,6 +351,10 @@ thisContext isExceptionContext dump.
##============================================================================
class PrimitiveFailureException(Exception)
{
}
class InstantiationFailureException(Exception)
{
}
@ -359,10 +363,6 @@ class NoSuchMessageException(Exception)
{
}
class PrimitiveFailureException(Exception)
{
}
class IndexOutOfRangeException(Exception)
{
}
@ -371,7 +371,7 @@ class SubclassResponsibilityException(Exception)
{
}
class InvalidArgumentException(Exception)
class NotImplementedException(Exception)
{
}
@ -379,6 +379,10 @@ class ErrorExceptionizationFailureException(Exception)
{
}
class InvalidArgumentException(Exception)
{
}
extend Apex
{
method(#class) primitiveFailed
@ -414,11 +418,16 @@ ctx := thisContext.
IndexOutOfRangeException signal: 'Out of range'.
}
method(#class) subclassResponsibility: message_name
method(#class) subclassResponsibility: method_name
{
SubclassResponsibilityException signal: ('Subclass must implment ' & message_name).
SubclassResponsibilityException signal: ('Subclass must implement ' & method_name).
}
method(#class) notImplemented: method_name
{
NotImplementedException signal: (method_name & ' not implemented by ' & (self name)).
}
method(#class) cannotExceptionizeError
{
## todo: accept the object