1.0.2 • Published 4 years ago

nv-data-dlink v1.0.2

Weekly downloads
4
License
ISC
Repository
-
Last release
4 years ago

nv-data-dlink

  • nv-data-dlink is very simple util of Double Linked List
  • two mode:
  • easy-mode no need to init with queue
  • queue-mode has more apis

install

  • npm install nv-data-dlink

usage

easy mode

  • const Easy = require("nv-data-dlink").Easy

examples

###init

var nd = new Easy({'abcd':'ABCD'})
nd.add_prev([1,2,3,4])
nd.prev.add_next(8)
nd.prev.add_prev("this-is-a-string")
nd.add_next(new Set([10,20,30]))
nd.next.add_next(new Map([[1,2],[3,4]]))
nd.next.next.add_prev(Symbol('sym'))

###relations

nd.index
nd.psibs
nd.fsibs
nd.head
nd.head.is_head()
nd.tail
nd.tail.is_tail()
> nd.index
3
> nd.psibs
[ [ 1, 2, 3, 4 ], this-is-a-string, 8 ]
> nd.fsibs
[ Set { 10, 20, 30 }, Symbol(sym), Map { 1 => 2, 3 => 4 } ]
> nd.head
[ 1, 2, 3, 4 ]
> nd.head.is_head()
true
> nd.tail
Map { 1 => 2, 3 => 4 }
> nd.tail.is_tail()
true
>

append,prepend,disconn

var nd1 = new Easy(function tst(){})
var nd2 = new Easy(Array)
nd.prepend(nd1)
nd.append(nd2)
nd.all
nd.disconn()
nd1.all

> nd.prepend(nd1)
[Function: tst]
> nd.append(nd2)
[Function: Array]
> nd.all
[
  [ 1, 2, 3, 4 ],
  this-is-a-string,
  8,
  [Function: tst],
  { abcd: 'ABCD' },
  [Function: Array],
  Set { 10, 20, 30 },
  Symbol(sym),
  Map { 1 => 2, 3 => 4 }
]
> nd.disconn()
{ abcd: 'ABCD' }
> nd1.all
[
  [ 1, 2, 3, 4 ],
  this-is-a-string,
  8,
  [Function: tst],
  [Function: Array],
  Set { 10, 20, 30 },
  Symbol(sym),
  Map { 1 => 2, 3 => 4 }
]
>

###swap

var st = nd1.next.next
st.swap(st.prev.prev.prev)
st.all
st.swap(st.prev)
st.all
st.swap(st.prev)
st.all
st.swap(st.tail)
st.swap(st)

>     st.swap(st.prev.prev.prev)
8
>     st.all
[
  [ 1, 2, 3, 4 ],
  this-is-a-string,
  Set { 10, 20, 30 },
  [Function: tst],
  [Function: Array],
  8,
  Symbol(sym),
  Map { 1 => 2, 3 => 4 }
]
>
undefined
>     st.swap(st.prev)
this-is-a-string
>     st.all
[
  [ 1, 2, 3, 4 ],
  Set { 10, 20, 30 },
  this-is-a-string,
  [Function: tst],
  [Function: Array],
  8,
  Symbol(sym),
  Map { 1 => 2, 3 => 4 }
]
>     st.swap(st.prev)
[ 1, 2, 3, 4 ]
>     st.all
[
  Set { 10, 20, 30 },
  [ 1, 2, 3, 4 ],
  this-is-a-string,
  [Function: tst],
  [Function: Array],
  8,
  Symbol(sym),
  Map { 1 => 2, 3 => 4 }
]
>     st.swap(st.tail)
Map { 1 => 2, 3 => 4 }
>     st.swap(st)
Set { 10, 20, 30 }
>

queue mode

  • const Queue = require("nv-data-dlink").Queue

examples

var queue = new Queue()
> queue.size
0
> queue.is_empty()
true
>

###init

var nd = queue.init_with_data({'abcd':'ABCD'})
> queue
[ { abcd: 'ABCD' }, ->, ..., { size: 1 }, ..., ->, { abcd: 'ABCD' } ]
>

###add nodes

nd.add_prev([1,2,3,4])
nd.prev.add_next(8)
nd.prev.add_prev("this-is-a-string")
nd.add_next(new Set([10,20,30]))
nd.next.add_next(new Map([[1,2],[3,4]]))
nd.next.next.add_prev(Symbol('sym'))
> queue.size
7
> Array.from(queue)
[
  [ [ 1, 2, 3, 4 ], 0 ],
  [ this-is-a-string, 1 ],
  [ 8, 2 ],
  [ { abcd: 'ABCD' }, 3 ],
  [ Set { 10, 20, 30 }, 4 ],
  [ Symbol(sym), 5 ],
  [ Map { 1 => 2, 3 => 4 }, 6 ]
]
>
> queue.forEach((r,i)=>{console.log(typeof(r.data))})
object
string
number
object
object
symbol
object
undefined
>

> queue.datas
[
  [ 1, 2, 3, 4 ],
  'this-is-a-string',
  8,
  { abcd: 'ABCD' },
  Set { 10, 20, 30 },
  Symbol(sym),
  Map { 1 => 2, 3 => 4 }
]
>

###relations

> queue.index_of(nd)
3
>
> queue.get_which(3)
{ abcd: 'ABCD' }
> nd.index
3
>
> queue.find(r=>(r.data instanceof Set))
[ Set { 10, 20, 30 } ]
>

> nd.psibs
[ [ 1, 2, 3, 4 ], this-is-a-string, 8 ]
>
> nd.fsibs
[ Set { 10, 20, 30 }, Symbol(sym), Map { 1 => 2, 3 => 4 } ]

> queue.includes(nd)
true
> queue.includes_data(nd.data)
true
>
> queue.get_id(nd)
'75d19a4e-5a7f-467a-b7d1-2ba629b47984'
> queue.get_node_with_id('75d19a4e-5a7f-467a-b7d1-2ba629b47984')
{ abcd: 'ABCD' }
>
> queue.nodes[5]
Symbol(sym)
>
> queue.distance_between(nd,queue.nodes[5])
-2
> queue.distance_between(queue.nodes[5],nd)
2

###move

queue.qid '44b991c2-441b-4877-ab53-46f87eea5f81'

var nqueue = queue.move_to() nqueue Queue {} nqueue.size 7 queue.size 0

nqueue.qid 'a3b661b4-a91a-4c47-b549-2e679a8af11f'

###pop_range

var queue = nqueue
> queue.nodes
[
  [ 1, 2, 3, 4 ],
  this-is-a-string,
  8,
  { abcd: 'ABCD' },
  Set { 10, 20, 30 },
  Symbol(sym),
  Map { 1 => 2, 3 => 4 }
]
>
> var from = queue.nodes[3]
> var to = queue.nodes[5]
var nqueue = queue.pop_range(from,to);
> nqueue.nodes
[ { abcd: 'ABCD' }, Set { 10, 20, 30 } ]
>
> queue.nodes
[
  [ 1, 2, 3, 4 ],
  this-is-a-string,
  8,
  Symbol(sym),
  Map { 1 => 2, 3 => 4 }
]
>

###disconn append

> nqueue.nodes
[ { abcd: 'ABCD' }, Set { 10, 20, 30 } ]
>
var nd0 = nqueue.nodes[0].disconn()
var nd1 = nqueue.nodes[0].disconn()
queue.get_which(2).append(nd0)
nd0.append(nd1)
> queue.nodes
[
  [ 1, 2, 3, 4 ],
  this-is-a-string,
  8,
  { abcd: 'ABCD' },
  Set { 10, 20, 30 },
  Symbol(sym),
  Map { 1 => 2, 3 => 4 }
]

###split

> var nqueue = queue.split(nd)
undefined
> nqueue
Queue {}
> nqueue.nodes
[
  { abcd: 'ABCD' },
  Set { 10, 20, 30 },
  Symbol(sym),
  Map { 1 => 2, 3 => 4 }
]
> queue.nodes
[ [ 1, 2, 3, 4 ], this-is-a-string, 8 ]
>

###add_with queue

var queue0 = new Queue()
var nd = queue0.init_with_data(1)
nd.add_next(20)
nd.next.add_next(3)
var queue1 = new Queue()
var nd = queue1.init_with_data(4)
nd.add_next(20)
nd.next.add_next(10)
queue0.add_with(queue1)
> queue0.nodes
[ 1, 20, 3, 4, 20, 10 ]
> queue1.nodes
[]
>

sort,reverse

    > queue0.nodes
    [ 1, 20, 3, 4, 20, 10 ]
    >
    > queue0.sort()
    Queue {}
    > queue0.nodes
    [ 1, 3, 4, 10, 20, 20 ]
    >
    > queue0.reverse()
    > queue0.nodes
    [ 20, 20, 10, 4, 3, 1 ]
    >

###insert

queue0.nodes
[ 20, 20, 10, 4, 3, 1 ]
>
queue0.insert_with_data(999,3)
> queue0.nodes
[
  20, 20, 10, 999,
  4,  3,  1
]
>

###splice

queue0.nodes
[
  20, 20, 10, 999,
  4,  3,  1
]
>
var queue1 = new Queue()
queue1.init_from_data_array(['a','b','c'])
> queue1.nodes
[ a, b, c ]
>
queue0.splice(1,2,queue1)
> queue0.nodes
[
  20,  a, b, c,
  999, 4, 3, 1
]
> queue1.nodes
[]
>

###swap

queue0.nodes
[ 20, 999, 4, 3, 1 ]
>
var nd = queue0.head
nd.swap(nd.next.next)
> queue0.nodes
[ 4, 999, 20, 3, 1 ]
>
//swap with queue
nd.swap(nd.queue)
> queue0.nodes
[ 3, 1, 20, 4, 999 ]
>

###swap_inplace

> queue0.nodes
[ 1, 20, 3, 'b', 20, 10 ]
queue0.swap_inplace_with_index(0,2,3,4)
//swap [1,20] with ['b']
> queue0.nodes
[ 'b', 3, 1, 20, 20, 10 ]
>

###swap_with

var q0 = new Queue()
q0.init_from_data_array(['A','B','C'])
var q1 = new Queue()
q1.init_from_data_array(['x','y'])
> q0.swap_with(q1)
undefined
> q0.nodes
[ x, y ]
> q1.nodes
[ A, B, C ]
>

METHODS

easy mode

nd.all                    nd.disconn                nd.distance_to            nd.fsib
nd.fsibs                  nd.get_mids               nd.has_mid_between        nd.head
nd.index                  nd.is_after               nd.is_before              nd.is_head
nd.is_lsib_of             nd.is_neighbor_of         nd.is_rsib_of             nd.is_tail
nd.psib                   nd.psibs                  nd.tail

nd.add_next               nd.add_prev               nd.append                 nd.constructor
nd.is_in_same_queue_with  nd.is_lonely              nd.prepend                nd.split_together_with
nd.swap

nd.data                   nd.id                     nd.next                   nd.prev
nd.qid                    nd.queue

queue mode

q.QHT                      q.add_with                 q.constructor              q.datas
q.distance_between         q.fforeach                 q.find                     q.forEach
q.from_to_foreach          q.get_id                   q.get_node_with_id         q.get_which
q.head                     q.ids                      q.includes                 q.includes_data
q.index_of                 q.indexes_of_data          q.init_from_data_array     q.init_with_data
q.insert_head              q.insert_tail              q.insert_with_data         q.insert_with_node
q.is_empty                 q.move_to                  q.next_data_iter           q.next_entry_iter
q.next_node_iter           q.nodes                    q.pforeach                 q.pop_range
q.prev_data_iter           q.prev_entry_iter          q.prev_node_iter           q.reset
q.reverse                  q.rm_range                 q.size                     q.sort
q.splice                   q.split                    q.swap_inplace_with_index  q.swap_inplace_with_node
q.swap_with                q.tail

q.id                       q.next                     q.prev                     q.qid
q.queue

LICENSE

  • ISC