2015-09-26 21:27:36 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function init(list) {
|
|
|
|
list._idleNext = list;
|
|
|
|
list._idlePrev = list;
|
2021-10-26 15:54:27 +00:00
|
|
|
return list;
|
2015-09-26 21:27:36 +00:00
|
|
|
}
|
|
|
|
|
2017-07-30 20:27:09 +00:00
|
|
|
// Show the most idle item.
|
2015-09-26 21:27:36 +00:00
|
|
|
function peek(list) {
|
2016-10-29 22:54:27 +00:00
|
|
|
if (list._idlePrev === list) return null;
|
2015-09-26 21:27:36 +00:00
|
|
|
return list._idlePrev;
|
|
|
|
}
|
|
|
|
|
2017-07-30 20:27:09 +00:00
|
|
|
// Remove an item from its list.
|
2015-09-26 21:27:36 +00:00
|
|
|
function remove(item) {
|
|
|
|
if (item._idleNext) {
|
|
|
|
item._idleNext._idlePrev = item._idlePrev;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item._idlePrev) {
|
|
|
|
item._idlePrev._idleNext = item._idleNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
item._idleNext = null;
|
|
|
|
item._idlePrev = null;
|
|
|
|
}
|
|
|
|
|
2017-07-30 20:27:09 +00:00
|
|
|
// Remove an item from its list and place at the end.
|
2015-09-26 21:27:36 +00:00
|
|
|
function append(list, item) {
|
2016-04-26 14:33:19 +00:00
|
|
|
if (item._idleNext || item._idlePrev) {
|
|
|
|
remove(item);
|
|
|
|
}
|
|
|
|
|
2017-07-30 20:27:09 +00:00
|
|
|
// Items are linked with _idleNext -> (older) and _idlePrev -> (newer).
|
2016-10-29 22:54:27 +00:00
|
|
|
// Note: This linkage (next being older) may seem counter-intuitive at first.
|
2015-09-26 21:27:36 +00:00
|
|
|
item._idleNext = list._idleNext;
|
|
|
|
item._idlePrev = list;
|
2016-04-26 14:33:19 +00:00
|
|
|
|
2017-07-30 20:27:09 +00:00
|
|
|
// The list _idleNext points to tail (newest) and _idlePrev to head (oldest).
|
2016-04-26 14:33:19 +00:00
|
|
|
list._idleNext._idlePrev = item;
|
2015-09-26 21:27:36 +00:00
|
|
|
list._idleNext = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isEmpty(list) {
|
|
|
|
return list._idleNext === list;
|
|
|
|
}
|
2017-02-15 21:33:33 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init,
|
|
|
|
peek,
|
|
|
|
remove,
|
|
|
|
append,
|
2023-02-26 10:34:02 +00:00
|
|
|
isEmpty,
|
2017-02-15 21:33:33 +00:00
|
|
|
};
|