// permute returns Trees that their vertices and edges all exist in the original tree who's vertex
// is the 'originalRoot' field of the treePermutations
498012
func (tp *treePermutations) permute() []*Tree {
	tp.computeDescendantPermutations()

	it := newBFSIterator(tp.originalRoot)
	for {
		v := it.Next()
		if v == nil {
			break
		}

		if len(v.Descendants) == 0 {
			continue
		}

		// Iterate over all permutations where v exists
		// and separate them to 2 sets: a indiceSet where it exists and a indiceSet where it doesn't
		var permutationsWhereVexists []*TreeVertex
		var permutationsWhereVdoesntExist []*TreeVertex
		for _, perm := range tp.permutations {
			if perm.Exists(v.Id) {
				permutationsWhereVexists = append(permutationsWhereVexists, perm)
			} else {
				permutationsWhereVdoesntExist = append(permutationsWhereVdoesntExist, perm)
			}
		}

		// Remove the permutations where v exists from the permutations
		tp.permutations = permutationsWhereVdoesntExist

		// Next, we replace each occurrence of a permutation where v exists,
		// with multiple occurrences of its descendants permutations
		for _, perm := range permutationsWhereVexists {
			// For each permutation of v's descendants, clone the graph
			// and create a new graph with v replaced with the permutated graph
			// of v connected to the descendant permutation
			for _, permutation := range tp.descendantPermutations[v] {
				subGraph := &TreeVertex{
					Id:          v.Id,
					Data:        v.Data,
					Descendants: permutation,
				}
				newTree := perm.Clone()
				newTree.replace(v.Id, subGraph)
				// Add the new option to the permutations
				tp.permutations = append(tp.permutations, newTree)
			}
		}
	}

	res := make([]*Tree, len(tp.permutations))
	for i, perm := range tp.permutations {
		res[i] = perm.ToTree()
	}
	return res
}
234513
func (c bySortConfig) Swap(i, j int) {
	c.rows[i], c.rows[j] = c.rows[j], c.rows[i]
}
417128
func EulerDirected(g Iterator) (walk []int, ok bool) {
	n := g.Order()
	degree := make([]int, n) // outdegree - indegree for each vertex
	edgeCount := 0
	for v := range degree {
		g.Visit(v, func(w int, _ int64) (skip bool) {
			edgeCount++
			degree[v]++
			degree[w]--
			return
		})
	}
	if edgeCount == 0 {
		return []int{}, true
	}

	start, end := -1, -1
	for v := range degree {
		switch {
		case degree[v] == 0:
		case degree[v] == 1 && start == -1:
			start = v
		case degree[v] == -1 && end == -1:
			end = v
		default:
			return []int{}, false
		}
	}

	// Make a copy of g
	h := make([][]int, n)
	for v := range h {
		g.Visit(v, func(w int, _ int64) (skip bool) {
			h[v] = append(h[v], w)
			return
		})
	}

	// Find a starting point with neighbors.
	if start == -1 {
		for v, neighbors := range h {
			if len(neighbors) > 0 {
				start = v
				break
			}
		}
	}

	for stack := []int{start}; len(stack) > 0; {
		n := len(stack)
		v := stack[n-1]
		stack = stack[:n-1]
		for len(h[v]) > 0 {
			stack = append(stack, v)
			v, h[v] = h[v][0], h[v][1:]
			edgeCount--
		}
		walk = append(walk, v)
	}
	if edgeCount > 0 {
		return []int{}, false
	}
	for i, j := 0, len(walk)-1; i < j; i, j = i+1, j-1 {
		walk[i], walk[j] = walk[j], walk[i]
	}
	return walk, true
}
104734
func (pt PrimitiveTypes) Swap(i, j int) {
	pt[i], pt[j] = pt[j], pt[i]
}
199704
func (b *Sortable) Swap(i, j int) {
	b.Data[i], b.Data[j] = b.Data[j], b.Data[i]
}
1056220
func NewPermutationGenerator(m int64, seed int64) *PermutationGenerator {
	// figure out 'a' and 'c', return PermutationGenerator
	a := multiplierFromModulus(m, seed)
	c := int64(22695479)
	return &PermutationGenerator{a, c, m}
}
1075097
func (k *Kites) Shuffle() {
	shuffled := make(Kites, len(*k))
	for i, v := range rand.Perm(len(*k)) {
		shuffled[v] = (*k)[i]
	}

	*k = shuffled
}
242225
func shuffleNodes(nodes []*nodeState) {
	n := len(nodes)
	rand.Shuffle(n, func(i, j int) {
		nodes[i], nodes[j] = nodes[j], nodes[i]
	})
}
436275
func shuffle(edges []lite.Edge) {
	for i := len(edges) - 1; i > 0; i-- {
		j := rand.Intn(i + 1)
		edges[j], edges[i] = edges[i], edges[j]
	}
}
427316
func Perm(n int) []int {
	rng.Lock()
	defer rng.Unlock()
	return rng.rand.Perm(n)
}
1078665
func (g *Graph) simplify() {

	if g == nil {
		return
	}

	for _, node := range g.Out {
		if TypePath == node.ThisString() {
			s := node.GetAt(0).ThisString()

			switch s {
			case "if":
				node.This = TypeIf
				node.DeleteAt(0)
			case "end":
				node.This = TypeEnd
				node.DeleteAt(0)
			case "else":
				node.This = TypeElse
				node.DeleteAt(0)
			case "for":
				node.This = TypeFor
				node.DeleteAt(0)
			case "break":
				node.This = TypeBreak
				node.DeleteAt(0)
			}
		}
	}

}
246358
func (iter *PermutationIterator) Reset() {
	iter.iters = 0

	iter.indices = make([]int, iter.n)
	for i := range iter.indices {
		iter.indices[i] = i
	}

	iter.cycles = make([]int, iter.n-(iter.n-iter.r))
	for i := range iter.cycles {
		iter.cycles[i] = iter.n - i
	}

	iter.res = make([]interface{}, iter.r, iter.r)
}
486512
func (r *Rand) Perm(n int) []int {
	m := make([]int, n)
	for i := 0; i < n; i++ {
		j := r.Intn(i + 1)
		m[i] = m[j]
		m[j] = i
	}
	return m
}
205398
func (node *Node) traverse(t *ImmutableTree, ascending bool, cb func(*Node) bool) bool {
	return node.traverseInRange(t, nil, nil, ascending, false, 0, func(node *Node, depth uint8) bool {
		return cb(node)
	})
}
180504
func (g Graph) NodesByKind(nodeKinds ...string) []graph.Node {
	ret := []graph.Node{}

	kinds := sets.NewString(nodeKinds...)
	for _, node := range g.internal.Nodes() {
		if kinds.Has(g.Kind(node)) {
			ret = append(ret, node)
		}
	}

	return ret
}
542629
func (g *Graph) EdgesFrom(v Vertex) []Edge {
	var result []Edge
	from := hashcode(v)
	for _, e := range g.Edges() {
		if hashcode(e.Source()) == from {
			result = append(result, e)
		}
	}

	return result
}
432895
func (from *Tree) Diff(to *Tree) (Changes, error) {
	return DiffTree(from, to)
}