hashtable

How do I get the number of keys in a hash table in Lua?

喜欢而已 提交于 2019-12-03 06:30:18
问题 myTable = {} myTable["foo"] = 12 myTable["bar"] = "blah" print(#myTable) -- this prints 0 Do I actually have to iterate through the items in the table to get the number of keys? numItems = 0 for k,v in pairs(myTable) do numItems = numItems + 1 end print(numItems) -- this prints 2 回答1: I experimented with both the # operator and table.getn(). I thought table.getn() would do what you wanted but as it turns out it's returning the same value as #, namely 0. It appears that dictionaries insert nil

Why is accessing an element of a dictionary by key O(1) even though the hash function may not be O(1)?

痴心易碎 提交于 2019-12-03 06:26:21
问题 I see how you can access your collection by key. However, the hash function itself has a lot of operations behind the scenes, doesn't it? Assuming you have a nice hash function which is very efficient, it still may take many operations. Can this be explained? 回答1: the HashFunc itself has a lot of operations behind the scenes That is certainly true. However, the number of these operations depends on the size of the key , not on the size of the hash table into which the key is inserted: the

Hash Table: Why deletion is difficult in open addressing scheme

☆樱花仙子☆ 提交于 2019-12-03 05:46:36
问题 I am trying to understand the open addressing method. I refer to T. H. Cormen's book on this topic, which states that deletion is difficult in open addressing. I am completely stuck at this paragraph: Deletion from an open-address hash table is difficult. When we delete a key from slot i , we cannot simply mark that slot as empty by storing NIL in it. Doing so might make it impossible to retrieve any key k during whose insertion we had probed slot i and found it occupied. I don't understand

Getting the current hash key in a ForEach-Object loop in powershell

青春壹個敷衍的年華 提交于 2019-12-03 05:19:06
I've got a hash table: $myHash = @{ "key1" = @{ "Entry 1" = "one" "Entry 2" = "two" } "key 2" = @{ "Entry 1" = "three" "Entry 2" = "four" } } I'm doing a loop through to get the objects: $myHash.keys | ForEach-Object { Write-Host $_["Entry 1"] } Works fine, but what can I use to figure out which of the keys of $myHash I'm in? $_.Name doesn't return anything. I'm stumped. Help? Frode F. I like to use GetEnumerator() when looping a hashtable. It will give you a property value with the object, and a property key with it's key/name. Try: $myHash.GetEnumerator() | % { Write-Host "Current hashtable

Difference between HashMap and HashTable purely in Data Structures

放肆的年华 提交于 2019-12-03 05:18:51
What is the difference between HashTable and HashMap purely in context of data structures (and not in Java or any other language) ? I have seen people using these terms interchangeably for the same concept. Does it have no difference at all purely in context of data structures ? In Computing Science terminology, a map is an associative container mapping from a key to a value. In other words, you can do operations like "for key K remember value V" and later "for key K get the value". A map can be implemented in many ways - for example, with a (optionally balanced) binary tree, or a hash table,

Hash tables v self-balancing search trees

喜你入骨 提交于 2019-12-03 05:18:07
I am curious to know what is the reasoning that could overweighs towards using a self-balancing tree technique to store items than using a hash table. I see that hash tables cannot maintain the insertion-order, but I could always use a linked list on top to store the insertion-order sequence. I see that for small number of values, there is an added cost of of the hash-function, but I could always save the hash-function together with the key for faster lookups. I understand that hash tables are difficult to implement than the straight-forward implementation of a red-black tree, but in a

What's the difference between Hashtable and Dictionary?

北城以北 提交于 2019-12-03 04:39:16
What's the difference between Dictionary and Hashtable and how do I work with the Dictionary class in Java? AlexR Dictionary is an abstract base class of Hashtable . Both are still in JDK for backwards compatibility with old code. We are expected to use HashMap and other implementations of Map interface introduced in Java 1.2. The javadoc for Dictionary has your answer. The Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values. You don't work directly with Dictionary , since it is an abstract class. Also note the following from the same

How many hash buckets

六眼飞鱼酱① 提交于 2019-12-03 04:24:59
问题 If I notice that a hash table (or any other data structure built on a hash table) is filling up, at what point should you build a new table with more buckets. And given n items in the table so far, how do you figure out how many buckets to use in the new one? So let's say I have 100 buckets. Should I reorganize it when there are 50 items in it? 500? 5000? Or should I look for the most-full bucket and key on that? Then when I hit that point how big do I make the new hash table? Related to this

Is there HashTable structure in Wolfram Mathematica?

梦想与她 提交于 2019-12-03 04:23:20
问题 I want to use a Structure like HashTable. Is there similar structure in Wolfram Mathematica ? 回答1: Update: Mathematica version 10 introduced the Association data structure (tutorial). There are a number of possibilities. The easiest possibility, which works well if you don't need to add or delete keys from your table, or change their associated values, is to construct a list of rules with the key on the left-hand side and the value on the right-hand side, and use Dispatch on it. If you do

graph - What are the disadvantages if I replace each linked list in adjacency-list with hash table?

落花浮王杯 提交于 2019-12-03 03:52:33
问题 In CLRS excise 22.1-8 (I am self learning, not in any universities) Suppose that instead of a linked list, each array entry Adj[u] is a hash table containing the vertices v for which (u,v) ∈ E. If all edge lookups are equally likely, what is the expected time to determine whether an edge is in the graph? What disadvantages does this scheme have? Suggest an alternate data structure for each edge list that solves these problems. Does your alternative have disadvantages compared to the hash