From 81da4cd39d67db53ea95c6ddb5855e050a3f4391 Mon Sep 17 00:00:00 2001 From: Bernard Blackham Date: Fri, 6 May 2005 11:41:34 +0000 Subject: [PATCH] Remove vending machine bootup time. Generate permutation in a smarter fashion. --- sql-edition/servers/Idler.py | 55 +++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/sql-edition/servers/Idler.py b/sql-edition/servers/Idler.py index e56f8d7..d11fe90 100755 --- a/sql-edition/servers/Idler.py +++ b/sql-edition/servers/Idler.py @@ -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() -- 2.20.1