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