how to perform opertions on a hash table? - hashtable

For example hash function hash(x) = x mod 10. Perform the operation on examples like this and create a hash table
add(17, A)
remove(17)
add(27, A)
add(27,B)
How do I perform an operation on it and create a hash table?

Related

Update functions in Hash Table in Racket

I am a beginner in Racket and I am trying to updare a hash table using hash-update! where the value is a mutable set. Below are the code lines:
(hash-update! hash key (curryr set-add! new_val) (mutable-set 1))
However I receive an error
expected: set?
given: #<void>
argument position: 1st
other arguments...:
x: 2
where I tried 2 as the new_val
Any suggestions ?
This is because the updater is supposed to be a function that takes a value as input and produces a new value output. Since the set is mutable and you're using set-add! to mutate it, the "updater" isn't returning a new value, just mutating the old one and producing void.
There are two ways to fix this:
Mutable sets as values, mutate them separately, not inside hash-update!.
Immutable sets as values, use a functional updater inside hash-update!.
Since you specified you want the values as mutable sets, I will show (1).
The most basic thing you can do is hash-ref to get a mutable-set, and then use set-add! on that.
(set-add! (hash-ref hash key) new-val)
However, this doesn't work when there is no mutable-set value for that key yet. It needs to be added to the table when it doesn't exist yet which why is why you have the (mutable-set 1) failure-result argument. The solution to this isn't hash-update!, it's hash-ref!.
(set-add! (hash-ref! hash key (mutable-set 1)) new-val)
Although it would probably be better if you wrapped the failure-result in a thunk
(set-add! (hash-ref! hash key (λ () (mutable-set 1))) new-val)

Hash functions in R: keys and values as doubles?

I'm fairly new to R. I want to use a hash function to hold key-value pairs as doubles/numerics.
I have a function f(x) = y where the function f takes the input x as a double and returns y as a double. I use this function f(x) over 10^8 times in my code (and I would like to use it much more often), and 99% of the time f(x) has been computed once already. I would like to store my answers as key-value pairs, so I can find them instead of calculating them again.
I read the article below about using environments as hash tables, but I cannot figure out how to use doubles as key-value pairs.
https://www.r-bloggers.com/hash-table-performance-in-r-part-i/
How do I use doubles as key-value pairs in a hash function in R?

Generete unique random number on large number range

what i ask about is if exist a way to generate unique random numbers without helper structures.
I mean if already exist some mathematics functions (or algorithms) that natively generate random numbers only at once on a field (i would not try to write some kind of hash function specific for this problem).
This because i would generate a lot of unique numbers (integer) choosen between 0 and 10.000.000.000 (about 60% of the field), so a random repetition is not so improbable and store previously generated number in a structure for a subsequent lookup (even if well optimized, like bit arrays) could be too expensive (spatially and temporally).
P.S.
(Note that when i write random i really mean pseudo random)
If you want to ensure uniqueness then do not use a hash function, but instead use an encryption function to encrypt the numbers 0, 1, 2, 3 ... Since encryption is reversible then every number (up to the block size) is uniquely encrypted and will produce a unique result.
You can either write a simple Feistel cypher with a convenient block size or else use the Hasty Pudding cypher, which allows a large range of block sizes. Whenever an input number generates too large an output, then just go to the next input number.
Changing the key of the cypher will generate a different series of output numbers. the same series of numbers can be regenerated whenever needed by remembering the key and starting again with 0, 1, 2 ... There is no need to store the entire sequence. As you say, the sequence is pseudo-random and so can be regenerated easily if you know the key.
Instead of pseudo-random numbers, you could try so-called quasi-random numbers, which are more accurately called low-discrepancy sequences. [1]
[1] https://en.wikipedia.org/wiki/Low-discrepancy_sequence

Clarification on PRNG Algorithm as per ANSI X9.31

Newbee question:
I am studying ANSI X9.31 -1998 for implementing PRNG as per section 2.4. I am not able to properly understand the representation of varibles used - like "ede".
Is the "ede" an operation or a variable ?
Why a * is used before X? Is it some kind of a standard representation?
Is there any specific document which describes all these?
"A.2.4 Generating Pseudo Random Numbers Using the DEA
Let ede*X(Y) represent the DEA multiple encryption of Y under the key *X.
Let *K be a DEA key pair reserved only for the generation of pseudo random numbers, let V be a 64-bit seed value which is also kept secret, and let XOR be the exclusive-or operator. Let DT be a date/time vector which is updated on each iteration. I is an intermediate value. A 64-bit vector R is generated as follows:
I = ede*K(DT)
R = ede*K(I XOR V) and a new V is generated by V = ede*K(R XOR I).
Successive values of R may be concatenated to produce a pseudo random number of the desired length."
EDE means Encrypt, Decrypt, Encrypt, the usual protocol when using Triple DES.
The use of * looks a lot like the more usual subscription common to cryptography articles:
EDEX(Y) to mean using X as the key for the algorithm EDE.

How to implement hash table with chaining?

This is probably a stupid question but, I cant for the love of god figure out what I'm missing here in the theory behind hash tables with chaining.
This is what I understand:
A hash table uses a hash to associate a key to a location where a value is stored. Sometimes a hash will produce the same location for different keys, ie collisions may occur.
In this case we can implement chaining by storing all values with the same location to a linked list at that location.
What I don't understand is this:
When you enter a key and the hash function produces a location at which there is chaining, how does it determine which value in the linked list at that location belongs to that specific key, as opposed to another key involved in the collision?
I realize this is basic theory, but if anyone could point out errors in my reasoning or tell me what I'm missing I would very much appreciate it.
Simple way: maintain a linked list of "hash table entries", which are key/value pairs. Once you get to the bucket, check your query key against all keys in the bucket.
When you enter a key and the hash function produces a location at which there is chaining, how does it determine which value in the linked list at that location belongs to that specific key, as opposed to another key involved in the collision?
You just resort to linear search of the bucket by key.
You may appreciate the following mini hash table implementation written in F#, taken from this blog post:
> let hashtbl xs =
let spine = [|for _ in xs -> ResizeArray()|]
let f k = spine.[k.GetHashCode() % spine.Length]
Seq.iter (fun (k, v) -> (f k).Add (k, v)) xs
fun k -> Seq.pick (fun (k', v) -> if k=k' then Some v else None) (f k);;
val hashtbl : seq<'a * 'b> -> ('a -> 'b) when 'a : equality
This hashtbl function takes an association sequence xs of key-value pairs, builds a hash table represented as a spine array of ResizeArray buckets and returns a lambda function that finds the appropriate bucket and does a linear search in it for the given key k. The linear search is performed using the Seq.pick function.

Resources