def Sort(IN, MAX, OUT): """ Sort a stream of Packets to an output stream """ max = MAX.receive_once(9999) - 1 array = [] for i, p in enumerate(IN.iter_packets()): if i == max: break array.append(p) # this is designed to stream results during the sort operation, which is # theoretically "better" than sorting everything up front with the sort() # function, though in practice it's likely that sort() is faster in most # cases by virtue of being implemented in C. j = 0 k = len(array) n = k # no. of packets to be sent out while n > 0: curr_min = None for i in range(k): if array[i] is not None: s = array[i].get_contents() if curr_min is None or s < curr_min: # was `cmp(s, t) < 0` j = i curr_min = s # if (array[j] is None) break OUT.send(array[j]) array[j] = None n -= 1
def GenerateTestData(COUNT, OUT): """"Generates stream of packets under control of a counter""" count = COUNT.receive_once() if count is None: return for i in range(count, 0, -1): s = "%06d" % i if not OUT.send(s): break
def GenerateTestDataDumb(COUNT, OUT): """"Generates stream of packets under control of a counter Fails to break if OUT is closed """ count = COUNT.receive_once() if count is None: return for i in range(count, 0, -1): s = "%06d" % i OUT.send(s)
def GenerateOptionalFixedArray(COUNT, OUT): """"Generates stream of packets under control of a counter""" count = COUNT.receive_once() if count is None: return for outport in OUT: logger.info("writing to port %s" % outport) for i in range(count, 0, -1): s = "%06d" % i if outport.is_closed(): break outport.send(s)
def GenerateFixedSizeArray(COUNT, OUT): """"Generates stream of packets under control of a counter""" count = COUNT.receive_once() if count is None: return for outport in OUT: for i in range(count, 0, -1): s = "%06d" % i if OUT.is_closed(): break # if (out_port_array[k].is_connected()): outport.send(s)
def GenSS(COUNT, OUT): """Generates stream of 5-packet substreams under control of a counter """ count = COUNT.receive_once() OUT.send(Packet.Type.OPEN) for i in range(count): s = "%06d" % (count - i) OUT.send(s) if i < count - 1: # prevent empty bracket pair at end if i % 5 == 5 - 1: OUT.send(Packet.Type.CLOSE) OUT.send(Packet.Type.OPEN) OUT.send(Packet.Type.CLOSE)
def GenerateRandom(COUNT, OUT): """Generate a stream of random numbers""" count = COUNT.receive_once() for i in range(count): OUT.send(random())