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

View File

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

View File

@ -4141,7 +4141,10 @@ static int compile_dictionary_expression (moo_t* moo)
x.ptr = msg; x.ptr = msg;
x.len = 11; 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 || if (emit_byte_instruction (moo, BCODE_DUP_STACKTOP) <= -1 ||
compile_association_expression(moo) <= -1 || compile_association_expression(moo) <= -1 ||
add_symbol_literal(moo, &x, 0, &si) <= -1 || add_symbol_literal(moo, &x, 0, &si) <= -1 ||