// Shuffle returns the vector with its components shuffled in the order according to mask.
//
// Example:
// 		vec := vec4.T{1, 2, 3, 4}
//		vec.Shuffle(vec4.WZYX)
//		check := vec == T{4, 3, 2, 1} // true
//
761351
func (vec *T) Shuffle(mask ShuffleMask) *T {
	*vec = vec.Shuffled(mask)
	return vec
}
1316557
func (n *healthCheckSorter) Swap(i, j int) {
	n.Checks[i], n.Checks[j] = n.Checks[j], n.Checks[i]
	n.Vec[i], n.Vec[j] = n.Vec[j], n.Vec[i]
}
1030107
func (vec Vector) Less(i, j int) bool {
	switch {
	case vec[i].Metric.Before(vec[j].Metric):
		return true
	case vec[j].Metric.Before(vec[i].Metric):
		return false
	case vec[i].Timestamp.Before(vec[j].Timestamp):
		return true
	default:
		return false
	}
}
612720
func (vector *BitVector) Insert(bit byte, index int) {
	vector.indexAssert(index)
	vector.length++

	// Append an additional byte if necessary.
	if vector.bytesLength() > len(vector.data) {
		vector.data = append(vector.data, 0)
	}

	byteIndex := uint32(index >> 3)
	byteOffset := uint32(index % 8)
	var bitToInsert byte
	if bit == 1 {
		bitToInsert = 1 << byteOffset
	}

	oldByte := vector.data[byteIndex]
	// This bit will need to be shifted into the next byte
	leftoverBit := (oldByte & 0x80) >> 7
	// Make masks to pull off the bits below and above byteOffset
	// This mask has the byteOffset lowest bits set.
	bottomMask := byte((1 << byteOffset) - 1)
	// This mask has the 8 - byteOffset top bits set.
	topMask := ^bottomMask
	top := (oldByte & topMask) << 1
	newByte := bitToInsert | (oldByte & bottomMask) | top

	vector.data[byteIndex] = newByte
	// Shift the rest of the bytes in the slice one higher, append
	// the leftoverBit obtained above.
	shiftHigher(leftoverBit, vector.data[byteIndex+1:])
}
1075097
func (k *Kites) Shuffle() {
	shuffled := make(Kites, len(*k))
	for i, v := range rand.Perm(len(*k)) {
		shuffled[v] = (*k)[i]
	}

	*k = shuffled
}
564910
func Shuffle(from FixedDataGrid) FixedDataGrid {
	_, rows := from.Size()
	if inst, ok := from.(*DenseInstances); ok {
		for i := 0; i < rows; i++ {
			j := rand.Intn(i + 1)
			inst.swapRows(i, j)
		}
		return inst
	} else {
		return LazyShuffle(from)
	}
}
16317
func ShuffleString(a []string) []string {
	for i := range a {
		j := rand.Intn(i + 1)
		a[i], a[j] = a[j], a[i]
	}

	return a
}
554520
func Shuffle(str string) string {
	if str == "" {
		return str
	}

	runes := []rune(str)
	index := 0

	for i := len(runes) - 1; i > 0; i-- {
		index = rand.Intn(i + 1)

		if i != index {
			runes[i], runes[index] = runes[index], runes[i]
		}
	}

	return string(runes)
}
1319190
func ShuffleStrings(strings []string) []string {
	newStrings := make([]string, len(strings))
	newIndexes := rand.Perm(len(strings))

	for o, n := range newIndexes {
		newStrings[n] = strings[o]
	}

	return newStrings
}
877449
func (x Vector) Diff() Vector {
	n := len(x)

	if n < 2 {
		return Vector{NA}
	} else {
		d := make(Vector, n-1)

		i := 1
		for i < n {
			d[i-1] = x[i] - x[i-1]
			i++
		}
		return d
	}
}
520727
func shuffle(input []string) []string {
	tmp := make([]string, len(input))
	r := rand.New(rand.NewSource(time.Now().Unix()))
	for i, j := range r.Perm(len(input)) {
		tmp[i] = input[j]
	}
	return tmp
}
1274507
func (ung UniformGenerator) ShuffleRange(arr []interface{}, low, high int) {
	if low < 0 || low > high || high >= len(arr) {
		panic(fmt.Sprintf("Illegal subarray range %d ~ %d", low, high))
	}
	for i := low; i <= high; i++ {
		r := int32(i) + ung.Int32n(int32(high-i+1))
		arr[i], arr[r] = arr[r], arr[i]
	}
}
454991
func (v Vector) Normalise() {
	w := blas.Snrm2(len(v), v, 1)
	blas.Sscal(len(v), 1/w, v, 1)
}
240360
func shuffleUp(heap Interface, i int) {
	for {
		pi := (i - 1) / 2 // (i + 1) / 2 - 1, parent
		if i == pi || !heap.Less(pi, i) {
			break
		}
		heap.Swap(pi, i)
		i = pi
	}
}
503676
func ShuffleGen(xs interface{}, rng *rand.Rand) {
	chk := ty.Check(
		new(func([]ty.A, *rand.Rand)),
		xs, rng)
	vxs := chk.Args[0]

	// Implements the Fisher-Yates shuffle: http://goo.gl/Hb9vg
	xsLen := vxs.Len()
	swapper := swapperOf(vxs.Type().Elem())
	for i := xsLen - 1; i >= 1; i-- {
		j := rng.Intn(i + 1)
		swapper.swap(vxs.Index(i), vxs.Index(j))
	}
}
1296408
func (cl *CommandList) Shuffle(start, end int) {
	if start < 0 || end < 0 {
		cl.cmdQ.PushBack(&command{"shuffle", nil, cmdNoReturn})
		return
	}
	cl.cmdQ.PushBack(&command{fmt.Sprintf("shuffle %d:%d", start, end), nil, cmdNoReturn})
}
108596
func (addrs byPriorityWeight) shuffleByWeight() {
	sum := 0
	for _, addr := range addrs {
		sum += int(addr.Weight)
	}
	for sum > 0 && len(addrs) > 1 {
		s := 0
		n := rand.Intn(sum)
		for i := range addrs {
			s += int(addrs[i].Weight)
			if s > n {
				if i > 0 {
					t := addrs[i]
					copy(addrs[1:i+1], addrs[0:i])
					addrs[0] = t
				}
				break
			}
		}
		sum -= int(addrs[0].Weight)
		addrs = addrs[1:]
	}
}