Principle ES6 Map

Map key ES6 may be any data structure, and will not be repeated.

Then map the underlying principle is Shane?

Map using the linked list, hash ideas to achieve.

First, Map can be achieved deleted and deleted data may be an intermediate value. The advantage is that the list anywhere in the middle of adding, deleting elements are very fast, without moving other elements directly change a pointer on it. .

In many cases data storage, will lead the chain is too long, resulting in slow search efficiency, so we can create a bucket (container storage object), based on hash (hash value by the algorithm becomes a fixed value) to draw distribution data, to prevent the chain is too long.

 

 

FIG follows: inside the tub 3 positions, each position is an object, next attribute points to the next object in the object is not associated with together.

 

 

 

The Map property value and attribute names are stored in the value of the object.

Simple analog Map, code is as follows:

function Mymap () {// Constructor
    this.init();
}
// initialization function that creates barrels (array), each location is an object, set the next property on the properties of each object and initialized to null. Mymap.prototype.init = function () { this.tong = new Array(8); for (var i = 0; i
< 8; i++) { this.tong[i] = new Object(); this.tong[i].next = null; } }; //adding data. Mymap.prototype.set = function (key, value) { var index = this.hash (key); // set to obtain the key provided on the location of the current var TempBucket = this.tong [index]; // get the current position of the object while (TempBucket.next) {// if the current object is traversing a link is not empty if (TempBucket.next.key == key) {// To set attribute already exists, its value is covered. TempBucket.next.value = value; return; // return, do not continue traversal } else { TempBucket = TempBucket.next; // pointer to the next object. } } TempBucket.next = {// next object is null, adding objects. key: key, value: value, next: null } };
//Query data Mymap.prototype.get = function (key) { var index = this.hash(key); var TempBucket = this.tong[index]; while(TempBucket){ if(TempBucket.key == key){ return TempBucket.value; }else{ TempBucket = TempBucket.next; } } return undefined; }
//delete data Mymap.prototype.delete = function(key){ var index = this.hash(key); var TempBucket = this.tong[index]; while(TempBucket){ if(TempBucket.next.key == key){ TempBucket.next = TempBucket.next.next; return true; }else{ TempBucket = TempBucket.next; } } }
// see if there is a current property Mymap.prototype.has = function(key){ var index = this.hash(key); var TempBucket = this.tong[index]; while(TempBucket){ if(TempBucket.key == key){ return true; }else{ TempBucket = TempBucket.next; } } return false; }
// Clear the map Mymap.prototype.clear = function(){ this.init(); } // property that the average assigned to each location, such that a chain is not too long. Mymap.prototype.hash = function (key) { was index = 0; if (typeof key == "string") { for (var i = 0; i < 3; i++) { index = index + isNaN(key.charCodeAt(i)) ? 0 : key.charCodeAt(i); } } else if (typeof key == 'object') { index = 0; } else if (typeof key == 'number') { index = isNaN(key) ? 7 : key; } else { index = 1; } return index % 8; } var map = new Mymap (); // constructor used instantiate Map

map.set ( 'name', 'ZWQ');
as map.get ( 'name');
map.has ( 'name);

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/jiaobaba/p/11918975.html