complex-list代码解析
来源:互联网 发布:淘宝刷手被发现 编辑:程序博客网 时间:2024/06/07 16:47
/**
列表节点构造函数
*/
function ListItem(content){
this.link = null;this.content = content;
}
/**
节点销毁函数
*/
ListItem.prototype.destroy = function(){this.content = null;
this.link = null;
};
/**
将当前结点追加到
*/
ListItem.prototype.linkTo = function(item){if(item){
if(item.link){
this.link = item.link;
}
item.link = this;
}
};
ListItem.prototype.empty = function(){
return 'undefined' === typeof this.content;
};
ListItem.prototype.apply = function(func){
if('undefined' !== typeof this.content){
return func(this.content,this);
}else{
return false;
}
};
function defaultSorter(){
return false;
}
function List(sortfunction){
this.head = new ListItem();
this.sorter = sortfunction || defaultSorter;
this.length = 0;
}
List.prototype.destroy = function(){
this.purge();
this.length = null;
this.sorter = null;
this.head = null;
};
List.prototype.purge = function(){
var item = this.head, ditem;
while(item){
ditem = item;
item = item.link;
ditem.destroy();
}
this.head.content = void 0;
this.length=0;
};
List.prototype.empty = function(){
return !this.head.content;
};
List.prototype.add = function(content,afteritem){
if(this.head.empty()){
this.head.content = content;
this.length = 1;
return this.head;
}
var newitem = new ListItem(content);
var item = afteritem || this.lastItemToSatisfy(this.sorter.bind(null,content));
if(!item){
this.head.linkTo(newitem);
this.head = newitem;
}else{
newitem.linkTo(item);
}
this.length++;
return newitem;
};
List.prototype.removeOne = function(listitem){
var item = this.head, previtem;
while(item){
if(item===listitem){
if(item===this.head){
if(item.link){
this.head = item.link;
item.destroy();
}else{
this.head.content = void 0;
}
}else{
previtem.link = item.link;
item.destroy();
}
this.length--;
return;
}
previtem = item;
item = item.link;
}
};
List.prototype.findOne = function(criterionfunction){
var item = this.firstItemToSatisfy(criterionfunction);
if(item){
return item.content;
}
};
List.prototype.firstItemToSatisfy = function(func){
var check=false, item = this.head;
while(!check&&item){
check = item.apply(func);
if('boolean' !== typeof check){
throw 'func needs to return a boolean value';
}
if(check){
return item;
}else{
item = item.link;
}
}
return item;
};
List.prototype.lastItemToSatisfy = function(func){
var check, item = this.head, ret;
while(item){
check = item.apply(func);
if('boolean' !== typeof check){
throw 'func needs to return a boolean value';
}
if(!check){
return ret;
}else{
ret = item;
item = item.link;
}
}
return ret;
};
List.prototype.traverse = function(func){
var item = this.head;
while(item){
item.apply(func);
item = item.link;
}
};
List.prototype.traverseConditionally = function(func){
var result, item = this.head;
while(item){
result = item.apply(func);
if('undefined' !== typeof result){
return result;
}
item = item.link;
}
};
List.prototype.dumpToConsole = function(){
this.traverse(console.log.bind(console));
};
function drainer(arry,countobj,content){
arry[countobj.count] = content;
countobj.count++;
}
List.prototype.drain = function(){
var ret = new Array(this.length),countobj={count:0};
this.traverse(drainer.bind(null,ret,countobj));
this.purge();
return ret;
};
module.exports = List;
0 0
- complex-list代码解析
- complex
- Complex
- Complex
- complex
- Complex
- 【cvc-complex-type.2.4.a】Spring的xml解析错误
- cvc-complex-type.2.3: Element 'list' cannot have character [children], because the type's content ty
- digester List解析案例
- jacson 解析list
- STL List::sort() 解析
- 列表解析 list comprehension
- json 解析list
- List源码解析
- Java_ArrayList与List解析
- list.h 解析
- GSON解析list
- linux内核list解析
- iOS开发 —— MVVM设计模式
- 在Xcode的模拟器中的textField中输入中文
- 条款14:在资源管理中心小心copy行为之重难点
- 证券基础--风险投资《阿凡达》的幕后英雄
- 黑马程序员——Java基础日记——面向对象
- complex-list代码解析
- USACO 2.4 Cow Tours (Floyd最短路径 + DFS)
- A016-布局之RelativeLayout
- 多线程NSThread状态控制—— 新建、就绪、运行、阻塞、死亡几个状态
- 80X86的物理地址形成(实模式+保护模式)——段式寻址
- 安卓控件使用系列34:ListView列表控件显示表单的使用方法1
- [python]学习笔记6-python数字
- 读《Effective Java》第一篇中文博客!
- 差旅费报销 总结