Skip to content

Commit

Permalink
Merge pull request #4 from lopezm94/sysimg_patch
Browse files Browse the repository at this point in the history
Remove usage of Channels
  • Loading branch information
lopezm94 authored Jun 28, 2017
2 parents ea48d03 + 5a9f42f commit 2c70437
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 28 deletions.
2 changes: 2 additions & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
julia 0.4

Combinatorics
33 changes: 5 additions & 28 deletions src/Match/Match.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Extract expression sub-trees.
"""
module Match

using Combinatorics

export @match
export unkeyword!

Expand All @@ -23,32 +25,6 @@ iscommutative(op::Symbol) = _iscommutative(Val{op})
_iscommutative(::Type{Val{:(+)}}) = true
_iscommutative{T<:Val}(::Type{T}) = false

"""
Iterator of all possible permutations.
"""
permutations(r::Range) = permutations(collect(r))
function permutations{T}(v::Vector{T})
c = Channel{Vector{T}}(1)
@schedule permfactory(v, c)
c
end

function permfactory{T}(v::Vector{T}, c::Channel{Vector{T}})
stack = Vector{Tuple{Vector{T}, Vector{T}}}()
push!(stack, (Vector{T}(), v))
while !isempty(stack)
state = pop!(stack)
taken = copy(state[1])
left = copy(state[2])
isempty(left) && (put!(c,taken); continue)
for i in 1:length(left)
new_state = (push!(copy(taken), left[i]), vcat(left[1:i-1],left[i+1:end]))
push!(stack, new_state)
end
end
put!(c,Vector{T}())
end

"""
Output true if dictionary 'd' has a key 's' with a different value than 'v'.
Otherwise add value to dictionary and output false.
Expand Down Expand Up @@ -99,8 +75,9 @@ function commutative_match(offset, symbols, expr, formula)
eargs, margs = expr.args, formula.args
n = length(eargs)-offset
(!isref(expr) | match(symbols, eargs[1], margs[1])) || return false
chnl = permutations(1+offset:length(eargs))
for perm in chnl
indexes = collect(1+offset:length(eargs))
perms = permutations(indexes)
for perm in perms
isempty(perm) && break
success = true
d = copy(symbols)
Expand Down

0 comments on commit 2c70437

Please sign in to comment.