Skip to content

Instantly share code, notes, and snippets.

@treed
Created July 23, 2009 00:10
Show Gist options
  • Save treed/152406 to your computer and use it in GitHub Desktop.
Save treed/152406 to your computer and use it in GitHub Desktop.
.sub uniq :method
.param pmc block :optional :named("!BLOCK")
.param int block_flag :opt_flag
.local pmc uarray, pair, hash, val, key, values
.local int i, len
uarray = new 'CardinalArray'
hash = new 'CardinalHash'
i = 0
len = self.'elems'()
loop:
if i == len goto loop_done
key = self[i]
pair = new 'CardinalArray'
pair.'push'(i)
pair.'push'(key)
val = pair
unless block_flag, finish
key = block(key)
$P0 = hash.'value?'(key)
if $P0 goto skip
finish:
hash[key] = val
skip:
inc i
goto loop
loop_done:
values = hash.'values'()
values.'sort!'()
i = 0
len = values
fetch_loop:
if i == len goto done
$P0 = values[i]
$P1 = $P0[1]
uarray.'push'($P1)
inc i
goto fetch_loop
done:
.return (uarray)
.end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment