-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dictionary.ts
42 lines (41 loc) · 1.05 KB
/
Dictionary.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
class ItemD<T, Q> {
f: T;
s: Q;
constructor(_f: T, _s: Q) {
this.f = _f;
this.s = _s;
}
}
export class Dictionary<T, Q> {
public items: Array<ItemD<T,Q>>;
constructor() {
this.items = new Array<ItemD<T,Q>>();
}
public Add(key: T, value: Q) {
this.items.push(new ItemD(key, value))
}
Key(key: T): Q {
for(let i = 0; i < this.items.length; i++) {
if(this.items[i].f == key) {
return this.items[i].s;
}
}
throw new Error("Error in method \"Key\"");
}
Value(value: Q): T {
for(let i = 0; i < this.items.length; i++) {
if(this.items[i].s == value) {
return this.items[i].f;
}
}
throw new Error("Error in method \"Value\"");
}
GetIndex(key: T): number {
for(let i = 0; i < this.items.length; i++) {
if(this.items[i].f == key) {
return i;
}
}
return -1;
}
}