# Idx.Permutation

Permutation is a precise definition on how elements within index range should be reorganized

Consider ordering a buffer of integers. This could be accomplished it two steps:

1. Based on some Ordering, we create a Permutation object, which captures all necessary positions
2. Apply permutation to the buffer
``````  val buf = Stream(0, 3, 1, 2, 4).toBuffer

buf.stream.tp // Prints:  Stream(0, 3, 1, 2, 4)

// Creating permutation based on given ordering
val p = Idx.Permutation.sorting(buffer)

p.tp               // Prints: Permutation{range=1 <>> 4,(1,3)(2,1)(3,2)}

p.reposition(buf)  // Applying permutation

buf.stream.tp           // Prints Stream(0, 1, 2, 3, 4)
``````
Source
__.scala
class java.lang.Object
trait scala.Matchable
class Any

## Def

def pairStream: Stream[(Int, Int)]

All changes

All changes

Returns a stream of all changed positions as a tuple: ('old position','new position')

Source
__.scala
def position(old: Int): Int

New index

New index

Given current position, returns new position

Source
__.scala

New positions

New positions

Returns a pack of new positions within the range

`````` // Repositioning range from 3 to 6 r reverse indexes = 6,5,4,3

val p = Idx.Permutation(3 <> 6, Pack(6, 5, 4, 3))

p.tp  // Prints: Permutation{range=3 <>> 7,(3,6)(4,5)(5,4)(6,3)}
``````
Source
__.scala
def range

Range of change

Range of change

Range of indexes which changed

Source
__.scala
def reposition[B](l: Idx.M[B]): Unit

Apply to mutable Idx

Apply to mutable Idx

Apply all changes to given mutable indexed collection

Source
__.scala
def reposition[B](l: Idx[B], update: (Int, B) => Unit): Unit

Apply to Idx

Apply to Idx

Apply all changes to given indexed collection, with the help of mutation function

Source
__.scala
def validate: Unit

Check for consistency

Check for consistency

It is possible to create inconsistent Permutation. For example, the same position could be changed twice, etc.

This operation will throw an exception if inconsistency found

Source
__.scala