Advertisement
DolenkoArtem

Hashtable

May 23rd, 2025
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. from math import log, ceil
  2.  
  3. class HashTable:
  4.     def __init__(self, nc, lk, ld, nums):
  5.         self.nc = nc
  6.         self.lk = lk
  7.         self.ld = ld
  8.         self.nums = nums
  9.         self.base = len(nums)
  10.         self.ll = ceil(log(nc, self.base))
  11.         self.lc = lk + ld + self.ll
  12.         self.lbd = self.lc*nc
  13.         self.db = (chr(0)*(lk+ld) +
  14.                    chr(0)*self.ll)*nc
  15.    
  16.     def add(self, k, d):
  17.         if len(k) > self.lk:
  18.             SyntaxError("too long key")
  19.         elif len(d) > self.ld:
  20.             SyntaxError("too long data")
  21.        
  22.         i0 = self.h(k) % self.nc
  23.         if db[i0*self.lc:(i0+1)*self.lc]:
  24.             pass
  25.        
  26.     def h(self, msg, i=-1):
  27.         # i=0  - sha256
  28.         # i=-1 - my algorithm
  29.         if i == 0:
  30.             from hashlib import sha256
  31.             return int.from_bytes(
  32.             sha256(msg.encode()).digest())
  33.    
  34.         if self.nc <= 10:
  35.             p = 11
  36.         elif self.nc <= 100:
  37.             p = 101
  38.         elif self.nc <= 1000:
  39.             p = 1009
  40.         else:
  41.             p = 10007
  42.    
  43.         res = 0
  44.         for i in msg:
  45.             res += ord(i)
  46.         res *= p
  47.         return res
  48.        
  49.     def srepl(l, s, i):
  50.         return l[:i] + s + l[i+1:]
  51.    
  52.     def __str__(self):
  53.         return self.db
  54.  
  55. # IN PLAN
  56. # hashtable-object, hash-method,
  57. # hashtable.add(...)
  58.  
  59. nums = "0123456789ABCDEF"
  60. h = HashTable(9, 3, 5, nums)
  61. print(h.db)
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement