OpenCores
URL https://opencores.org/ocsvn/bluespec_md6/bluespec_md6/trunk

Subversion Repositories bluespec_md6

[/] [bluespec_md6/] [trunk/] [C_implementation/] [treewalk.py] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 kfleming
# treewalk.py
2
# Ronald L. Rivest
3
# 9/14/07
4
# Compute hash of sequence D[0..n-1]
5
# by first applying leaf_hash to each D[i]
6
# and then applying combine_hash(x,y) to each
7
# pair to reduce number of values by two, repeatedly
8
# until only one value is left. (Complicated way of
9
# saying that we are computing up a binary tree....)
10
# Hash of an empty subtree (no leaves) is zero by definition.
11
 
12
def H(i,k):
13
    """
14
    Compute level-k hash function on D[i..i+(2**k)-1]
15
    k = level of hash; k = 0 for leaf, parent has level = 1 + level(child)
16
    k = Infinity for initial call.
17
    Assume X(i) == True iff D[i] exists (i.e. i is not past EOF).
18
    In sequential implementation, X called on non-decreasing values for i.
19
    """
20
    if not X(i):
21
        return 0                     # empty subtree
22
    if k == 0:
23
        return leaf_hash(D[i])       # leaf
24
    if k != Infinity:
25
        # CILK spawn:
26
        L = H(i,k-1)                 # left subtree
27
        # CILK spawn:
28
        R = H(i+2**(k-1),k-1)        # right subtree
29
        # CILK sync:
30
        return combine_hash(L,R)     # combine them
31
    else:
32
        # determine correct level k dynamically
33
        k = 0
34
        L = H(i,k)
35
        while X(i+2**k):
36
            # now L = hash on D[i..i+2**k-1]
37
            R = H(i+2**k,k)
38
            L = combine_hash(L,R)
39
            k += 1
40
        return L
41
 
42
def leaf_hash(D):
43
    """ Dummy leaf_hash just returns input """
44
    return D
45
 
46
def combine_hash(L,R):
47
    """ Dummy combine_hash just returns sum of inputs """
48
    return L+R
49
 
50
# Global variable array D is input to be hashed
51
D = range(20)
52
 
53
def X(i):
54
    """ Test if D[i] exists """
55
    print "X",i
56
    return i<len(D)
57
 
58
Infinity = 1000
59
 
60
# Test call
61
print H(0,Infinity)

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.