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 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()
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()
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
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
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()
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)
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))
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)
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()
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()
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))
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
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()
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()
def test_iloc_getitem(): temp = PriorityDict(enumerate(string.lowercase)) for pos, letter in enumerate(string.lowercase): assert temp[temp.iloc[pos]] == letter temp._check()
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()
def test_clear(): temp = PriorityDict(enumerate(string.lowercase)) assert len(temp) == 26 temp.clear() assert len(temp) == 0 temp._check()
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()
def test_elements(): temp = PriorityDict({'a': 3, 'b': 2, 'c': 1}) assert ['c', 'b', 'b', 'a', 'a', 'a'] == list(temp.elements())
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()
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
def test_reversed(): temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase)) assert list(reversed(temp)) == list(reversed(string.lowercase)) temp._check()
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
def test_iter(): temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase)) assert list(iter(temp)) == list(string.lowercase) temp._check()
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
def test_index_keyerror(): temp = PriorityDict((val, pos) for pos, val in enumerate(string.lowercase)) temp.index('aa')
def test_delitem(): temp = PriorityDict(enumerate(string.lowercase)) del temp[13] assert len(temp) == 25 temp._check()
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()
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()
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
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()
def test_pop_error(): temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase)) temp.pop('blah')
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)
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()
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
def test_iloc(): temp = PriorityDict(enumerate(string.lowercase)) assert len(temp.iloc) == len(string.lowercase)
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]
def test_has_key(): temp = PriorityDict((val, key) for key, val in enumerate(string.lowercase)) assert temp.has_key('r') temp._check()