JavaScript implementations for JavaScript Hashmap:hashmap

Source: Internet
Author: User

function HashMap () {
/**
* Map Size
*/
var size = 0;
/**
* Maximum container default length
*/
var length = 256
var loadfactor = 0.75;
/**
* Prime number
*/
var prime = 1000000;
/**
* Object
*/
var table = new Array (length);
/**
* Set up the hash prime number
* @param {Object} p
*/
This.setprime = function (p) {
This.prime = p;
}
/**
* Get build hash prime
*/
This.getprime = function () {
return this.prime;
}
/**
The calculation method of hash value in the current period
*/
This.hash = Bkdrhash;
/**
* HashMap's Put method
* @param {Object} key
* @param {Object} value
*/
This.put = function (key, value) {
/**
* Re-hash if the loading factor is greater than 0.75
*/
if (Size/length > Loadfactor) {
Rehash (this);
}
var counter = 0;
var code = This.hash (key);
If the specified location is empty, it is stored directly
if (typeof table[code] = = ' undefined ') {
size = size + 1;
Table[code] = new Entry (key, value);
If the specified position is not empty and the key value is equal, overwrite the
}
Else
if (Table[code].key = = key) {
Table[code].value = = value;
}
Hash conflict occurred
else {
var entry = Table[code];
Access Hash List
while (entry.next!= undefined) {
Entry = Entry.next;
if (Entry.key = = key) {
Covered
Entry.value =
Value
Break
}else if (Entry.next = = undefined) {
New
Entry.next = new entry (key,value);
Entry.next.prev = entry;
Size = Siez + 1;
Break
}
}
}
}
/**
* value from HashMap via key
* @param {Object} key value
*/
This.load = function (key) {
var hashcode = This.hash (key);
if (typeof Table[hashcode]!= ' undefined ') {
var entry = Table[hashcode];
if (Entry.key = = key) {
return entry.value;
}else {
while (entry.next!= undefined) {
Entry = Entry.next;
if (Entry.key = = key) {
return entry.value;
}
}
}
}
return null; This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20121114/33820.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.