@trudbot/map
TypeScript icon, indicating that this package has built-in type declarations

0.0.4 • Public • Published

Easy-map

这是一个c++风格的map, 底层使用红黑树实现

Install

npm install @trudbot/map

Usage

import {EasyMap} from '@trudbot/map';

const map = new EasyMap<number, number>();

map.insert(1, 2);
map.insert(2, 3);
console.log(map.get(1)); // 2

map.erase(1);

console.log(map.get(1)); // null

const proxy = map.createProxy(p => {
    if (typeof p === 'number') return p;
    if (typeof p === 'string') {
        const num = parseInt(p);
        return isNaN(num) ? null : num;
    }
    return null;
});

console.log(proxy.get(2)); // 3
proxy[2] = 4;
console.log(proxy.get(2)); // 4
proxy[2] ++;
console.log(proxy.get(2)); // 5

API

EasyMap<K, V>

constructor(config?: {compare?: Compare<K>, defaultValue?: V})

  • compare 比较函数, 需要返回COMPARE_RESULT枚举值
export type Compare<K> = (a: K, b: K) =>
    COMPARE_RESULT.MORE |  // a > b
    COMPARE_RESULT.EQUAL |  // a === b
    COMPARE_RESULT.LESS;  // a < b
  • defaultValue 默认值 该值只会在使用proxy时起作用, 如
const map = new map<number, number>({defaultValue: 0});
const proxy = map.createProxy(p => {
    if (typeof p === 'number') return p;
    if (typeof p === 'string') {
        const num = parseInt(p);
        return isNaN(num) ? null : num;
    }
    return null;
});

console.log(proxy[1]); // 0

insert(key: K, value: V): RBNode<K, V>

插入一个键值对, 返回值为插入的节点

erase(key: K): RBNode<K, V> | null

删除一个键值对, 返回删除的节点

get(key: K): V | null

获取一个键值对, 如果不存在则返回null

clear()

清空所有键值对

keys(): K[]

返回所有的键, 按键升序

values(): V[]

返回所有的值, 按键升序

entries(): {key: K, value:V}[]

返回所有的键值对, 按键升序

size(): number

返回map中键值对的数量

count(key: K): number

返回指定键的数量, 0或1

has(key: K): boolean

返回map中是否存在指定键

createProxy(transform: (key: K) => T): ProxyMap<T>

创建一个map代理, 代理 支持通过中括号表达式存取键值对。

forEach(callback: (value: V, key: K, map: EasyMap<K, V>) => void): void

按键升序遍历map中的键值对

ProxyMap

proxyMap支持通过中括号表达式存取键值对。

transform(p: PropertyKey): V

在createProxy时传入的转换函数, 用于将中括号表达式的键转换为map中的键 PropertyKeystring | number | symbol的联合类型。

const map = new map<number, number>({defaultValue: 0});
const proxy = map.createProxy(p => {
    if (typeof p === 'number') return p;
    if (typeof p === 'string') {
        const num = parseInt(p);
        return isNaN(num) ? null : num;
    }
    return null;
});

proxy[1] // map.get(1)
proxy[1] = 2 // map.insert(1, 2), 此处与insert不完全等价, 因为proxy[1]只会返回true
delete proxy[1] // map.erase(1), 此处与erase不完全等价, 因为delete proxy[1]只会返回true
1 in proxy  // map.has(1)
proxy[1] ++ // map.insert(1, map.get(1))
// ...

Readme

Keywords

none

Package Sidebar

Install

npm i @trudbot/map

Weekly Downloads

7

Version

0.0.4

License

MIT

Unpacked Size

19.1 kB

Total Files

5

Last publish

Collaborators

  • trudbot_u