| OLD | NEW |
| (Empty) |
| 1 /** | |
| 2 * @fileoverview This file contains js create a helper Map object | |
| 3 * @author lzheng@chromium.com (Lei Zheng) | |
| 4 */ | |
| 5 | |
| 6 /** | |
| 7 * A Map class that implemeted using two arrays. | |
| 8 */ | |
| 9 Map = function() { | |
| 10 /** | |
| 11 * Array that hold all Keys. | |
| 12 * @private | |
| 13 */ | |
| 14 this.keysArray = []; // Keys | |
| 15 /** | |
| 16 * Array that hold all values. | |
| 17 * @private | |
| 18 */ | |
| 19 this.valsArray = []; // Values | |
| 20 | |
| 21 /** | |
| 22 * Index of where to insert new entries in key and value array. | |
| 23 * @private | |
| 24 */ | |
| 25 this.mapIndex_ = 0; | |
| 26 }; | |
| 27 | |
| 28 /** | |
| 29 * Insert a key, value pair to map. | |
| 30 * @param {object} key An object that supports "==". | |
| 31 * @param {object} value The entry in map. | |
| 32 */ | |
| 33 Map.prototype.insert = function(key, value) { | |
| 34 var index = this.findIndex(key); | |
| 35 if (index == -1) { | |
| 36 this.keysArray[this.mapIndex_] = key; | |
| 37 this.valsArray[this.mapIndex_] = value; | |
| 38 ++this.mapIndex_; | |
| 39 } | |
| 40 else { | |
| 41 this.valsArray[index] = value; | |
| 42 } | |
| 43 }; | |
| 44 | |
| 45 /** | |
| 46 * Get the entry associated with a key. | |
| 47 * @param {object} key for the entry. | |
| 48 * @return {object} The entry in map. | |
| 49 */ | |
| 50 Map.prototype.keys = function() { | |
| 51 return this.keysArray; | |
| 52 }; | |
| 53 | |
| 54 /** | |
| 55 * Get the entry associated with a key. | |
| 56 * @param {object} key for the entry. | |
| 57 * @return {object} The entry in map. | |
| 58 */ | |
| 59 Map.prototype.get = function(key) { | |
| 60 var index = this.findIndex(key); | |
| 61 if (index != -1) { | |
| 62 return this.valsArray[index]; | |
| 63 } | |
| 64 return null; | |
| 65 }; | |
| 66 | |
| 67 | |
| 68 | |
| 69 /** | |
| 70 * Remove an entry associated with a key. | |
| 71 * @param {object} key for the entry. | |
| 72 */ | |
| 73 Map.prototype.remove = function(key) { | |
| 74 var index = this.findIndex(key); | |
| 75 if (index != -1) { | |
| 76 this.keysArray = this.removeFromArray(keysArray, index); | |
| 77 this.valsArray = this.removeFromAarry(valsArray, index); | |
| 78 --this.mapIndex_; | |
| 79 } | |
| 80 return; | |
| 81 }; | |
| 82 | |
| 83 /** | |
| 84 * Get the total entries in the map. | |
| 85 * @return {int} The total number of entries. | |
| 86 */ | |
| 87 Map.prototype.size = function() { | |
| 88 return this.mapIndex_; | |
| 89 }; | |
| 90 | |
| 91 /** | |
| 92 * Clear up everything in the map. | |
| 93 */ | |
| 94 Map.clear = function() { | |
| 95 this.mapIndex_ = 0; | |
| 96 this.keysArray = []; | |
| 97 this.valsArray = []; | |
| 98 }; | |
| 99 | |
| 100 /** | |
| 101 * Find the index associated with a key. | |
| 102 * @private | |
| 103 * @param {object} key for the entry. | |
| 104 * @return {int} The index of this entry. | |
| 105 */ | |
| 106 Map.prototype.findIndex = function(key) { | |
| 107 var result = -1; | |
| 108 for (var i = 0; i < this.keysArray.length; i++) { | |
| 109 if (this.keysArray[i] == key) { | |
| 110 result = i; | |
| 111 break; | |
| 112 } | |
| 113 } | |
| 114 return result; | |
| 115 }; | |
| 116 | |
| 117 /** | |
| 118 * @private | |
| 119 * Remove an entry from the map. | |
| 120 * @param {int} index The index of the entry. | |
| 121 */ | |
| 122 Map.prototype.removeAt = function(array, index) { | |
| 123 var first_half = array.slice(0, index); | |
| 124 var second_half = array.slice(index + 1); | |
| 125 return first_half.concat(second_half); | |
| 126 } | |
| OLD | NEW |