rewrote a few methods to use if/while instead of ifTrue:/whileTrue: in Collect.moo

This commit is contained in:
hyunghwan.chung 2017-02-07 18:09:07 +00:00
parent 5c07a48c5a
commit a05793a66d
3 changed files with 75 additions and 64 deletions

View File

@ -80,11 +80,17 @@ class(#character) Symbol(String)
(* TODO: make this a primitive for performance *)
(* convert a symbol to a string *)
| size str |
| size str i |
size := self basicSize.
str := String basicNew: size.
0 priorTo: size do: [:i | str at: i put: (self at: i) ].
##0 priorTo: size do: [:i | str at: i put: (self at: i) ].
i := 0.
while (i < size)
{
str at: i put: (self at: i).
i := i + 1
}.
^str.
}
@ -136,7 +142,7 @@ class Set(Collection)
method initialize: size
{
(size <= 0) ifTrue: [size := 2].
if (size <= 0) { size := 2 }.
self.tally := 0.
self.bucket := Array new: size.
}
@ -155,11 +161,12 @@ class Set(Collection)
newbuc := Array new: newsz.
0 priorTo: bs do: [:i |
ass := self.bucket at: i.
(ass notNil) ifTrue: [
if (ass notNil)
{
index := (ass key hash) rem: newsz.
[(newbuc at: index) notNil] whileTrue: [index := (index + 1) rem: newsz].
while ((newbuc at: index) notNil) { index := (index + 1) rem: newsz }.
newbuc at: index put: ass
]
}.
].
^newbuc.
@ -174,15 +181,16 @@ class Set(Collection)
hv := key hash.
index := hv rem: bs.
[(ass := self.bucket at: index) notNil]
whileTrue: [
(key = ass key) ifTrue: [
(* found *)
upsert ifTrue: [ass value: value].
^ass
].
index := (index + 1) rem: bs.
].
while ((ass := self.bucket at: index) notNil)
{
if (key = ass key)
{
(* found *)
if (upsert) { ass value: value }.
^ass
}.
index := (index + 1) rem: bs.
}.
##upsert ifFalse: [^ErrorCode.NOENT].
if (upsert) {} else { ^ErrorCode.NOENT }.
@ -193,7 +201,7 @@ class Set(Collection)
self.bucket := self __make_expanded_bucket: bs.
bs := self.bucket size.
index := hv rem: bs.
[(self.bucket at: index) notNil] whileTrue: [index := (index + 1) rem: bs ].
while ((self.bucket at: index) notNil) { index := (index + 1) rem: bs }.
}.
ass := Association key: key value: value.
@ -215,16 +223,16 @@ class Set(Collection)
hv := key hash.
index := hv rem: bs.
[(ass := self.bucket at: index) notNil]
whileTrue: [
(key = ass key) ifTrue: [
(* found *)
self.bucket at: index put: assoc.
##^assoc.
^self.
].
index := (index + 1) rem: bs.
].
while ((ass := self.bucket at: index) notNil)
{
if (key = ass key)
{
(* found *)
self.bucket at: index put: assoc.
^self. ## it must return self for the instructions generated by the compiler.
}.
index := (index + 1) rem: bs.
}.
(* not found *)
ntally := self.tally + 1.
@ -233,21 +241,20 @@ class Set(Collection)
self.bucket := self __make_expanded_bucket: bs.
bs := self.bucket size.
index := hv rem: bs.
[(self.bucket at: index) notNil] whileTrue: [index := (index + 1) rem: bs ].
while ((self.bucket at: index) notNil) { index := (index + 1) rem: bs }.
}.
self.tally := ntally.
self.bucket at: index put: assoc.
##^assoc.
^self.
^self. ## it must return self for the instructions generated by the compiler.
}
method at: key
{
| ass |
ass := self __find: key or_upsert: false with: nil.
(ass isError) ifTrue: [^ass].
if (ass isError) { ^ass }.
^ass value
}
@ -255,7 +262,7 @@ class Set(Collection)
{
| ass |
ass := self __find: key or_upsert: false with: nil.
(ass isError) ifTrue: [^error_block value].
if (ass isError) { ^error_block value }.
^ass value
}
@ -268,7 +275,7 @@ class Set(Collection)
{
| ass |
ass := self __find: (assoc key) or_upsert: false with: nil.
(ass isError) ifTrue: [^error_block value].
if (ass isError) { ^error_block value }.
^ass
}
@ -306,11 +313,11 @@ class Set(Collection)
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.
].
while ((ass := self.bucket at: index) notNil)
{
if (key = ass key) { ^index }.
index := (index + 1) rem: bs.
}.
^ErrorCode.NOENT.
}
@ -331,24 +338,25 @@ class Set(Collection)
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.
if (ass isNil)
{
(* done. the slot at the current index is nil *)
i := self.tally
}
else
{
(* 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.
].
(* move an element if necessary *)
if ((y > x and: [(z <= x) or: [z > y]]) or: [(y < x) and: [(z <= x) and: [z > y]]])
{
self.bucket at: x put: (self.bucket at: y).
x := y.
}.
i := i + 1
].
i := i + 1
}.
].
self.bucket at: x put: nil.
@ -362,7 +370,7 @@ class Set(Collection)
{
| index |
index := self __find_index: key.
(index isError) ifTrue: [ ^index ].
if (index isError) { ^index }.
^self __remove_at: index.
}
@ -370,11 +378,10 @@ class Set(Collection)
{
| index |
index := self __find_index: key.
(index isError) ifTrue: [ ^error_block value ].
if (index isError) { ^error_block value }.
^self __remove_at: index.
}
method removeAllKeys
{
(* remove all items from a dictionary *)
@ -408,7 +415,7 @@ class Set(Collection)
bs := self.bucket size.
0 priorTo: bs by: 1 do: [:i |
| ass |
(ass := self.bucket at: i) notNil ifTrue: [block value: ass value]
if ((ass := self.bucket at: i) notNil) { block value: ass value }.
].
}
@ -418,7 +425,7 @@ class Set(Collection)
bs := self.bucket size.
0 priorTo: bs by: 1 do: [:i |
| ass |
(ass := self.bucket at: i) notNil ifTrue: [block value: ass key]
if ((ass := self.bucket at: i) notNil) { block value: ass key }.
].
}
@ -428,7 +435,7 @@ class Set(Collection)
bs := self.bucket size.
0 priorTo: bs by: 1 do: [:i |
| ass |
(ass := self.bucket at: i) notNil ifTrue: [block value: ass key value: ass value]
if ((ass := self.bucket at: i) notNil) { block value: ass key value: ass value }.
].
}
}
@ -523,13 +530,13 @@ class SystemDictionary(Dictionary)
method at: key
{
(key class ~= Symbol) ifTrue: [InvalidArgumentException signal: 'key is not a symbol'].
if (key class ~= Symbol) { InvalidArgumentException signal: 'key is not a symbol' }.
^super at: key.
}
method at: key put: value
{
(key class ~= Symbol) ifTrue: [InvalidArgumentException signal: 'key is not a symbol'].
if (key class ~= Symbol) { InvalidArgumentException signal: 'key is not a symbol' }.
^super at: key put: value
}
}

View File

@ -308,6 +308,7 @@ class MyObject(Object)
(*a removeKey: 'bbb'.
a remove: :(#bbb).*)
1 to: 100 do: [ :i | a at: i put: (i * 2) ].
a keysAndValuesDo: [:k :v |
k dump.
v dump.

View File

@ -4141,7 +4141,10 @@ static int compile_dictionary_expression (moo_t* moo)
x.ptr = msg;
x.len = 11;
/* if the method returns self, i don't need DUP_STACKTOP and POP_STACKTOP
/* [ATTENTION]
* if the method returns self, i don't need DUP_STACKTOP and POP_STACKTOP.
* if the method retruns something else, DUP_STACKTOP and POP_STACKTOP is needed
* to emulate message cascading.
if (emit_byte_instruction (moo, BCODE_DUP_STACKTOP) <= -1 ||
compile_association_expression(moo) <= -1 ||
add_symbol_literal(moo, &x, 0, &si) <= -1 ||