// Circulant returns a virtual circulant graph with n vertices
// in which vertex i is adjacent to vertex (i + j) mod n and vertex (i - j) mod n
// for each j in the list s.
962244
func Circulant(n int, s ...int) *Virtual {
	switch {
	case n < 0:
		return nil
	case n == 0:
		return null
	case n == 1:
		return singleton()
	}

	s1 := make([]int, 0, 2*len(s))
	for _, j := range s {
		j %= n
		if j < 0 {
			j += n
		}
		s1 = append(s1, j)
		s1 = append(s1, n-j)
	}
	sort.Ints(s1)
	var t []int
	prev := -1
	for _, j := range s1 {
		// Remove zeroes and duplicates.
		if j == prev || j == 0 || j == n {
			continue
		}
		prev = j
		t = append(t, j)
	}
	deg := len(t)
	switch {
	case deg == 0:
		return Empty(n)
	case deg == 1 && t[0] == 1:
		return Cycle(n)
	case deg == n-1:
		return Kn(n)
	}

	g := generic0(n, func(v, w int) (edge bool) {
		d := v - w
		if d < 0 {
			d = -d
		}
		i := sort.SearchInts(t, d)
		return i < deg && t[i] == d
	})

	g.degree = func(v int) int { return deg }

	g.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) {
		start := sort.SearchInts(t, n-v+a)
		for i := start; i < deg; i++ {
			if do(v+t[i]-n, 0) {
				return true
			}
		}
		start = sort.SearchInts(t, a-v)
		stop := sort.SearchInts(t, n-v)
		for i := start; i < stop; i++ {
			if do(v+t[i], 0) {
				return true
			}
		}
		return
	}
	return g
}
1001529
func (l *Loop) Vertex(i int) Point {
	return l.vertices[i%len(l.vertices)]
}
315247
func (g *SimpleGraph) V() int {
	g.mutex.RLock()
	defer g.mutex.RUnlock()

	return g.v
}
1137273
func (g Graph) EdgeSubgraph(edgeFn EdgeFunc) Graph {
	out := New()
	for _, node := range g.Nodes() {
		out.internal.AddNode(node)
	}
	out.addEdges(g.internal.Edges(), edgeFn)
	return out
}
1022044
func drawCircle(radius float64, sides int) {
	gl.Begin(gl.LINE_LOOP)
	for a := 0.0; a < 2*math.Pi; a += (2 * math.Pi / float64(sides)) {
		gl.Vertex2d(math.Sin(a)*radius, math.Cos(a)*radius)
	}
	gl.Vertex3f(0, 0, 0)
	gl.End()
}
870103
func (g *Graph) Reverse() *Graph {
	reversed := New(Directed)
	if g.Kind == Undirected {
		reversed = New(Undirected)
	}
	// O(V)
	for _ = range g.nodes {
		reversed.MakeNode()
	}
	// O(V + E)
	for _, node := range g.nodes {
		for _, edge := range node.edges {
			reversed.MakeEdge(reversed.nodes[edge.end.index].container,
				reversed.nodes[node.index].container)
		}
	}
	return reversed
}
342780
func NewUndirectedGraph(self, absent float64) *UndirectedGraph {
	return &UndirectedGraph{
		nodes: make(map[int]graph.Node),
		edges: make(map[int]edgeHolder),

		self:   self,
		absent: absent,
	}
}
1062789
func (ci CellID) VertexNeighbors(level int) []CellID {
	halfSize := sizeIJ(level + 1)
	size := halfSize << 1
	f, i, j, _ := ci.faceIJOrientation()

	var isame, jsame bool
	var ioffset, joffset int
	if i&halfSize != 0 {
		ioffset = size
		isame = (i + size) < maxSize
	} else {
		ioffset = -size
		isame = (i - size) >= 0
	}
	if j&halfSize != 0 {
		joffset = size
		jsame = (j + size) < maxSize
	} else {
		joffset = -size
		jsame = (j - size) >= 0
	}

	results := []CellID{
		ci.Parent(level),
		cellIDFromFaceIJSame(f, i+ioffset, j, isame).Parent(level),
		cellIDFromFaceIJSame(f, i, j+joffset, jsame).Parent(level),
	}

	if isame || jsame {
		results = append(results, cellIDFromFaceIJSame(f, i+ioffset, j+joffset, isame && jsame).Parent(level))
	}

	return results
}
794879
func (s *Set) CartesianProduct(t *Set) *Set {
	// Create a set of ordered pair permutations between the sets
	cpSet := New()

	// Enumerate the source set
	for _, x := range s.Enumerate() {
		// Enumerate the target set
		for _, y := range t.Enumerate() {
			// Create pair, insert elements, insert into set
			cpSet.Add(Pair{
				X: x,
				Y: y,
			})
		}
	}

	return cpSet
}
512626
func StronglyConnected(g *Graph) [][]Vertex {
	vs := g.Vertices()
	acct := sccAcct{
		NextIndex:   1,
		VertexIndex: make(map[Vertex]int, len(vs)),
	}
	for _, v := range vs {
		// Recurse on any non-visited nodes
		if acct.VertexIndex[v] == 0 {
			stronglyConnected(&acct, g, v)
		}
	}
	return acct.SCC
}
498279
func C(center Vec, radius float64) Circle {
	return Circle{
		Center: center,
		Radius: radius,
	}
}
1299412
func NewCircularBuffer(size uint) *CircularBuffer {
	return &CircularBuffer{
		buffer: make([]interface{}, size),
		size:   size,
		avail:  make(chan bool, size),
	}
}
582553
func (e *joinReOrderSolver) walkGraphAndComposeJoin(u int) {
	e.visited[u] = true
	for _, edge := range e.graph[u] {
		v := edge.nodeID
		if !e.visited[v] {
			e.resultJoin = e.newJoin(e.resultJoin, e.group[v])
			e.walkGraphAndComposeJoin(v)
		}
	}
}
582702
func (g *graphObject) V(call goja.FunctionCall) goja.Value {
	return g.Vertex(call)
}
532407
func (m *merkleBlock) calcTreeWidth(height uint32) uint32 {
	return (m.numTx + (1 << height) - 1) >> height
}
1061557
func (g *Graph) Add(v Vertex) Vertex {
	g.init()
	g.vertices.Add(v)
	g.debug.Add(v)
	return v
}
416860
func (g *Graph) sccUndirected() [][]Node {
	components := make([][]Node, 0)
	for _, node := range g.nodes {
		if node.state == unseen {
			component := make([]Node, 0)
			g.bfs(node, &component)
			components = append(components, component)
		}
	}
	return components
}