Skip to content

Commit

Permalink
feat: add chap8 book code
Browse files Browse the repository at this point in the history
  • Loading branch information
javaswing committed Nov 23, 2023
1 parent 8f16891 commit a90894a
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 2 deletions.
96 changes: 96 additions & 0 deletions code/chap8/book/dictionary/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { defaultToString } from '../../../utils';
import ValuePair from './value-pair';

/**
* 字典数据结构
*
* @description
*
* 字典数据结构的存储方式为[键,值]的形式,其中的键可以是任意数据类型。字典也称为:映射、符号表、关联数组
* 集合采用的是[值,值]的存储方式
*
* 注:该数据结构在ES6版本的Map是一样的
*/
export default class Dictionary<K, V> {
private table: { [key: string]: ValuePair<K, V> };
private toStrFn: (v: any) => any;

constructor(toStrFn = defaultToString) {
this.toStrFn = toStrFn;
this.table = {};
}

hasKey(k: K) {
return this.table[this.toStrFn(k)] != null;
}

set(k: K, v: V) {
if (k !== null && v !== null) {
const strK = this.toStrFn(k);
this.table[strK] = new ValuePair(k, v);
return true;
}
return false;
}

remove(k: K) {
if (this.hasKey(k)) {
delete this.table[this.toStrFn(k)];
return true;
}
return false;
}

get(k: K) {
if (this.hasKey(k)) {
const v = this.table[this.toStrFn(k)];
return v.value;
}
return undefined;
}

keyValues() {
return Object.values(this.table);
}

keys() {
return this.keyValues().map((e) => e.key);
}

values() {
return this.keyValues().map((e) => e.value);
}

forEach(cb: (k: K, v: V) => any) {
const vp = this.keyValues;

for (let i = 0; i < vp.length; i++) {
const r = cb(vp[i].key, vp[i].value);
if (r === false) break;
}
}

size() {
return Object.keys(this.table).length;
}

clear() {
this.table = {};
}

isEmpty() {
return this.size() === 0;
}

toString() {
if (this.isEmpty()) {
return '';
}
const valuePairs = this.keyValues();
let objString = `${valuePairs[0].toString()}`;
for (let i = 1; i < valuePairs.length; i++) {
objString = `${objString},${valuePairs[i].toString()}`;
}
return objString;
}
}
12 changes: 12 additions & 0 deletions code/chap8/book/dictionary/value-pair.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default class ValuePair<K, V> {
value: V;
key: K;
constructor(key: K, value: V) {
this.key = key;
this.value = value;
}

toString() {
return `[${this.key}: ${this.value}]`;
}
}
Empty file added code/chap8/book/type.ts
Empty file.
27 changes: 27 additions & 0 deletions code/chap8/test/book/dictionary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Dictionary from '../../book/dictionary';

describe('test dictionary', () => {
it('test dictionary hasKey', () => {
const d = new Dictionary<string, number>();

expect(d.hasKey('our')).toBeFalsy();
});

it('test dictionary set', () => {
const d = new Dictionary<string, number>();
d.set('test', 1);
expect(d.toString()).toBe(`[test: 1]`);
});

it('test dictionary remove', () => {
const d = new Dictionary<string, number>();
d.set('test', 1);
expect(d.remove).toBeTruthy();
});

it('test dictionary get', () => {
const d = new Dictionary<string, number>();
d.set('javaswing', 1);
expect(d.get('javaswing')).toBe(1);
});
});
11 changes: 11 additions & 0 deletions code/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@ export const defaultCompare = (a, b) => {
if (a === b) return 0;
return a < b ? Compare.LESS_THAN : Compare.BIGGER_THAN;
};

export const defaultToString = (v: any) => {
if (v === null) {
return 'NULL';
} else if (v === undefined) {
return 'UNDEFINED';
} else if (typeof v === 'string' || v instanceof String) {
return `${v}`;
}
return v.toString();
};
3 changes: 1 addition & 2 deletions docs/zh/guide/introduction.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# 介绍


Todo List
主要是为了面试,想深入了解并分析下数据结构与算法。

0 comments on commit a90894a

Please sign in to comment.