Skip to content

Commit

Permalink
lib: refactor internal/linkedlist
Browse files Browse the repository at this point in the history
PR-URL: #11406
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
jasnell committed Feb 22, 2017
1 parent ca48071 commit d61a511
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions lib/internal/linkedlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,26 @@ function init(list) {
list._idleNext = list;
list._idlePrev = list;
}
exports.init = init;

// create a new linked list
function create() {
const list = { _idleNext: null, _idlePrev: null };
init(list);
return list;
}
exports.create = create;

// show the most idle item
function peek(list) {
if (list._idlePrev === list) return null;
return list._idlePrev;
}
exports.peek = peek;


// remove the most idle item from the list
function shift(list) {
const first = list._idlePrev;
remove(first);
return first;
}
exports.shift = shift;


// remove a item from its list
function remove(item) {
Expand All @@ -44,8 +38,6 @@ function remove(item) {
item._idleNext = null;
item._idlePrev = null;
}
exports.remove = remove;


// remove a item from its list and place at the end.
function append(list, item) {
Expand All @@ -62,10 +54,17 @@ function append(list, item) {
list._idleNext._idlePrev = item;
list._idleNext = item;
}
exports.append = append;


function isEmpty(list) {
return list._idleNext === list;
}
exports.isEmpty = isEmpty;

module.exports = {
init,
create,
peek,
shift,
remove,
append,
isEmpty
};

0 comments on commit d61a511

Please sign in to comment.