hashtable

What's the problem with this code? [hashtable in C]

给你一囗甜甜゛ 提交于 2019-12-04 22:49:07
I'm implementing a hash table. The following is my function for initializing it. Im getting some errors which I cant understand why. I have also quoted what valgrind says. typedef struct HashTable { int size ; struct List *head; struct List *tail; }HashTable; typedef struct List { char *number; char *name; int time; struct List *next; }List; #define size_of_table 211 HashTable *createHashTable(void) { HashTable *new_table = malloc(sizeof(*new_table)*size_of_table); //line 606 if (new_table == NULL) { return NULL; } int i=0; for(i; i<size_of_table; i++) { new_table[i].size=0; new_table[i].head

Hashtable and list side by side?

天涯浪子 提交于 2019-12-04 20:03:54
I need a data structure that is ordered but also gives fast random access and inserts and removes. Linkedlists are ordered and fast in inserts and removes but they give slow random access. Hashtables give fast random access but are not ordered. So, it seems to nice to use both of them together. In my current solution, my Hashtable includes iterators of the list and the List contains the actual items. Nice and effective. Okay, it requires double the memory but that's not an issue. I have heard that some tree structures could do this also, but are they as fast as this solution? The most

Java Object and array memory location

拈花ヽ惹草 提交于 2019-12-04 20:00:05
I'm writing an array-backed hashtable in Java, where the type of key and value are Object; no other guarantee. The easiest way for me code-wise is to create an object to hold them: public class Pair { public Object key; public Object value; } And then create an array public Pair[] storage = new Pair[8]; But how does the jvm treat that in memory? Which is to say, will the array actually: be an array of pointers to Pair() objects sitting elsewhere, or contain the actual data? edit Since the objects are instantiated later as new Pair(), they're randomly placed in the heap. Is there any good way

How to deal with old references to a resized hash table?

喜你入骨 提交于 2019-12-04 19:55:22
I'm currently working on a hash table implementation in C. I'm trying to implement dynamic resizing, but came across a problem. If resizing a hash table means creating a new one with double (or half) the size, rehashing, and deleting the old one, how can I deal with old references the user may have made to the old table? Example code (I've omitted error checking just for this example): int main(int argc, char *argv[]) { ht = ht_create(5) /* make hashtable with size 5 */ ht_insert("john", "employee"); /* key-val pair "john -> employee" */ ht_insert("alice", "employee"); char *position = ht_get

How to traverse keys of a Hashtable in alphabetical order?

若如初见. 提交于 2019-12-04 18:05:15
What is the easiest way to traverse a hashtable's keys in ascending alphabetical order? This is fairly dependent upon what the type of the key is. But lets assume for a minute that they are strings. You could use the following LINQ query Hashtable table = GetHashTable(); var keys = table.Keys.Cast<String>().OrderBy(x => x); For more complex structures the LINQ query is only slightly different. Lets assume you had the following definition for a key struct Name { public string First; public string Last; // Equality code omitted } The LINQ code would be the following Hashtable table =

Swift, how to implement Hashable protocol based on object reference?

烈酒焚心 提交于 2019-12-04 16:07:38
问题 I've started to learn swift after Java. In Java I can use any object as a key for HashSet, cause it has default hashCode and equals based on object identifier. How to achieve the same behaviour in Swift? 回答1: If you are working with classes and not structs, you can use the ObjectIdentifier struct. Note that you also have to define == for your class in order to conform to Equatable ( Hashable requires it). It would look something like this: class MyClass: Hashable { } func ==(lhs: MyClass, rhs

What is the main implementation idea behind sparse hash table?

北战南征 提交于 2019-12-04 15:53:34
问题 Why does Google sparsehash open-source library has two implementations: a dense hashtable and a sparse one? 回答1: The dense hashtable is your ordinary textbook hashtable implementation. The sparse hashtable stores only the elements that have actually been set, divided over a number of arrays. To quote from the comments in the implementation of sparse tables: // The idea is that a table with (logically) t buckets is divided // into t/M *groups* of M buckets each. (M is a constant set in //

iterating through Enumeration of hastable keys throws NoSuchElementException error

左心房为你撑大大i 提交于 2019-12-04 14:55:27
问题 I am trying to iterate through a list of keys from a hash table using enumeration however I keep getting a NoSuchElementException at the last key in list? Hashtable<String, String> vars = new Hashtable<String, String>(); vars.put("POSTCODE","TU1 3ZU"); vars.put("EMAIL","job.blogs@lumesse.com"); vars.put("DOB","02 Mar 1983"); Enumeration<String> e = vars.keys(); while(e.hasMoreElements()){ System.out.println(e.nextElement()); String param = (String) e.nextElement(); } Console output: EMAIL

Convert Hashtable to xml string and back to HashTable without using .NET Serializer

喜你入骨 提交于 2019-12-04 14:14:28
问题 Does anyone know how to convert a Hashtable to an XML String then back to a HashTable without using the .NET based XMLSerializer. The XMLSerializer poses some security concerns when code runs inside of IE and the browser's protected mode is turned on - So basically I am looking for an easy way to convert that Hashtable to string and back to a Hashtable. Any sample code would be greatly appreciated. Thanks 回答1: You could use the DataContractSerializer class: using System; using System

Timeout Mechanism for Hashtable

♀尐吖头ヾ 提交于 2019-12-04 13:22:14
I have a hashtable that under heavy-traffic. I want to add timeout mechanism to hashtable, remove too old records. My concerns are, - It should be lightweight - Remove operation has not time critical. I mean (timeout value is 1 hour) remove operation can be after 1 hour or and 1 hour 15 minute. There is no problem. My opinion is, I create a big array (as ring buffer)that store put time and hashtable key, When adding to hashtable, using array index find a next slot on array put time, if array slot empty, put insertion time and HT key, if array slot is not empty, compare insertion time for