Ejemplo n.º 1
0
def test_iloc_delitem_slice():
    temp = PriorityDict(enumerate(string.lowercase))
    that = list(enumerate(string.lowercase))
    del temp.iloc[5:20:3]
    del that[5:20:3]
    assert temp.items() == that
    temp._check()
Ejemplo n.º 2
0
def test_isub_small():
    temp = PriorityDict((val, val) for val in range(100))
    that = PriorityDict((val, val) for val in range(10))
    temp -= that
    assert all(temp[val] == 0 for val in range(10))
    assert all(temp[val] == val for val in range(10, 100))
    temp._check()
Ejemplo n.º 3
0
def test_len():
    temp = PriorityDict()
    assert len(temp) == 0
    val = dict((letter, pos) for pos, letter in enumerate(string.lowercase))
    temp.update(val)
    assert len(temp) == 26
    temp._check()
Ejemplo n.º 4
0
def test_isub_small():
    temp = PriorityDict((val, val) for val in range(100))
    that = PriorityDict((val, val) for val in range(10))
    temp -= that
    assert all(temp[val] == 0 for val in range(10))
    assert all(temp[val] == val for val in range(10, 100))
    temp._check()
Ejemplo n.º 5
0
def test_iloc_delitem_slice():
    temp = PriorityDict(enumerate(string.lowercase))
    that = list(enumerate(string.lowercase))
    del temp.iloc[5:20:3]
    del that[5:20:3]
    assert temp.items() == that
    temp._check()
def dijkstra(data, source, sink):
    visit = {}
    dis_dict = defaultdict(lambda: math.inf)
    dis_dict[source] = 0
    expand_queue = PriorityDict()
    expand_queue[source] = 0
    # distance = 0
    while len(expand_queue) > 0:
        source = expand_queue.smallest()
        i = expand_queue[source]
        # if i != distance:
        #     print("distance", i)
        # distance = i
        expand_queue.pop_smallest()
        visit[source] = i
        if source == sink:
            return i
        if source in data:
            # print("source", source, " in data", "neighbours", len(data[source]))
            for s in data.get(source):
                if s not in visit or visit[s] > i + 1:
                    if s not in expand_queue or i + 1 < expand_queue[s]:
                        expand_queue[s] = i + 1

    return math.inf
Ejemplo n.º 7
0
def test_clean():
    temp = PriorityDict((val, num) for num, val in enumerate(string.lowercase))
    assert len(temp) == 26
    temp.clean()
    temp._check()
    assert len(temp) == 25
    temp.clean(10)
    temp._check()
    assert len(temp) == 15
Ejemplo n.º 8
0
def test_iand_big():
    temp_vals = list((val, rand(100)) for val in range(100))
    that_vals = list((val, rand(100)) for val in range(100))
    temp = PriorityDict(temp_vals)
    that = PriorityDict(that_vals)
    temp &= that
    assert all(temp[pos] == min(temp_vals[pos][1], that_vals[pos][1])
               for pos in range(100))
    temp._check()
Ejemplo n.º 9
0
def test_iand_big():
    temp_vals = list((val, rand(100)) for val in range(100))
    that_vals = list((val, rand(100)) for val in range(100))
    temp = PriorityDict(temp_vals)
    that = PriorityDict(that_vals)
    temp &= that
    assert all(temp[pos] == min(temp_vals[pos][1], that_vals[pos][1])
               for pos in range(100))
    temp._check()
Ejemplo n.º 10
0
def test_gt():
    temp = PriorityDict((val, val) for val in range(100))
    that = PriorityDict((val, val) for val in range(100))
    that[50] = -50
    assert temp > that
    del that[0]
    assert temp > that
    del temp[1]
    assert not (temp > that)
Ejemplo n.º 11
0
def test_and():
    temp_vals = list((val, rand(100)) for val in range(25, 75))
    that_vals = list((val, rand(100)) for val in range(100))
    temp = PriorityDict(temp_vals)
    that = PriorityDict(that_vals)
    other = temp & that
    other._check()
    assert len(other) == 50
    assert all(other[pos] == min(temp_vals[pos - 25][1], that_vals[pos][1])
               for pos in range(25, 75))
Ejemplo n.º 12
0
def test_le():
    temp = PriorityDict((val, val) for val in range(100))
    that = PriorityDict((val, val) for val in range(100))
    assert temp <= that
    temp[50] = -50
    assert temp <= that
    del temp[0]
    assert temp <= that
    del that[1]
    assert not (temp <= that)
Ejemplo n.º 13
0
def test_iloc_delitem():
    temp = PriorityDict(enumerate(string.lowercase))
    that = list(enumerate(string.lowercase))

    while len(temp) > 0:
        pos = rand(len(temp))
        del that[pos]
        del temp.iloc[pos]
        assert temp.items() == that
        temp._check()
Ejemplo n.º 14
0
def test_iloc_delitem():
    temp = PriorityDict(enumerate(string.lowercase))
    that = list(enumerate(string.lowercase))

    while len(temp) > 0:
        pos = rand(len(temp))
        del that[pos]
        del temp.iloc[pos]
        assert temp.items() == that
        temp._check()
Ejemplo n.º 15
0
def test_ior_small():
    temp_vals = list((val, rand(100)) for val in range(100))
    that_vals = list((val, rand(100)) for val in range(10))
    temp = PriorityDict(temp_vals)
    that = PriorityDict(that_vals)
    temp |= that
    assert all(temp[pos] == max(temp_vals[pos][1], that_vals[pos][1])
               for pos in range(10))
    assert all(temp[pos] == temp_vals[pos][1] for pos in range(10, 100))
    temp._check()
Ejemplo n.º 16
0
def test_ior_small():
    temp_vals = list((val, rand(100)) for val in range(100))
    that_vals = list((val, rand(100)) for val in range(10))
    temp = PriorityDict(temp_vals)
    that = PriorityDict(that_vals)
    temp |= that
    assert all(temp[pos] == max(temp_vals[pos][1], that_vals[pos][1])
               for pos in range(10))
    assert all(temp[pos] == temp_vals[pos][1]
               for pos in range(10, 100))
    temp._check()
Ejemplo n.º 17
0
def test_or():
    temp_vals = list((val, rand(100)) for val in range(100))
    that_vals = list((val, rand(100)) for val in range(50))
    temp = PriorityDict(temp_vals)
    that = PriorityDict(that_vals)
    other = temp | that
    other._check()
    assert len(other) == 100
    assert all(other[pos] == max(temp_vals[pos][1], that_vals[pos][1])
               for pos in range(50))
    assert all(other[pos] == temp_vals[pos][1] for pos in range(50, 100))
Ejemplo n.º 18
0
def test_clean():
    temp = PriorityDict((val, num) for num, val in enumerate(string.lowercase))
    assert len(temp) == 26
    temp.clean()
    temp._check()
    assert len(temp) == 25
    temp.clean(10)
    temp._check()
    assert len(temp) == 15
Ejemplo n.º 19
0
    def calculate_shortest_paths_from(self, source_id):
        distance = {}
        previous = {}
        q = None
        if self.use_priority_queue:
            q = PriorityDict()
        else:
            q = []
        ind = []
        distance[source_id] = 0
        for x in self.nodes:
            if x is not source_id:
                distance[x] = float("inf")
                previous[x] = None
            if self.use_priority_queue:
                q[x] = distance[x]
            else:
                q.append(x)

        while len(q):
            u = None
            if self.use_priority_queue:
                u = q.pop_smallest()
            else:
                u = self.get_node_with_minimum_distance(q, distance, ind)
                index = ind.pop()
                if type(index) is int:
                    del q[index]
                else:
                    break

            if isinstance(self.edges[u], dict):
                for v in self.edges[u]:
                    if v in q:
                        alt = distance[u] + self.edges[u][v].strength
                        if alt < distance[v]:
                            distance[v] = alt
                            previous[v] = u
                            if self.use_priority_queue:
                                q[v] = distance[v]
        if not self.num_threads:
            self.shortest_paths[source_id] = distance
            self.shortest_paths_guide[source_id] = previous
        else:
            self.lock.acquire()
            self.shortest_paths[source_id] = distance
            self.shortest_paths_guide[source_id] = previous
            self.lock.release()
        return
Ejemplo n.º 20
0
def test_setdefault():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    assert temp.setdefault('d', -1) == 3
    assert temp.setdefault('blah', 10) == 10
    temp._check()
    assert len(temp) == 27
    assert temp['k'] == 10
    temp._check()
Ejemplo n.º 21
0
def test_copy():
    this = PriorityDict({'a': 0, 'b': 1, 'c': 2, 'd': 3})
    that = this.copy()
    del this['d']
    assert len(that) == 4
    this._check()
    that._check()
Ejemplo n.º 22
0
def test_setdefault():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    assert temp.setdefault('d', -1) == 3
    assert temp.setdefault('blah', 10) == 10
    temp._check()
    assert len(temp) == 27
    assert temp['k'] == 10
    temp._check()
Ejemplo n.º 23
0
def test_len():
    temp = PriorityDict()
    assert len(temp) == 0
    val = dict((letter, pos) for pos, letter in enumerate(string.lowercase))
    temp.update(val)
    assert len(temp) == 26
    temp._check()
Ejemplo n.º 24
0
def test_iloc_getitem():
    temp = PriorityDict(enumerate(string.lowercase))
    for pos, letter in enumerate(string.lowercase):
        assert temp[temp.iloc[pos]] == letter
    temp._check()
Ejemplo n.º 25
0
def test_pop():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    assert temp.pop('c') == 2
    temp._check()
    assert temp.pop('blah', -1) == -1
    temp._check()
Ejemplo n.º 26
0
def test_clear():
    temp = PriorityDict(enumerate(string.lowercase))
    assert len(temp) == 26
    temp.clear()
    assert len(temp) == 0
    temp._check()
Ejemplo n.º 27
0
def test_iloc_getitem():
    temp = PriorityDict(enumerate(string.lowercase))
    for pos, letter in enumerate(string.lowercase):
        assert temp[temp.iloc[pos]] == letter
    temp._check()
Ejemplo n.º 28
0
def test_tally():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    temp.tally(list(string.lowercase))
    for pos, key in enumerate(string.lowercase):
        assert temp[key] == (pos + 1)
    temp._check()
Ejemplo n.º 29
0
def test_elements():
    temp = PriorityDict({'a': 3, 'b': 2, 'c': 1})
    assert ['c', 'b', 'b', 'a', 'a', 'a'] == list(temp.elements())
Ejemplo n.º 30
0
def test_get():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    assert temp.get('a') == 0
    assert temp.get('y') == 24
    assert temp.get('blah') == None
    temp._check()
Ejemplo n.º 31
0
def test_bisect_right():
    temp = PriorityDict((val, (val / 10) * 10) for val in range(100))
    for val in range(100):
        assert temp.bisect_right(val) == ((val + 10) / 10) * 10
Ejemplo n.º 32
0
def test_reversed():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    assert list(reversed(temp)) == list(reversed(string.lowercase))
    temp._check()
Ejemplo n.º 33
0
def test_update_big():
    temp = PriorityDict((val, pos) for pos, val in enumerate(string.lowercase))
    temp.update((val, -pos) for pos, val in enumerate(string.lowercase[5:25]))
    for pos, val in enumerate(string.lowercase[5:25]):
        assert temp[val] == -pos
Ejemplo n.º 34
0
def test_iter():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    assert list(iter(temp)) == list(string.lowercase)
    temp._check()
Ejemplo n.º 35
0
def test_index():
    temp = PriorityDict((val, pos) for pos, val in enumerate(string.lowercase))
    for key in string.lowercase:
        pos = temp.index(key)
        assert temp.iloc[pos] == key
Ejemplo n.º 36
0
def test_index_keyerror():
    temp = PriorityDict((val, pos) for pos, val in enumerate(string.lowercase))
    temp.index('aa')
Ejemplo n.º 37
0
def test_iter():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    assert list(iter(temp)) == list(string.lowercase)
    temp._check()
Ejemplo n.º 38
0
def test_delitem():
    temp = PriorityDict(enumerate(string.lowercase))
    del temp[13]
    assert len(temp) == 25
    temp._check()
Ejemplo n.º 39
0
def test_bisect_right():
    temp = PriorityDict((val, (val / 10) * 10) for val in range(100))
    for val in range(100):
        assert temp.bisect_right(val) == ((val + 10) / 10) * 10
Ejemplo n.º 40
0
def test_contains():
    temp = PriorityDict(enumerate(string.lowercase))
    assert all(pos in temp for pos in range(len(string.lowercase)))
    assert 26 not in temp
    assert not (-1 in temp)
    temp._check()
Ejemplo n.º 41
0
def test_iadd_big():
    temp = PriorityDict((val, val) for val in range(100))
    that = PriorityDict((val, val) for val in range(100))
    temp += that
    assert all(temp[val] == 2 * val for val in range(100))
    temp._check()
Ejemplo n.º 42
0
def test_ne():
    temp = PriorityDict((val, val) for val in range(100))
    that = PriorityDict((val, val) for val in range(100))
    assert not (temp != that)
    that[50] = -50
    assert temp != that
Ejemplo n.º 43
0
def test_most_common():
    temp = PriorityDict({'a': 1, 'b': 2, 'c': 3, 'd': 4})
    assert temp.most_common() == [('d', 4), ('c', 3), ('b', 2), ('a', 1)]
    assert temp.most_common(2) == [('d', 4), ('c', 3)]
    temp._check()
Ejemplo n.º 44
0
def test_pop_error():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    temp.pop('blah')
Ejemplo n.º 45
0
def test_contains():
    temp = PriorityDict(enumerate(string.lowercase))
    assert all(pos in temp for pos in range(len(string.lowercase)))
    assert 26 not in temp
    assert not (-1 in temp)
    temp._check()
Ejemplo n.º 46
0
def test_isdisjoint():
    temp = PriorityDict((val, val) for val in range(50))
    that = PriorityDict((val, val) for val in range(50, 100))
    assert temp.isdisjoint(that)
Ejemplo n.º 47
0
def test_most_common():
    temp = PriorityDict({'a': 1, 'b': 2, 'c': 3, 'd': 4})
    assert temp.most_common() == [('d', 4), ('c', 3), ('b', 2), ('a', 1)]
    assert temp.most_common(2) == [('d', 4), ('c', 3)]
    temp._check()
Ejemplo n.º 48
0
def test_isdisjoint():
    temp = PriorityDict((val, val) for val in range(50))
    that = PriorityDict((val, val) for val in range(50, 100))
    assert temp.isdisjoint(that)
Ejemplo n.º 49
0
def test_popitem():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    assert (temp.popitem() == ('z', 25))
    assert (temp.popitem() == ('y', 24))
    assert (temp.popitem(0) == ('a', 0))
    temp._check()
Ejemplo n.º 50
0
def test_tally():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    temp.tally(list(string.lowercase))
    for pos, key in enumerate(string.lowercase):
        assert temp[key] == (pos + 1)
    temp._check()
Ejemplo n.º 51
0
def test_delitem():
    temp = PriorityDict(enumerate(string.lowercase))
    del temp[13]
    assert len(temp) == 25
    temp._check()
Ejemplo n.º 52
0
def test_count():
    seq = list((val, pos) for pos, val in enumerate(string.lowercase))
    pd = PriorityDict.count(val for val, pos in seq for num in range(pos))
    c = Counter(val for val, pos in seq for num in range(pos))
    assert pd == c
Ejemplo n.º 53
0
def test_iloc():
    temp = PriorityDict(enumerate(string.lowercase))
    assert len(temp.iloc) == len(string.lowercase)
Ejemplo n.º 54
0
def test_update_big():
    temp = PriorityDict((val, pos) for pos, val in enumerate(string.lowercase))
    temp.update((val, -pos) for pos, val in enumerate(string.lowercase[5:25]))
    for pos, val in enumerate(string.lowercase[5:25]):
        assert temp[val] == -pos
Ejemplo n.º 55
0
def test_iloc_getitem_slice():
    temp = PriorityDict(enumerate(string.lowercase))
    that = list(enumerate(string.lowercase))
    assert temp.iloc[5:20:3] == [5, 8, 11, 14, 17]
Ejemplo n.º 56
0
def test_index():
    temp = PriorityDict((val, pos) for pos, val in enumerate(string.lowercase))
    for key in string.lowercase:
        pos = temp.index(key)
        assert temp.iloc[pos] == key
Ejemplo n.º 57
0
def test_has_key():
    temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase))
    assert temp.has_key('r')
    temp._check()
Ejemplo n.º 58
0
def test_index_keyerror():
    temp = PriorityDict((val, pos) for pos, val in enumerate(string.lowercase))
    temp.index('aa')
Ejemplo n.º 59
0
def test_count():
    seq = list((val, pos) for pos, val in enumerate(string.lowercase))
    pd = PriorityDict.count(val for val, pos in seq for num in range(pos))
    c = Counter(val for val, pos in seq for num in range(pos))
    assert pd == c
Ejemplo n.º 60
0
def test_iadd_big():
    temp = PriorityDict((val, val) for val in range(100))
    that = PriorityDict((val, val) for val in range(100))
    temp += that
    assert all(temp[val] == 2 * val for val in range(100))
    temp._check()