当前位置 : 主页 > 手机开发 > 其它 >

Cocos2d-JS 实现Map Vector

来源:互联网 收集:自由互联 发布时间:2021-06-13
/** * 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元素的
/**
 * 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
 * @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: 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 = 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 = function (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;
};
网友评论