Logo Search packages:      
Sourcecode: zeitgeist version File versions

def _zeitgeist::lrucache::LRUCache::__init__ (   self,
  max_size 
)

The size of the cache (in number of cached items) is guaranteed to
   never exceed 'size'

Definition at line 41 of file lrucache.py.

00041                                   :
            """The size of the cache (in number of cached items) is guaranteed to
               never exceed 'size'"""
            self._max_size = max_size
            self._heap = []
            self._map = {}
            self._current_id = 0
      
      def __len__(self):
            return len(self._map)
      
      def __contains__(self, key):
            return key in self._map
      
      def __setitem__(self, key, value):
            if key in self._map:
                  item = self._map[key]
                  item.id = self._current_id
                  item.value = value
                  heapify(self._heap)                 
            elif self._current_id > self._max_size - 1:
                  new = LRUCache._Item(self._current_id, key, value)
                  old = heapreplace(self._heap, new)
                  del self._map[old.key]
                  self._map[key] = new
            else:
                  new = LRUCache._Item(self._current_id, key, value)
                  self._map[key] = new
                  heappush(self._heap, new)
            
            self._current_id += 1
      
      def __getitem__(self, key):
            item = self._map[key]
            item.id = self._current_id
            heapify(self._heap)
            self._current_id += 1
            return item.value
      
      def __iter__(self):
            """Iteration is not in any particular order!"""
            for item in self._heap:
                  yield item.value
                  yield item.value


Generated by  Doxygen 1.6.0   Back to index