Repository navigation
Expand file tree
/
Copy pathindexed-list.js
More file actions
110 lines (100 loc) · 2.76 KB
/
Copy pathindexed-list.js
File metadata and controls
110 lines (100 loc) · 2.76 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
/**
* @private
* @class
* @name IndexedList
* @classdesc A ordered list-type data structure that can provide item look up by key, but also return return a list.\.
*/
class IndexedList {
constructor() {
this._list = [];
this._index = {};
}
/**
* @private
* @function
* @name IndexedList#push
* @description Add a new item into the list with a index key.
* @param {string} key - Key used to look up item in index.
* @param {object} item - Item to be stored.
*/
push(key, item) {
if (this._index[key]) {
throw Error("Key already in index " + key);
}
const location = this._list.push(item) - 1;
this._index[key] = location;
}
/**
* @private
* @function
* @name IndexedList#has
* @description Test whether a key has been added to the index.
* @param {string} key - The key to test.
* @returns {boolean} Returns true if key is in the index, false if not.
*/
has(key) {
return this._index[key] !== undefined;
}
/**
* @private
* @function
* @name IndexedList#get
* @description Return the item indexed by a key.
* @param {string} key - The key of the item to retrieve.
* @returns {object} The item stored at key.
*/
get(key) {
const location = this._index[key];
if (location !== undefined) {
return this._list[location];
}
return null;
}
/**
* @private
* @function
* @name IndexedList#remove
* @description Remove the item indexed by key from the list.
* @param {string} key - The key at which to remove the item.
* @returns {boolean} Returns true if the key exists and an item was removed, returns false if no item was removed.
*/
remove(key) {
const location = this._index[key];
if (location !== undefined) {
this._list.splice(location, 1);
delete this._index[key];
// update index
for (key in this._index) {
const idx = this._index[key];
if (idx > location) {
this._index[key] = idx - 1;
}
}
return true;
}
return false;
}
/**
* @private
* @function
* @name IndexedList#list
* @description Returns the list of items.
* @returns {object[]} The list of items.
*/
list() {
return this._list;
}
/**
* @private
* @function
* @name IndexedList#clear
* @description Remove all items from the list.
*/
clear() {
this._list.length = 0;
for (const prop in this._index) {
delete this._index[prop];
}
}
}
export { IndexedList };