Skip to content

Commit

Permalink
Fix Array's - and & operators.
Browse files Browse the repository at this point in the history
  • Loading branch information
treed committed Aug 12, 2009
1 parent 5aa1105 commit 035af4a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 65 deletions.
40 changes: 0 additions & 40 deletions src/builtins/op.pir
Expand Up @@ -30,27 +30,6 @@ src/builtins/op.pir - Cardinal ops
.return ($P0)
.end

.sub 'infix:-' :multi(CardinalArray,CardinalArray)
.param pmc a
.param pmc b
$P0 = new 'CardinalArray'
.local pmc it
it = iter a
$P3 = get_hll_global['Bool'], 'False'
iter_loop:
unless it goto done
$P1 = shift it
$P2 = b.'include?'($P1)
$I0 = 'infix:=='($P2, $P3)
eq $I0, 1, appendit
#eq $P2, $P3, appendit
goto iter_loop
appendit:
$P0.'push'($P1)
goto iter_loop
done:
.return($P0)
.end

.sub 'infix:*' :multi(_,_)
.param num a
Expand Down Expand Up @@ -115,25 +94,6 @@ src/builtins/op.pir - Cardinal ops
.return ($I0)
.end

.sub 'infix:&' :multi(CardinalArray,CardinalArray)
.param pmc a
.param pmc b
.local pmc intersection
intersection = new 'CardinalArray'
.local pmc item
.local pmc it
it = iter a
loop:
unless it goto loop_end
item = shift it
$I0 = b.'include?'(item)
unless $I0, loop
intersection.'push'(item)
goto loop
loop_end:
.return (intersection)
.end

.sub 'infix:*' :multi(CardinalString,CardinalInteger)
.param pmc a
.param pmc b
Expand Down
62 changes: 38 additions & 24 deletions src/classes/Array.pir
Expand Up @@ -819,9 +819,9 @@ if passed, otherwise returns nil.
if len == i goto done

$P0 = self[i]
$I0 = block($P0)
$P1 = block($P0)

unless $I0 goto finish_loop
unless $P1 goto finish_loop

delete self[i]

Expand Down Expand Up @@ -1787,48 +1787,62 @@ Operator form for either repetition (when argument is an Integer), or as a short
.sub 'infix:&' :multi('CardinalArray','CardinalArray')
.param pmc this
.param pmc that
.local pmc array
.local pmc inter, it, incl, item

inter = new 'CardinalArray'

it = iter this

array = this + that
array.'uniq!'()
this_loop:
unless it goto this_end

item = shift it
incl = that.'include?'(item)
unless incl goto this_loop

inter.'push'(item)
goto this_loop

this_end:
it = iter that

.return (array)
that_loop:
unless it goto that_end

item = shift it
incl = this.'include?'(item)
unless incl goto that_loop

inter.'push'(item)
goto that_loop

that_end:
inter.'uniq!'()

.return (inter)
.end

.sub 'infix:-' :multi('CardinalArray','CardinalArray')
.param pmc this
.param pmc that
.local pmc array, hash, key, includes
.local pmc array, includes, elem
.local int i, len

array = new 'CardinalArray'
hash = new 'CardinalHash'
len = elements that
i = 0

hash_loop:
if i == len goto hash_done

key = that[i]
hash[key] = 1

i = i + 1
goto hash_loop

hash_done:
len = elements this
i = 0

diff_loop:
if i == len goto diff_done

key = this[i]
includes = hash.'includes?'(key)
elem = this[i]
includes = that.'include?'(elem)

i = i + 1
unless includes goto diff_loop
if includes goto diff_loop

array.'push'(key)
array.'push'(elem)

goto diff_loop

Expand Down
2 changes: 1 addition & 1 deletion t/array/mathop.t
Expand Up @@ -39,7 +39,7 @@ is d, "25 50", "Array multiplication with String"
a = [1,3,5,7,8]
b = [1,5,8,9]
c = a & b
is c, [1,5,8], "Array & (union)"
is c, [1,5,8], "Array & (intersection)"

a = [1,2] << "a" << 3 << [4,5]
is a, [1,2,"a",3,[4,5]], "Array <<"
Expand Down

0 comments on commit 035af4a

Please sign in to comment.