parent
0cca4403c1
commit
eb93939f68
@ -0,0 +1,137 @@
|
||||
/* 键值对 int->String */
|
||||
class Entry {
|
||||
public key: number;
|
||||
public val: string;
|
||||
constructor(key: number, val: string) {
|
||||
this.key = key;
|
||||
this.val = val;
|
||||
}
|
||||
}
|
||||
|
||||
/* 数组的初始化和基本操作 */
|
||||
class ArrayList {
|
||||
|
||||
private readonly elements: Entry[];
|
||||
constructor(length: number) {
|
||||
this.elements = new Array(length);
|
||||
this.initialize();
|
||||
}
|
||||
|
||||
/* 初始化 */
|
||||
private initialize() {
|
||||
this.elements.fill(null as any, 0, this.elements.length - 1);
|
||||
}
|
||||
|
||||
/* 新增和删除 */
|
||||
public set(key: number, val: string | null) {
|
||||
this.isOutOfRange(key);
|
||||
if (val !== null) {
|
||||
this.elements[key] = new Entry(key, val);
|
||||
}
|
||||
this.elements[key] = null as any;
|
||||
}
|
||||
|
||||
/* 获取 */
|
||||
public get(key: number): string {
|
||||
return this.elements[key].val;
|
||||
}
|
||||
|
||||
public entrySet() {
|
||||
let arr = [];
|
||||
for (let i = 0; i < this.elements.length; i++) {
|
||||
if (this.elements[i] !== null) {
|
||||
arr.push(this.elements[i]);
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
public valueSet() {
|
||||
let arr = [];
|
||||
for (let i = 0; i < this.elements.length; i++) {
|
||||
if (this.elements[i] !== null) {
|
||||
arr.push(this.elements[i].val);
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
public keySet() {
|
||||
let arr = [];
|
||||
for (let i = 0; i < this.elements.length; i++) {
|
||||
if (this.elements[i] !== null) {
|
||||
arr.push(this.elements[i].key);
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
private isOutOfRange(key: number) {
|
||||
if (key > this.elements.length - 1) {
|
||||
throw new Error('Out of array range');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 基于数组简易实现的哈希表 */
|
||||
class ArrayHashMap {
|
||||
// 初始化一个长度为 100 的桶(数组)
|
||||
private bucket: ArrayList;
|
||||
|
||||
constructor() {
|
||||
this.bucket = new ArrayList(100);
|
||||
}
|
||||
|
||||
/* 哈希函数 */
|
||||
private hashFunc(key: number): number {
|
||||
return key % 100;
|
||||
}
|
||||
|
||||
/* 查询操作 */
|
||||
public get(key: number): string | null {
|
||||
let index = this.hashFunc(key);
|
||||
let val = this.bucket.get(index);
|
||||
if (val === null) {
|
||||
return null;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
/* 添加操作 */
|
||||
public put(key: number, val: string) {
|
||||
let index = this.hashFunc(key);
|
||||
this.bucket.set(index, val);
|
||||
}
|
||||
|
||||
/* 删除操作 */
|
||||
public remove(key: number) {
|
||||
let index = this.hashFunc(key);
|
||||
// 置为 null ,代表删除
|
||||
this.bucket.set(index, null);
|
||||
}
|
||||
|
||||
/* 获取所有键值对 */
|
||||
public entrySet(): Entry[] {
|
||||
return this.bucket.entrySet();
|
||||
}
|
||||
|
||||
/* 获取所有键 */
|
||||
public keySet(): number[] {
|
||||
return this.bucket.keySet();
|
||||
}
|
||||
|
||||
/* 获取所有值 */
|
||||
public valueSet(): string[] {
|
||||
return this.bucket.valueSet();
|
||||
}
|
||||
|
||||
/* 打印哈希表 */
|
||||
public print() {
|
||||
let entrySet = this.entrySet();
|
||||
for (const entry of entrySet) {
|
||||
console.info(`${entry.key} -> ${entry.val}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default ArrayHashMap;
|
@ -0,0 +1,16 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es6",
|
||||
"moduleResolution": "node",
|
||||
"resolveJsonModule": true,
|
||||
"module": "esnext",
|
||||
"strict": true,
|
||||
"importHelpers": true,
|
||||
"noEmit": false,
|
||||
"sourceMap": true,
|
||||
"baseUrl": ".",
|
||||
"esModuleInterop": true,
|
||||
"downlevelIteration": true,
|
||||
},
|
||||
"include": ["./**/*"],
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/codes/java" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
Loading…
Reference in new issue