Remove vending machine bootup time. Generate permutation in a smarter fashion.
authorBernard Blackham <dagobah@ucc.gu.uwa.edu.au>
Fri, 6 May 2005 11:41:34 +0000 (11:41 +0000)
committerBernard Blackham <dagobah@ucc.gu.uwa.edu.au>
Fri, 6 May 2005 11:41:34 +0000 (11:41 +0000)
sql-edition/servers/Idler.py

index e56f8d7..d11fe90 100755 (executable)
@@ -93,28 +93,37 @@ class TrainIdler(Idler):
 class OrderMaker:
        def __init__(self, n=8):
                self.n = n
-               self.a = []
-               self.u = []
-               self.s = []
-               for x in range(0,n):
-                       self.u.append(False)
-               self.go()
-
-       def go(self):
-               from copy import deepcopy
-               if len(self.s) == self.n:
-                       self.a.append(deepcopy(self.s))
-               else:
-                       for x in range(0,self.n):
-                               if self.u[x]: continue
-                               self.s.append(x)
-                               self.u[x] = True
-                               self.go()
-                               self.u[x] = False
-                               self.s.pop()
-
-       def order(self):
-               return self.a
+               self.make_factorials(n)
+       
+       def make_factorials(self, n):
+               self.factorial = []
+               a = 1
+               for i in range(1,n+1):
+                       self.factorial.append(a)
+                       a *= i
+
+       def order(self, index):
+               used = []
+               for i in range(0,self.n):
+                       used.append(i)
+               i = self.n-1
+               j = 0
+               res = []
+               while i >= 0:
+                       a = index/self.factorial[i]
+                       index %= self.factorial[i]
+                       res.append(a+1)
+                       i -= 1
+                       j += 1
+               for i in range(0,self.n):
+                       tmp = used[res[i]-1]
+                       for j in range(res[i],self.n):
+                               used[j-1] = used[j]
+                       res[i] = tmp
+               return res
+
+       def __getitem__(self, i):
+               return self.order(i)
 
 class GrayIdler(Idler):
        def __init__(self, v, one=None, zero=None, reorder=0):
@@ -128,7 +137,7 @@ class GrayIdler(Idler):
                self.reorder = reorder
                global orderings
                if not orderings:
-                       orderings = OrderMaker().order()
+                       orderings = OrderMaker()
 
        def next(self):
                output = self.do_next_state()

UCC git Repository :: git.ucc.asn.au