hashtable

Internals of how the HashMap put() and get() methods work (basic logic only )

最后都变了- 提交于 2019-11-27 23:14:57
When we put a key instance say "key" and a Value instance say "value" in a HashMap class using put() method , what does the HashMap class do internally . How does it retrieve the value back when we say hashMap.get(key) ? Edit : I do not want details here , basically trying to understand the bigger picture and the role of equals() and hashcode() method in put() and get() operations. amicngh If you talk about higher picture it is just like below.Here i refer item as a key of Map While Putting items. Calculate hashcode of key If basket with that hashcode is present then use the equals method on

What is the Big O performance of maps in golang?

浪尽此生 提交于 2019-11-27 23:08:52
问题 The "Map types" section of the go language specification describes the interface and general usage of map types and the "Go maps in action" post on The Go Blog casually mentions hash tables and "fast lookups, adds, and deletes". The current runtime/hashmap.go source code describes its implementation as a hashtable (which are typically amortized O(1) ); however, I don't see any guarantee of performance characteristics (such as Big O performance) in the language specification or other materials

Hash function for floats

寵の児 提交于 2019-11-27 22:09:26
I'm currently implementing a hash table in C++ and I'm trying to make a hash function for floats... I was going to treat floats as integers by padding the decimal numbers, but then I realized that I would probably reach the overflow with big numbers... Is there a good way to hash floats? You don't have to give me the function directly, but I'd like to see/understand different concepts... Notes: I don't need it to be really fast, just evenly distributed if possible. I've read that floats should not be hashed because of the speed of computation, can someone confirm/explain this and give me other

convert HashTable to Dictionary in C#

旧巷老猫 提交于 2019-11-27 21:06:06
问题 how to convert a HashTable to Dictionary in C#? is it possible? for example if I have collection of objects in HashTable and if I want to convert it to a dictionary of objects with a specific type, how to do that? 回答1: public static Dictionary<K,V> HashtableToDictionary<K,V> (Hashtable table) { return table .Cast<DictionaryEntry> () .ToDictionary (kvp => (K)kvp.Key, kvp => (V)kvp.Value); } 回答2: var table = new Hashtable(); table.Add(1, "a"); table.Add(2, "b"); table.Add(3, "c"); var dict =

What's the big O for JavaScript's array when used as a hash?

ぐ巨炮叔叔 提交于 2019-11-27 20:41:43
What's the big O for JavaScript's array access when used as a hash? For example, var x= []; for(var i=0; i<100000; i++){ x[i.toString()+'a'] = 123; // using string to illustrate x[alpha] } alert(x['9999a']); // linear search? One can hope JS engines will not use a linear search internally O(n), but is this for sure? Accessing object properties and array elements in JavaScript is syntacticly assumed to be done in constant time : O(1). Performance characteristics are not guaranteed in the ECMAScript specification, but all the modern JavaScript engines retrieve object properties in constant time.

Jump Table Switch Case question

一曲冷凌霜 提交于 2019-11-27 20:21:22
问题 I am trying to understand some things about jump tables and its relationship between a switch case statement. I was told that a jump table is a O(1) structure that the compiler generates which makes lookup of values essentially about as fast as you can get. However in some cases a Hashtable/Dictionary might be faster. I was also told this will only work if the switch case contains ordered data values. Can someone please confirm or deny this and explain what a jump table is, it's importance

Generate same unique hash code for all anagrams

帅比萌擦擦* 提交于 2019-11-27 20:15:42
Recently, I attended an interview and faced a good question regarding hash collisions. Question : Given a list of strings, print out the anagrams together. Example : i/p : {act, god, animal, dog, cat} o/p : act, cat, dog, god I want to create hashmap and put the word as key and value as list of anagrams To avoid collision, I want to generate unique hash code for anagrams instead of sorting and using the sorted word as key. I am looking for hash algorithm which take care of collision other than using chaining. I want algorithm to generate same hash code for both act and cat... so that it will

What is a hash table and how do you make it in C? [closed]

此生再无相见时 提交于 2019-11-27 19:31:58
I have a few questions on a data-structure called a hash table (also know as associative array) and how it is implemented in C. How do you make a hash table in C? What is a hashtable and how do you implement it? Why would I want to use a hash table rather than an array? NOTE: I know this is a really broad question which will require a large answer but, I made this because I had some people asking me what it all was. so I put it on here to fully explain it and help anyone else out. Prerequisites For this answer I'm going to assume you know how to use pointers, structs, and have a basic

How to do associative array/hashing in JavaScript

你离开我真会死。 提交于 2019-11-27 19:21:54
问题 I need to store some statistics using JavaScript in a way like I'd do it in C#: Dictionary<string, int> statistics; statistics["Foo"] = 10; statistics["Goo"] = statistics["Goo"] + 1; statistics.Add("Zoo", 1); Is there an Hashtable or something like Dictionary<TKey, TValue> in JavaScript? How could I store values in such a way? 回答1: Use JavaScript objects as associative arrays. Associative Array: In simple words associative arrays use Strings instead of Integer numbers as index. Create an

Using as a concrete type conforming to protocol AnyObject is not supported

依然范特西╮ 提交于 2019-11-27 19:00:44
I'm using Swift 2 and using WeakContainer as a way to store a set of weak objects, much like NSHashTable.weakObjectsHashTable() struct WeakContainer<T: AnyObject> { weak var value: T? } public protocol MyDelegate : AnyObject { } Then in my ViewController, I declare public var delegates = [WeakContainer<MyDelegate>] But it is error Using MyDelegate as a concrete type conforming to protocol AnyObject is not supported I see that the error is that WeakContainer has value member declared as weak , so T is expected to be object. But I also declare MyDelegate as AnyObject , too. How to get around