hashtable

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

99封情书 提交于 2019-11-26 22:43:15
问题 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 ,

Why does LinkedHashSet<E> extend HashSet<e> and implement Set<E>

断了今生、忘了曾经 提交于 2019-11-26 22:14:11
Opened a LinkedHashSet source code today and found some interesting thing: public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, java.io.Serializable { The question is: why do they need both "extends HashSet" and "implements Set" when HashSet already is the Set? Kevin Bourrillion I've asked Josh Bloch, and he informs me that it was a mistake. He used to think, long ago, that there was some value in it, but he since "saw the light". Clearly JDK maintainers haven't considered this to be worth backing out later. sepp2k They didn't need to explicitly write implements Set<E

How to keep the order of elements in hashtable

六眼飞鱼酱① 提交于 2019-11-26 21:28:42
问题 I have a hashtable . values() method returns values in some order different from the order in which i am inserted.How can i get the values in the same order as i inserted?Using LinkedHashmap is an alternative but it is not synchronized. 回答1: Use a LinkedHashMap. Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list

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

*爱你&永不变心* 提交于 2019-11-26 21:23:31
问题 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. 回答1: If you talk about higher picture it is just like below.Here i refer item as a key of Map While Putting

How Do I Choose Between a Hash Table and a Trie (Prefix Tree)?

孤街浪徒 提交于 2019-11-26 21:09:43
So if I have to choose between a hash table or a prefix tree what are the discriminating factors that would lead me to choose one over the other. From my own naive point of view it seems as though using a trie has some extra overhead since it isn't stored as an array but that in terms of run time (assuming the longest key is the longest english word) it can be essentially O(1) (in relation to the upper bound). Maybe the longest english word is 50 characters? Hash tables are instant look up once you get the index . Hashing the key to get the index however seems like it could easily take near 50

Hash function for floats

穿精又带淫゛_ 提交于 2019-11-26 20:54:34
问题 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

How to implement the Hashable Protocol in Swift for an Int array (a custom string struct)

99封情书 提交于 2019-11-26 19:47:04
I am making a structure that acts like a String , except that it only deals with Unicode UTF-32 scalar values. Thus, it is an array of UInt32 . (See this question for more background.) What I want to do I want to be able to use my custom ScalarString struct as a key in a dictionary. For example: var suffixDictionary = [ScalarString: ScalarString]() // Unicode key, rendered glyph value // populate dictionary suffixDictionary[keyScalarString] = valueScalarString // ... // check if dictionary contains Unicode scalar string key if let renderedSuffix = suffixDictionary[unicodeScalarString] { // do

Rehashing process in hashmap or hashtable

喜夏-厌秋 提交于 2019-11-26 19:31:08
问题 How is the rehashing process done in a hashmap or hashtable when the size exceeds the maxthreshold value? Are all pairs just copied to a new array of buckets? EDIT: What happen to the elements in the same bucket (in linked list) after rehashing? I mean will they remain in same bucket after rehashing? 回答1: The maximum threshold in the question is called the load factor. It is advisable to have a load factor of around 0.75. Load factor is defined as (m/n) where n is the total size of the hash

Why are there no hashtables in the C standard library?

旧街凉风 提交于 2019-11-26 19:21:34
问题 Why is that there is no Hashtable support as part of Standard C Library? Is there any specific reason for this? 回答1: There is no hashtable in the standard C library because either: no-one has submitted a proposal to the working group; or the working group has deemed it unnecessary. That's the way ISO works. Proposals are put forward and accepted or rejected. You have to be careful what you add to the standard library since you have two conflicting groups. As a user, you might want every data

C - Segmentation Fault with strcmp?

天大地大妈咪最大 提交于 2019-11-26 19:12:38
I appear to be getting a segmentation fault somewhere with the strcmp function. I'm still very new to C and I can't see why it gives me the error. int linear_probe(htable h, char *item, int k){ int p; int step = 1; do { p = (k + step++) % h->capacity; }while(h->keys[p] != NULL && strcmp(h->keys[p], item) != 0); return p; } gdb: Program received signal SIGSEGV, Segmentation fault. 0x0000003a8e331856 in __strcmp_ssse3 () from /lib64/libc.so.6 (gdb) frame 1 #1 0x0000000000400ea6 in linear_probe (h=0x603010, item=0x7fffffffde00 "ksjojf", k=-1122175319) at htable.c:52 Edit: insertion code and