forum.boolean.name

forum.boolean.name (http://forum.boolean.name/index.php)
-   JavaScript / HTML (http://forum.boolean.name/forumdisplay.php?f=136)
-   -   node js связанный список (http://forum.boolean.name/showthread.php?t=18635)

pax 16.10.2013 21:34

node js связанный список
 
Написал связанный список для своих нужд (знаю есть готовые, но этж велосипед и опыт).

PHP код:

function LinkedList() {
    
this.first null;
    
this.last null;
    
this.length 0;
}

module.exports LinkedList;

LinkedList.prototype.addToStart = function (value) {
    var 
first this.first;
    var 
newItem = {
        
beforenull,
        
afterfirst,
        
valuevalue
    
};

    if (
first) {
        
first.before newItem;
    }

    if (
this.last == null) {
        
this.last newItem;
    }

    
this.first newItem;
    
this.length++;

    return 
newItem;
};
LinkedList.prototype.add =
    
LinkedList.prototype.addToEnd = function (value) {
        var 
last this.last;
        var 
newItem = {
            
beforelast,
            
afternull,
            
valuevalue
        
};

        if (
last) {
            
last.after newItem;
        }

        if (
this.first == null) {
            
this.first newItem;
        }

        
this.last newItem;
        
this.length++;

        return 
newItem;
    };


LinkedList.prototype.insertBefore = function (valuelistItem) {
    if (!
listItem) {
        throw new 
Error('listItem == null');
    }

    var 
newItem = {
        
beforelistItem.before,
        
afterlistItem,
        
valuevalue
    
};

    if (
newItem.before == null) {
        
this.first newItem;
    } else {
        
newItem.before.after newItem;
    }

    
listItem.before newItem;

    
this.length++;
    return 
newItem;
};

LinkedList.prototype.insertAfter = function (valuelistItem) {
    if (!
listItem) {
        throw new 
Error('listItem == null');
    }

    var 
newItem = {
        
beforelistItem,
        
afterlistItem.after,
        
valuevalue
    
};

    if (
newItem.after == null) {
        
this.last newItem;
    } else {
        
newItem.after.before newItem;
    }

    
listItem.after newItem;

    
this.length++;

    return 
newItem;
};

LinkedList.prototype.remove = function (listItem) {

    if (
listItem.before) {
        
listItem.before.after listItem.after;
    } else {
        
this.first listItem.after;
    }

    if (
listItem.after) {
        
listItem.after.before listItem.before;
    } else {
        
this.last listItem.before;
    }
    
this.length--;
};


LinkedList.prototype.foreach = function (processItemCallbackonEndCallback) {
    var 
item this.first;

    function 
nextItemProcessor(item) {
        return function () {
            if (
item) {
                
processItemCallback(itemnextItemProcessor(item.after));
            } else {
                if (
onEndCallbackonEndCallback();
            }
        };
    }

    if (
item) {
        
processItemCallback(itemnextItemProcessor(item.after));
    } else {
        if (
onEndCallbackonEndCallback();
    }
}; 

тесты:
PHP код:

var util = require('util');
var 
LinkedList = require('./LinkedList');

var list = new 
LinkedList();

list.
add('one'); // добавление в конец
list.add('two'); // добавление в конец
var three = list.add("three");  // добавление в конец

var items = [];
console.log(list.length);
list.foreach(function (
itemnext) {
    
items.push(item.value);
    
next();
}, function () {
    
console.log(util.inspect(items));
});

var 
five = list.insertAfter('five'three); // вставка после

list.insertBefore('four'five); // вставка до

list.insertAfter('six'five); // вставка после

items = [];
console.log(list.length);
list.foreach(function (
itemnext) {
    
items.push(item.value);
    
next();
}, function () {
    
console.log(util.inspect(items));
});

items = [];

var 
two three.before;
list.
remove(three); // удаление
list.insertAfter(3two); // вставка после
list.remove(list.last); // удаление

console.log(list.length);

list.foreach(function (
itemnext) {
    
items.push(item.value);
    
next();
}, function () {
    
console.log(util.inspect(items));
});

list.
addToEnd(6); // добавление в конец списка
items = [];

console.log(list.length);

list.foreach(function (
itemnext) {
    
items.push(item.value);
    
next();
}, function () {
    
console.log(util.inspect(items));
});

list.
remove(list.first); // удаление первого элемента
list.remove(list.first); // удаление первого элемента
list.addToStart(1); // добавление в начало
list.insertAfter('два', list.first); // добавление после первого
list.insertBefore('zero', list.first); // добавление перед первым
items = [];

console.log(list.length);

list.foreach(function (
itemnext) {
    
items.push(item.value);
    
next();
}, function () {
    
console.log(util.inspect(items));
}); 

результаты тестов:
Код:

C:\nodejs\node.exe LinkedList.js
3
[ 'one', 'two', 'three' ]
6
[ 'one', 'two', 'three', 'four', 'five', 'six' ]
5
[ 'one', 'two', 3, 'four', 'five' ]
6
[ 'one', 'two', 3, 'four', 'five', 6 ]
7
[ 'zero', 1, 'два', 3, 'four', 'five', 6 ]

Process finished with exit code 0


moka 16.10.2013 23:21

Ответ: node js связанный список
 
Клёво.
Чтобы сделать модуль для front-end'а и back-end'а, замени:
PHP код:

module.exports LinkedList

На:
PHP код:

if (module !== undefined) {
  
module.exports LinkedList;



pax 16.10.2013 23:38

Ответ: node js связанный список
 
Можно еще добавить функцию очистки
PHP код:

LinkedList.prototype.clear = function () {
    
this.first null;
    
this.last null;
    
this.length 0;
}; 

Upd: финальная версия
PHP код:

function LinkedList() {
    
this.first null;
    
this.last null;
    
this.length 0;
}

if (
module !== undefined) {
    
module.exports LinkedList;
}

LinkedList.prototype.addToStart = function (value) {
    var 
first this.first;
    var 
newItem = {
        
beforenull,
        
afterfirst,
        
valuevalue
    
};

    if (
first) {
        
first.before newItem;
    }

    if (
this.last == null) {
        
this.last newItem;
    }

    
this.first newItem;
    
this.length++;

    return 
newItem;
};

LinkedList.prototype.add =
    
LinkedList.prototype.addToEnd = function (value) {
        var 
last this.last;
        var 
newItem = {
            
beforelast,
            
afternull,
            
valuevalue
        
};

        if (
last) {
            
last.after newItem;
        }

        if (
this.first == null) {
            
this.first newItem;
        }

        
this.last newItem;
        
this.length++;

        return 
newItem;
    };


LinkedList.prototype.insertBefore = function (valuelistItem) {
    if (!
listItem) {
        throw new 
Error('listItem == null');
    }

    var 
newItem = {
        
beforelistItem.before,
        
afterlistItem,
        
valuevalue
    
};

    if (
newItem.before == null) {
        
this.first newItem;
    } else {
        
newItem.before.after newItem;
    }

    
listItem.before newItem;

    
this.length++;
    return 
newItem;
};

LinkedList.prototype.insertAfter = function (valuelistItem) {
    if (!
listItem) {
        throw new 
Error('listItem == null');
    }

    var 
newItem = {
        
beforelistItem,
        
afterlistItem.after,
        
valuevalue
    
};

    if (
newItem.after == null) {
        
this.last newItem;
    } else {
        
newItem.after.before newItem;
    }

    
listItem.after newItem;

    
this.length++;

    return 
newItem;
};

LinkedList.prototype.remove = function (listItem) {

    if (
listItem.before) {
        
listItem.before.after listItem.after;
    } else {
        
this.first listItem.after;
    }

    if (
listItem.after) {
        
listItem.after.before listItem.before;
    } else {
        
this.last listItem.before;
    }
    
this.length--;
};

LinkedList.prototype.clear = function () {
    
this.first null;
    
this.last null;
    
this.length 0;
};


LinkedList.prototype.foreach = function (processItemCallbackonEndCallback) {
    function 
nextItemProcessor(item) {
        return function () {
            if (
item) {
                
processItemCallback(itemnextItemProcessor(item.after));
            } else {
                if (
onEndCallbackonEndCallback();
            }
        };
    }
    
nextItemProcessor(this.first)();
}; 


pax 17.10.2013 10:09

Ответ: node js связанный список
 
Еще один метод toArray, а так же теперь нельзя удалить элемент из чужого списка (каждый элемент хранит ссылку на список)
PHP код:

function LinkedList() {
    
this.first null;
    
this.last null;
    
this.length 0;
}

if (
module !== undefined) {
    
module.exports LinkedList;
}

LinkedList.prototype.addToStart = function (value) {
    var 
first this.first;
    var 
newItem = {
        
beforenull,
        
afterfirst,
        
valuevalue,
        list: 
this
    
};

    if (
first) {
        
first.before newItem;
    }

    if (
this.last == null) {
        
this.last newItem;
    }

    
this.first newItem;
    
this.length++;

    return 
newItem;
};

LinkedList.prototype.add =
    
LinkedList.prototype.addToEnd = function (value) {
        var 
last this.last;
        var 
newItem = {
            
beforelast,
            
afternull,
            
valuevalue,
            list: 
this
        
};

        if (
last) {
            
last.after newItem;
        }

        if (
this.first == null) {
            
this.first newItem;
        }

        
this.last newItem;
        
this.length++;

        return 
newItem;
    };


LinkedList.prototype.insertBefore = function (valuelistItem) {
    if (!
listItem) {
        throw new 
Error('listItem == null');
    }

    var 
newItem = {
        
beforelistItem.before,
        
afterlistItem,
        
valuevalue,
        list: 
this
    
};

    if (
newItem.before == null) {
        
this.first newItem;
    } else {
        
newItem.before.after newItem;
    }

    
listItem.before newItem;

    
this.length++;
    return 
newItem;
};

LinkedList.prototype.insertAfter = function (valuelistItem) {
    if (!
listItem) {
        throw new 
Error('listItem == null');
    }

    var 
newItem = {
        
beforelistItem,
        
afterlistItem.after,
        
valuevalue,
        list: 
this
    
};

    if (
newItem.after == null) {
        
this.last newItem;
    } else {
        
newItem.after.before newItem;
    }

    
listItem.after newItem;

    
this.length++;

    return 
newItem;
};

LinkedList.prototype.remove = function (listItem) {
    if (!
listItem) {
        throw new 
Error('listItem == null');
    }

    if (
listItem.list !== this) {
        throw new 
Error('listItem from another list!');
    }

    if (
listItem.before) {
        
listItem.before.after listItem.after;
    } else {
        
this.first listItem.after;
    }

    if (
listItem.after) {
        
listItem.after.before listItem.before;
    } else {
        
this.last listItem.before;
    }

    
listItem.list = null;
    
listItem.before null;
    
listItem.after null;
    
    
this.length--;
};

LinkedList.prototype.clear = function () {
    
this.foreach(function(elementnext){
        
element.before null;
        
element.after null;
        
element.list = null;
        
next();
    });
    
this.first null;
    
this.last null;
    
this.length 0;
};


LinkedList.prototype.toArray = function () {
    var array = new Array(
this.length);
    var 
element this.first;
    var 
index 0;
    while (
element) {
        array[
index++] = element.value;
        
element element.after;
    }
    return array;
};


LinkedList.prototype.foreach = function (processItemCallbackonEndCallback) {
    function 
nextItemProcessor(item) {
        return function () {
            if (
item) {
                
processItemCallback(itemnextItemProcessor(item.after));
            } else {
                if (
onEndCallbackonEndCallback();
            }
        };
    }

    
nextItemProcessor(this.first)();
}; 


moka 17.10.2013 13:46

Ответ: node js связанный список
 
forEach :)

h1dd3n 17.10.2013 18:50

Ответ: node js связанный список
 
весь linq туда

pax 17.10.2013 20:09

Ответ: node js связанный список
 
Цитата:

Сообщение от moka (Сообщение 268958)
forEach :)

Если расценивать не как ключевое слово..., а меня устраивает в нижнем регистре))

moka 17.10.2013 20:21

Ответ: node js связанный список
 
Ну это имхо не явное исключение :)
Тем более если forEach то будет более консистентно со всеми другими подобными либами, и также даст больше разницы между ключевым словом - т.к. разница есть и не стоит её стирать..
имхо.


Часовой пояс GMT +4, время: 08:53.

vBulletin® Version 3.6.5.
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Перевод: zCarot