Esempio n. 1
0
    def test_upc_rbt(self):
        upc_data = read_csv("UPC-random.csv")
        expected_data = read_csv("input.dat")

        key_func = lambda row: row[0]  # the unique identifier of the row
        tree = RedBlackTree(key=key_func)
        self._test_bst_case(upc_data, tree, key_func)
        self._test_rbt(tree)

        time_before = default_timer()
        for expected in expected_data:
            search = tree.search(key_func(expected))
            self.assertEqual(search[2], expected[2])  # descriptions
        print("RBT Search time: {}".format(default_timer() - time_before))
Esempio n. 2
0
 def insert(self, key):
     self.ACT_LEN += 1
     self.MAX_LEN = max(self.MAX_LEN, self.ACT_LEN)
     hashed_key = hash(key) % self.HASHMAP_LEN
     if self.hash_table[hashed_key][0] == "table":
         self.hash_table[hashed_key][1].append(key)
         self.MAX_LEN = max(self.MAX_LEN,
                            len(self.hash_table[hashed_key][1]))
         if len(self.hash_table[hashed_key][1]) > self.MAX_THRESHOLD:
             tree = RedBlackTree()
             for key in self.hash_table[hashed_key][1]:
                 tree.insert(key)
             self.hash_table[hashed_key] = ("tree", tree)
     else:
         self.hash_table[hashed_key][1].insert(key)
         self.MAX_LEN = max(self.MAX_LEN,
                            self.hash_table[hashed_key][1].MAX_LEN)
Esempio n. 3
0
 def test_trees(self):
     for case in basic_cases:
         with self.subTest(type="binary search tree", case=case):
             self._test_bst_case(case, BinarySearchTree())
         with self.subTest(type="red-black tree", case=case):
             tree = RedBlackTree()
             self._test_bst_case(case, tree)
             self._test_rbt(tree)
class BoundedPriorityQueue:
    """Simple bounded priority queue which uses a Red Black Tree as the underlying data structure."""
    def __init__(self, k):
        self.maxkey = -float('inf')
        if k < 0:
            raise ValueError("k should be larger than 0")
        self.k = k
        self._bpq = RedBlackTree()

    def insert(self, key, item):
        if self.k == 0:
            return self.maxkey
        if len(self._bpq) < self.k or key < self.maxkey:
            self._bpq.insert(key, item)  # O (lg n)
        if len(self._bpq) > self.k:
            self._bpq.delete(self._bpq.maximum[0])  # O(lg n)
        self.maxkey = self._bpq.maximum[0]  # (lg n)

    def __setitem__(self, key, item=0):
        assert (type(key)) in [int, float], "Invalid type of key."
        self.insert(key, item)

    @property
    def isfull(self):
        return len(self._bpq) == self.k

    def iteritems(self):
        return self._bpq.iteritems()

    def __repr__(self):
        return repr(self._bpq.root)

    def __str__(self):
        return str(list(self._bpq.iteritems()))
Esempio n. 5
0
from process import Process
from rbt import RedBlackTree

Process1 = Process(1, "High", 20, 0)
Process2 = Process(2, "Low", 20, 4)
Process3 = Process(3, "High", 25, 10)
Process4 = Process(4, "High", 40, 20)
Process5 = Process(5, "High", 80, 30)
Process6 = Process(6, "High", 90, 40)

rbt = RedBlackTree()
rbt.addProcess(Process1)
rbt.addProcess(Process2)
rbt.addProcess(Process3)
rbt.addProcess(Process4)
rbt.addProcess(Process5)
rbt.addProcess(Process6)

#print("Number of Processes: " + str(rbt.getProcessesCount()))
#print("Number of Nodes: " + str(rbt.count))

#print("MINUMUM: " + str(rbt.getMinimum()))

rbt.display()
process = rbt.getProcess()
print("First Process to Run: " + process.__repr__())
print("Number of Processes: " + str(rbt.getProcessesCount()))
rbt.display()
"""
process = rbt.getProcess()
print("Second Process to Run: " + process.__repr__())
Esempio n. 6
0
 def __init__(self, processQ, timerInterrupt):
     super().__init__(processQ, timerInterrupt)
     self.targetBound = timerInterrupt
     self.readyTree = RedBlackTree()
Esempio n. 7
0
class CFS(base.BaseScheduler):
    #==============================================
    #Intialize the red black tree for CFS
    #Params:
    #   processQ = Deque of processes to run
    #   timerInterrupt = Allows scheduler to check
    #                    on running process and to
    #                    make decisions
    #Return:
    #   None
    #==============================================
    def __init__(self, processQ, timerInterrupt):
        super().__init__(processQ, timerInterrupt)
        self.targetBound = timerInterrupt
        self.readyTree = RedBlackTree()

    #==============================================
    #Checks to see if the tree is empty
    #Params:
    #   None
    #Return:
    #   Boolean indiciating if tree is empty or
    #   not
    #==============================================
    def empty(self):
        return self.readyTree.getProcessesCount() == 0

    #==============================================
    #Add a process to the red black tree
    #Param:
    #   1) process = the process to be added
    #Return:
    #   None
    #==============================================
    def addProcess(self, process):
        self.readyTree.addProcess(process)

    #==============================================
    #Get the next process to run from the tree
    #Params:
    #   None
    #Return:
    #   Next process on the queue
    #   None if no process in queue
    #==============================================
    def removeProcess(self):
        return self.readyTree.getProcess()

    #==============================================
    #Get the next process for the scheduler to run.
    #This implements the scheduler heuristics
    #Params:
    #   curProc = Current process that's running on
    #             scheduler
    #Return:
    #   Next process in the queue via call to
    #   removeProcess()
    #==============================================
    def getNext(self, curProc):
        if curProc is not None and curProc.get_status() == INCOMPLETE:
            self.addProcess(curProc)
        elif curProc is not None and curProc.get_status() == COMPLETE:
            curProc = None

        processCount = self.readyTree.getProcessesCount()

        if processCount > 0:
            self.timerInterrupt = math.ceil(self.targetBound / processCount)

        nextProc = self.removeProcess()
        return nextProc
 def __init__(self, k):
     self.maxkey = -float('inf')
     if k < 0:
         raise ValueError("k should be larger than 0")
     self.k = k
     self._bpq = RedBlackTree()