# HG changeset patch # User Laman # Date 2017-05-07 17:29:12 # Node ID e6752944fe6becf8bc37cccfc8df12728bf8fd17 # Parent 34f4027c1bd62db77975f61c1aec8bbe130a7544 hashtree loading and saving diff --git a/src/hashtree.py b/src/hashtree.py --- a/src/hashtree.py +++ b/src/hashtree.py @@ -1,6 +1,5 @@ import hashlib import os -import collections class HashTree: @@ -9,7 +8,7 @@ class HashTree: ## Prepares a tree containing leafCount leaves. def __init__(self,leafCount): - self.store=[None]*(leafCount*2-1) + self.store=[b""]*(leafCount*2-1) self.leafStart=leafCount-1 self.index=self.leafStart self.leafCount=leafCount @@ -27,7 +26,23 @@ class HashTree: res.buildTree() return res - + + @classmethod + def load(cls,filename): + with open(filename,"rb") as f: + stat=os.fstat(f.fileno()) + size=stat.st_size + nodeCount=size//HashTree.HASH_LEN + res=cls((nodeCount+1)//2) + + for i in range(nodeCount): + res.store[i]=f.read(HashTree.HASH_LEN) + + def save(self,filename): + with open(filename,"wb") as f: + for h in self.store: + f.write(h) + ## Inserts a leaf at the first empty position. #