|
| 1 | +# Copyright 2013, Michael H. Goldwasser |
| 2 | +# |
| 3 | +# Developed for use with the book: |
| 4 | +# |
| 5 | +# Data Structures and Algorithms in Python |
| 6 | +# Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser |
| 7 | +# John Wiley & Sons, 2013 |
| 8 | +# |
| 9 | +# This program is free software: you can redistribute it and/or modify |
| 10 | +# it under the terms of the GNU General Public License as published by |
| 11 | +# the Free Software Foundation, either version 3 of the License, or |
| 12 | +# (at your option) any later version. |
| 13 | +# |
| 14 | +# This program is distributed in the hope that it will be useful, |
| 15 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +# GNU General Public License for more details. |
| 18 | +# |
| 19 | +# You should have received a copy of the GNU General Public License |
| 20 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + |
| 22 | +from .positional_list import PositionalList |
| 23 | + |
| 24 | +class FavoritesList: |
| 25 | + """List of elements ordered from most frequently accessed to least.""" |
| 26 | + |
| 27 | + #------------------------------ nested _Item class ------------------------------ |
| 28 | + class _Item: |
| 29 | + __slots__ = '_value', '_count' # streamline memory usage |
| 30 | + def __init__(self, e): |
| 31 | + self._value = e # the user's element |
| 32 | + self._count = 0 # access count initially zero |
| 33 | + |
| 34 | + #------------------------------- nonpublic utilities ------------------------------- |
| 35 | + def _find_position(self, e): |
| 36 | + """Search for element e and return its Position (or None if not found).""" |
| 37 | + walk = self._data.first() |
| 38 | + while walk is not None and walk.element()._value != e: |
| 39 | + walk = self._data.after(walk) |
| 40 | + return walk |
| 41 | + |
| 42 | + def _move_up(self, p): |
| 43 | + """Move item at Position p earlier in the list based on access count.""" |
| 44 | + if p != self._data.first(): # consider moving... |
| 45 | + cnt = p.element()._count |
| 46 | + walk = self._data.before(p) |
| 47 | + if cnt > walk.element()._count: # must shift forward |
| 48 | + while (walk != self._data.first() and |
| 49 | + cnt > self._data.before(walk).element()._count): |
| 50 | + walk = self._data.before(walk) |
| 51 | + self._data.add_before(walk, self._data.delete(p)) # delete/reinsert |
| 52 | + |
| 53 | + #------------------------------- public methods ------------------------------- |
| 54 | + def __init__(self): |
| 55 | + """Create an empty list of favorites.""" |
| 56 | + self._data = PositionalList() # will be list of _Item instances |
| 57 | + |
| 58 | + def __len__(self): |
| 59 | + """Return number of entries on favorites list.""" |
| 60 | + return len(self._data) |
| 61 | + |
| 62 | + def is_empty(self): |
| 63 | + """Return True if list is empty.""" |
| 64 | + return len(self._data) == 0 |
| 65 | + |
| 66 | + def access(self, e): |
| 67 | + """Access element e, thereby increasing its access count.""" |
| 68 | + p = self._find_position(e) # try to locate existing element |
| 69 | + if p is None: |
| 70 | + p = self._data.add_last(self._Item(e)) # if new, place at end |
| 71 | + p.element()._count += 1 # always increment count |
| 72 | + self._move_up(p) # consider moving forward |
| 73 | + |
| 74 | + def remove(self, e): |
| 75 | + """Remove element e from the list of favorites.""" |
| 76 | + p = self._find_position(e) # try to locate existing element |
| 77 | + if p is not None: |
| 78 | + self._data.delete(p) # delete, if found |
| 79 | + |
| 80 | + def top(self, k): |
| 81 | + """Generate sequence of top k elements in terms of access count.""" |
| 82 | + if not 1 <= k <= len(self): |
| 83 | + raise ValueError('Illegal value for k') |
| 84 | + walk = self._data.first() |
| 85 | + for j in range(k): |
| 86 | + item = walk.element() # element of list is _Item |
| 87 | + yield item._value # report user's element |
| 88 | + walk = self._data.after(walk) |
| 89 | + |
| 90 | + def __repr__(self): |
| 91 | + """Create string representation of the favorites list.""" |
| 92 | + return ', '.join('({0}:{1})'.format(i._value, i._count) for i in self._data) |
| 93 | + |
| 94 | +if __name__ == '__main__': |
| 95 | + fav = FavoritesList() |
| 96 | + for c in 'hello. this is a test of mtf': # well, not the mtf part... |
| 97 | + fav.access(c) |
| 98 | + k = min(5, len(fav)) |
| 99 | + print('Top {0}) {1:25} {2}'.format(k, [x for x in fav.top(k)], fav)) |
0 commit comments