def profile(): import time int_alphabet = ['0', '1'] call_alphabet = ['<0', '<1'] ret_alphabet = ['0>', '1>'] Q = ['q0', 'q1'] P = ['p0', 'p1'] q0 = 'q0' Qf = Q calls = [('q0', '<1', 'q0', 'p0'), ('q0', '<0', 'q1', 'p0'), ('q1', '<1', 'q0', 'p1'), ('q1', '<0', 'q1', 'p1')] interns = [('q0', '0', 'q1'), ('q0', '1', 'q0'), ('q1', '0', 'q0'), ('q1', '1', 'q1')] returns = [('q0', 'p0', '1>', 'q0'), ('q0', 'p1', '1>', 'q1'), ('q1', 'p0', '0>', 'q0'), ('q1', 'p1', '0>', 'q1')] vpa = DVPA.build(call_alphabet=call_alphabet, int_alphabet=int_alphabet, ret_alphabet=ret_alphabet, states=Q, start_state=q0, accept_states=Qf, stack_alphabet=P, call_transitions=calls, int_transitions=interns, ret_transitions=returns) s1 = "1.<0.1.<1.0.0.1>.0>.0.1.1.1".split('.') * 1000000 stackmap = mapping() vpa.rename(stack_func=stackmap) t = time.time() vpa.membership(s1) t = time.time() - t print "finished ", (len(s1) / 1000000.0) / t, "MB/s"
def rename(self, state_func=None, alp_func=None, stack_func=None): # states first if state_func is None: state_func = mapping() start = state_func(self.start) states = {state_func(s) for s in self.states} accept = {state_func(s) for s in self.accept} # alphabet x3 alp = None if alp_func is None: # identity alp = self.alphabet alp_func = (lambda x: x, lambda x: x, lambda x: x) else: alp = ({alp_func[0](a) for a in self.alphabet[0] }, {alp_func[1](a) for a in self.alphabet[1]}, {alp_func[2](a) for a in self.alphabet[2]}) # stack alphabet stack = None if stack_func is None: stack = self.stack stack_func = lambda x: x else: stack = {stack_func(gamma) for gamma in self.stack} newsink = None if self.sink is not None: newsink = state_func(self.sink) rand_stack_item = iter(self.stack).next() tr = (collections.defaultdict(lambda: (newsink, rand_stack_item)), collections.defaultdict(lambda: newsink), collections.defaultdict(lambda: newsink)) for q, a, qn, gamma in self.itercalls(): if qn != self.sink: tr[0][state_func(q), alp_func[0](a)] = (state_func(qn), stack_func(gamma)) for q, a, qn in self.iterinterns(): if qn != self.sink: tr[1][state_func(q), alp_func[1](a)] = state_func(qn) for q, gamma, a, qn in self.iterreturns(): if qn != self.sink: tr[2][state_func(q), stack_func(gamma), alp_func[2](a)] = \ state_func(qn) delta_c = lambda q, a: tr[0][q, a] delta_i = lambda q, a: tr[1][q, a] delta_r = lambda q, gamma, a: tr[2][q, gamma, a] delta = (delta_c, delta_i, delta_r) return DVPA(alp, states, start, accept, stack, delta, tr, newsink)
def rename(self, state_func=None, alp_func=None): if state_func is None: state_func = mapping() if alp_func is None: # identity alp_func = lambda x: x start = {state_func(s) for s in self.start} states = {state_func(s) for s in self.states} accept = {state_func(s) for s in self.accept} reject = {state_func(s) for s in self.reject} tr = collections.defaultdict(set) for s, a, ns in self.itertransitions(): tr[state_func(s), alp_func(a)].add(state_func(ns)) delta = lambda s, a: tr.get((s, a), empty_set) return NFA(self.alphabet, states, start, accept, reject, delta, tr)
def rename(self, state_func=None, alp_func=None, stack_func=None): # states first if state_func is None: state_func = mapping() start = {state_func(s) for s in self.start} states = {state_func(s) for s in self.states} accept = {state_func(s) for s in self.accept} # alphabet x3 alp = None if alp_func is None: # identity alp = self.alphabet alp_func = (lambda x: x, lambda x: x, lambda x: x) else: alp = ({alp_func[0](a) for a in self.alphabet[0] }, {alp_func[1](a) for a in self.alphabet[1]}, {alp_func[2](a) for a in self.alphabet[2]}) # stack alphabet stack = None if stack_func is None: stack = self.stack stack_func = lambda x: x else: stack = {stack_func(gamma) for gamma in self.stack} tr = (collections.defaultdict(set), collections.defaultdict(set), collections.defaultdict(set)) for q, a, qn, gamma in self.itercalls(): tr[0][state_func(q), alp_func[0](a)].add( (state_func(qn), stack_func(gamma))) for q, a, qn in self.iterinterns(): tr[1][state_func(q), alp_func[1](a)].add(state_func(qn)) for q, gamma, a, qn in self.iterreturns(): tr[2][state_func(q), stack_func(gamma), alp_func[2](a)].add(\ state_func(qn)) delta_c = lambda q, a: tr[0][q, a] delta_i = lambda q, a: tr[1][q, a] delta_r = lambda q, gamma, a: tr[2][q, gamma, a] delta = (delta_c, delta_i, delta_r) return VPA(alp, states, start, accept, stack, delta, tr)
def rename(self, state_func=None, alp_func=None): if state_func is None: state_func = mapping() alp = None if alp_func is None: # identity alp_func = lambda x: x alp = self.alphabet else: alp = {alp_func(a) for a in self.alphabet} start = state_func(self.start) states = {state_func(s) for s in self.states} accept = {state_func(s) for s in self.accept} reject = {state_func(s) for s in self.reject} newsink = None if self.sink is not None: newsink = state_func(self.sink) tr = collections.defaultdict(lambda: newsink) for q, a, qn in self.itertransitions(): tr[state_func(q), alp_func(a)] = state_func(qn) delta = lambda q, a: tr[q, a] return DFA(alp, states, start, accept, reject, delta, tr, newsink)