1. 字典(Dictionary)
<待补充>
export class Dictionary {
constructor(toStrFn = defaultToString) {
this.toStrFn = toStrFn;
this.table = {};
}
set(key, value) {
if(key != null && value !=null) {
const tableKey = this.toStrFn(key)
const pair = new ValuePair(key, value)
this.table[tableKey] = pair;
return true;
}
return false;
}
get(key) {
if(this.hasKey(key)) {
const tableKey = this.toStrFn(key);
const pair = this.table[tableKey];
return pair.value;
}
return undefined;
}
remove(key) {
if(this.hasKey(key)) {
const tableKey = this.toStrFn(key);
delete this.table[tableKey];
return true;
}
return false;
}
hasKey(key) {
const tableKey = this.toStrFn(key);
return this.table[tableKey] != null;
}
size() {
return Object.keys(this.table).length;
}
isEmpty() {
return this.size() === 0
}
keys() {
return this.keyValues().map(item => item.key);
}
values() {
return this.keyValues().map(item => item.value);
}
keyValues() {
return Object.values(this.table);
}
toString() {
if(this.isEmpty()) {
return '';
}
const valuePairs = this.keyValues();
let str = valuePairs[0].toString();
for(let i = 1; i < valuePairs.length; i++) {
str = `${str}, ${valuePairs[i].toString()}`;
}
return str;
}
}
class ValuePair {
constructor(key, value) {
this.key = key;
this.value = value;
}
toString() {
let keyStr = '', valueStr = '';
try {
keyStr = `${this.key}`;
} catch(e) {
keyStr = this.key.toString();
}
try {
valueStr = `${this.value}`;
} catch(e) {
valueStr = this.value.toString();
}
return `{${keyStr}: ${valueStr}}`;
}
}
const defaultToString = item => {
if(item === null) {
return 'NULL';
}
if(item === undefined) {
return 'UNDEFINED';
}
if(typeof item === 'string' || item instanceof String) {
return `${item}`;
}
return item.toString();
}
const dic = new Dictionary();
const obj = {};
const sym = Symbol();
dic.set('a', 1);
dic.set('b', true);
dic.set(obj, 1);
dic.set(sym, 1);
2. 哈希表(Hash)
<待补充>
export class HashTable {
constructor(toStrFn = defaultToString) {
this.toStrFn = toStrFn;
this.table = {};
}
put(key, value) {
if(key != null && value !=null) {
const tableKey = this.hashCode(key)
const pair = new ValuePair(key, value)
this.table[tableKey] = pair;
return true;
}
return false;
}
get(key) {
if(this.hasKey(key)) {
const tableKey = this.hashCode(key);
const pair = this.table[tableKey];
return pair.value;
}
return undefined;
}
remove(key) {
if(this.hasKey(key)) {
const tableKey = this.hashCode(key);
delete this.table[tableKey];
return true;
}
return false;
}
forEach(fn) {
const keys = this.keys();
for(let i = 0; i < keys; i++) {
const key = keys[i];
const tableKey = this.hashCode(key);
const item = this.item[tableKey];
fn(item, key, i);
}
}
hasKey(key) {
const tableKey = this.hashCode(key);
return this.table[tableKey] != null;
}
size() {
return Object.keys(this.table).length;
}
isEmpty() {
return this.size() === 0
}
keys() {
return this.keyValues().map(item => item.key);
}
values() {
return this.keyValues().map(item => item.value);
}
keyValues() {
return Object.values(this.table);
}
toString() {
if(this.isEmpty()) {
return '';
}
const valuePairs = this.keyValues();
let str = valuePairs[0].toString();
for(let i = 1; i < valuePairs.length; i++) {
str = `${str}, ${valuePairs[i].toString()}`;
}
return str;
}
hashCode(key) {
if(typeof key === 'number') {
return key;
}
const tableKey = this.toStrFn(key);
let hash = 0;
for(let i = 0; i < tableKey.length; i++) {
hash += tableKey.charCodeAt(i);
}
hash = hash % 72;
return hash;
}
}
class ValuePair {
constructor(key, value) {
this.key = key;
this.value = value;
}
toString() {
let keyStr = '', valueStr = '';
try {
keyStr = `${this.key}`;
} catch(e) {
keyStr = this.key.toString();
}
try {
valueStr = `${this.value}`;
} catch(e) {
valueStr = this.value.toString();
}
return `{${keyStr}: ${valueStr}}`;
}
}
const defaultToString = item => {
if(item === null) {
return 'NULL';
}
if(item === undefined) {
return 'UNDEFINED';
}
if(typeof item === 'string' || item instanceof String) {
return `${item}`;
}
return item.toString();
}
const dic = new Dictionary();
const obj = {};
const sym = Symbol();
dic.set('a', 1);
dic.set('b', true);
dic.set(obj, 1);
dic.set(sym, 1);
3. 集合(Set)
<待补充>
export class Set {
constructor() {
this.items = {};
this.length = 0;
}
add(item) {
if(this.has(item)) {
return this;
}
this.items[item] = item;
this.length++;
return this;
}
delete() {
if(this.has(item)) {
this.length--;
return delete this.items[item];
}
return false;
}
clear() {
this.items = {};
this.length = 0;
}
has(item) {
return item in this.items;
}
size() {
return this.length;
}
keys() {
return Object.keys(this.items);
}
values() {
return Object.values(this.items);
}
entries() {
return Object.entries(this.items);
}
forEach(callback) {
let index = 0;
for(let item in this.items) {
if(this.items.hasOwnProperty(item)) {
callback(item, index, this);
index++;
}
}
}
union(set) {
const unionSet = new Set();
if(set instanceof Set) {
this.forEach((key) => {
if(!unionSet.has(key)) {
unionSet.add(this.items[key]);
}
});
set.forEach((key) => {
if(!unionSet.has(key)) {
unionSet.add(this.items[key]);
}
});
}
return unionSet;
}
intersection(set) {
const intersectionSet = new Set();
if(set instanceof Set) {
const keys = this.keys().concat(set.keys());
for(const key of keys) {
if(this.has(key) && set.has(key)) {
intersectionSet.add(this.items[key]);
}
}
}
return intersectionSet;
}
difference() {
}
}
下一篇:【数据结构】自己动手使用JavaScript实现二叉搜索树、二叉堆(第三篇)-CSDN博客
注:以上,如有不合理之处,还请帮忙指出,大家一起交流学习~