home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2387 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  2.0 KB  |  91 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import threading
  5. _marker = object()
  6.  
  7. class LRUCache(object):
  8.     
  9.     def __init__(self, size):
  10.         if size < 1:
  11.             raise ValueError('size must be >1')
  12.         size < 1
  13.         self.clock = []
  14.         for i in xrange(0, size):
  15.             self.clock.append({
  16.                 'key': _marker,
  17.                 'ref': False })
  18.         
  19.         self.size = size
  20.         self.maxpos = size - 1
  21.         self.hand = 0
  22.         self.data = { }
  23.         self.lock = threading.Lock()
  24.  
  25.     
  26.     def __contains__(self, key):
  27.         return key in self.data
  28.  
  29.     
  30.     def __getitem__(self, key, default = None):
  31.         
  32.         try:
  33.             datum = self.data[key]
  34.         except KeyError:
  35.             return default
  36.  
  37.         (pos, val) = datum
  38.         self.clock[pos]['ref'] = True
  39.         hand = pos + 1
  40.         if hand > self.maxpos:
  41.             hand = 0
  42.         
  43.         self.hand = hand
  44.         return val
  45.  
  46.     
  47.     def __setitem__(self, key, val, _marker = _marker):
  48.         hand = self.hand
  49.         maxpos = self.maxpos
  50.         clock = self.clock
  51.         data = self.data
  52.         lock = self.lock
  53.         end = hand - 1
  54.         if end < 0:
  55.             end = maxpos
  56.         
  57.         while None:
  58.             current = clock[hand]
  59.             ref = current['ref']
  60.             if ref is True:
  61.                 current['ref'] = False
  62.                 hand = hand + 1
  63.                 if hand > maxpos:
  64.                     hand = 0
  65.                 
  66.             if ref is False or hand == end:
  67.                 lock.acquire()
  68.                 
  69.                 try:
  70.                     oldkey = current['key']
  71.                     if oldkey in data:
  72.                         del data[oldkey]
  73.                     
  74.                     current['key'] = key
  75.                     current['ref'] = True
  76.                     data[key] = (hand, val)
  77.                     hand += 1
  78.                     if hand > maxpos:
  79.                         hand = 0
  80.                     
  81.                     self.hand = hand
  82.                 finally:
  83.                     lock.release()
  84.  
  85.                 break
  86.                 continue
  87.             continue
  88.             return None
  89.  
  90.  
  91.