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); });
}
}