// Defines a total ordering on Loops that does not depend on the cyclic
// order of loop vertices. This function is used to choose which loop to
// invert in the case where several loops have exactly the same area.
766489
func compareLoops(a, b *Loop) int {
	if na, nb := a.NumVertices(), b.NumVertices(); na != nb {
		return na - nb
	}
	ai, aDir := a.CanonicalFirstVertex()
	bi, bDir := b.CanonicalFirstVertex()
	if aDir != bDir {
		return aDir - bDir
	}
	for n := a.NumVertices() - 1; n >= 0; n, ai, bi = n-1, ai+aDir, bi+bDir {
		if cmp := a.Vertex(ai).Cmp(b.Vertex(bi).Vector); cmp != 0 {
			return cmp
		}
	}
	return 0
}
1005607
func (p *PhysicalProperty) AllSameOrder() (bool, bool) {
	if len(p.Items) == 0 {
		return true, false
	}
	for i := 1; i < len(p.Items); i++ {
		if p.Items[i].Desc != p.Items[i-1].Desc {
			return false, false
		}
	}
	return true, p.Items[0].Desc
}
327695
func (d *requestDistributor) loop() {
	for {
		select {
		case <-d.stopChn:
			d.lock.Lock()
			elem := d.reqQueue.Front()
			for elem != nil {
				req := elem.Value.(*distReq)
				close(req.sentChn)
				req.sentChn = nil
				elem = elem.Next()
			}
			d.lock.Unlock()
			return
		case <-d.loopChn:
			d.lock.Lock()
			d.loopNextSent = false
		loop:
			for {
				peer, req, wait := d.nextRequest()
				if req != nil && wait == 0 {
					chn := req.sentChn // save sentChn because remove sets it to nil
					d.remove(req)
					send := req.request(peer)
					if send != nil {
						peer.queueSend(send)
					}
					chn <- peer
					close(chn)
				} else {
					if wait == 0 {
						// no request to send and nothing to wait for; the next
						// queued request will wake up the loop
						break loop
					}
					d.loopNextSent = true // a "next" signal has been sent, do not send another one until this one has been received
					if wait > distMaxWait {
						// waiting times may be reduced by incoming request replies, if it is too long, recalculate it periodically
						wait = distMaxWait
					}
					go func() {
						d.clock.Sleep(wait)
						d.loopChn <- struct{}{}
					}()
					break loop
				}
			}
			d.lock.Unlock()
		}
	}
}
917984
func (statement *Statement) OrderBy(order string) *Statement {
	if len(statement.OrderStr) > 0 {
		statement.OrderStr += ", "
	}
	statement.OrderStr += order
	return statement
}
595346
func (tr Matrix) InverseTransform(points []float64) {
	d := tr.Determinant() // matrix determinant
	for i, j := 0, 1; j < len(points); i, j = i+2, j+2 {
		x := points[i]
		y := points[j]
		points[i] = ((x-tr[4])*tr[3] - (y-tr[5])*tr[2]) / d
		points[j] = ((y-tr[5])*tr[0] - (x-tr[4])*tr[1]) / d
	}
}
356026
func (s *ScoreSort) Order(ascending bool) *ScoreSort {
	s.ascending = ascending
	return s
}
999231
func (l *Loop) initOriginAndBound() {
	if len(l.vertices) < 3 {
		// Check for the special "empty" and "full" loops (which have one vertex).
		if !l.isEmptyOrFull() {
			l.originInside = false
			return
		}

		// This is the special empty or full loop, so the origin depends on if
		// the vertex is in the southern hemisphere or not.
		l.originInside = l.vertices[0].Z < 0
	} else {
		// Point containment testing is done by counting edge crossings starting
		// at a fixed point on the sphere (OriginPoint). We need to know whether
		// the reference point (OriginPoint) is inside or outside the loop before
		// we can construct the ShapeIndex. We do this by first guessing that
		// it is outside, and then seeing whether we get the correct containment
		// result for vertex 1. If the result is incorrect, the origin must be
		// inside the loop.
		//
		// A loop with consecutive vertices A,B,C contains vertex B if and only if
		// the fixed vector R = B.Ortho is contained by the wedge ABC. The
		// wedge is closed at A and open at C, i.e. the point B is inside the loop
		// if A = R but not if C = R. This convention is required for compatibility
		// with VertexCrossing. (Note that we can't use OriginPoint
		// as the fixed vector because of the possibility that B == OriginPoint.)
		l.originInside = false
		v1Inside := OrderedCCW(Point{l.vertices[1].Ortho()}, l.vertices[0], l.vertices[2], l.vertices[1])
		if v1Inside != l.ContainsPoint(l.vertices[1]) {
			l.originInside = true
		}
	}

	// We *must* call initBound before initializing the index, because
	// initBound calls ContainsPoint which does a bounds check before using
	// the index.
	l.initBound()

	// Create a new index and add us to it.
	l.index = NewShapeIndex()
	l.index.Add(l)
}
53442
func (q *Query) Order(orders ...string) *Query {
loop:
	for _, order := range orders {
		if order == "" {
			continue
		}
		ind := strings.Index(order, " ")
		if ind != -1 {
			field := order[:ind]
			sort := order[ind+1:]
			switch internal.UpperString(sort) {
			case "ASC", "DESC", "ASC NULLS FIRST", "DESC NULLS FIRST",
				"ASC NULLS LAST", "DESC NULLS LAST":
				q = q.OrderExpr("? ?", types.F(field), types.Q(sort))
				continue loop
			}
		}

		q.order = append(q.order, fieldAppender{order})
	}
	return q
}
523306
func inversionsCount(array, buffer []int) int {
	if len(array) <= 1 {
		return 0
	}
	cleft := inversionsCount(buffer[:len(array)/2], array[:len(array)/2])
	cright := inversionsCount(buffer[len(array)/2:], array[len(array)/2:])
	ccross := inversionsCombine(array[:len(array)/2], array[len(array)/2:], buffer)
	return cleft + ccross + cright
}
267361
func (l *Loop) Intersects(o *Loop) bool {
	// Given two loops, A and B, A.Intersects(B) if and only if !A.Complement().Contains(B).
	//
	// This code is similar to Contains, but is optimized for the case
	// where both loops enclose less than half of the sphere.
	if !l.bound.Intersects(o.bound) {
		return false
	}

	// Check whether there are any edge crossings, and also check the loop
	// relationship at any shared vertices.
	relation := &intersectsRelation{}
	if hasCrossingRelation(l, o, relation) {
		return true
	}
	if relation.foundSharedVertex {
		return false
	}

	// Since there are no edge intersections or shared vertices, the loops
	// intersect only if A contains B, B contains A, or the two loops contain
	// each other's boundaries.  These checks are usually cheap because of the
	// bounding box preconditions.  Note that neither loop is empty (because of
	// the bounding box check above), so it is safe to access vertex(0).

	// Check whether A contains B, or A and B contain each other's boundaries.
	// (Note that A contains all the vertices of B in either case.)
	if l.subregionBound.Contains(o.bound) || l.bound.Union(o.bound).IsFull() {
		if l.ContainsPoint(o.Vertex(0)) {
			return true
		}
	}
	// Check whether B contains A.
	if o.subregionBound.Contains(l.bound) {
		if o.ContainsPoint(l.Vertex(0)) {
			return true
		}
	}
	return false
}
535124
func (p *Polygon) findLoopNestingError() error {
	// First check that the loop depths make sense.
	lastDepth := -1
	for i, l := range p.loops {
		depth := l.depth
		if depth < 0 || depth > lastDepth+1 {
			return fmt.Errorf("loop %d: invalid loop depth (%d)", i, depth)
		}
		lastDepth = depth
	}
	// Then check that they correspond to the actual loop nesting.  This test
	// is quadratic in the number of loops but the cost per iteration is small.
	for i, l := range p.loops {
		last := p.LastDescendant(i)
		for j, l2 := range p.loops {
			if i == j {
				continue
			}
			nested := (j >= i+1) && (j <= last)
			const reverseB = false

			if l.containsNonCrossingBoundary(l2, reverseB) != nested {
				nestedStr := ""
				if !nested {
					nestedStr = "not "
				}
				return fmt.Errorf("invalid nesting: loop %d should %scontain loop %d", i, nestedStr, j)
			}
		}
	}
	return nil
}
441091
func (bg *bg_BG) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
	return locales.PluralRuleOther
}
1194261
func NewOrdering(dimensionsOrder []string) Ordering {
	dimensionOrderMap := make(map[string]int)
	for index, strVal := range dimensionsOrder {
		dimensionOrderMap[strVal] = index
	}
	return &orderedOrdering{
		dimensionOrderMap: dimensionOrderMap,
	}
}
254890
func Cycle(s T, length int) T {
	rv := reflectSlice(s)
	if rv.Len() >= length {
		return rv.Slice(0, length).Interface()
	}

	if rv.Len() == 0 {
		panic("empty slice")
	}

	// Allocate a new slice of the appropriate length.
	out := reflect.MakeSlice(rv.Type(), length, length)

	// Copy elements to out.
	for pos := 0; pos < length; {
		pos += reflect.Copy(out.Slice(pos, length), rv)
	}

	return out.Interface()
}
107321
func (def *Synthdef) topsort(root *Ugen) []*Ugen {
	stack := newStack()

	def.topsortr(root, stack, 0)

	var (
		i     = 0
		n     = stack.Size()
		ugens = make([]*Ugen, n)
	)
	for v := stack.Pop(); v != nil; v = stack.Pop() {
		ugens[i] = v.(*Ugen)
		i = i + 1
	}
	return ugens
}
870325
func EulerUndirected(g Iterator) (walk []int, ok bool) {
	n := g.Order()
	out := make([]int, n) // outdegree for each vertex
	edgeCount := 0
	for v := range out {
		g.Visit(v, func(w int, _ int64) (skip bool) {
			edgeCount++
			if v != w {
				out[v]++
			}
			return
		})
	}
	if edgeCount == 0 {
		return []int{}, true
	}

	start, oddDeg := -1, 0
	for v := range out {
		if out[v]&1 == 1 {
			start = v
			oddDeg++
		}
	}
	if !(oddDeg == 0 || oddDeg == 2) {
		return []int{}, false
	}

	// Find a starting point with neighbors.
	if start == -1 {
		for v := 0; v < n; v++ {
			if g.Visit(v, func(w int, _ int64) (skip bool) {
				start = w
				return true
			}) {
				break
			}
		}
	}

	h := Copy(g)
	for stack := []int{start}; len(stack) > 0; {
		n := len(stack)
		v := stack[n-1]
		stack = stack[:n-1]
		for h.Degree(v) > 0 {
			stack = append(stack, v)
			var w int
			h.Visit(v, func(u int, _ int64) (skip bool) {
				w = u
				return true
			})
			h.DeleteBoth(v, w)
			edgeCount--
			if v != w {
				edgeCount--
			}
			v = w
		}
		walk = append(walk, v)
	}
	if edgeCount > 0 {
		return []int{}, false
	}
	return walk, true
}
693766
func singleEdgeLoop(a, b Point) *Loop {
	vertices := []Point{a, b, Point{a.Add(b.Vector).Normalize()}}
	loop := LoopFromPoints(vertices)
	// The resulting loop may be clockwise, so invert it if necessary.
	loop.Normalize()
	return loop
}