chain-lightning
Advanced tools
+98
-17
@@ -195,16 +195,2 @@ /* | ||
| List.prototype.get = function get( slot ) { | ||
| if ( ! slot || slot.list !== this ) { return ; } | ||
| return slot.element ; | ||
| } ; | ||
| List.prototype.set = function set( slot , element ) { | ||
| if ( ! slot || slot.list !== this ) { return ; } | ||
| slot.element = element ; | ||
| } ; | ||
| List.prototype.slotOf = function slotOf( element , fromSlot ) { | ||
@@ -462,2 +448,96 @@ var current ; | ||
| List.prototype.get = function get( slot ) { | ||
| if ( ! slot || slot.list !== this ) { return ; } | ||
| return slot.element ; | ||
| } ; | ||
| List.prototype.set = function set( slot , element ) { | ||
| if ( ! slot || slot.list !== this ) { return ; } | ||
| slot.element = element ; | ||
| } ; | ||
| List.prototype.delete = | ||
| List.prototype.remove = function remove( slot ) { | ||
| if ( ! slot || slot.list !== this ) { return false ; } | ||
| if ( slot.previous ) { slot.previous.next = slot.next ; } | ||
| if ( slot.next ) { slot.next.previous = slot.previous ; } | ||
| if ( this.head === slot ) { this.head = slot.next ; } | ||
| if ( this.tail === slot ) { this.tail = slot.previous ; } | ||
| slot.list = slot.previous = slot.next = null ; | ||
| this.length -- ; | ||
| return true ; | ||
| } ; | ||
| List.prototype.moveAfter = function moveAfter( slot , afterSlot ) { | ||
| if ( | ||
| ! slot || slot.list !== this || ! afterSlot || afterSlot.list !== this || | ||
| slot === afterSlot || afterSlot === slot.previous | ||
| ) { | ||
| return false ; | ||
| } | ||
| var beforeSlot = afterSlot.next ; | ||
| if ( this.head === slot ) { this.head = slot.next ; } | ||
| if ( this.tail === slot ) { this.tail = slot.previous ; } | ||
| else if ( this.tail === afterSlot ) { this.tail = slot ; } | ||
| if ( slot.previous ) { slot.previous.next = slot.next ; } | ||
| if ( slot.next ) { slot.next.previous = slot.previous ; } | ||
| slot.previous = afterSlot ; | ||
| afterSlot.next = slot ; | ||
| slot.next = beforeSlot ; | ||
| if ( beforeSlot ) { beforeSlot.previous = slot ; } | ||
| return true ; | ||
| } ; | ||
| List.prototype.moveBefore = function moveBefore( slot , beforeSlot ) { | ||
| if ( | ||
| ! slot || slot.list !== this || ! beforeSlot || beforeSlot.list !== this || | ||
| slot === beforeSlot || beforeSlot === slot.next | ||
| ) { | ||
| return false ; | ||
| } | ||
| var afterSlot = beforeSlot.previous ; | ||
| if ( this.head === slot ) { this.head = slot.next ; } | ||
| else if ( this.head === beforeSlot ) { this.head = slot ; } | ||
| if ( this.tail === slot ) { this.tail = slot.previous ; } | ||
| if ( slot.previous ) { slot.previous.next = slot.next ; } | ||
| if ( slot.next ) { slot.next.previous = slot.previous ; } | ||
| slot.next = beforeSlot ; | ||
| beforeSlot.previous = slot ; | ||
| slot.previous = afterSlot ; | ||
| if ( afterSlot ) { afterSlot.next = slot ; } | ||
| return true ; | ||
| } ; | ||
| List.prototype.moveToTail = function moveToTail( slot ) { return this.moveAfter( slot , this.tail ) ; } ; | ||
| List.prototype.moveToHead = function moveToHead( slot ) { return this.moveBefore( slot , this.head ) ; } ; | ||
| List.prototype.insertAfter = function insertAfter( afterSlot , ... elements ) { | ||
@@ -518,2 +598,3 @@ if ( afterSlot.list !== this || ! elements.length ) { return ; } | ||
| var lastSlot = null , | ||
| nextSlot , | ||
| slot = this.head ; | ||
@@ -525,2 +606,3 @@ | ||
| slot.previous = lastSlot ; | ||
| nextSlot = slot.next ; // backup that | ||
@@ -534,5 +616,6 @@ if ( fn.call( thisArg , slot.element , slot , this ) ) { | ||
| this.length -- ; | ||
| slot.list = slot.previous = slot.next = null ; | ||
| } | ||
| slot = slot.next ; | ||
| slot = nextSlot ; | ||
| } | ||
@@ -545,3 +628,1 @@ | ||
+1
-1
| { | ||
| "name": "chain-lightning", | ||
| "version": "0.1.0", | ||
| "version": "0.1.1", | ||
| "description": "Linked list", | ||
@@ -5,0 +5,0 @@ "main": "lib/chainlightning.js", |
@@ -449,2 +449,171 @@ /* | ||
| it( ".remove()/.delete()" , () => { | ||
| var list , | ||
| e1 = { v: 'jack' } , | ||
| e2 = { v: 'bob' } , | ||
| e3 = { v: 'steve' } , | ||
| e4 = { v: 'bobby' } ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| list.remove( list.slotOf( e2 ) ) ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List() ; | ||
| list.remove( list.slotOf( e2 ) ) ; | ||
| expect( [ ... list ] ).to.equal( [] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e2 ) ; | ||
| list.remove( list.slotOf( e2 ) ) ; | ||
| expect( [ ... list ] ).to.equal( [] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e2 , e1 , e3 ) ; | ||
| list.remove( list.slotOf( e2 ) ) ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e3 , e2 ) ; | ||
| list.remove( list.slotOf( e2 ) ) ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| } ) ; | ||
| it( ".moveAfter()/.moveToTail()" , () => { | ||
| var list , | ||
| e1 = { v: 'jack' } , | ||
| e2 = { v: 'bob' } , | ||
| e3 = { v: 'steve' } , | ||
| e4 = { v: 'bobby' } ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveAfter( list.slotOf( e1 ) , list.slotOf( e1 ) ) ).to.be.false() ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e2 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveAfter( list.slotOf( e1 ) , list.slotOf( e2 ) ) ).to.be.true() ; | ||
| expect( [ ... list ] ).to.equal( [ e2 , e1 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveAfter( list.slotOf( e1 ) , list.slotOf( e3 ) ) ).to.be.true() ; | ||
| expect( [ ... list ] ).to.equal( [ e2 , e3 , e1 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveToTail( list.slotOf( e1 ) ) ).to.be.true() ; | ||
| expect( [ ... list ] ).to.equal( [ e2 , e3 , e1 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveAfter( list.slotOf( e2 ) , list.slotOf( e1 ) ) ).to.be.false() ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e2 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveAfter( list.slotOf( e2 ) , list.slotOf( e2 ) ) ).to.be.false() ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e2 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveAfter( list.slotOf( e2 ) , list.slotOf( e3 ) ) ).to.be.true() ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e3 , e2 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveToTail( list.slotOf( e2 ) ) ).to.be.true() ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e3 , e2 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveAfter( list.slotOf( e3 ) , list.slotOf( e1 ) ) ).to.be.true() ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e3 , e2 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveAfter( list.slotOf( e3 ) , list.slotOf( e2 ) ) ).to.be.false() ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e2 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveAfter( list.slotOf( e3 ) , list.slotOf( e3 ) ) ).to.be.false() ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e2 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveToTail( list.slotOf( e3 ) ) ).to.be.false() ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e2 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| } ) ; | ||
| it( ".moveBefore()/.moveToHead()" , () => { | ||
| var list , | ||
| e1 = { v: 'jack' } , | ||
| e2 = { v: 'bob' } , | ||
| e3 = { v: 'steve' } , | ||
| e4 = { v: 'bobby' } ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveBefore( list.slotOf( e1 ) , list.slotOf( e1 ) ) ).to.be.false() ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e2 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveToHead( list.slotOf( e1 ) ) ).to.be.false() ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e2 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveBefore( list.slotOf( e1 ) , list.slotOf( e2 ) ) ).to.be.false() ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e2 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveBefore( list.slotOf( e1 ) , list.slotOf( e3 ) ) ).to.be.true() ; | ||
| expect( [ ... list ] ).to.equal( [ e2 , e1 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveBefore( list.slotOf( e2 ) , list.slotOf( e1 ) ) ).to.be.true() ; | ||
| expect( [ ... list ] ).to.equal( [ e2 , e1 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveToHead( list.slotOf( e2 ) ) ).to.be.true() ; | ||
| expect( [ ... list ] ).to.equal( [ e2 , e1 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveBefore( list.slotOf( e2 ) , list.slotOf( e2 ) ) ).to.be.false() ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e2 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveBefore( list.slotOf( e2 ) , list.slotOf( e3 ) ) ).to.be.false() ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e2 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveBefore( list.slotOf( e3 ) , list.slotOf( e1 ) ) ).to.be.true() ; | ||
| expect( [ ... list ] ).to.equal( [ e3 , e1 , e2 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveToHead( list.slotOf( e3 ) ) ).to.be.true() ; | ||
| expect( [ ... list ] ).to.equal( [ e3 , e1 , e2 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveBefore( list.slotOf( e3 ) , list.slotOf( e2 ) ) ).to.be.true() ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e3 , e2 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| expect( list.moveBefore( list.slotOf( e3 ) , list.slotOf( e3 ) ) ).to.be.false() ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e2 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| } ) ; | ||
| it( ".insertAfter()" , () => { | ||
@@ -451,0 +620,0 @@ var list , |
43422
23.13%1084
21.39%