![]() |
|
|
| |
|
||||
In computing, an associative array, also known as a map, lookup table, or dictionary, is an abstract data type very closely related to the mathematical concept of a function with a finite domain. Conceptually, an associative array is composed of a collection of keys and a collection of values, and each key is associated with one value. The operation of finding the value associated with a key is called a lookup or indexing, and this is the most important operation supported by an associative array. The relationship between a key and its value is sometimes called a mapping or binding. For example, if the value associated with the key The operations that are usually defined for an associative array are:
ExamplesOne can think of a telephone book as an example of an associative array, where names are the keys and phone numbers are the values. Another example would be a dictionary where words are the keys and definitions are the values. A database is a sort of generalized associative array. Data structures for associative arraysAssociative arrays are usually used when lookup is the most frequent operation. For this reason, implementations are usually designed to allow speedy lookup, at the expense of slower insertion and a larger storage footprint than other data structures (such as association lists). Efficient representationsThere are two main efficient data structures used to represent associative arrays, the hash table and the self-balancing binary search tree. Skip lists are also an alternative, though relatively new and not as widely used. Relative advantages and disadvantages include:
Association listsA simple but generally inefficient type of associative array is an association list, often called an alist for short, which simply stores a linked list of key-value pairs. Each lookup does a linear search through the list looking for a key match. Strong advantages of association lists include:
Specialized representationsIf the keys have a specific type, one can often use specialized data structures to gain performance. For example, integer-keyed maps can be implemented using Patricia trees or Judy arrays, and are useful space-saving replacements for sparse arrays. Because this type of data structure can perform longest-prefix matching, they're particularly useful in applications where a single value is assigned to most of a large range of keys with a common prefix except for a few exceptions, such as in routing tables. String-keyed maps can avoid extra comparisons during lookups by using tries. Language supportAssociative arrays are known by many names in different programming languages. In Smalltalk and Python they are called dictionaries; in Perl they are called hashes; in Java they are called Maps [1] (http://java.sun.com/j2se/1.4.2/docs/api/java/util/Map.html) and in Common Lisp they are called hash tables. "Hash table" is also the name of the most common data structure used to store an associative array. In the scripting language Lua, associative arrays, called tables, are used as the primitive building block for all data structures, even arrays. Likewise, in JavaScript, all objects are associative arrays. Associative arrays can be implemented in any programming language, and one or more implementations of them is typically found either built into the language or the standard library distributed with it (C is a noticeable exception, as neither the language nor the standard library directly provide one. It is not difficult to write one in C, though). SmalltalkIn Smalltalk a dictionary is used: phonebook := Dictionary new. phonebook at: 'Sally Smart' put: '555-9999'. phonebook at: 'John Doe' put: '555-1212'. phonebook at: 'J. Random Hacker' put: '553-1337'. To access an entry the message phonebook at: 'Sally Smart' gives '555-9999' TCLIn Tcl every array is an associative array. set "phonebook(Sally Smart)" 555-9999 set "phonebook(John Doe)" 555-1212 set "phonebook(J. Random Hacker)" 553-1337 The first argument of the To access an entry and put it on standard output puts "$phonebook(Sally Smart)" The result is here 555-9999 PythonIn Python, associative arrays are called dictionaries. Dictionary literals are marked with curly braces: phonebook = {'Sally Smart':'555-9999',
'John Doe':'555-1212',
'J. Random Hacker':'553-1337'}
To access a entry in python simply use the array indexing operator. For example the statement C++C++ also has a form of associative array called #include <map> #include <string> int main()
{
std::map<std::string, std::string> phone_book;
phone_book["Sally Smart"] = "555-9999";
phone_book["John Doe"] = "555-1212";
phone_book["J. Random Hacker"] = "553-1337";
return 0;
}
In C++, DD offers direct support for associative arrays in the core language. The equivalent example would be:
LispIn Lisp and Scheme, association lists are commonly used, as in the following S-expression: '(("Sally Smart" . "555-9999")
("John Doe" . "555-1212")
("J. Random Hacker" . "553-1337"))
The syntax Common Lisp also supports a hash table data type. Hash tables have greater overhead than alists, but provide much faster access when there are many elements. PerlOne of Perl's distinguishing features is its built-in, language-level support for associative arrays. Modern Perl vernacular refers to associative arrays as hashes; the term associative array is found in older documentation, but is considered somewhat archaic. Perl hashes are flat in that they can only use scalars as keys or values, but these may include references to arrays or other hashes. A hash variable is preceded by a %phone_book = ( "Sally Smart", "555-9999", "John Doe", "555-1212", "J. Random Hacker","553-1337" ); Perl offers the %phone_book = ( "Sally Smart" => "555-9999", "John Doe" => "555-1212", "J. Random Hacker => "553-1337" ); Accessing a hash element uses the syntax JavaScriptMore correctly known as ECMAScript, JavaScript is a prototype based object-oriented language. Objects can be dynamically extended with new properties. An object is a mapping from property names to values, that is an associative array. The only distinguishing factor is that objects have a prototype link to the object they inherit from. Doing a lookup for a property will forward the lookup to the prototype if the object does not define the property itself. An object literal is written as var myObject = { "Sally Smart" : "555-9999",
"John Doe" : "555-1212",
"J. Random Hacker" : "553-1337" };
If the property name is a valid identifier, the quotes can be omitted, e.g.: var myOtherObject = { foo : 42, bar : false }
Lookup is written using property access notation, either square brackets, which always works, or dot notation, which only works for identifier keys: myObject["John Doe"]
myOtherObject.foo
The ECMAScript standard doesn't say how objects should be implemented. Some implementations are efficient, probably a hash map, while others seem to use a linear time lookup (for instance, Microsoft JScript in Internet Explorer). External links
|
||
|
|
|
|
|
|
Copyright 2008 WordIQ.com - Privacy Policy
::
Terms of Use
:: Contact Us
:: About Us This article is licensed under the GNU Free Documentation License. It uses material from the Wikipedia article "Associative array". |