美文网首页React & JS
React JSX实现HashMap

React JSX实现HashMap

作者: Autonavi | 来源:发表于2017-03-21 15:27 被阅读0次

    function HashMap() {
    let KEYS = [];
    let VALUES = [];

    this.isEmpty = function isEmpty() {
    return KEYS.length === 0;
    };

    this.containsKey = function containsKey(key) {
    let i = 0;
    for (i = 0; i < KEYS.length; i++) {
    if (KEYS[i] === key) {
    return true;
    }
    }
    return false;
    };

    this.containsValue = function containsValue(value) {
    let i = 0;
    for (i = 0; i < VALUES.length; i++) {
    if (VALUES[i] === value) {
    return true;
    }
    }
    return false;
    };

    this.put = function put(key, value) {
    let flag = false;
    let i = 0;
    for (i = 0; i < VALUES.length; i++) {
    if (KEYS[i] === key) {
    KEYS[i] = key;
    VALUES[i] = value;
    flag = true;
    }
    }
    if (!flag) {
    KEYS.push(key);
    VALUES.push(value);
    }
    };

    this.get = function get(key) {
    let i = 0;
    for (i = 0; i < VALUES.length; i++) {
    if (KEYS[i] === key) {
    return VALUES[i];
    }
    }
    return null;
    };

    this.remove = function remove(key) {
    let i = 0;
    for (i = 0; i < VALUES.length; i++) {
    if (KEYS[i] === key) {
    KEYS.splice(i, 1);
    VALUES.splice(i, 1);
    return;
    }
    }
    };

    this.values = function values() {
    const _VALUES = [];
    let i = 0;
    for (i = 0; i < VALUES.length; i++) {
    _VALUES.push(VALUES[i]);
    }
    return _VALUES;
    };

    this.keySet = function keySet() {
    const _KEYS = [];
    let i = 0;
    for (i = 0; i < KEYS.length; i++) {
    _KEYS.push(KEYS[i]);
    }
    return _KEYS;
    };

    this.size = function size() {
    return KEYS.lenth;
    };

    this.clear = function clear() {
    KEYS = [];
    VALUES = [];
    };
    }

    export default HashMap;

    // 参考文献:http://www.cnblogs.com/chunyansong/p/5485759.html?spm=a1z2e.8028000.0.0.UAtnWF

    相关文章

      网友评论

        本文标题:React JSX实现HashMap

        本文链接:https://www.haomeiwen.com/subject/gwrgnttx.html