// the first 21 positions are the percentiles by 5's from 0 to 100
// the 22nd position is the bonus 99th percentile
// this makes looping over the values easier
551894
func pausePercentiles(pauses []uint64, ngc uint32) []uint64 {
	if ngc < uint32(len(pauses)) {
		pauses = pauses[:ngc]
	}

	sort.Sort(uint64slice(pauses))

	pctls := make([]uint64, 22)

	// Take care of 0th and 100th specially
	pctls[0] = pauses[0]
	pctls[20] = pauses[len(pauses)-1]

	// 5th - 95th
	for i := 1; i < 20; i++ {
		idx := len(pauses) * i / 20
		pctls[i] = pauses[idx]
	}

	// Add 99th
	idx := len(pauses) * 99 / 100
	pctls[21] = pauses[idx]

	return pctls
}
702179
func CalcBudget(totalSize int64, firstTierSize int64, o *MergePlanOptions) (
	budgetNumSegments int) {
	tierSize := firstTierSize
	if tierSize < 1 {
		tierSize = 1
	}

	maxSegmentsPerTier := o.MaxSegmentsPerTier
	if maxSegmentsPerTier < 1 {
		maxSegmentsPerTier = 1
	}

	tierGrowth := o.TierGrowth
	if tierGrowth < 1.0 {
		tierGrowth = 1.0
	}

	for totalSize > 0 {
		segmentsInTier := float64(totalSize) / float64(tierSize)
		if segmentsInTier < float64(maxSegmentsPerTier) {
			budgetNumSegments += int(math.Ceil(segmentsInTier))
			break
		}

		budgetNumSegments += maxSegmentsPerTier
		totalSize -= int64(maxSegmentsPerTier) * tierSize
		tierSize = int64(float64(tierSize) * tierGrowth)
	}

	return budgetNumSegments
}
420927
func (h Histogram) AsPercentiles() []Percentile {
	if h.Count == 0 {
		return nil
	}
	var percentiles []Percentile
	for _, bucket := range h.Buckets {
		if bucket.Count == 0 {
			continue
		}
		if bucket.Count == h.Count || math.IsInf(bucket.UpperBound, 0) {
			percentiles = append(percentiles, Percentile{
				Percentile: 100,
				Value:      time.Duration(bucket.UpperBound * float64(time.Second)),
			})
			return percentiles
		}
		percentiles = append(percentiles, Percentile{
			Percentile: 100 * (float64(bucket.Count) / float64(h.Count)),
			Value:      time.Duration(bucket.UpperBound * float64(time.Second)),
		})
	}
	return percentiles
}
137403
func (d *Document) lineStartIndexes() []int {
	// TODO: Cache, because this is often reused.
	// (If it is used, it's often used many times.
	// And this has to be fast for editing big documents!)
	lc := d.LineCount()
	lengths := make([]int, lc)
	for i, l := range d.Lines() {
		lengths[i] = len(l)
	}

	// Calculate cumulative sums.
	indexes := make([]int, lc+1)
	indexes[0] = 0 // https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/prompt_toolkit/document.py#L189
	pos := 0
	for i, l := range lengths {
		pos += l + 1
		indexes[i+1] = pos
	}
	if lc > 1 {
		// Pop the last item. (This is not a new line.)
		indexes = indexes[:lc]
	}
	return indexes
}
928026
func (s *MultiTermvectorService) Positions(positions bool) *MultiTermvectorService {
	s.positions = &positions
	return s
}
348727
func percentile(orderedObservations []float64, l int, p float64) float64 {
	return orderedObservations[int(p*float64(l))]
}
1100661
func UsePercentiles(percentiles []float64) ReporterOption {
	return func(r *Reporter) error {
		if len(percentiles) == 0 {
			return fmt.Errorf("Must specify at least 1 percentile")
		}
		var err error
		r.percentiles = percentiles
		r.p, err = getPercentileNames(percentiles)
		return err
	}
}
212733
func (s Seq) Percentile(percent float64) (percentile float64) {
	l := s.Len()
	if l == 0 {
		return 0
	}

	if percent < 0 || percent > 1.0 {
		panic("percent out of range [0.0, 1.0)")
	}

	sorted := s.Sort()
	index := percent * float64(l)
	if index == float64(int64(index)) {
		i := f64i(index)
		ci := sorted.GetValue(i - 1)
		c := sorted.GetValue(i)
		percentile = (ci + c) / 2.0
	} else {
		i := f64i(index)
		percentile = sorted.GetValue(i)
	}

	return percentile
}
469148
func NewPosition(StartLine int, EndLine int, StartPos int, EndPos int) *Position {
	return &Position{
		StartLine: StartLine,
		EndLine:   EndLine,
		StartPos:  StartPos,
		EndPos:    EndPos,
	}
}
1155815
func (s *TermvectorsService) Positions(positions bool) *TermvectorsService {
	s.positions = &positions
	return s
}
370570
func parsePriorityStats(line string, ps *PriorityStats) error {
	var (
		value uint64
		err   error
	)
	switch {
	case strings.HasPrefix(line, "Unused:"):
		fields := strings.Fields(line)
		rawValue := fields[len(fields)-1]
		valueStr := strings.TrimSuffix(rawValue, "%")
		value, err = strconv.ParseUint(valueStr, 10, 64)
		if err != nil {
			return err
		}
		ps.UnusedPercent = value
	case strings.HasPrefix(line, "Metadata:"):
		fields := strings.Fields(line)
		rawValue := fields[len(fields)-1]
		valueStr := strings.TrimSuffix(rawValue, "%")
		value, err = strconv.ParseUint(valueStr, 10, 64)
		if err != nil {
			return err
		}
		ps.MetadataPercent = value
	}
	return nil
}
311630
func (sl *SkipList) ByPosition(position uint64) common.Comparator {
	n, _ := sl.searchByPosition(position+1, nil, nil)
	if n == nil {
		return nil
	}

	return n.entry
}
838599
func (s *MultiTermvectorItem) Positions(positions bool) *MultiTermvectorItem {
	s.positions = &positions
	return s
}
584942
func (a Aggregations) PercentilesBucket(name string) (*AggregationPipelinePercentilesMetric, bool) {
	if raw, found := a[name]; found {
		agg := new(AggregationPipelinePercentilesMetric)
		if raw == nil {
			return agg, true
		}
		if err := json.Unmarshal(raw, agg); err == nil {
			return agg, true
		}
	}
	return nil, false
}
560891
func (t *TimerSnapshot) Percentiles(ps []float64) []float64 {
	return t.histogram.Percentiles(ps)
}
1367606
func SixvalFloat64(data []float64, p int) (min, lq, med, avg, uq, max float64) {
	n := len(data)

	// Special cases 0 and 1
	if n == 0 {
		return
	}

	if n == 1 {
		min = data[0]
		lq = data[0]
		med = data[0]
		avg = data[0]
		uq = data[0]
		max = data[0]
		return
	}

	// First pass (min, max, coarse average)
	var sum float64
	min, max = math.MaxFloat64, -math.MaxFloat64
	for _, v := range data {
		if v < min {
			min = v
		}
		if v > max {
			max = v
		}
		sum += v
	}
	avg = sum / float64(n)

	// Second pass: Correct average
	var corr float64
	for _, v := range data {
		corr += v - avg
	}
	avg += corr / float64(n)

	// Median
	sort.Float64s(data)
	if n%2 == 1 {
		med = data[(n-1)/2]
	} else {
		med = (data[n/2] + data[n/2-1]) / 2
	}

	// Percentiles
	if p < 0 {
		p = 0
	}
	if p > 100 {
		p = 100
	}
	lq = percentilFloat64(data, p)
	uq = percentilFloat64(data, 100-p)
	return
}
283970
func PercentValue(value, sum float64) (s string) {
	value *= 100 / sum
	s = AbsoluteValue(value, sum) + "% "
	return
}