simple_hash.html
001|
002| from sys import argv
003| from os import path
004| from goodies import KWARG
005| from files import SPC,EOL,CR,LF, linefile
006|
007| BasePath = r'C:\CJS\prj\Python\ws\@sandbox'
008| HtmlPath = path.join(BasePath, 'out')
009|
010|
011| Words = [
012| 'TEA', 'tea', 'Python', 'water', 'Earth', 'crazyshit',
013| 'new', 'thing', 'her', 'that', 'something', 'post', 'last', 'both', 'us', 'know',
014| 'most', 'over', 'right', 'here', 'things', 'another', 'three', 'your', 'same', 'its',
015| 'she', 'well', 'does', 'never', 'long', 'being', 'will', 'seems', 'were', 'off',
016| 'go', 'amp', 'part', 'show', 'years', 'might', 'world', 'make', 'always', 'where',
017| 'life', 'bit', 'number', 'why', 'too', 'say', 'down', 'may', 'still', 'light',
018| 'science', 'story', 'after', 'real', 'did', 'such', 'idea', 'work', 'while', 'lot',
019| 'day', 'each', 'series', 'year', 'again', 'could', 'line', 'find', 'system', 'between',
020| 'space', 'big', 'least', 'use', 'numbers', 'through', 'left', 'different', 'far', 'got',
021| 'end', 'actually', 'reality', 'makes', 'little', 'going', 'since', 'book', 'him', 'made',
022| 'need', 'take', 'hard', 'pretty',
023| ]
024|
025|
026| class bloom_filter:
027| def __init__ (self):
028| self.bf = [0]*256
029|
030| @property
031| def maxi (self): return max(self.bf)
032|
033| @property
034| def zeros (self): return sum(1 for n in self.bf if n==0)
035|
036| @property
037| def filled (self): return sum(1 for n in self.bf if n)
038|
039| def _hash_word (self, word:str):
040| return 0xff & hash(word)
041|
042| def __len__ (self): return len(self.bf)
043| def __iter__ (self): return iter(self.bf)
044| def __getitem__ (self, ix): return self.bf[ix]
045|
046| def __iadd__ (self, word:str):
047| h = self._hash_word(word)
048| self.bf[h] += 1
049| return self
050|
051| def __isub__ (self, word:str):
052| h = self._hash_word(word)
053| self.bf[h] += 1
054| return self
055|
056| def __repr__ (self):
057| return ' '.join(str(n) for n in self.bf)
058|
059|
060| def demo_bloom_filter ():
061| bf = bloom_filter()
062|
063| for word in Words:
064| bf += word
065|
066| print(len(bf))
067| print(f'Max: {bf.maxi}')
068| print(f'Used: {bf.filled:3d}')
069| print(f'Zero: {bf.zeros:3d}')
070| print()
071| print(bf)
072| print()
073| return bf
074|
075|
076|
077| def simple_hash (s:str) -> int:
078| n = 0
079| for c in s:
080| n += ord(c)
081| n <<= 1
082| return n & 0xff
083|
084|
085| def string_hash (s:str) -> int:
086| n = 1
087| for c in s:
088| n *= ord(c)
089| n >>= 1
090| return n & 0xff
091|
092|
093| def demo_string_hashing ():
094| bloom = [0]*256
095|
096| for word in Words:
097| h = 0xff & hash(word)
098|
099|
100| print(f'{word:<12s} = {h:02x}')
101| bloom[h] += 1
102|
103| print(' '.join(str(n) for n in bloom))
104| print(f'Max={max(bloom)}')
105| print(f'Zeros: {sum(1 for n in bloom if n==0)}')
106| return bloom
107|
108|
109| def main ():
110| return demo_bloom_filter()
111|
112|
113|
114| if __name__ == '__main__':
115| print(argv[0])
116| print()
117| rv = main()
118| print()
119|
120| '''eof'''
121|