-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvector.ts
231 lines (199 loc) · 4.54 KB
/
vector.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* Vector : Convenience class Wrapper around Array()
*/
interface callback<U> {
(item: U): void;
}
class Vector <T> {
/**
* Underlying Array
*/
a: Array<T>;
constructor() {
this.a = new Array<T>();
}
/**
*
* @param item : Item to be appended into current Vector
*/
append(item:T){
this.a.push(item);
}
/**
* @param item : Item to be deleted from current Vector
*/
delete(item:T) {
let pos = this.a.indexOf(item);
if (pos > -1) {
this.a.splice(pos, 1);
} else {
throw(`Item ${item} not found`);
}
}
/**
* @param index : Index of item to be deleted from current Vector
*/
deleteAt(index: number){
if (index < this.a.length) {
this.a.splice(index, 1);
} else {
throw(`Index ${index} not in range`);
}
}
/**
*
* @param index : Index of item to be inserted into current Vector
* @param item : Item to be inserted
*/
insertAt(index:number, item:T) {
if (index < this.a.length) {
this.a.splice(index, 0, item);
} else {
throw(`Index ${index} not in range`);
}
}
/**
* Delete last item from current Vector
*/
deleteAtEnd(){
this.a.splice(this.a.length-1, 1);
}
/**
*
* @param item : Item to be inserted at the end of current Vector
*/
insertAtEnd(item: T) {
this.a.splice(this.a.length, 0, item);
}
/**
* Delete 1st Item of current Vector
*/
deleteAtStart(){
this.a.splice(0, 1);
}
/**
*
* @param item : Item to be inserted at start of current Vector
*/
insertAtStart(item:T) {
this.a.splice(0, 0, item);
}
/**
*
* @param index : Index of item to be substituted,
* @param item : Substitute item.
*/
substituteAt(index:number, item:T) {
if (index < this.a.length) {
this.a.splice(index, 1, item);
} else {
throw(`Index ${index} not in range`);
}
}
/**
*
* @param item : Item to be prepended to Vector.
*/
prepend(item:T){
this.a.splice(0, 0, item);
}
/**
*
* @param oldItem : item to be substituted.
* @param newItem : Substitute
*/
substitute(oldItem:T, newItem: T) {
let pos = this.a.indexOf(oldItem);
if (pos > -1) {
this.a.splice(pos, 1, newItem);
} else {
throw(`Item ${oldItem} not found`);
}
}
/**
*
* @param v : Vector to be concatenated to current Vector.
*/
concat(v: Vector<T>) : Vector<T>{
let w = new Vector<T>();
let temp = [...this.a];
w.a = temp.concat([...v.a]);
return w;
}
/**
* Delete all elements from current Vector.
*/
clear() {
this.a.splice(0, this.a.length)
}
/**
* Return the underlying array from current Vector.
*/
toArray() : Array<T>{
return this.a;
}
/**
*
* @param v Build vector from Array v.
*/
fromArray(v:Array<T>) {
this.a = [...v];
}
/**
*
* @param item : item which is checked for inclusion.
*/
includes(item: T) : boolean{
return this.a.includes(item);
}
forEach(fn: callback<T>) {
this.a.forEach(fn);
}
/**
* @param fn: Sorting function
*/
sort(fn:any=undefined) {
this.a.sort(fn);
}
/**
*
* @param i fist position for swap.
* @param j second position for swap.
*/
swapAt(i:number, j:number){
let tmp = this.a[i];
this.substituteAt(i, this.a[j])
this.substituteAt(j, tmp);
}
/**
*
* @param i Index of item whose value is returned
*/
getValue(i: number) : T {
return this.a[i];
}
/**
*
* @param i Index of value to be set
* @param value value to be set
*/
setValue(i: number, value:T) {
if (i < this.a.length) {
this.a[i] = value;
} else {
throw(`Index ${i} not in range`);
}
}
/**
* Returns vector length
*/
length() : number {
return this.a.length;
}
}
function concatVectors <T> (v1: Vector<T>, v2: Vector<T>){
let w = new Vector<T>();
w.fromArray([...v1.a].concat([...v2.a]));
return w;
}
export { Vector, concatVectors };