-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
fixed-array.ts
116 lines (97 loc) · 2.3 KB
/
fixed-array.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* @category memop
*/
import sort from './timsort';
/**
* @zh 定长数组。
*/
export default class FixedArray<T = {}> {
private _count: number;
private _data: Array<T | undefined>;
/**
* 构造函数。
* @param size 数组长度。
*/
constructor (size: number) {
this._count = 0;
this._data = new Array(size);
}
public _resize (size: number) {
if (size > this._data.length) {
for (let i = this._data.length; i < size; ++i) {
this._data[i] = undefined;
}
}
}
/**
* @zh 当前有效数据长度。
*/
get length () {
return this._count;
}
/**
* @zh 获取数组元素。
*/
get data () {
return this._data;
}
/**
* @zh 将数组清空。
*/
public reset () {
for (let i = 0; i < this._count; ++i) {
this._data[i] = undefined;
}
this._count = 0;
}
/**
* @zh 把一个对象插入到数组末尾。
* @param val 一个数组元素
*/
public push (val) {
if (this._count >= this._data.length) {
this._resize(this._data.length * 2);
}
this._data[this._count] = val;
++this._count;
}
/**
* @zh 删除数组最后一个元素并返回。
*/
public pop () {
--this._count;
if (this._count < 0) {
this._count = 0;
}
const ret = this._data[this._count];
this._data[this._count] = undefined;
return ret;
}
/**
* @zh 删除指定位置的元素并将最后一个元素移动至该位置。
* @param idx 数组索引。
*/
public fastRemove (idx) {
if (idx >= this._count || idx < 0) {
return;
}
const last = this._count - 1;
this._data[idx] = this._data[last];
this._data[last] = undefined;
this._count -= 1;
}
/**
* @zh 返回某个数组元素对应的下标。
* @param val 数组元素。
*/
public indexOf (val) {
return this._data.indexOf(val);
}
/**
* @zh 对数组进行排序。
* @param cmp 比较函数。
*/
public sort (cmp) {
return sort(this._data, 0, this._count, cmp);
}
}