Ejemplo n.º 1
0
def positive_find_test():
	ok = True
	t = treap.treap()
	for i in xrange(0, n, 2):
		t[i] = i
	for i in xrange(0, n, 2):
		myut.assertEqual(t[i], i)
Ejemplo n.º 2
0
    def rescale(self, now):
        """ "A common feature of the above techniques—indeed, the key technique
        that allows us to track the decayed weights efficiently—is that they
        maintain counts and other quantities based on g(ti − L), and only scale
        by g(t − L) at query time. But while g(ti −L)/g(t−L) is guaranteed to
        lie between zero and one, the intermediate values of g(ti − L) could
        become very large. For polynomial functions, these values should not
        grow too large, and should be effectively represented in practice by
        floating point values without loss of precision. For exponential
        functions, these values could grow quite large as new values of
        (ti − L) become large, and potentially exceed the capacity of common
        floating point types. However, since the values stored by the
        algorithms are linear combinations of g values (scaled sums), they
        can be rescaled relative to a new landmark. That is, by the analysis
        of exponential decay in Section VI-A, the choice of L does not affect
        the final result. We can therefore multiply each value based on L by a
        factor of exp(−α(L′ − L)), and obtain the correct value as if we had
        instead computed relative to a new landmark L′ (and then use this new
        L′ at query time). This can be done with a linear pass over whatever
        data structure is being used."
        """

        old_start_time = self.start_time
        self.start_time = self.current_time_in_fractional_seconds()
        self.next_scale_time = now + RESCALE_THRESHOLD_S
        new_values = treap.treap()
        for key in self.values.keys():
            new_key = key * math.exp(
                -self.alpha * (self.start_time - old_start_time))
            new_values[new_key] = self.values[key]
        self.values = new_values
Ejemplo n.º 3
0
    def rescale(self, now):
        """ "A common feature of the above techniques—indeed, the key technique
        that allows us to track the decayed weights efficiently—is that they
        maintain counts and other quantities based on g(ti − L), and only scale
        by g(t − L) at query time. But while g(ti −L)/g(t−L) is guaranteed to
        lie between zero and one, the intermediate values of g(ti − L) could
        become very large. For polynomial functions, these values should not
        grow too large, and should be effectively represented in practice by
        floating point values without loss of precision. For exponential
        functions, these values could grow quite large as new values of
        (ti − L) become large, and potentially exceed the capacity of common
        floating point types. However, since the values stored by the
        algorithms are linear combinations of g values (scaled sums), they
        can be rescaled relative to a new landmark. That is, by the analysis
        of exponential decay in Section VI-A, the choice of L does not affect
        the final result. We can therefore multiply each value based on L by a
        factor of exp(−α(L′ − L)), and obtain the correct value as if we had
        instead computed relative to a new landmark L′ (and then use this new
        L′ at query time). This can be done with a linear pass over whatever
        data structure is being used."
        """

        old_start_time = self.start_time
        self.start_time = self.current_time_in_fractional_seconds()
        self.next_scale_time = now + RESCALE_THRESHOLD_S
        new_values = treap.treap()
        for key in self.values.keys():
            new_key = key * math.exp(-self.alpha *
                                     (self.start_time - old_start_time))
            new_values[new_key] = self.values[key]
        self.values = new_values
Ejemplo n.º 4
0
def negative_find_test():
	ok = True
	t = treap.treap()
	for i in xrange(0, n, 2):
		t[i] = i
	for i in xrange(1, n, 2):
		myut.require_exceptions(functools.partial(t.find, i), ( exceptions.LookupError, ))
Ejemplo n.º 5
0
def repeat_length_test():
	t = treap.treap()
	for i in xrange(n):
		t[i] = i
	for i in xrange(n):
		t[i] = i
	myut.assertEqual(len(t), n)
Ejemplo n.º 6
0
 def __init__(self):
     self.lastEventTime = 0.0
     self.simulatorReady = False if sg.args.backnoise else True
     self.simulationDone = False
     self.simulationStatistics = []
     self.cacheStatistics_vm = []
     self.cacheStatistics_hw = []
     self.urStatistics_nActCons = []
     self.urStatistics_nReqPSec = []
     if sg.args.parallel:
         printInfo("DISCLAIMER: Parallel simulation is a test feature!")
         #   calcFairThroughput
         self.calcFT_pool = mp.Pool()
         #   eventQueueKeeper
         self.mpManager = mp.Manager()
         self.commQueue_in = self.mpManager.Queue(maxsize=1)
         commPipe_chld, self.commPipe_out = mp.Pipe(False)
         self.communicationLock = mp.Lock()
         self.eventQueueProcess = mp.Process(
             target=eventQueueKeeper,
             args=(commPipe_chld, self.commQueue_in, self.communicationLock)
         )
         self.eventQueueProcess.start()
         self.step = self.step_parallel
         self.eventPush = self.eventPush_parallel
         self.eventUpdateTime = self.eventUpdateTime_parallel
         self.deleteEvent = self.deleteEvent_parallel
     else:
         self.eventQueue = treap.treap()
         self.step = self.step_sequential
         self.eventPush = self.eventPush_sequential
         self.eventUpdateTime = self.eventUpdateTime_sequential
         self.deleteEvent = self.deleteEvent_sequential
     return
Ejemplo n.º 7
0
 def __init__(self):
     self.lastEventTime = 0.0
     self.simulatorReady = False if sg.args.backnoise else True
     self.simulationDone = False
     self.simulationStatistics = []
     self.cacheStatistics_vm = []
     self.cacheStatistics_hw = []
     self.urStatistics_nActCons = []
     self.urStatistics_nReqPSec = []
     if sg.args.parallel:
         printInfo("DISCLAIMER: Parallel simulation is a test feature!")
         #   calcFairThroughput
         self.calcFT_pool = mp.Pool()
         #   eventQueueKeeper
         self.mpManager = mp.Manager()
         self.commQueue_in = self.mpManager.Queue(maxsize=1)
         commPipe_chld, self.commPipe_out = mp.Pipe(False)
         self.communicationLock = mp.Lock()
         self.eventQueueProcess = mp.Process(target=eventQueueKeeper,
                                             args=(commPipe_chld,
                                                   self.commQueue_in,
                                                   self.communicationLock))
         self.eventQueueProcess.start()
         self.step = self.step_parallel
         self.eventPush = self.eventPush_parallel
         self.eventUpdateTime = self.eventUpdateTime_parallel
         self.deleteEvent = self.deleteEvent_parallel
     else:
         self.eventQueue = treap.treap()
         self.step = self.step_sequential
         self.eventPush = self.eventPush_sequential
         self.eventUpdateTime = self.eventUpdateTime_sequential
         self.deleteEvent = self.deleteEvent_sequential
     return
Ejemplo n.º 8
0
def iterator_test():
	lst = range(n)
	random_lst = random_swaps(lst[:])
	t = treap.treap()
	for x in random_lst:
		t[x] = x
	lst2 = list(t.iterator())
	myut.assertTrue(lst == lst2)
Ejemplo n.º 9
0
def duplication_behavior_unique():
	#t = treap.treap(allow_duplicates=False)
	t = treap.treap()
	t[1] = 1
	t[2] = 2
	t[2] = 2
	t[3] = 3
	myut.assertTrue(list(t) == [1, 2, 3])
Ejemplo n.º 10
0
def max_test():
	lst = range(n)
	random_lst = random_swaps(lst[:])
	t = treap.treap()
	for x in random_lst:
		t[x] = x
	least = t.find_max()
	myut.assertEqual(least, n-1)
Ejemplo n.º 11
0
def min_max_length_test():
	t = treap.treap()
	for i in xrange(n):
		t[i] = 0
	myut.assertEqual(len(t), n)
	t.remove_min()
	myut.assertEqual(len(t), n-1)
	t.remove_max()
	myut.assertEqual(len(t), n-2)
Ejemplo n.º 12
0
	def create(self, sequence):
		self.treap = treap.treap()

		for i in sequence:
			self.treap[i] = i

		self.content = []

		self.treap.inorder_traversal(self.add)
Ejemplo n.º 13
0
 def __init__(self, n, dups=False):
     self.n = n
     if dups:
         import duptreap
         self.treap = duptreap.duptreap()
     else:
         import treap
         self.treap = treap.treap()
     self.__iter__ = self.treap.reverse_iterator
     self.have_evicted = False
Ejemplo n.º 14
0
def del_insert_del_insert():
	t = treap.treap()
	for i in xrange(n):
		t[i] = 0
	for i in xrange(n):
		t.remove_min()
	for i in xrange(n):
		t[i] = 0
	for i in xrange(n):
		t.remove_min()
Ejemplo n.º 15
0
def depth_test():
	# O(n^2), so we don't use n - we use something small.
	# We assume very little about the resulting depths - in particular, even though this datastructure should very nearly always be log2(n) deep, we assume that
	# worst case behavior is possible - IE that depth can be as high as n.  We also don't make any effort to show that an empty treap has a depth of 0
	my_n = min(n, 100)
	for i in xrange(my_n):
		t = treap.treap()
		for j in xrange(i):
			t[j] = j
		myut.assertTrue(0 <= t.depth() <= i)
Ejemplo n.º 16
0
	def __init__(self, n, dups=False):
		self.n = n
		if dups:
			import duptreap
			self.treap = duptreap.duptreap()
		else:
			import treap
			self.treap = treap.treap()
		self.__iter__ = self.treap.reverse_iterator
		self.have_evicted = False
Ejemplo n.º 17
0
def remove_max_test():
	# O(n^2) test!
	lst = range(n)
	t = treap.treap()
	for i in lst:
		t[i] = 0
	# taking advantage of the fact that the keys are the same as our lst indices
	for i in lst:
		t.remove_max()
		myut.assertTrue(list(t) == lst[:-(i+1)])
Ejemplo n.º 18
0
def reremoval_test():
	t = treap.treap()
	# fill the treap with 0..n
	for i in xrange(n):
		t[i] = i
	# remove all the odd values
	for i in xrange(1,n,2):
		del t[i]
	# check that we have nothing but even values left
	ordered_values = list(t.iterator())
	for i in [ 1, 3, 7 ]:
		myut.require_exceptions(functools.partial(t.remove, i), ( exceptions.LookupError, ))
Ejemplo n.º 19
0
 def __init__(self, args, resDirName):
     self.eventQueue = treap.treap()
     self.lastEventTime = 0
     self.urRef = None
     self.topArgs = args
     self.simulatorReady = False if args.backnoise else True
     self.simulationDone = False
     self.simulationStatistics = []
     self.urStatistics_nActCons = []
     self.urStatistics_nReqPSec = []
     self.simResDirName = resDirName
     return
Ejemplo n.º 20
0
def removal_test():
	t = treap.treap()
	# fill the treap with 0..n
	for i in xrange(n):
		t[i] = i
	# remove all the odd values
	for i in xrange(1,n,2):
		del t[i]
	# check that we have nothing but even values left
	ordered_values = list(t.iterator())
	result = ordered_values == range(0, n, 2)
	myut.assertEqual(result, True)
Ejemplo n.º 21
0
def string_test():
	def random_char():
		return chr(97 + int(random.random() * 32))

	t = treap.treap()
	dict = {}
	for i in xrange(n):
		strng = '%s%s' % (random_char(), random_char())
		t[strng] = None
		dict[strng] = None
	lst = dict.keys()
	lst.sort()
	myut.assertTrue(list(t) == lst)
Ejemplo n.º 22
0
def remove_min_test():
	# O(n^2) test!
	lst = range(n)
	t = treap.treap()
	for i in lst:
		t[i] = 0
	# taking advantage of the fact that the keys are the same as our lst indices
	for i in lst:
		if i % (n / 5) == 0:
			myut.assertTrue(t.check_heap_invariant())
			myut.assertTrue(t.check_tree_invariant())
		t.remove_min()
		myut.assertTrue(list(t) == lst[i+1:])
Ejemplo n.º 23
0
    def __init__(self, unit=None, size=DEFAULT_SIZE, alpha=DEFAULT_ALPHA):
        """Create a new exponentially-decaying random reservoir.

        :param size: the number of samples to keep in the sampling reservoir
        :param alpha: the exponential decay factor; the higher this is, the
            more biased the reservoir will be towards newer values.
        """

        self.unit = unit
        self.size = size
        self.alpha = alpha
        self.start_time = self.current_time_in_fractional_seconds()
        self.next_scale_time = self.start_time + RESCALE_THRESHOLD_S
        self.values = treap.treap()
Ejemplo n.º 24
0
def remove_sequence_test(reverse):
	t = treap.treap()
	lst = range(n)
	for item in lst:
		t[item] = 0
	if reverse:
		lst.reverse()
		pop = t.remove_max
	else:
		pop = t.remove_min
	for i in xrange(len(lst)):
		value_from_treap = pop()
		myut.assertEqual((lst[i], 0), value_from_treap)
	myut.assertTrue(not bool(t))
Ejemplo n.º 25
0
    def __init__(self, unit=None, size=DEFAULT_SIZE,
                 alpha=DEFAULT_ALPHA):
        """Create a new exponentially-decaying random reservoir.

        :param size: the number of samples to keep in the sampling reservoir
        :param alpha: the exponential decay factor; the higher this is, the
            more biased the reservoir will be towards newer values.
        """

        self.unit = unit
        self.size = size
        self.alpha = alpha
        self.start_time = self.current_time_in_fractional_seconds()
        self.next_scale_time = self.start_time + RESCALE_THRESHOLD_S
        self.values = treap.treap()
Ejemplo n.º 26
0
def removal_from_empty_test():
	t = treap.treap()
	for i in [ 1, 3, 7 ]:
		myut.require_exceptions(functools.partial(t.remove, i), ( exceptions.LookupError, ))
Ejemplo n.º 27
0
def failed_removal_from_one_test():
	t = treap.treap()
	t[5] = 5
	myut.require_exceptions(functools.partial(t.remove, 10), ( exceptions.LookupError, ))
Ejemplo n.º 28
0
 def __init__(self):
     self._treap = treap.treap()
Ejemplo n.º 29
0
def simple_length_test():
	t = treap.treap()
	for i in xrange(n):
		myut.assertEqual(len(t), i)
		t[i] = i
	myut.assertEqual(len(t), n)
Ejemplo n.º 30
0
def empty_0_test():
	t = treap.treap()
	myut.assertEqual(t.depth(), 0)
Ejemplo n.º 31
0
def nonempty_test():
	t = treap.treap()
	t[1] = 1
	myut.assertTrue(bool(t) == True)
Ejemplo n.º 32
0
def empty_test():
	t = treap.treap()
	myut.assertTrue(not (bool(t) == True))
Ejemplo n.º 33
0
def eventQueueKeeper(inPipe, outQueue, commLock):
    eventQueue = treap.treap()
    keepRunning = True
    prevNextEv = None
    printInfo("eventQueueKeeper is started")

    while keepRunning:
        if inPipe.poll():
            inc_ev, extra = inPipe.recv()
            if outQueue.empty():
                if eventQueue:
                    prevNextEv = eventQueue.find_min()
                    outQueue.put(prevNextEv)
                    eventQueue.remove(prevNextEv)
                else:
                    prevNextEv = None
            # add new event
            if extra is None:
                if prevNextEv is None:
                    outQueue.put(inc_ev)
                    commLock.release()
                    prevNextEv = inc_ev
                    continue
                elif prevNextEv < inc_ev:
                    commLock.release()
                    eventQueue[inc_ev] = inc_ev
                    continue
                else:
                    outQueue.get()
                    outQueue.put(inc_ev)
                    commLock.release()
                    eventQueue[prevNextEv] = prevNextEv
                    prevNextEv = inc_ev
                    continue
            else:  # perform some action on event
                action, val = extra
                if action == sg.ACTION_UPDATE:
                    if prevNextEv is None:
                        eventQueue.remove(inc_ev)
                        inc_ev.time = val
                        eventQueue[inc_ev] = inc_ev
                        prevNextEv = eventQueue.find_min()
                        outQueue.put(prevNextEv)
                        commLock.release()
                        eventQueue.remove(prevNextEv)
                        continue
                    elif prevNextEv == inc_ev:
                        outQueue.get()
                        inc_ev.time = val
                        if prevNextEv < inc_ev:
                            eventQueue[inc_ev] = inc_ev
                            prevNextEv = eventQueue.find_min()
                            outQueue.put(prevNextEv)
                            commLock.release()
                            eventQueue.remove(prevNextEv)
                            continue
                        else:
                            outQueue.put(inc_ev)
                            commLock.release()
                            prevNextEv = inc_ev
                            continue
                    else:
                        eventQueue.remove(inc_ev)
                        inc_ev.time = val
                        if prevNextEv < inc_ev:
                            commLock.release()
                            eventQueue[inc_ev] = inc_ev
                            continue
                        else:
                            outQueue.get()
                            outQueue.put(inc_ev)
                            commLock.release()
                            eventQueue[prevNextEv] = prevNextEv
                            prevNextEv = inc_ev
                            continue
                elif action == sg.ACTION_DELETE:
                    if prevNextEv == inc_ev:
                        outQueue.get()
                        if eventQueue:
                            prevNextEv = eventQueue.find_min()
                            outQueue.put(prevNextEv)
                            commLock.release()
                            eventQueue.remove(prevNextEv)
                            continue
                        else:
                            prevNextEv = None
                            commLock.release()
                            continue
                    else:
                        commLock.release()
                        eventQueue.remove(inc_ev)
                        continue
                elif action == sg.ACTION_STOP:
                    if not eventQueue:
                        commLock.release()
                        printInfo("stop eventQueueKeeper")
                        keepRunning = False
                        break
        if eventQueue and outQueue.empty():
            prevNextEv = eventQueue.find_min()
            outQueue.put(prevNextEv)
            eventQueue.remove(prevNextEv)

    printInfo("eventQueueKeeper finished successfully")
Ejemplo n.º 34
0
def successful_removal_from_one_test():
	t = treap.treap()
	t[5] = 5
	del t[5]
Ejemplo n.º 35
0
def value_test():
	t = treap.treap()
	for i in xrange(n):
		t[i] = i*3
	for i in xrange(n):
		myut.assertEqual(t[i], i*3)
Ejemplo n.º 36
0
 def __init__(self, commLock):
     self.eventQueue = treap.treap()
     self.keepRunning = True
     self.commLock = commLock
     printInfo("eventQueueKeeper is started")
Ejemplo n.º 37
0
def empty_max_test():
	t = treap.treap()
	myut.require_exceptions(t.find_max, (exceptions.LookupError, ))