forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainers.ts
More file actions
166 lines (135 loc) · 4.49 KB
/
containers.ts
File metadata and controls
166 lines (135 loc) · 4.49 KB
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
import definition = require("utils/containers");
export class ArraySortHelper implements definition.ArraySortHelper {
public static sort<T>(keys: Array<T>, index: number, length: number, compareFn: (a: T, b: T) => number) {
if (length < 2) {
return;
}
if (keys.length === length) {
keys.sort(compareFn);
return;
}
var sorted = keys.splice(index, length);
sorted.sort(compareFn);
var args: Array<Object> = [0, index];
keys.splice.apply(keys, args.concat(sorted));
}
}
export class StringComparer implements definition.IEqualityComparer<string> {
public equals(x: string, y: string): boolean {
return x === y;
}
public getHashCode(str: string): number {
var res = 0, len = str.length;
for (var i = 0; i < len; i++) {
res = res * 31 + str.charCodeAt(i);
}
return res;
}
}
interface Entry<TKey, TValue> {
hashCode: number;
next: Entry<TKey, TValue>;
key: TKey;
value: TValue;
}
export class Dictionary<TKey, TValue> implements definition.Dictionary<TKey, TValue> {
private _comparer: definition.IEqualityComparer<TKey>;
private _count: number = 0;
private _entries: Array<Entry<TKey, TValue>>;
private _version: number = 0;
constructor(comparer: definition.IEqualityComparer<TKey>) {
this._comparer = comparer;
this._entries = new Array<Entry<TKey, TValue>>();
}
get count(): number {
return this._count;
}
public forEach(callbackfn: (key: TKey, value: TValue) => void) {
var currentVersion = this._version;
for (var index in this._entries) {
var entry = this._entries[index];
callbackfn(entry.key, entry.value);
if (currentVersion !== this._version) {
throw new Error("Cannot modify Dictionary while enumerating.");
}
}
}
public clear(): void {
if (this.count > 0) {
this._entries = new Array<Entry<TKey, TValue>>();
this._count = 0;
this._version++;
}
}
public remove(key: TKey): boolean {
if (!key) {
throw new Error("key cannot be null/undefined.");
}
var hash = this._comparer.getHashCode(key);
var previousEntry: Entry<TKey, TValue> = null;
for (var entry = this._entries[hash]; entry; entry = entry.next) {
if (entry.hashCode === hash && this._comparer.equals(entry.key, key)) {
if (previousEntry) {
previousEntry.next = entry.next;
}
else {
this._entries[hash] = entry.next;
}
return true;
}
this._count--;
this._version++;
previousEntry = entry;
}
return false;
}
public get(key: TKey): TValue {
var entry = this.findEntry(key);
if (entry) {
return entry.value;
}
return undefined;
}
public has(key: TKey): boolean {
return (this.findEntry(key) ? true : false);
}
public set(key: TKey, value: TValue): void {
if (!key) {
throw new Error("key cannot be null or undefined.");
}
var hash = this._comparer.getHashCode(key);
var lastEntryForHash: Entry<TKey, TValue> = null;
for (var entry = this._entries[hash]; entry; entry = entry.next) {
lastEntryForHash = entry;
if (entry.hashCode === hash && this._comparer.equals(entry.key, key)) {
entry.value = value;
this._version++;
return;
}
}
this._count++;
var newEntry = <Entry<TKey, TValue>>{};
newEntry.hashCode = hash;
newEntry.key = key;
newEntry.value = value;
if (lastEntryForHash) {
lastEntryForHash.next = newEntry;
}
else {
this._entries[hash] = newEntry;
}
this._version++;
}
private findEntry(key: TKey): Entry<TKey, TValue> {
if (!key) {
throw new Error("key cannot be null or undefined.");
}
var hash = this._comparer.getHashCode(key);
for (var entry = this._entries[hash]; entry; entry = entry.next) {
if (entry.hashCode === hash && this._comparer.equals(entry.key, key)) {
return entry;
}
}
return null;
}
}