Skip to content

Instantly share code, notes, and snippets.

@Varriount
Last active August 29, 2015 13:56
Show Gist options
  • Save Varriount/8852176 to your computer and use it in GitHub Desktop.
Save Varriount/8852176 to your computer and use it in GitHub Desktop.
Iterator bug
C:\64\Nimrod>nimrod c --run test.nim
config\nimrod.cfg(37, 2) Hint: added path: 'C:\Users\Clay\.babel\pkgs\windows-1.0' [Path]
config\nimrod.cfg(37, 2) Hint: added path: 'C:\Users\Clay\.babel\pkgs\babel-0.1.0' [Path]
config\nimrod.cfg(37, 2) Hint: added path: 'C:\Users\Clay\.babel\pkgs\' [Path]
Hint: used config file 'C:\64\nimrod\config\nimrod.cfg' [Conf]
Hint: system [Processing]
Hint: test [Processing]
Hint: os [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: times [Processing]
Hint: winlean [Processing]
Hint: algorithm [Processing]
Hint: sequtils [Processing]
lib\pure\algorithm.nim(155, 4) Hint: 'itemCount' is declared but not used [XDeclaredButNotUsed]
test.nim(4, 9) Hint: 'test.productA(groups: varargs[seq[T]]): seq[T]' is declared but not used [XDeclaredButNotUsed]
test.nim(43, 5) Hint: 'test.productB(groups: openarray[seq[T]]): seq[seq[T]]' is declared but not used [XDeclaredButNotUsed]
gcc.exe -c -w -IC:\64\nimrod\lib -o c:\64\nimrod\nimcache\nimrod_test.o c:\64\nimrod\nimcache\nimrod_test.c
gcc.exe -c -w -IC:\64\nimrod\lib -o c:\64\nimrod\nimcache\nimrod_system.o c:\64\nimrod\nimcache\nimrod_system.c
gcc.exe -c -w -IC:\64\nimrod\lib -o c:\64\nimrod\nimcache\pure_os.o c:\64\nimrod\nimcache\pure_os.c
gcc.exe -c -w -IC:\64\nimrod\lib -o c:\64\nimrod\nimcache\pure_algorithm.o c:\64\nimrod\nimcache\pure_algorithm.c
c:\64\nimrod\nimcache\nimrod_test.c: In function 'testInit':
c:\64\nimrod\nimcache\nimrod_test.c:491:102: error: expected ')' before 'Len0'
asgnRefNoCycle((void**) &re_113596, LOC4.ClPrc((*(TY113131***) (&LOC5)), (*(TY113131***) (&LOC5))Len0, 1, LOC4.ClEnv));
^
c:\64\nimrod\nimcache\nimrod_test.c:491:102: error: too few arguments to function 'LOC4.ClPrc'
Error: execution of an external program failed; rerun with --parallelBuild:1 to see the error message
when isMainModule:
var
t: float
minTime: float = 99999999
var res: seq[seq[int]]
for a in 0..2:
t = cpuTime()
for b in 0..10:
for re in productIter[int](@[@[1,2,3,4,5,6,7,8,9,10], @[1,2,3,4,5,6,7,8,9,10], @[1,2,3,4,5,6,7,8,9,10]]):
minTime = min(minTime, cpuTime()-t)
echo(minTime)
echo(repr(res))
sleep(1000)
iterator productIter*[T](groups: varargs[seq[T]], order = Ascending): seq[T]
{.closure.} =
## Produces a sequence containing the cartesian product of the input
## sequences.
## **Warning** This procedure has multiplicative complexity in time.
template orderSwitch(a, b: expr): expr =
if order == Ascending: a else: b
let groupsLen = groups.len
# Handle corner cases
if groupsLen == 0:
return
elif groupsLen == 1:
yield groups[0]
return
# Setup the representative index seqs
var
indexes: seq[int] # Current index positions we are on
initial = newSeq[int](groupsLen) # Initial index positions
resultLen = 1 # Size of the result sequence
currentIndex = groupsLen-1 # The current index in `indexes` we are changing
itemCount = 0 # How many sequences have been generated so far
nextItem: seq[T] = @[] # replace with newSeq as soon as #853 is fixed
nextItem.setLen(groupsLen)
# Determine how much memory the result will need, and allocate it.
# Also, set up the array of initial and current index values.
for i, subGroup in groups:
if len(subGroup) == 0:
return
resultLen *= len(subGroup)
initial[i] = orderSwitch(len(subGroup), len(subGroup)-1)
indexes = orderSwitch(newSeq[int](groupsLen), initial)
template indexLimit: expr = orderSwitch(initial[currentIndex], -1)
template indexReset: expr = orderSwitch(0, initial[currentIndex])
template indexShift(a): expr = orderSwitch(inc(a), dec(a))
while true:
while indexes[currentIndex] == indexLimit:
indexes[currentIndex] = indexReset
currentIndex.dec()
if currentIndex == -1:
return
indexShift(indexes[currentIndex])
for i, index in indexes:
# echo(i, ' ', index)
nextItem[i] = groups[i][index]
# echo(repr(next))
yield nextItem
currentIndex = groupsLen-1
indexShift(indexes[currentIndex])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment