hashtable

Conditional inclusion of a key-value pair in a hash [closed]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 01:27:28
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . Can someone help me shorten the following method? I began with this, which I liked just fine: def self.some_hash { "foo" => "bar" } end Now I want to add an optional key. The tersest syntax I can think of is this: def self.some_hash(some_key=nil) answer = { "foo" => "bar" } answer[some_key] = "yucky, long-winded

Hashtable of mutable variable in Ocaml

耗尽温柔 提交于 2019-12-09 18:14:53
问题 I need to use hashtable of mutable variable in Ocaml, but it doesn't work out. let link = Hashtbl.create 3;; let a = ref [1;2];; let b = ref [3;4];; Hashtbl.add link a b;; # Hashtbl.mem link a;; - : bool = true # a := 5::!a;; - : unit = () # Hashtbl.mem link a;; - : bool = false Is there any way to make it works? 回答1: Mutable variables that may happen to have the same content can still be distinguished because they are stored at different locations in memory. They can be compared with the

Data structure with two way O(1) lookup. Hashtable?

喜欢而已 提交于 2019-12-09 17:48:21
问题 I'm implementing a system where I have a list of names and each person has 1 phone number. I need to be able to take a name and look up the phone number, or take a phone number and look up a name. I know I could do this by having two hashtables - one which goes from names to phone numbers and one which goes from phone numbers to names. Then I can look up in either direction in O(1) time. However this seems like I am storing too much data - every name and every phone number is stored twice. Is

Cuckoo hashing in C

假如想象 提交于 2019-12-09 14:46:01
问题 Does anybody have an implementation of Cuckoo hashing in C? If there was an Open Source, non GPL version it would be perfect! Since Adam mentioned it in his comment, anyone knows why it is not much used? Is it just a matter of implementation or the good theoretical properties do not materialize in practice? 回答1: http://www.mpi-inf.mpg.de/~sanders/programs/cuckoo/ HTH 回答2: As other answers have pointed out, it's true that the simplest cuckoo hashtable requires that the table be half empty.

Does the STL contain a hashtable? [duplicate]

懵懂的女人 提交于 2019-12-09 14:31:00
问题 This question already has answers here : Closed 9 years ago . Possible Duplicates: Hashtable in C++? can anybody offer a simple hash_map example in C++? Does the STL contain an implementation of a hashtable? If so, can you provide a brief example of how to use it? 回答1: Current standard implementation doesn't, STL::TR1 does, see Unordered Map. Most modern compilers have a TR1 implementation, if that fails, you may always use the Boost TR1 implementation. MSVC has it for VS2008 via service pack

Pointer to generic type

流过昼夜 提交于 2019-12-09 13:30:24
问题 In the process of transforming a given efficient pointer-based hash map implementation into a generic hash map implementation, I stumbled across the following problem: I have a class representing a hash node (the hash map implementation uses a binary tree) THashNode <KEY_TYPE, VALUE_TYPE> = class public Key : KEY_TYPE; Value : VALUE_TYPE; Left : THashNode <KEY_TYPE, VALUE_TYPE>; Right : THashNode <KEY_TYPE, VALUE_TYPE>; end; In addition to that there is a function that should return a pointer

Hashtable with multiple values for single key

江枫思渺然 提交于 2019-12-09 08:56:06
问题 I want to store multiple values in single key like: HashTable obj = new HashTable(); obj.Add("1", "test"); obj.Add("1", "Test1"); Right now this throws an error. 回答1: you can put your test,test1,test2,... in a table and then put this table in a Hashtable as a value for the key which will be the same for all them. For example try something like this: List<string> list = new List<string>(); list.Add("test"); list.Add("test1"); and then: HashTable obj = new HashTable(); obj.Add("1", list); 回答2:

A faster hash function

孤街浪徒 提交于 2019-12-09 07:19:51
问题 I'm trying to implement my own hash function, i add up the ASCII numbers of each string, using java. I find the hash code by finding the mod of the size of the hash table and the sum. size%sum. I was wondering if there was a way to use the same process but reduce collisions, when searching for the string? Thanks in advance. 回答1: I would look at the code for String and HashMap as these have a low collision rate and don't use % and handle negative numbers. From the source for String public int

Does a useful Haskell HashMap/HashTable/Dictionary library exist?

送分小仙女□ 提交于 2019-12-09 02:51:51
问题 I'm looking for a monad-free, constant access query O(1) associative array. Consider the hypothetical type: data HT k v = ??? I want to construct an immutable structure once: fromList :: Foldable t, Hashable k => t (k,v) -> HT k v I want to subsequently query it repeatedly with constant time access:: lookup :: Hashable k => HT k v -> k -> Maybe v There appears to be two candidate libraries which fall short: unordered-containers hashtables unordered-containers unordered-containers contains

Alternatives to 2D Arrays in Java

╄→гoц情女王★ 提交于 2019-12-09 01:59:27
问题 I have a lookup table that should be accessed by two separate key values. One ugly way to do this is: int[][] myArray = new int[256][256]; myArray[key1][key2] = 25; where key1 and key2 are keys that were previously generated dynamically. But this is rather ugly. It seems that a better way to do this would be to use a Map, but those require a single key, not two. Java doesn't natively support tuples, so what should I use instead? (It also seems awkward to use an array as the key). EDIT : The