hashtable

How to write a correct Hash Table destructor in c++

人盡茶涼 提交于 2019-12-30 11:25:06
问题 I am writing a c++ Hashtable Here is my destructor: HashMap::~HashMap() { for (int i=0; i<cap; i++) { Node* ptr = Hashtable[i]; while (ptr!=NULL) { Node* delptr; delptr=ptr; ptr=ptr->next; delete delptr; } } delete [] Hashtable; } My add function: void HashMap::add(const std::string& key, const std::string& value) { int index = hashfunction(key)%cap;; Node* ptr=Hashtable[index]; Node* newnode=new Node; if (contains(key)==false) { if (ptr == nullptr) { newnode->key=key; newnode->value=value;

Understanding Dean Edwards' addevent JavaScript

一曲冷凌霜 提交于 2019-12-30 10:46:22
问题 I need help understanding this piece of code. What is the point of handler.guid ? Why is there a need for a hash table? What is the point of: if ( element["on" + type]) { handlers[0] = element["on" + type]; } What does the "this" refer to in handleEvent , the element or the the addEvent function? function addEvent(element, type, handler) { // assign each event handler a unique ID if (!handler.$$guid) handler.$$guid = addEvent.guid++; // create a hash table of event types for the element if (

Cannot iterate Hashtable in VBA (Excel)

ぐ巨炮叔叔 提交于 2019-12-30 10:13:30
问题 I am using Hashtable (mscorlib.dll referenced). I fill it with data, I can get any item (as long as I convert the request type to exactly same type what's stored in hashtable), .ContainsValue/Key - all that works. But I cannot iterate through it via For Each loop. I've tried all methods I could find on the internet (For Each element..., where element is DictionaryEntry, via GetEnumerator), but none works - I can roll through the table via enumerator, but I can't reach the key nor value of

Sorting Hashtable by Order in Which It Was Created

不想你离开。 提交于 2019-12-30 09:29:27
问题 This is similar to How to keep the order of elements in hashtable, except for .NET. Is there any Hashtable or Dictionary in .NET that allows you to access it's .Index property for the entry in the order in which it was added to the collection? 回答1: A NameValueCollection can retrieve elements by index (but you cannot ask for the index of a specific key or element). So, var coll = new NameValueCollection(); coll.Add("Z", "1"); coll.Add("A", "2"); Console.WriteLine("{0} = {1}", coll.GetKey(0),

What the iteration cost on a HashSet also depend on the capacity of backing map?

限于喜欢 提交于 2019-12-30 06:19:16
问题 From the JavaDocs of HashSet: This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets. Iterating over this set requires time proportional to the sum of the HashSet instance's size (the number of elements) plus the "capacity" of the backing HashMap instance (the number of buckets). Thus, it's very important not to set the initial capacity too high (or the load factor too

Can we write a Hashtable to a file?

假如想象 提交于 2019-12-30 02:08:09
问题 I have a Hashtable<string,string> , in my program I want to record the values of the Hashtable to process later. My question is: can we write object Hastable to a file? If so, how can we later load that file? 回答1: Yes, using binary serialization (ObjectOutputStream): FileOutputStream fos = new FileOutputStream("t.tmp"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(yourHashTable); oos.close(); Then you can read it using ObjectInputStream The objects that you put inside

Why do we use linear probing in Hash tables when there is separate chaining linked with lists?

六眼飞鱼酱① 提交于 2019-12-29 11:36:08
问题 I recently learned about different methods to deal with collisions in hash tables. And saw that the separate chaining with linked lists is always more time efficient, and for space efficiency we allocate a predefined memory for Linear probing which later on we might not use,for separate chaining we utilize memory dynamically, so is separate chaining with linked list not more efficient than Linear probing?if yes why do we then use Linear probing at all? 回答1: I'm surprised that you saw chained

Why do we use linear probing in Hash tables when there is separate chaining linked with lists?

穿精又带淫゛_ 提交于 2019-12-29 11:35:41
问题 I recently learned about different methods to deal with collisions in hash tables. And saw that the separate chaining with linked lists is always more time efficient, and for space efficiency we allocate a predefined memory for Linear probing which later on we might not use,for separate chaining we utilize memory dynamically, so is separate chaining with linked list not more efficient than Linear probing?if yes why do we then use Linear probing at all? 回答1: I'm surprised that you saw chained

What are the differences between Hashmap vs Hashtable in theory?

ぐ巨炮叔叔 提交于 2019-12-29 08:11:54
问题 Are there are differences between hashmap and hashtable in theory? I don't mean in the concrete definitions given in Java (or the implementation), but in theory. Isn't a hashtable a map that uses hashing ... hence a hashmap? 回答1: According to Wikipedia, they are the same: In computing, a hash table (hash map) is a data structure used to implement an associative array (...) According to Wikibooks, it's the same: A hash table, or a hash map, is a data structure that associates keys with values.

If key in hashtable is a class object, how does containsKey work?

杀马特。学长 韩版系。学妹 提交于 2019-12-29 07:52:16
问题 When we put a class Object (that has say three data members) in a hashtable, how can I prevent putting another entry into the hash table whose key has the same three data members ? Cos I am guessing this will be a new object. So hashtable.containsKey() will return false even when there is a key (this class object) that has the very same data members as the one that is waiting to be inserted. More clearly: I have a class like class Triplet { private Curr curr; private Prev prev; private Next