1
0
mirror of https://github.com/kalmarek/Groups.jl.git synced 2024-11-19 06:30:29 +01:00

clean-up findfirst, findnext

This commit is contained in:
kalmarek 2018-03-27 21:48:02 +02:00
parent 3a6506ae40
commit fc879f068b

View File

@ -289,45 +289,36 @@ end
issubsymbol(s::GSymbol, t::GSymbol) = issubsymbol(s::GSymbol, t::GSymbol) =
s.str == t.str && (0 s.pow t.pow || 0 s.pow t.pow) s.str == t.str && (0 s.pow t.pow || 0 s.pow t.pow)
function findfirst(W::GWord, Z::GWord) """doc
Find the first linear index k>=i such that Z < W.symbols[k:k+length(Z)-1]
"""
function findnext(W::GWord, Z::GWord, i::Int)
n = length(Z.symbols) n = length(Z.symbols)
if n == 0 if n == 0
return 0 return 0
elseif n == 1 elseif n == 1
for (i,s) in enumerate(W.symbols) for idx in i:endof(W.symbols)
if is_subsymbol(Z.symbols[1], s) if issubsymbol(Z.symbols[1], W.symbols[idx])
return i return idx
end end
end end
return 0 return 0
else else
for (idx,a) in enumerate(W.symbols) for idx in i:endof(W.symbols) - n + 1
if idx + n - 1 > length(W.symbols) foundfirst = issubsymbol(Z.symbols[1], W.symbols[idx])
break lastmatch = issubsymbol(Z.symbols[end], W.symbols[idx+n-1])
end if foundfirst && lastmatch
foundfirst = is_subsymbol(Z.symbols[1], a) # middles match:
if foundfirst if view(Z.symbols, 2:n-1) == view(W.symbols, idx+1:idx+n-2)
middlematch = W.symbols[idx+1:idx+n-2] == Z.symbols[2:end-1]
lastmatch = is_subsymbol(Z.symbols[end], W.symbols[idx+n-1])
if middlematch && lastmatch
return idx return idx
end end
end end
end end
end end
return 0 return 0
end end
function findnext(W::GWord, Z::GWord, i::Integer) findfirst(W::GWord, Z::GWord) = findnext(W, Z, 1)
t = findfirst(GWord{eltype(W.symbols)}(W.symbols[i:end]), Z)
if t > 0
return t+i-1
else
return 0
end
end
function replace!(W::GWord, index, toreplace::GWord, replacement::GWord; check=true) function replace!(W::GWord, index, toreplace::GWord, replacement::GWord; check=true)
n = length(toreplace.symbols) n = length(toreplace.symbols)