Struct comlib_string::RollingHash[][src]

pub struct RollingHash<M = Mod1e9p7> where
    M: InvertibleModulus + Copy
{ /* fields omitted */ }
Expand description

Rolling hash for strings

Current implementation

The rolling hash is based on the following idea: Let s = c0c1c2…cn-1 be a string and let x be an element of a modular group. Now we can construct a hash h = c0 + c1x + c2x2 + … + cn-1xn-1 for the whole string. Because h is evaluated in a modular group, it is not unique, but it is unlikely to find two strings which produce the same hash unless they are produced by an adversary1. Now the interesting part is that we can compute the hash for any substring sl…r = clcl+1…cr-1cr as hl…r = (clxl + cl+1xl+1 + … + crxr) / xl. The sum can be computed efficiently by storing the terms in a Binary indexed tree which allow querying the sum over a range in O(log n) time. The Binary indexed tree also allows updating the terms of the sum in O(log n) time meaning that we can modify the string one character at a time.

1: The change of a collision attack is tried to be mitigated by randomly choosing the value of x for each run.

Implementations

Constructs new RollingHash.

The x is chosen randomly

Constructs new RollingHash which uses the given x.

Gets the hash of the substring over the given range.

Note that the range is given in characters, not in bytes like with str.

Replaces the character at the given index with new one.

Returns the x used for hashing.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.