-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfingerprint.py
More file actions
72 lines (62 loc) · 2.05 KB
/
fingerprint.py
File metadata and controls
72 lines (62 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 14 21:36:49 2019
@author: devd
"""
import random
import sample_streams as strm
from sketch_abstract import Sketch
from random import getrandbits
def is_prime(n):
""""pre-condition: n is a nonnegative integer
post-condition: return True if n is prime and False otherwise.
this code stolen from stackoverflow"""
if n < 2:
return False;
if n % 2 == 0:
return n == 2 # return False
k = 3
while k*k <= n:
if n % k == 0:
return False
k += 2
return True
def choose_p(n):
"""p must be prime and also poly(n)."""
target = pow(n,3)
while True:
target += 1
if is_prime(target):
break
return target
class Fingerprint_Sketch(Sketch):
"""Maps a length n vector to an integer. The integer value of the sketch
is a fingerprint of the input defined by the stream. The same input
defined by two different streams will sketch to the same fingerprint value.
For any two different inputs, the probability that they will be sketched
to the same fingerprint value is polynomially low."""
def __init__(self, n, seed=None):
if seed==None:
seed=getrandbits(32)
self.p = choose_p(n)
random.seed(seed)
self.r = random.randint(1,self.p-1)
self.sketch = 0
def update(self, element):
"""Sketches each element in the stream """
index, value = element
self.sketch += value * pow(self.r, index, self.p)
def process_stream(self, stream):
"""See super()"""
super(Fingerprint_Sketch, self).process_stream(stream)
def query(self):
"""Returns the fingerprint of the input stream."""
return self.sketch
if __name__=='__main__':
n = 100
f1 = Fingerprint_Sketch(n)
f1.process_stream(strm.SampleStream(n))
f2 = Fingerprint_Sketch(n)
f2.process_stream(strm.SampleStream2(n))
print(f1.query(), f2.query())