Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix Array.uniq to use a hash.
  • Loading branch information
treed committed Jul 13, 2009
1 parent 5261d92 commit 430ad39
Showing 1 changed file with 12 additions and 31 deletions.
43 changes: 12 additions & 31 deletions src/classes/Array.pir
Expand Up @@ -231,49 +231,30 @@ Return a sorted copy of the list
=item uniq(...)

=cut

# TODO Rewrite it. It's too naive.
# TODO: Accept a block.

.sub uniq :method
.local pmc ulist
.local pmc key
.local pmc val
.local pmc uval
.local int len
.local int i
.local int ulen
.local int ui
.local pmc uarray, hash, val
.local int i, len

uarray = new 'CardinalArray'
hash = new 'CardinalHash'

ulist = new 'CardinalArray'
len = self.'elems'()
i = 0
len = self.'elems'()

loop:
if i == len goto done

val = self[i]
hash[val] = 0

ui = 0
ulen = ulist.'elems'()
inner_loop:
if ui == ulen goto inner_loop_done

uval = ulist[ui]
if uval == val goto found

inc ui
goto inner_loop
inner_loop_done:

ulist.'push'(val)

found:

inc i
inc i
goto loop

done:
.return(ulist)
uarray = hash.'keys'()

.return (uarray)
.end

.sub 'uniq!' :method
Expand Down

0 comments on commit 430ad39

Please sign in to comment.