Example #1
0
    def put(self, key: Key, value):
        r = self.rank(key)

        if r >= len(self._items):
            self._items.append(KeyValue(key, value))
        elif eq(self._items[r].key, key):
            self._items[r].value = value
        else:
            shift_right(self._items, r)
            self._items[r] = KeyValue(key, value)
Example #2
0
    def test_find_index_of_item(self):
        assert index_of([3, 1, 2], 3) == 0
        assert index_of([3, 1, 2], 1) == 1
        assert index_of([3, None, 2], None) == 1
        assert index_of([3, None, 2], 2) == 2

        items = list(range(100))
        random.shuffle(items)
        assert index_of(items, 44) == items.index(44)

        assert index_of([KeyValue(Key(1)),
                         KeyValue(Key(4)),
                         KeyValue(Key(2))], KeyValue(Key(4))) == 1
Example #3
0
    def default_items():
        keys = [Key(1), Key(4), Key(2), Key(8), Key(12)]
        values = [100, 400, 200, 800, 1200]

        items = []
        for i in range(len(keys)):
            items.append(KeyValue(keys[i], values[i]))
        return items
Example #4
0
    def test_compare_primitives(self):
        assert compare(1, 2) == -1
        assert compare(2, 2) == 0
        assert compare(2, 1) == 1

        assert compare(1.1, 1.2) == -1
        assert compare(2.1, 1.9) == 1
        assert compare(2.1, 2.1) == 0

        assert compare(KeyValue(1), KeyValue(2)) == -1
        assert compare(KeyValue(2), KeyValue(2)) == 0
        assert compare(KeyValue(2), KeyValue(1)) == 1
Example #5
0
def _insert_if_present(streams, stream_idx, queue):
    item = next(streams[stream_idx], None)
    if item is not None:
        queue.insert(KeyValue(item, stream_idx))
Example #6
0
 def _index_of_key(self, key):
     return index_of(self._items, KeyValue(key))
Example #7
0
 def put(self, key: Key, value):
     item_idx = self._index_of_key(key)
     if item_idx is None:
         self._items.append(KeyValue(key, value))
     else:
         self._items[item_idx].value = value
Example #8
0
    def test_check_equals(self):
        assert eq(None, None)

        assert not eq(1, 2)
        assert eq(2, 2)
        assert not eq(2, 1)

        assert not eq(1.1, 1.2)
        assert not eq(2.1, 1.9)
        assert eq(2.1, 2.1)

        assert not eq(KeyValue(1), KeyValue(2))
        assert eq(KeyValue(2), KeyValue(2))
        assert not eq(KeyValue(2), KeyValue(1))

        assert not eq(KeyValue(Key(1)), KeyValue(Key(2)))
        assert eq(KeyValue(Key(2)), KeyValue(Key(2)))
        assert not eq(KeyValue(Key(2)), KeyValue(Key(1)))