rip func 返回 null port reset ast shift
以下代码是 typescript 语言来写的,其实和 es6 面向对象的写法基本一致。大家阅读后都明白这些方法的作用。
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 | 无 |
- 1 export class HashTable {
- 2 private items: { [key: string] : HashValue
- };
- 3@SerializeProperty({
- list: true,
- type: Array
- }) 4 private itemList: Array;
- 5 constructor() {
- 6 super();
- 7 this.items = {};
- 8 this.itemList = [];
- 9
- }
- 10 11 set(key: string, value: T) : void {
- 12
- var vl = new HashValue();
- 13 vl.key = key;
- 14 vl.value = value;
- 15
- var index = this.itemList.length;
- 16
- if (this.has(key)) {
- 17 index = this.items[key].index;
- 18
- }
- 19 vl.index = index;
- 20 this.itemList[index] = value;
- 21 this.items[key] = vl;
- 22
- }
- 23 24 del(key: string) : void {
- 25
- if (this.has(key)) {
- 26
- var index = this.items[key].index;
- 27
- if (index > -1) {
- 28 this.itemList.splice(index, 1);
- 29
- }
- 30 delete this.items[key];
- 31 this.resetIndex();
- 32
- }
- 33
- }
- 34 35 resetIndex() : void {
- 36 37 this.foreach((k, v: T) = >{
- 38
- var index = this.itemList.indexOf(v);
- 39 this.items[k].index = index;
- 40
- });
- 41
- }
- 42 43 has(key: string) : boolean {
- 44
- return key in this.items;
- 45
- }
- 46 47 get(key: string) : T {
- 48
- if (this.has(key)) {
- 49
- return this.items[key].value;
- 50
- }
- 51
- return null;
- 52
- }
- 53 54 count() : number {
- 55
- return this.itemList.length;
- 56
- }
- 57 58 all() : Array {
- 59
- return this.itemList;
- 60
- }
- 61 62 first() {
- 63
- return this.itemList[0];
- 64
- }
- 65 66 last() {
- 67
- return this.itemList[this.itemList.length - 1];
- 68
- }
- 69 70 getByIndex(index: number) : T {
- 71
- return this.itemList[index];
- 72
- }
- 73 74 //遍历 扩展
- 75 foreach(callback) {
- 76
- for (var key in this.items) {
- 77 callback(key, this.items[key].value);
- 78
- }
- 79
- }
- 80 81 //获取index
- 82 indexOf(key) {
- 83
- if (this.has(key)) {
- 84
- return this.items[key].index;
- 85
- }
- 86
- }
- 87 88 //插入
- 89 insertAt(index: number, value: T, key: string) {
- 90 this.itemList.splice(index, 0, value);
- 91
- var hashV = new HashValue();
- 92 hashV.index = index;
- 93 hashV.key = key;
- 94 hashV.value = value;
- 95 this.items[key] = hashV;
- 96 this.resetIndex();
- 97
- }
- 98 99 sort(callback: Function) {
- 100 this.itemList.sort((a: T, b: T) = >{
- return callback(a, b);
- 101
- });
- 102
- }
- 103
- }
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 | 无 |
- 1 export class List {
- 2 private items: Array;
- 3 4 private checkIndex(index) : boolean {
- 5
- return ! (index < 0 || isNaN(index) || index >= this.items.length);
- 6
- }
- 7 constructor() {
- 8 super();
- 9 this.items = new Array();
- 10
- }
- 11 12 length() : number {
- 13
- return this.items.length;
- 14
- }
- 15 16 add(value: T) : void {
- 17 this.items.push(value);
- 18
- }
- 19 addList(valueList: List) {
- 20
- for (var i = 0; i < valueList.length(); i++) {
- 21 22
- var value = valueList.get(i);
- 23 this.items.push(value);
- 24
- }
- 25
- }
- 26 pop() : T {
- 27
- return this.items.pop();
- 28
- }
- 29 30 shift() {
- 31 this.items.shift();
- 32
- }
- 33 34 remove(index: number) : void {
- 35
- if (this.checkIndex(index)) {
- 36 this.items.splice(index, 1);
- 37 38
- }
- 39
- }
- 40
- /**
- 41 * 從指定索引處開始刪除指定個數的元素
- 42 * @param from
- 43 * @param count
- 44 */
- 45 removeMany(from: number, count: number) {
- 46 47
- if (this.checkIndex(from)) {
- 48 this.items.splice(from, count);
- 49
- }
- 50
- }
- 51 52 clear() : void {
- 53 this.items = [];
- 54
- }
- 55 56 contains(value: T) : boolean {
- 57
- for (var i in this.items) {
- 58
- return value == this.items[i];
- 59
- }
- 60
- return false;
- 61
- }
- 62 63 indexOf(value: T) : number {
- 64
- return this.items.indexOf(value);
- 65
- }
- 66 67 insert(index: number, value: T) {
- 68 //this.checkIndex(index) && this.items.splice(index , 0, value);
- 69 this.items.splice(index, 0, value);
- 70
- }
- 71 72 get(index: number) : T {
- 73
- return this.items[index];
- 74
- }
- 75 set(index, value: T) {
- 76 this.items[index] = value;
- 77
- }
- 78 all() : Array {
- 79
- return this.items;
- 80
- }
- 81 foreach(callback: (i: number, item: T) = >any) {
- 82
- var len = this.items.length;
- 83
- for (var i = 0; i < len; i++) {
- 84
- if (callback(i, this.items[i]) === false) break;
- 85
- }
- 86
- }
- 87 reverseForeach(callback) {
- 88
- var len = this.items.length;
- 89
- for (var i = len - 1; i >= 0; i--) {
- 90
- if (callback(i, this.items[i]) === false) break;
- 91
- }
- 92
- }
- 93 sort(callback: Function) {
- 94 this.items.sort((a: T, b: T) = >{
- return callback(a, b);
- });
- 95
- }
- 96
- }
js 实现存储对象的数据结构 hashTable 和 list
来源: http://www.bubuko.com/infodetail-2212858.html