paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | Set objects. |
paul@6 | 5 | |
paul@528 | 6 | Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@6 | 7 | |
paul@6 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@6 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@6 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@6 | 11 | version. |
paul@6 | 12 | |
paul@6 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@6 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@6 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@6 | 16 | details. |
paul@6 | 17 | |
paul@6 | 18 | You should have received a copy of the GNU General Public License along with |
paul@6 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@6 | 20 | """ |
paul@6 | 21 | |
paul@528 | 22 | from __builtins__.iteration.iterator import itemiterator |
paul@542 | 23 | from __builtins__.mapping import hashtable |
paul@528 | 24 | |
paul@542 | 25 | class frozenset(hashtable): |
paul@542 | 26 | |
paul@542 | 27 | "An immutable set representation holding a collection of distinct values." |
paul@542 | 28 | |
paul@542 | 29 | def __init__(self, iterable=None): |
paul@542 | 30 | |
paul@542 | 31 | "Initialise the set with any given 'iterable'." |
paul@542 | 32 | |
paul@542 | 33 | self.clear() |
paul@542 | 34 | |
paul@542 | 35 | if iterable is not None: |
paul@542 | 36 | for value in iterable: |
paul@542 | 37 | self._setitem(self.buckets, value) |
paul@542 | 38 | |
paul@542 | 39 | # Implementation methods. |
paul@542 | 40 | |
paul@542 | 41 | def _find_entry(self, value, index): |
paul@542 | 42 | |
paul@542 | 43 | "Search for 'value', using an 'index' identifying the bucket involved." |
paul@542 | 44 | |
paul@542 | 45 | i = 0 |
paul@542 | 46 | |
paul@542 | 47 | for found in self.buckets[index]: |
paul@542 | 48 | if found == value: |
paul@542 | 49 | return i |
paul@542 | 50 | i += 1 |
paul@542 | 51 | |
paul@542 | 52 | return None |
paul@542 | 53 | |
paul@542 | 54 | def _resize(self, capacity): |
paul@542 | 55 | |
paul@542 | 56 | "Resize the hashtable to have the given 'capacity'." |
paul@542 | 57 | |
paul@542 | 58 | buckets = self._get_buckets(capacity) |
paul@542 | 59 | |
paul@542 | 60 | for value in self._items(): |
paul@542 | 61 | self._setitem(buckets, value) |
paul@542 | 62 | |
paul@542 | 63 | self.buckets = buckets |
paul@542 | 64 | |
paul@542 | 65 | def _setitem(self, buckets, value): |
paul@6 | 66 | |
paul@542 | 67 | "Set in the 'buckets' an item having the given 'value'." |
paul@542 | 68 | |
paul@542 | 69 | index, i = self._get_entry(value) |
paul@542 | 70 | |
paul@542 | 71 | # With no existing entry, append to the bucket. |
paul@542 | 72 | |
paul@542 | 73 | if i is None: |
paul@542 | 74 | buckets[index].append(value) |
paul@542 | 75 | self.size += 1 |
paul@542 | 76 | |
paul@542 | 77 | # With an existing entry, replace the item. |
paul@542 | 78 | |
paul@542 | 79 | else: |
paul@542 | 80 | buckets[index][i] = value |
paul@542 | 81 | |
paul@542 | 82 | # String representations. |
paul@542 | 83 | |
paul@542 | 84 | def _str(self, name): |
paul@542 | 85 | |
paul@542 | 86 | "Return a string representation." |
paul@542 | 87 | |
paul@542 | 88 | b = buffer() |
paul@542 | 89 | b.append(name) |
paul@542 | 90 | b.append("(") |
paul@542 | 91 | b.append(self._items().__repr__()) |
paul@542 | 92 | b.append(")") |
paul@542 | 93 | return str(b) |
paul@542 | 94 | |
paul@542 | 95 | def __str__(self): |
paul@542 | 96 | |
paul@542 | 97 | "Return a string representation." |
paul@542 | 98 | |
paul@542 | 99 | return self._str("frozenset") |
paul@542 | 100 | |
paul@542 | 101 | __repr__ = __str__ |
paul@542 | 102 | |
paul@542 | 103 | # Public special methods. |
paul@542 | 104 | |
paul@542 | 105 | def __contains__(self, value): |
paul@542 | 106 | |
paul@542 | 107 | "Return whether 'value' is in the set." |
paul@542 | 108 | |
paul@542 | 109 | index, i = self._get_entry(value) |
paul@542 | 110 | return i is not None |
paul@6 | 111 | |
paul@6 | 112 | def __iter__(self): |
paul@6 | 113 | |
paul@6 | 114 | "Return an iterator." |
paul@6 | 115 | |
paul@290 | 116 | return itemiterator(list(self)) |
paul@6 | 117 | |
paul@542 | 118 | # Public conventional methods. |
paul@542 | 119 | |
paul@542 | 120 | def copy(self): pass |
paul@542 | 121 | def difference(self, other): pass |
paul@542 | 122 | def intersection(self, other): pass |
paul@542 | 123 | def issubset(self, other): pass |
paul@542 | 124 | def issuperset(self, other): pass |
paul@6 | 125 | def symmetric_difference(self, other): pass |
paul@542 | 126 | def union(self, other): pass |
paul@542 | 127 | |
paul@542 | 128 | class set(frozenset): |
paul@542 | 129 | |
paul@542 | 130 | "A mutable set representation holding a collection of distinct values." |
paul@542 | 131 | |
paul@542 | 132 | # String representation methods. |
paul@542 | 133 | |
paul@542 | 134 | def __str__(self): |
paul@542 | 135 | |
paul@542 | 136 | "Return a string representation." |
paul@542 | 137 | |
paul@542 | 138 | return self._str("set") |
paul@542 | 139 | |
paul@542 | 140 | __repr__ = __str__ |
paul@542 | 141 | |
paul@542 | 142 | # Public conventional methods. |
paul@542 | 143 | |
paul@542 | 144 | def add(self, value): |
paul@542 | 145 | |
paul@542 | 146 | "Add the given 'value' to the set." |
paul@542 | 147 | |
paul@542 | 148 | capacity = len(self.buckets) |
paul@542 | 149 | |
paul@542 | 150 | if self.size > capacity: |
paul@542 | 151 | self._resize(capacity * 2) |
paul@542 | 152 | |
paul@542 | 153 | self._setitem(self.buckets, value) |
paul@542 | 154 | |
paul@542 | 155 | def difference_update(self, other): pass |
paul@542 | 156 | |
paul@542 | 157 | def discard(self, value): |
paul@542 | 158 | |
paul@542 | 159 | "Remove 'value' from the set, if present." |
paul@542 | 160 | |
paul@542 | 161 | try: |
paul@542 | 162 | self.remove(value) |
paul@542 | 163 | except KeyError: |
paul@542 | 164 | pass |
paul@542 | 165 | |
paul@542 | 166 | def intersection_update(self, other): pass |
paul@542 | 167 | |
paul@542 | 168 | def pop(self): pass |
paul@542 | 169 | |
paul@542 | 170 | def remove(self, value): |
paul@542 | 171 | |
paul@542 | 172 | "Remove 'value' from the set." |
paul@542 | 173 | |
paul@542 | 174 | self._remove_entry(value) |
paul@542 | 175 | |
paul@6 | 176 | def symmetric_difference_update(self, other): pass |
paul@542 | 177 | |
paul@6 | 178 | def update(self, other): pass |
paul@6 | 179 | |
paul@6 | 180 | # vim: tabstop=4 expandtab shiftwidth=4 |