11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_cifunction init(list) {
41cb0ef41Sopenharmony_ci  list._idleNext = list;
51cb0ef41Sopenharmony_ci  list._idlePrev = list;
61cb0ef41Sopenharmony_ci  return list;
71cb0ef41Sopenharmony_ci}
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci// Show the most idle item.
101cb0ef41Sopenharmony_cifunction peek(list) {
111cb0ef41Sopenharmony_ci  if (list._idlePrev === list) return null;
121cb0ef41Sopenharmony_ci  return list._idlePrev;
131cb0ef41Sopenharmony_ci}
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci// Remove an item from its list.
161cb0ef41Sopenharmony_cifunction remove(item) {
171cb0ef41Sopenharmony_ci  if (item._idleNext) {
181cb0ef41Sopenharmony_ci    item._idleNext._idlePrev = item._idlePrev;
191cb0ef41Sopenharmony_ci  }
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  if (item._idlePrev) {
221cb0ef41Sopenharmony_ci    item._idlePrev._idleNext = item._idleNext;
231cb0ef41Sopenharmony_ci  }
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  item._idleNext = null;
261cb0ef41Sopenharmony_ci  item._idlePrev = null;
271cb0ef41Sopenharmony_ci}
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci// Remove an item from its list and place at the end.
301cb0ef41Sopenharmony_cifunction append(list, item) {
311cb0ef41Sopenharmony_ci  if (item._idleNext || item._idlePrev) {
321cb0ef41Sopenharmony_ci    remove(item);
331cb0ef41Sopenharmony_ci  }
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  // Items are linked  with _idleNext -> (older) and _idlePrev -> (newer).
361cb0ef41Sopenharmony_ci  // Note: This linkage (next being older) may seem counter-intuitive at first.
371cb0ef41Sopenharmony_ci  item._idleNext = list._idleNext;
381cb0ef41Sopenharmony_ci  item._idlePrev = list;
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  // The list _idleNext points to tail (newest) and _idlePrev to head (oldest).
411cb0ef41Sopenharmony_ci  list._idleNext._idlePrev = item;
421cb0ef41Sopenharmony_ci  list._idleNext = item;
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_cifunction isEmpty(list) {
461cb0ef41Sopenharmony_ci  return list._idleNext === list;
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_cimodule.exports = {
501cb0ef41Sopenharmony_ci  init,
511cb0ef41Sopenharmony_ci  peek,
521cb0ef41Sopenharmony_ci  remove,
531cb0ef41Sopenharmony_ci  append,
541cb0ef41Sopenharmony_ci  isEmpty,
551cb0ef41Sopenharmony_ci};
56