Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import log, ceil
- class HashTable:
- def __init__(self, nc, lk, ld, nums):
- self.nc = nc
- self.lk = lk
- self.ld = ld
- self.nums = nums
- self.base = len(nums)
- self.ll = ceil(log(nc, self.base))
- self.lc = lk + ld + self.ll
- self.lbd = self.lc*nc
- self.db = (chr(0)*(lk+ld) +
- chr(0)*self.ll)*nc
- def add(self, k, d):
- if len(k) > self.lk:
- SyntaxError("too long key")
- elif len(d) > self.ld:
- SyntaxError("too long data")
- i0 = self.h(k) % self.nc
- if db[i0*self.lc:(i0+1)*self.lc]:
- pass
- def h(self, msg, i=-1):
- # i=0 - sha256
- # i=-1 - my algorithm
- if i == 0:
- from hashlib import sha256
- return int.from_bytes(
- sha256(msg.encode()).digest())
- if self.nc <= 10:
- p = 11
- elif self.nc <= 100:
- p = 101
- elif self.nc <= 1000:
- p = 1009
- else:
- p = 10007
- res = 0
- for i in msg:
- res += ord(i)
- res *= p
- return res
- def srepl(l, s, i):
- return l[:i] + s + l[i+1:]
- def __str__(self):
- return self.db
- # IN PLAN
- # hashtable-object, hash-method,
- # hashtable.add(...)
- nums = "0123456789ABCDEF"
- h = HashTable(9, 3, 5, nums)
- print(h.db)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement