COCOS2D-JS implementation Map Vector

/** * Map * Created by Lovell on 16/6/20. */pg.Map = cc.Class.extend({ _elements : null});/** * Create Map * @returns < /span>{*} */pg.Map.create = function (){ var res = new pg.Map(); span> res.clear(); return res;};/** * The length of the Map element< span style="color:#629755;font-style:italic;"> * @returns {*}< /span> */pg.Map.prototype.< span style="color:#ffc66d;">size = function () {return this._elements.length; };/** * Determine whether the Map is empty * @returns {boolean} */pg.Map.prototype.isEmpty = function () {< span style="color:#cc7832;font-weight:bold;">return (this._elements.length <1);};pg. Map.prototype.clear = function ( ) {this._elements = [];};/** * Add elements to the Map  * @param key * @param value */pg.Map.prototype.put < /span>= function (key, value) {< span style="color:#cc7832;font-weight:bold;">if (this.containKey(key)) {this.remove(key); } this._elements.push< /span>({ key: key, value: value });};/**  * Insert elements into the Map * @param index * @param key * @param value */pg.Map.prototype.insert = function (index, key, value) {if (this.containKey(key)) {this.remove(key); } this. _elements.splice(index, 0,{ key: key, value< /span>: value });};/** * Delete the element with the specified Key * @param key * @returns {boolean} */pg.Map.prototype.remove = function (key) {var ret = false; try { for (var i=0; i<this._elements.length; i++) {if (this._elements[i].key == key) {this._elements.splice(i, 1) ; return true; }}} catch (e) {ret = false;  } return ret;};/** * Get the element value of the specified Key, return NULL if it fails * @param key * @returns {*}< span style="color:#629755;font-style:italic;"> */pg.Map.prototype.get = function (key) {try { for (var i = 0; i << span style="color:#cc7832;font-weight:bold;">this._elements.length ; i++) {if ( this._elements[i].key == key) {< span style="color:#cc7832;font-weight:bold;">return this._elements[i].value; }}} catch (e) {return null< span style="color:#cc7832;">; }};/** * Set the element value Value of the specified Key, return FALSE on failure * < sp an style="color:#629755;font-weight:bold;font-style:italic;">@param key * @ param value * @returns { boolean} */pg.Map. prototype.set = function (key, value) {try { for (var i = 0; i <this._elements.length; i++) {if (this._elements[i].key == key) { this._elements[i].value = value; return true< /span>; }}} catch (e) {return false ; }}; span>/** * Get the corresponding Value through Index * @param index * @returns {*} */ pg.Map.prototype.getValueByIndex = function (index) {return this._elements[index].value;};/ ** * Get the corresponding Index through Key * @param key * @returns {number} */pg.Map.prototype.getIndexByKey  span>= function (key) {try < /span>{ for (i = 0; i < this._elements.length; i++) {if (< span style="color:#cc7832;font-weight:bold;">this._elements[i].key == key) { return i; }}} catch (e) {ret urn -1; }};/** * Get Key through Index * @param index * @returns {*} */pg.Map.prototype.getKeyByIndex = function (index) {re turn this._elements[index].key;};pg.Map.prototype.removeByIndex = function (index) {this.remove (this._elements[index]. key);};< /span>/** * Get the element of the specified index * @param index * @returns {*} */pg.Map .prototype.element = function (index ) {if (index <0 || index >= this._elements.length) {return null; } return this._elements[in dex];};< /span>/** * Judging in the Map Whether it contains the element of the specified KEY * @param key * @returns {boolean} */pg.Map.prototype.containKey = function (key) {var ret = false< /span>; try { for (var i = 0; i <this._elements.length;  i++) {if (this ._elements[i].key == key) {ret = true; }}} catch (e) {ret = false; } return ret;};/** * Determine whether the Map contains elements of the specified VALUE *  span>@param  value * @returns {boolean} */pg.Map.prototype.containValue = function (value) {var ret = false; try { for (var i = 0 ; i <this._elements.length ; i++) {if (this._elements[i].value == value) {ret = true; }}} catch (e ) {ret = false; } return ret;};/** * Get the array of all VALUEs in the Map * @returns {Array} */pg.Map.prototype.values ​​= functi on () {var arr = []; for (var i = 0; i <this._elements.length; i++) {arr.push(this._elements[i].value)< span style="color:#cc7832;">; } return arr;};/** * Get an array of all KEYs in the Map * @returns {Array} */pg.< span style="color:#9876aa;">Map.prototype.keys = function () {var arr = []; for (var i = 0; i <this._elements. length; i++) {arr.push(this ._elements[i].key); } return arr;};< /pre> 
/*********************************************** ************************************************** ************************************************** *********************************** * Created by Lovell on 8/1/16. */pg.Vector = cc.Class.extend({ _array: null});pg.Vector.create = < span style="color:#cc7832;font-weight:bold;">function () {var res = new pg.Vector(); if  (res.init()) {return res; } res = null; return null span>;};< /span>pg.Vector.prototype.init = function () {this._array = []; return true;} ;pg.Vector.prototype.push = function (obj) {this._array.push(obj);< /span>};pg.Vector.prototype.at = function  (index) {if (index <0 || index >= this._array.length) {        return null; }    return this._array[index] ;};pg. Vector.prototype.erase = function (obj) {    for (var i = 0; i < this._array.length; i++) {        if (this._array[i] == obj) {            this._array.splice(i, 1);            break;        }    }};pg.Vector.prototype.eraseAt = function (index) {    this._array.splice(index, 1);};pg.Vector.prototype.contains = function (obj) {    for (va r i = 0; i < this._array.length; i++) {        if (this._array[i] == obj) {            return true;        }    }    return false;};pg.Vector.prototype.size = function () {    return this._array.length;};pg.Vector.prototype.insert = function (index, obj) {    this._array.splice(index, 0, obj);};pg.Vector.prototype.clear = function () {    this._array.length = 0;};

/** * Map * Created by Lovell on 16/6/20. */pg.Map = cc.Class.extend({    _elements : null});/** * 创建Map  * @returns {*} */pg.Map.create = function (){    var res = new pg.Map();    res.clear();    return res;};/** * Map元素的长度 * @returns {*} */pg.Map.prototype.size = function () {    return this._elements.length;};/** * 判断Map是否为空 * @returns {boolean} */pg.Map.prototype.isEmpty = function () {    return (this._elements.length < 1);};pg.Map.prototype.clear = function () {    this._elements = [];};/** * 向Map中增加元素 * @param key * @param value */pg.Map.prototype.put = function (key, value) {    if (this.containKey(key)) {        this.remove(key);    }    this._elements.push({        key: key,        value: value    });};/** * 向Map中插入元素 * @param index * @para m key * @param value */pg.Map.prototype.insert = function (index, key, value) {    if (this.containKey(key)) {        this.remove(key);    }    this._elements.splice(index,0,{        key: key,        value: value    });};/** * 删除指定Key的元素 * @param key * @returns  {boolean} */pg.Map.prototype.remove = function (key) {    var ret = false;    try {        for (var i=0; i<this._elements.length; i++) {            if (this._elements[i].key == key)            {                this._elements.splice(i, 1);                return true;            }        }    } catch (e) {        ret = false;    }    return ret;};/** * 获取指定Key的元素值Value,失败返回NULL * @param key * @returns {*} */pg.Map.prototype.get = function (key) {    try {        for (var i = 0; i < this._elements.length; i++) {            if (this._elements[i].key == key) {                return this._elements[i].value;            }        }    } catch (e) {        return null;    }};/** * 设置指定Key的元素值Value,失败返回FALSE * @param key * @param value * @returns {boolean} */pg.Map.prototype.set = function (key, value) {    try {        for (var i = 0; i < this._elements.length; i++) {            if (this._elements[i].key == key) {                this._elements[i].value = value;                return true;            }        }    } catch (e) {        return false;    }};/** * 通过Index获取对应的Value * @param index * @returns {*} */pg.Map.prototype.getValueByIndex = function (index) {    return this._elements[index].value;};/** * 通过Key获取对应的Index * @param key * @returns {number} */pg.Map.prototype.getIndexByKey = function (key) {    try {        for (i = 0; i < this._elements.length; i++) {            if (this._elements[i].key == key) {                return i;            }        }    } catch (e) {        return -1;    }};/** * 通过Index获得Key * @param index * @returns {*} */pg.Map.prototype.getKeyByIndex < /span>= function (index) {    return this._elements[index].key;};pg.Map.prototype.removeByIndex = function (index) {    this.remove(this._elements[index].key);};/** * 获取指定索引的元素 * @param index * @returns {*} */pg.Map.prototype.element = function (index) {    if (index < 0 || index >= this._elements.length) {        return null;    }    return this._elements[index];};/** * 判断Map中是否含有指定KEY的元素 * @param key * @returns {boolean} */pg.Map.prototype.containKey = function (key) {    var ret = false;    try {        for (var i = 0; i < this._elements.length; i++) {            if (this._elements[i].key == key) {                ret = true;            }        }    } catch (e) {        ret = false;    }    return ret;};/** * 判断Map中是否含有指定VALUE的元素 * @param value * @returns {boolean} */ pg.Map.prototype.containValue = function (value) {    var ret = false;    try {        for (var i = 0; i < this._elements.length; i++) {            if (this._elements[i]. value == value) {                ret = true;            }        }    } catch (e) {        ret = false;    }    return ret;};/** * 获取Map中所有VALUE的数组 * @returns {Array} */pg.Map.prototype.values = function () {    var arr = [];    for (var i = 0; i < this._elements.length; i++) {        arr.push(this._elements[i].value);    }    return arr;};/** * 获取Map中所有KEY的数组 * @returns {Array} */pg.Map.prototype.keys = function () {    var arr = [];    for (var i = 0; i < this._elements.length; i++) {        arr.push(this._elements[i].key);    }    return arr;};
/************************************************************************************************************************************************************************************** * Created by Lovell on 8/1/16. */pg.Vector = cc.Class.extend({    _array: null});pg.Vector.create = function () {    var res = new pg.Vector();    if (res.init()) {        return res;     }    res = null;    return null;};pg.Vector.prototype.init = function () {    this._array = [];    return true;};pg.Vector.prototype.push = function (obj) {    this._array.push(obj);};pg.Vector.prototype.at = function (index) {    if (index < 0 || index >= this._array.length) {        return null;    }    return this._array[index];};pg.Vector.prototype.erase = function (obj) {    for (var i = 0; i < this._array.length; i++) {        if (this._array[i] == obj) {            this._array.splice(i, 1);            break;        }    }};pg.Vector.prototype.eraseAt = function (index) {    this._array.splice(index, 1);};pg.Vector.prototype.contains = fun ction (obj) {    for (var i = 0; i < this._array.length; i++) {        if (this._array[i] == obj) {            return true;        }    }    return false;};pg.Vector.prototype.size = function () {    return this._array.length;};pg.Vector.prototype.insert = function (index, obj) {    this._array.splice(index, 0, obj);};pg.Vector.prototype.clear = function () {    this._array.length = 0;};

Leave a Comment

Your email address will not be published.