Skip to content

Commit

Permalink
Merge pull request JuliaLang#33 from JuliaStats/jmw/indexmap
Browse files Browse the repository at this point in the history
[RFC] Add levelsmap
  • Loading branch information
johnmyleswhite committed Jan 15, 2014
2 parents 0efba51 + 8e91aae commit 27a851a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ This package implements various stragies for computing ranks. Please refer to [W

Inversed run-length encoding. It takes the results of ``rle`` and reconstructs the original sequence.

* **levelsmap**(x)

Construct a dictionary that maps each of the ``n`` distinct values in ``x`` to a number between ``1`` and ``n``.

* **indexmap**(x)

Construct a dictionary that maps each distinct value in ``x`` to its first index.
Expand Down
1 change: 1 addition & 0 deletions src/Stats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ module Stats
rle, # run-length encoding
inverse_rle, # inverse run-length encoding
indexmap, # construct a map from element to index
levelsmap, # construct a map from n unique elements to [1, ..., n]
findat, # find the position within a for elements in b
indicatormat, # construct indicator matrix

Expand Down
13 changes: 13 additions & 0 deletions src/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ function indexmap{T}(a::AbstractArray{T})
return d
end

function levelsmap{T}(a::AbstractArray{T})
d = (T=>Int)[]
index = 1
for i = 1 : length(a)
@inbounds k = a[i]
if !haskey(d, k)
d[k] = index
index += 1
end
end
return d
end

function findat!{T}(r::IntegerArray, a::AbstractArray{T}, b::AbstractArray{T})
length(r) == length(b) || raise_dimerror()
d = indexmap(a)
Expand Down

0 comments on commit 27a851a

Please sign in to comment.