Exemplo n.º 1
0
    def test_correct(self) -> None:
        self.assertEqual(len(KeyedPQ()), 0)
        self.assertEqual(len(KeyedPQ([])), 0)

        dummy = DummyClass()
        pq: KeyedPQ[DummyClass] = KeyedPQ([('a', 1.0, dummy)])
        self.assertEqual(len(pq), 1)
        item = pq.peek()
        self.assertEqual(item.key, 'a')
        self.assertEqual(item.value, 1.0)
        self.assertIs(item.data, dummy)
Exemplo n.º 2
0
    def test_incorrect(self) -> None:
        with self.assertRaises(TypeError):
            typing.cast(typing.Any, KeyedPQ)([], [])

        with self.assertRaises(ValueError):
            KeyedPQ(typing.cast(typing.Any, [tuple()]))
        with self.assertRaises(ValueError):
            KeyedPQ(typing.cast(typing.Any, [('a',)]))
        with self.assertRaises(ValueError):
            KeyedPQ(typing.cast(typing.Any, [('a', 1.0)]))

        with self.assertRaises(TypeError):
            KeyedPQ(typing.cast(typing.Any, [(1.0, 1.0, None)]))
        with self.assertRaises(TypeError):
            KeyedPQ(typing.cast(typing.Any, [('a', 'a', None)]))
Exemplo n.º 3
0
    def test_different_heaps(self) -> None:
        pq2: KeyedPQ[None] = KeyedPQ()

        item_a = self.pq.add('a', 0.0, None)
        item_b = pq2.add('a', 0.0, None)

        self._assert_not_equal(item_a, item_b)
Exemplo n.º 4
0
 def __init__(self, reuse_timer: ReuseTimer,
              configuration: 'MIND.Configuration') -> None:
     self._reuse_timer: ReuseTimer = reuse_timer
     self._d_factor: float = configuration.d_factor
     self._min_d: Optional[int] = configuration.min_d
     self._max_d: Optional[int] = configuration.max_d
     self._pq: KeyedPQ[MIND.State._FileInfo] = KeyedPQ(max_heap=True)
Exemplo n.º 5
0
    def test_change_value(self) -> None:
        # Setup

        pq: KeyedPQ[None] = KeyedPQ()
        heap: typing.List[typing.List[float]] = []
        heap_lookup_dict: typing.Dict[str, typing.List[float]] = {}

        for i in range(10000):
            val = random.random()
            key = str(i)
            pq.add(key, val, None)
            heap_entry = [val]
            heap_lookup_dict[key] = heap_entry
            heapq.heappush(heap, heap_entry)

        for i in range(10000):
            val = random.random()
            key = str(i)
            pq.change_value(key, val)
            heap_lookup_dict[key][0] = val
            heapq.heapify(heap)

        # Test

        self.assertEqual(pq._export(), [e[0] for e in heap])
Exemplo n.º 6
0
 def __init__(self) -> None:
     # PQ of cached files in this class
     self.pq: KeyedPQ[OBMA.State._FileInfo] = KeyedPQ(max_heap=True)
     # Total size of cached files in this class
     self.total_size: int = 0
     # Counts bytes to be evicted (eviction is delayed until this
     # counter surpasses the eviction candidate's size)
     self.eviction_counter: int = 0
Exemplo n.º 7
0
 def _set_priorities(self, ts: int) -> None:
     old_pq = self._pq
     eva_of_file_at_time = self._eva_of_file_at_time
     self._pq = KeyedPQ((
         it.key,
         eva_of_file_at_time(it.data, ts),
         it.data,
     ) for it in old_pq)
     self._last_age_bin = self._age_binner(ts)
Exemplo n.º 8
0
    def test_others_item_key(self) -> None:
        pq2: KeyedPQ[None] = KeyedPQ()
        item2 = pq2.add('a', 0.0, None)

        self._assert_not_contained(item2)

        self.pq.add('a', 0.0, None)

        self._assert_not_contained(item2)
Exemplo n.º 9
0
    def test_non_equality(self) -> None:
        pq2: KeyedPQ[None] = KeyedPQ()

        self.assertTrue(self.pq != pq2)
        self.assertFalse(self.pq == pq2)

        self.assertTrue(self.pq != None)
        self.assertFalse(self.pq == None)

        self.assertTrue(self.pq != 4.0)
        self.assertFalse(self.pq == 4.0)
Exemplo n.º 10
0
def bench_pop(b: BenchTimer) -> None:
    s = StringSource()
    pq: KeyedPQ[None] = KeyedPQ()

    for _ in range(b.n + 10000):
        pq.add(next(s), random_01(), None)

    with b.time() as t:
        for _ in t:
            pq.pop()

    with b.offset() as t:
        for _ in t:
            pass
Exemplo n.º 11
0
    def test_incremental_push_small(self) -> None:
        for n in itertools.chain.from_iterable(itertools.repeat(i, 1000) for i in range(10)):
            # Setup

            pq: KeyedPQ[None] = KeyedPQ()
            heap: typing.List[float] = []

            # Test

            for i in range(n):
                val = random.random()
                pq.add(str(i), val, None)
                heapq.heappush(heap, val)
                self.assertEqual(pq._export(), heap)
Exemplo n.º 12
0
    def test_incremental_push(self) -> None:
        for n in range(1000):
            # Setup

            pq: KeyedPQ[None] = KeyedPQ()
            heap: typing.List[float] = []

            # Test

            for i in range(n):
                val = random.random()
                pq.add(str(i), val, None)
                heapq.heappush(heap, val)
                self.assertEqual(pq._export(), heap)
Exemplo n.º 13
0
def bench_change_value(b: BenchTimer) -> None:
    s = StringSource()
    pq: KeyedPQ[None] = KeyedPQ()

    for _ in range(10000):
        pq.add(next(s), random_01(), None)

    with b.time() as t:
        for _ in t:
            key = s.rand_existing()
            pq.change_value(key, random_01())

    with b.offset() as t:
        for _ in t:
            key = s.rand_existing()
            random_01()
Exemplo n.º 14
0
def bench_add(b: BenchTimer) -> None:
    s = StringSource()
    s_offset = StringSource()
    pq: KeyedPQ[None] = KeyedPQ()

    for _ in range(10000):
        pq.add(next(s), random_01(), None)
        next(s_offset)

    with b.time() as t:
        for _ in t:
            pq.add(next(s), random_01(), None)

    with b.offset() as t:
        for _ in t:
            next(s_offset)
            random_01()
Exemplo n.º 15
0
def bench_remove(b: BenchTimer) -> None:
    s = StringSource()
    s_remove = StringSource()
    s_offset = StringSource()
    pq: KeyedPQ[None] = KeyedPQ()

    for _ in range(b.n + 10000):
        pq.add(next(s), random_01(), None)
        next(s_offset)

    with b.time() as t:
        for _ in t:
            key = next(s_remove)
            del pq[key]

    with b.offset() as t:
        for _ in t:
            key = next(s_offset)
Exemplo n.º 16
0
        def __init__(self, configuration: 'EVA.Configuration',
                     storage: Storage) -> None:
            self._classifier: Classifier = Combine((
                _ReusedClassifier(self),
                configuration.classifier,
            ))
            self._age_bin_width: int = configuration.age_bin_width
            self._ewma_factor: float = configuration.ewma_factor
            self._eva_computation_interval: int = configuration.eva_computation_interval

            self._storage_size: int = storage.total_bytes

            self._pq: KeyedPQ[EVA.State._FileInfo] = KeyedPQ()

            self._age_binner: Binner = LinearBinner(width=self._age_bin_width)
            self._class_infos: DefaultDict[
                Hashable, EVA.State._ClassInfo] = DefaultDict(
                    lambda: EVA.State._ClassInfo(self._age_binner), )
            self._accesses_since_eva_computation: int = 0
            self._last_eva_computation_ts: int = 0
            self._last_age_bin: int = 0
Exemplo n.º 17
0
		def __init__(self, reuse_timer: ReuseTimer) -> None:
			self._reuse_timer: ReuseTimer = reuse_timer
			self._pq: KeyedPQ[None] = KeyedPQ(max_heap=True)
Exemplo n.º 18
0
 def setUp(self) -> None:
     self.pq: KeyedPQ[None] = KeyedPQ()
Exemplo n.º 19
0
 def __init__(self) -> None:
     self._pq: KeyedPQ[None] = KeyedPQ()
		def __init__(self, configuration: 'GreedyDual.Configuration') -> None:
			self._mode: Mode = configuration.mode
			self._pq: KeyedPQ[GreedyDual.State._FileInfo] = KeyedPQ()
			self._threshold: float = 0.0
Exemplo n.º 21
0
 def setUp(self) -> None:
     self.pq: KeyedPQ[None] = KeyedPQ(max_heap=True)
Exemplo n.º 22
0
 def test_duplicate_key(self) -> None:
     with self.assertRaises(KeyError):
         KeyedPQ([
             ('a', 1.0, None),
             ('a', 3.0, None),
         ])
Exemplo n.º 23
0
 def setUp(self) -> None:
     self.l: typing.List[float] = []
     self.pq: KeyedPQ[None] = KeyedPQ(max_heap=True)
Exemplo n.º 24
0
 def __init__(self) -> None:
     self._pq: KeyedPQ[LandlordState._FileInfo] = KeyedPQ()
     self._rent_threshold: float = 0.0
Exemplo n.º 25
0
 def setUp(self) -> None:
     self.pq: KeyedPQ[DummyClass] = KeyedPQ()
Exemplo n.º 26
0
 def _set_pq_from_iterable(self, iterable: typing.Iterable[typing.Tuple[str, float, None]]) -> None:
     self.pq = KeyedPQ(iterable, max_heap=True)
Exemplo n.º 27
0
 def __init__(self) -> None:
     super(Size.State, self).__init__()
     self._pq = KeyedPQ(max_heap=True)