hashtable

Stackoverflow with specialized Hashtbl (via Hashtbl.make)

南楼画角 提交于 2019-12-10 15:39:03
问题 I am using this piece of code and a stackoverflow will be triggered, if I use Extlib's Hashtbl the error does not occur. Any hints to use specialized Hashtbl without stackoverflow? module ColorIdxHash = Hashtbl.Make( struct type t = Img_types.rgb_t let equal = (==) let hash = Hashtbl.hash end ) (* .. *) let (ctable: int ColorIdxHash.t) = ColorIdxHash.create 256 in for x = 0 to width -1 do for y = 0 to height -1 do let c = Img.get img x y in let rgb = Color.rgb_of_color c in if not

javascript (java-like) hash code implementation

这一生的挚爱 提交于 2019-12-10 15:20:30
问题 The following code is my attempt at a fairly generic javascript hash code implementation. I'm planning to use this code in conjunction with a hash table implementation (e.g. jshashtable) that utilizes hashCode() if its defined for keys. I have attempted to adhere closely to java's hash code implementations for numbers, strings, and arrays. Questions: Are there any issues with this implementation regarding correctness or performance? Are there any pre-existing implementations for hash codes

HashTable和HashMap

微笑、不失礼 提交于 2019-12-10 15:12:57
Hashtable的应用非常广泛,HashMap是新框架中用来代替Hashtable的类,也就是说建议使用HashMap,不要使用Hashtable。可能你觉得Hashtable很好用,为什么不用呢?这里简单分析他们的区别。 1.Hashtable的方法是同步的,HashMap未经同步,所以在多线程场合要手动同步HashMap这个区别就像Vector和ArrayList一样。 查看Hashtable的源代码就可以发现,除构造函数外,Hashtable的所有 public 方法声明中都有 synchronized 关键字,而HashMap的源代码中则连 synchronized 的影子都没有,当然,注释除外。 2.Hashtable不允许 null 值(key 和 value 都不可以),HashMap允许 null 值(key和value都可以)。 先看个Hashtable正常输出的示例: Hashtable table = new Hashtable(); table.put("a-key", "a-value"); table.put("b-key", "b-value"); table.put("c-key", "c-value"); 输出如下: table.put(null, "a-value"); 运行之后异常如下: Exception in thread "main"

Can I override std::hash?

醉酒当歌 提交于 2019-12-10 13:50:03
问题 I can replace the actual implementation of std::hash with my own definition of std::hash in C++ 11 ? I mean from my codebase, without touching the standard library. I can't see any use for virtual function/polymorphism in this case, so I suppose that I can't alter the definition of std::hash anyway ? 回答1: Yes it's okay, and you don't have to modify the standard library in any way, just use template specialization: namespace std { template<> struct hash<YourSpecialType> { // ... }; } 回答2: You

Appending to a hash table in Bash

为君一笑 提交于 2019-12-10 11:38:33
问题 I am trying to make a hash table with the file name of all files in a directory and another integer number. Like "File Name" : "number". The code should be in bash 4.x. This is the code which I wrote: #!/bin/bash DIR=`ls` declare -A ARRAY ZERO=0 for FILES in $DIR do echo "We have $FILES" ARRAY+=(["$FILES"]="$ZERO") done echo "Done with filling up array!" for file in "${ARRAY[@]}" ; do KEY="${file%%:*}" VALUE="${file##*:}" printf "%s has number %s.\n" "$KEY" "$VALUE" done echo "We are done

How can I test that ConcurrentHashMap is truly thread-safe?

我只是一个虾纸丫 提交于 2019-12-10 10:33:15
问题 Just learning more about threads and concurrency, and thought of playing around with a regular hashtable and a ConcurrentHashMap. What would be a good way to test for concurrency for these hashtables? (obviously the hash table will fail this test) It would be cool if I could also somehow keep track of how many reads/writes the test performs to see which one (ht or conccurrent ht) faster. 回答1: This is an answer to your last edit about how you can test it. This also touches on Hot Licks comment

mmap vs O_DIRECT for random reads (what are the buffers involved?)

纵饮孤独 提交于 2019-12-10 06:28:44
问题 I am implementing a disk based hashtable supporting large amount of keys (26+ million). The value is deserialized. Reads are essentially random throughout the file, values are less than the page size, and I am optimising for SSDs. Safety/consistency are not such huge issues (performance matters). My current solution involves using a mmap() file with MADV_RANDOM | MADV_DONTNEED set to disable prefetching by the kernel and only load data as needed on-demand. I get the idea that the kernel reads

What load factor should be used when you know maximum possible no of elements in HashSet

孤街醉人 提交于 2019-12-10 04:17:29
问题 What load factor should I use when I really know the maximum possible no of elements in a HashSet ? I had heard that the default load factor of 0.75 is recommended as it offers good performance trade-offs between speed & space. Is this correct ? However a larger size HashSet would also takes more time in creation and more space. I am using HashSet just inorder to remove duplicate integers from a list of integers. 回答1: I spent some time playing around with load factors once, and it is shocking

Hashtable- Rehashing

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 03:34:22
问题 I have been told that Hashtable in .NET uses rehashing in order to reduce/avoid collision. Ie. “Rehasing works as follows: assume we have a set of hash different functions, H1 ... Hn, and when inserting or retrieving an item from the hash table, initially the H1 hash function is used. If this leads to a collision, H2 is tried instead and onwards up to Hn to avoid collision in Hashtable.” Assumption: We have a hashtable with n (where n < Infinity) element where asymptotic time complexity is o

What is the best hash function for uint64_t keys ranging from 0 to its max value?

你。 提交于 2019-12-10 03:00:40
问题 Assuming that we have a set of elements and want to store them in a hash map (for example std::unordered_set ), and each element has a key of type uint64_t whose value can vary from 0 to its maximum possible value, is it the best choice to use trivial hash function, where a hash value of a key is the key itself? Does it depend on container in use (i.e. Google's sparse hash vs std::unordered_map from STL)? The probability of appearance of key values is unknown. 回答1: If all you have to hash is