home *** CD-ROM | disk | FTP | other *** search
/ Mundo do CD-ROM 118 / cdrom118.iso / internet / webaroo / WebarooSetup.exe / Webaroo.msi / _A0DEB44B94924E89917E71AA90C5F226 / BitTorrent / PiecePicker.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2005-12-23  |  4.0 KB  |  171 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. from random import randrange, shuffle, choice
  5.  
  6. class PiecePicker(object):
  7.     
  8.     def __init__(self, numpieces, config):
  9.         self.config = config
  10.         self.numpieces = numpieces
  11.         self.interests = [
  12.             range(numpieces)]
  13.         self.pos_in_interests = range(numpieces)
  14.         self.numinterests = [
  15.             0] * numpieces
  16.         self.have = [
  17.             False] * numpieces
  18.         self.crosscount = [
  19.             numpieces]
  20.         self.started = []
  21.         self.seedstarted = []
  22.         self.numgot = 0
  23.         self.scrambled = range(numpieces)
  24.         shuffle(self.scrambled)
  25.  
  26.     
  27.     def got_have(self, piece):
  28.         numint = self.numinterests[piece]
  29.         self.crosscount[numint + self.have[piece]] -= 1
  30.         self.numinterests[piece] += 1
  31.         
  32.         try:
  33.             self.crosscount[numint + 1 + self.have[piece]] += 1
  34.         except IndexError:
  35.             self.crosscount.append(1)
  36.  
  37.         if self.have[piece]:
  38.             return None
  39.         
  40.         if numint == len(self.interests) - 1:
  41.             self.interests.append([])
  42.         
  43.         self._shift_over(piece, self.interests[numint], self.interests[numint + 1])
  44.  
  45.     
  46.     def lost_have(self, piece):
  47.         numint = self.numinterests[piece]
  48.         self.crosscount[numint + self.have[piece]] -= 1
  49.         self.numinterests[piece] -= 1
  50.         self.crosscount[(numint - 1) + self.have[piece]] += 1
  51.         if self.have[piece]:
  52.             return None
  53.         
  54.         self._shift_over(piece, self.interests[numint], self.interests[numint - 1])
  55.  
  56.     
  57.     def _shift_over(self, piece, l1, l2):
  58.         p = self.pos_in_interests[piece]
  59.         l1[p] = l1[-1]
  60.         self.pos_in_interests[l1[-1]] = p
  61.         del l1[-1]
  62.         newp = randrange(len(l2) + 1)
  63.         if newp == len(l2):
  64.             self.pos_in_interests[piece] = len(l2)
  65.             l2.append(piece)
  66.         else:
  67.             old = l2[newp]
  68.             self.pos_in_interests[old] = len(l2)
  69.             l2.append(old)
  70.             l2[newp] = piece
  71.             self.pos_in_interests[piece] = newp
  72.  
  73.     
  74.     def requested(self, piece, seed = False):
  75.         if piece not in self.started:
  76.             self.started.append(piece)
  77.         
  78.         if seed and piece not in self.seedstarted:
  79.             self.seedstarted.append(piece)
  80.         
  81.  
  82.     
  83.     def complete(self, piece):
  84.         if not not self.have[piece]:
  85.             raise AssertionError
  86.         self.have[piece] = True
  87.         self.crosscount[self.numinterests[piece]] -= 1
  88.         
  89.         try:
  90.             self.crosscount[self.numinterests[piece] + 1] += 1
  91.         except IndexError:
  92.             self.crosscount.append(1)
  93.  
  94.         self.numgot += 1
  95.         l = self.interests[self.numinterests[piece]]
  96.         p = self.pos_in_interests[piece]
  97.         l[p] = l[-1]
  98.         self.pos_in_interests[l[-1]] = p
  99.         del l[-1]
  100.         
  101.         try:
  102.             self.started.remove(piece)
  103.             self.seedstarted.remove(piece)
  104.         except ValueError:
  105.             self
  106.             self
  107.         except:
  108.             self
  109.  
  110.  
  111.     
  112.     def next(self, havefunc, seed = False):
  113.         bests = None
  114.         bestnum = 2 ** 30
  115.         if seed:
  116.             s = self.seedstarted
  117.         else:
  118.             s = self.started
  119.         for i in s:
  120.             if havefunc(i):
  121.                 if self.numinterests[i] < bestnum:
  122.                     bests = [
  123.                         i]
  124.                     bestnum = self.numinterests[i]
  125.                 elif self.numinterests[i] == bestnum:
  126.                     bests.append(i)
  127.                 
  128.             self.numinterests[i] < bestnum
  129.         
  130.         if bests:
  131.             return choice(bests)
  132.         
  133.         if self.numgot < self.config['rarest_first_cutoff']:
  134.             for i in self.scrambled:
  135.                 if havefunc(i):
  136.                     return i
  137.                     continue
  138.             
  139.             return None
  140.         
  141.         for i in xrange(1, min(bestnum, len(self.interests))):
  142.             for j in self.interests[i]:
  143.                 if havefunc(j):
  144.                     return j
  145.                     continue
  146.             
  147.         
  148.  
  149.     
  150.     def am_I_complete(self):
  151.         return self.numgot == self.numpieces
  152.  
  153.     
  154.     def bump(self, piece):
  155.         l = self.interests[self.numinterests[piece]]
  156.         pos = self.pos_in_interests[piece]
  157.         del l[pos]
  158.         l.append(piece)
  159.         for i in range(pos, len(l)):
  160.             self.pos_in_interests[l[i]] = i
  161.         
  162.         
  163.         try:
  164.             self.started.remove(piece)
  165.             self.seedstarted.remove(piece)
  166.         except ValueError:
  167.             pass
  168.  
  169.  
  170.  
  171.