1.0.4 • Published 2 years ago

nv-array-swap v1.0.4

Weekly downloads
1
License
ISC
Repository
-
Last release
2 years ago

nv-array-swap

  • nv-array-swap

install

  • npm install nv-array-swap

usage

example

const ary_swap = require("nv-array-swap")

ary_swap.iswap([10,20,30,40],0,3)
//[ 40, 20, 30, 10 ]
ary_swap.vswap([10,20,30,40],10,40)
//[ 40, 20, 30, 10 ]
ary_swap.viswap([10,20,30,40],{i:1,v:10},{i:3,v:40})
//[ 10, 20, 30, 40 ]
ary_swap.viswap([10,20,30,40],{i:0,v:10},{i:3,v:40})
//[ 40, 20, 30, 10 ]

ary_swap.reindex([10,20,30,40],1,0,3,2)
//[ 20, 10, 40, 30 ] 
// 0->1 , 1->0 2->3,3->2

swap_range

var x = ary_swap;

> var a = [10,20,30,40,50,60,70,80,90]

>
> x.swap_range(a,[1,3],[5,7])
[
  10, 60, 70, 40, 50,
  20, 30, 80, 90
]

slightly fast than using splice

    const f0 = ()=> {
       var a = [10,20,30,40,50,60,70,80,90]
       x.swap_range_using_splice(a,[1,3],[5,7])
       return(a)
    }


    const f1 = () => {
       var a = [10,20,30,40,50,60,70,80,90]
       x.swap_range(a,[1,3],[5,7])
       return(a)
    }

    const {sync} = require("nv-facutil-simple-test")

    > sync(10000000,f0)
    { rounds: 10000000, f: [Function: f0], costed: 3614.6889840364456 }
    > sync(10000000,f1)
    { rounds: 10000000, f: [Function: f1], costed: 2733.491766035557 }
    >

if overlapped , only swap the different part

   var a     = [10,20,30,40,50,60,70,80,90]
   var fst   =     [1,         5]
   var snd   =           [3,          7] 


   x.swap_range(a,fst,snd);  // will swap (1,2)  AND  (5,6)

   /*
            >        x.swap_range(a,fst,snd);
            [
              10, 60, 70, 40, 50,
              20, 30, 80, 90
            ]
            > a
            [
              10, 60, 70, 40, 50,
              20, 30, 80, 90
            ]
            >
   */

if equal OR included , do nothing

API

  • ary_swap.iswap(arr,i0,i1)
  • ary_swap.vswap(arr,v0,v1)
  • ary_swap.viswap(arr,d0,d1) // d={i:index,v:value}
  • ary_swap.reindex(arr,...whiches)

range

    {
      _swap_range_if_same_length: [Function: _swap_range_if_same_length],
      
      _swap_neighbor_range_iter: [Function: _swap_neighbor_range_iter] {
        REQUIREMENT: [Function (anonymous)]
      },
          //internal iter of _swap_neighbor_range
      _swap_neighbor_range: [Function: _swap_neighbor_range],
          // -- this is fast  AND inplace  

      _swap_range_init: [Function: _swap_range_init] { REQUIREMENT: [Function (anonymous)] },
      _fmt_one_rng: [Function: _fmt_one_rng],
      swap_range_fmt: [Function: swap_range_fmt],
      swap_range_init: [Function: swap_range_init],
      
      swap_range: [Function: swap_range],
      

      _swap_range_using_splice: [Function: _swap_range_using_splice],     //for test compare 
      swap_range_using_splice: [Function: swap_range_using_splice]        // for test compare
    }

LICENSE

  • ISC
1.0.2

2 years ago

1.0.1

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.0

4 years ago