aboutsummaryrefslogtreecommitdiff
path: root/diplomacy/utils/priority_dict.py
diff options
context:
space:
mode:
authorPhilip Paquette <pcpaquette@gmail.com>2018-09-26 07:48:55 -0400
committerPhilip Paquette <pcpaquette@gmail.com>2019-04-18 11:14:24 -0400
commit6187faf20384b0c5a4966343b2d4ca47f8b11e45 (patch)
tree151ccd21aea20180432c13fe4b58240d3d9e98b6 /diplomacy/utils/priority_dict.py
parent96b7e2c03ed98705754f13ae8efa808b948ee3a8 (diff)
Release v1.0.0 - Diplomacy Game Engine - AGPL v3+ License
Diffstat (limited to 'diplomacy/utils/priority_dict.py')
-rw-r--r--diplomacy/utils/priority_dict.py102
1 files changed, 102 insertions, 0 deletions
diff --git a/diplomacy/utils/priority_dict.py b/diplomacy/utils/priority_dict.py
new file mode 100644
index 0000000..99a75c9
--- /dev/null
+++ b/diplomacy/utils/priority_dict.py
@@ -0,0 +1,102 @@
+# ==============================================================================
+# Copyright (C) 2019 - Philip Paquette, Steven Bocco
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Affero General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Affero General Public License along
+# with this program. If not, see <https://www.gnu.org/licenses/>.
+# ==============================================================================
+""" Priority Dict implementation """
+import heapq
+
+# ------------------------------------------------
+# Adapted from (2018/03/14s): https://docs.python.org/3.6/library/heapq.html#priority-queue-implementation-notes
+# Unlicensed
+class PriorityDict(dict):
+ """ Priority Dictionary Implementation """
+
+ def __init__(self, **kwargs):
+ """ Initialize the priority queue.
+ :param kwargs: (optional) initial values for priority queue.
+ """
+ self.__heap = [] # Heap for entries. An entry is a triple (priority value, key, valid entry flag (boolean)).
+ # Dict itself maps key to entries. We override some dict methods (see __getitem__() below)
+ # to always return priority value instead of entry as dict value.
+ dict.__init__(self)
+ for key, value in kwargs.items():
+ self[key] = value
+
+ def __setitem__(self, key, val):
+ """ Sets a key with his associated priority
+ :param key: The key to set in the dictionary
+ :param val: The priority to associate with the key
+ :return: None
+ """
+ if key in self:
+ del self[key]
+ # Create entry with val, key and a boolean indicating that entry is valid (True).
+ entry = [val, key, True]
+ dict.__setitem__(self, key, entry)
+ heapq.heappush(self.__heap, entry)
+
+ def __delitem__(self, key):
+ """ Removes key from dict and marks associated heap entry as invalid (False). Raises KeyError if not found. """
+ entry = self.pop(key)
+ entry[-1] = False
+
+ def __getitem__(self, key):
+ """ Returns priority value associated to key. Raises KeyError if key not found. """
+ return dict.__getitem__(self, key)[0]
+
+ def __iter__(self):
+ """ Iterator over all keys based on their priority. """
+
+ def iterfn():
+ """ Iterator """
+ copy_of_self = self.copy()
+ while copy_of_self:
+ _, key = copy_of_self.smallest()
+ del copy_of_self[key]
+ yield key
+
+ return iterfn()
+
+ def smallest(self):
+ """ Finds the smallest item in the priority dict
+ :return: A tuple of (priority, key) for the item with the smallest priority
+ """
+ while self.__heap and not self.__heap[0][-1]:
+ heapq.heappop(self.__heap)
+ return self.__heap[0][:2] if self.__heap else None
+
+ def setdefault(self, key, d=None):
+ """ Sets a default for a given key """
+ if key not in self:
+ self[key] = d
+ return self[key]
+
+ def copy(self):
+ """ Return a copy of this priority dict.
+ :rtype: PriorityDict
+ """
+ return PriorityDict(**self)
+
+ def keys(self):
+ """ Make sure keys() iterates on keys based on their priority. """
+ return self.__iter__()
+
+ def values(self):
+ """ Makes sure values() iterates on priority values (instead of heap entries) from smallest to highest. """
+ return (self[k] for k in self)
+
+ def items(self):
+ """ Makes sure items() values are priority values instead of heap entries. """
+ return ((key, self[key]) for key in self)