js实现存储对象的数据结构hashTable和list
2017-07-17 14:32
323 查看
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); }); } }
相关文章推荐
- LinkedList实现堆栈数据结构的存储方式与队列的数据结构存储方式
- 利用修改div的位置+js对象存储div信息 实现简单的div自定义布局功能
- List集合根据存储对象的属性字段排序实现
- ssdb底层实现——ssdb底层是leveldb,leveldb根本上是skiplist(例如为存储多个list items,必然有多个item key,而非暴力string cat),用它来做redis的list和set等,势必在数据结构和算法层面上有诸多不适
- List集合存储字符串和自定义对象并遍历(for循环和迭代)
- js中json对象不规律key的遍历实现和json对象length属性的调用
- js实现List的部分功能
- js禁止document element对象选中文本实现代码
- JS JSON对象转为字符串的简单实现方法
- Struts2+JQuery+JSON实现异步交互,包括从后台获得单个值、对象、List和Map数据并从前台的表达中获取值
- js 鼠标拖动对象 可让任何div实现拖动效果
- 【数据结构】线性表的单链表存储结构表示和实现
- C#基础第四天-作业答案-Hashtable-list<KeyValuePair>泛型实现名片
- 用js实现一个map对象
- js中XMLHttpRequest对象实现GET、POST异步传输
- js实现倒计时及时间对象
- js面向对象实现切换面板
- 怎样在JSF中用js实现从dataTable到Ext对象的转换啊??
- 纯js实现无限空间大小的本地存储
- 对象容器 - Java对数据结构的封装 - List, ArrayList, LinkedList, Set, SortedSet, HashSet, Map, TreeMap