Created
September 27, 2015 00:46
-
-
Save firegurafiku/1a10e7335e2e81e46883 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RandomSampleDict(object): | |
def __init__(self): | |
self.data = {} | |
self.cache_ik = {} | |
self.cache_ki = {} | |
self.track = [] | |
def lookup(self, key): | |
return self.data[key] | |
def set(self, key, value): | |
self.data[key] = value | |
def add(self, key, value): | |
self.data[key] = value | |
if len(self.track) == 0: | |
i = len(self.data) - 1 | |
else: | |
i = self.track.pop() | |
self.cache_ik[i] = key | |
self.cache_ki[key] = i | |
def delete(self, number): | |
del self.data[key] | |
i = self.cache_ik[i] | |
del self.data_ik[i] | |
del self.data_ki[key] | |
self.track.append(i) | |
def random_sample_key(self): | |
key = None | |
while key is None: | |
i = randint(0, len(self.data)) | |
if i in self.cache_ik: | |
return self.cache_ik[i] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment