From f5035ed62dd99c2003a5e2a3fc647703eaeb439c Mon Sep 17 00:00:00 2001 From: Sei Lisa Date: Sun, 28 Apr 2024 11:06:59 +0200 Subject: [PATCH] Join InternalListFindList loops into one --- lslopt/lslbasefuncs.py | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/lslopt/lslbasefuncs.py b/lslopt/lslbasefuncs.py index 0239490..30e23f8 100644 --- a/lslopt/lslbasefuncs.py +++ b/lslopt/lslbasefuncs.py @@ -744,28 +744,20 @@ def InternalListFindList(lst, elems, start, end, stride, instance): return 0 if stride < 1: return -1 # stride 0 or negative returns -1 - if instance >= 0: - # Forward search - for i in xrange(start, end+2-L2, stride): - for j in xrange(L2): - if not InternalCompareElems(lst[i+j], elems[j]): - break # mismatch - else: - # no mismatch - if instance == 0: - return i - instance -= 1 - else: - # Backward search - for i in xrange(end+1-L2, start-1, -stride): - for j in xrange(L2): - if not InternalCompareElems(lst[i+j], elems[j]): - break # mismatch - else: - # no mismatch + range = xrange(start, end+2-L2, stride) if instance >= 0 else xrange( + end+1-L2, start-1, -stride) + for i in range: + for j in xrange(L2): + if not InternalCompareElems(lst[i+j], elems[j]): + break # mismatch + else: + # no mismatch + if instance < 0: instance += 1 - if instance == 0: - return i + if instance == 0: + return i + if instance > 0: + instance -= 1 return -1 def reduce(t):