js实现存储对象的数据结构hashTable和list
AxeSlide软件项目梳理 canvas绘图系列知识点整理
以下代码是typescript语言来写的,其实和es6面向对象的写法基本一致。大家阅读后都明白这些方法的作用。
hash
hash结构用于处理和表现类似key/value的键值对,其中key通常可用来快速查找,同时key是区分大小写;value用于存储对应于key的值。
实现该数据结构的几个方法:
| 函数名 | 说明 | 返回值 |
| set(key,value) | 添加项 | 无 |
| del(key) | 根据key删除一项 | 无 |
| has(key) | 是否包含某个key | bool |
| get(key) | 根据key值获取value | value |
| first() | 获取第一个value | value |
| last() | 获取最后一个value | value |
| count() | 获取项总数 | int |
| all() | 返回所有值的数组 | array |
| getByIndex(index) | 根据在数组中的index顺序获取value | value |
| foreach(callback) | 遍历所有值 | 无 |
| indexOf(key) | 根据key值获取在数组中的顺序值 | index,int |
| insertAt(index,value,key) | 在指定index位置插入key,value | 无 |
export class HashTable<T> {
private items: { [key: string]: HashValue<T> };
@SerializeProperty({ list: true, type: Array })
private itemList: Array<T>;
constructor() {
super();
this.items = {};
this.itemList = [];
}
set(key: string, value: T): void {
var vl = new HashValue<T>();
vl.key = key;
vl.value = value;
var index = this.itemList.length;
if (this.has(key)) {
index = this.items[key].index;
}
vl.index = index;
this.itemList[index] = value;
this.items[key] = vl;
}
del(key: string): void {
if (this.has(key)) {
var index = this.items[key].index;
if (index > -1) {
this.itemList.splice(index, 1);
}
delete this.items[key];
this.resetIndex();
}
}
resetIndex(): void {
this.foreach((k, v: T) => {
var index = this.itemList.indexOf(v);
this.items[k].index = index;
});
}
has(key: string): boolean {
return key in this.items;
}
get(key: string): T {
if (this.has(key)) {
return this.items[key].value;
}
return null;
}
count(): number {
return this.itemList.length;
}
all(): Array<T> {
return this.itemList;
}
first() {
return this.itemList[0];
}
last() {
return this.itemList[this.itemList.length - 1];
}
getByIndex(index: number): T {
return this.itemList[index];
}
//遍历 扩展
foreach(callback) {
for (var key in this.items) {
callback(key, this.items[key].value);
}
}
//获取index
indexOf(key) {
if (this.has(key)) {
return this.items[key].index;
}
}
//插入
insertAt(index: number, value: T, key: string) {
this.itemList.splice(index, 0, value);
var hashV = new HashValue<T>();
hashV.index = index;
hashV.key = key;
hashV.value = value;
this.items[key] = hashV;
this.resetIndex();
}
sort(callback: Function) {
this.itemList.sort((a: T, b: T) => { return callback(a, b);
});
}
}
List
js实现链表List数据结构的几个方法:
| 函数名 | 说明 | 返回值 |
| add(value) | 添加项 | 无 |
| addList(list) | 添加另一个集合 | 无 |
| pop() | 删除最后一项 并返回该项 | value |
| shift() | 删除第一项 并返回该项 | value |
| remove(index) | 根据索引值删除某一项 | 无 |
| removeMany(index,count) | 删除从指定位置开始的某几项 | 无 |
| clear() | 删除所有项 | 无 |
| contains(value) | 是否包含某个值 | boolean |
| indexOf(value) | 根据值获取在数组中的顺序值 | int |
| get(index) | 根据index获取value | value |
| set(index,value) | 设置index位置的value | 无 |
| length() | 获取项总数 | int |
| all() | 返回所有值的数组 | array |
| foreach(callback) | 遍历所有值 | 无 |
| reverseForeach(callback) | 倒序遍历所有值 | 无 |
| sort(callback) | 根据某个排序规则进行排序 | 无 |
| insert(index,value) | 在指定index位置插入value | 无 |
export class List<T> {
private items: Array<T>;
private checkIndex(index): boolean {
return !(index < 0 || isNaN(index) || index >= this.items.length);
}
constructor() {
super();
this.items = new Array<T>();
}
length(): number {
return this.items.length;
}
add(value: T): void {
this.items.push(value);
}
addList(valueList: List<T>) {
for (var i = 0; i < valueList.length(); i++) {
var value = valueList.get(i);
this.items.push(value);
}
}
pop(): T {
return this.items.pop();
}
shift() {
this.items.shift();
}
remove(index: number): void {
if (this.checkIndex(index)) {
this.items.splice(index,1);
}
}
/**
* 從指定索引處開始刪除指定個數的元素
* @param from
* @param count
*/
removeMany(from: number, count: number) {
if (this.checkIndex(from)) {
this.items.splice(from, count);
}
}
clear(): void {
this.items = [];
}
contains(value: T): boolean {
for (var i in this.items) {
return value == this.items[i];
}
return false;
}
indexOf(value: T): number {
return this.items.indexOf(value);
}
insert(index: number, value: T) {
//this.checkIndex(index) && this.items.splice(index , 0, value);
this.items.splice(index, 0, value);
}
get(index: number): T {
return this.items[index];
}
set(index, value: T) {
this.items[index] = value;
}
all(): Array<T> {
return this.items;
}
foreach(callback:(i:number,item:T)=>any) {
var len = this.items.length;
for (var i = 0; i < len; i++) {
if (callback(i, this.items[i]) === false) break;
}
}
reverseForeach(callback) {
var len = this.items.length;
for (var i = len - 1; i >= 0; i--) {
if (callback(i, this.items[i]) === false) break;
}
}
sort(callback: Function) {
this.items.sort((a: T, b: T) => { return callback(a, b); });
}
}
js实现存储对象的数据结构hashTable和list的更多相关文章
- js实现哈希表(HashTable)
在算法中,尤其是有关数组的算法中,哈希表的使用可以很好的解决问题,所以这篇文章会记录一些有关js实现哈希表并给出解决实际问题的例子. 第一部分:相关知识点 属性的枚举: var person = { ...
- JS基础学习——对象
JS基础学习--对象 什么是对象 对象object是JS的一种基本数据类型,除此之外还包括的基本数据类型有string.number.boolean.null.undefined.与其他数据类型不同的 ...
- javascript中的内置对象和数据结构
目录 简介 基础类型 undefined Boolean和Boolean对象 Number和BigInt String Symbol null Object Function Date Array K ...
- js object(对象)
http://www.cnblogs.com/pingchuanxin/p/5773326.html Object(对象)是在所有的编程语言中都十分重要的一个概念,对于事物我们可以把他们看作是一个对象 ...
- Node.js:全局对象
概要:本篇博客主要介绍了node.js中的全局对象. 在JavaScript中,通常window是全局对象,而node.js中的全局对象是global,所有全局变量(除了global本身之外)都是gl ...
- 4月5日--课堂笔记--JS内置对象
JavaScript 4.5 一. JS内置对象 1.数组Array a)创建语法1:var arr=new Array(参数); i. 没有参数:创建一个初始容量为0的数组 ii. ...
- js Web存储方式
JSON是数据交互中最常用的一种数据格式. 由于各种语言的语法都不同,在传递数据时,可以将自己语言中的数组.对象等转换为JSON字符串> 传递之后,可以讲JSON字符串,在解析为JSON对象. ...
- JAVA之旅(二十)—HashSet,自定义存储对象,TreeSet,二叉树,实现Comparator方式排序,TreeSet小练习
JAVA之旅(二十)-HashSet,自定义存储对象,TreeSet,二叉树,实现Comparator方式排序,TreeSet小练习 我们继续说一下集合框架 Set:元素是无序(存入和取出的顺序不一定 ...
- JS内置对象-String对象、Date日期对象、Array数组对象、Math对象
一.JavaScript中的所有事物都是对象:字符串.数组.数值.函数... 1.每个对象带有属性和方法 JavaScript允许自定义对象 2.自定义对象 a.定义并创建对象实例 b.使用函数来定义 ...
随机推荐
- javaScript-什么是变量?
什么是变量? 从字面上看,变量是可变的量:从编程角度讲,变量是用于存储某种/某些数值的存储器.我们可以把变量看做一个盒子,为了区分盒子,可以用BOX1,BOX2等名称代表不同盒子,BOX1就是盒子的名 ...
- <bits/stdc++.h>头文件介绍(包含源代码)
注:转自http://blog.csdn.net/charles_dong2/article/details/56909347,同为本人写的,有部分修改. 之前在一个小OJ上刷题时发现有人是这么写的: ...
- js 实现倒计时效果
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- javascript的运行过程以及setTimeout的运行机制
关于javascript的运行机制大家都应该有所了解了吧,其实javascript是一个单线程的机制,但是因为队列的关系它的表现会让我们感觉是一个多线程的错觉.javascript在运行的时候是这样的 ...
- 不用媒体查询做web响应式设计-遁地龙卷风
(0)写在前面 讲述知乎上看到的一篇文章中的一个案例,让我脑洞大开,佩服至极,特意第二天找到原文赞赏了 5元,原文地址https://zhuanlan.zhihu.com/p/27258076,案例用 ...
- canvas学习总结三:绘制路径-线段
Canvas绘图环境中有些属于立即绘制图形方法,有些绘图方法是基于路径的. 立即绘制图形方法仅有两个strokeRect(),fillRect(),虽然strokezText(),fillText() ...
- 如何运行jar文件
比如我要执行G:/weblogic文件夹下面的wls1036_generic.jar 文件. 1.cmd 2.跳转到G盘(G:回车),如果jar文件在桌面上,运行cd desktop进入桌面再定位到文 ...
- Google帝国研究——Google的产业构成
Google帝国研究--Goog ...
- noip借教室 题解
题目描述 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借教室的信息,我们自然 ...
- c++课程设计之菜单选择
a) 从键盘输入n个数,选择升序还是降序输出 b)创新了日历 c) 添加了射箭游戏 d)还加入了好玩的24点游戏 学生签名: 年 月 日 课程设计(论文)评阅意见 等 级 项 ...