How to quickly generate a new hash of a string after concatenating 2 strings

If my math is correct, I can quickly create a new hash value to concatenate the two lines, if I already have separate hash values ​​for each line. But only if the hash function has the form:

hash(n) = k * hash(n-1) + c(n), and h(0) = 0.

In this case

hash( concat(s1,s2) ) = k**length(s2) * hash(s1) + hash(s2)

eg.

h1  = makeHash32_SDBM( "abcdef",          6 );
h2  = makeHash32_SDBM( "ghijklmn",        8 );
h12 = makeHash32_SDBM( "abcdefghijklmn", 14 );
hx  = mod32_powI( 65599, 8 ) * h1 + h2;

h1  = 2534611139
h2  = 2107082500
h12 = 1695963591
hx  = 1695963591

Note that h12 = hx so this demonstrates the idea.

Now for SDBM hash k=65599. While it DJB hashhas k=33(or perhaps 31?) And h(0) = 5381, therefore, to make it work, you can install it instead h(0) = 0.

But modification on DJB hashuses xorinstead +to add each character.

http://www.cse.yorku.ca/~oz/hash.html

Is there another way to quickly calculate the hash value of concatenated strings if the hash function uses xorinstead +?

+3
1

, . - (, xor'e ..). ( + "" ).

, .

:
, f(x,y) :

h_abc = hashOf(h0, "abc")  
h_def = hashOf(h0, "def")  
(h_abcdef = f(h_abc, h_def)) == hashOf(h0, "abcdef")  

:

h_abc = hashOf(h1, "abc")  
(h_abcdef = hashOf(h_abc, "def")) == hashOf(h0, "abcdef")  

, , , "33" "2". "32" (2 ** 5), :

h_abcdef == (h_abc << (5*len(abc))) xor h_def
0

Source: https://habr.com/ru/post/1739747/


All Articles