// Calculate returns the overall score of the article.
962516
func (s Scores) Calculate() int64 {
	return s.Score1 + int64(0.1*float64(s.Score2)) + int64(0.01*float64(s.Score3)) + int64(0.001*float64(s.Score4)) + int64(0.0001*float64(s.Score5))
}
1291914
func (s *ExponentialStrategy) Calculate(attempt int) time.Duration {
	backoffDuration := s.cfg.BackoffMultiplier *
		time.Duration(math.Pow(2, float64(attempt)))
	return backoffDuration
}
881146
func calculateCount(cMap map[string]int) int {
	count := 0
	for _, val := range cMap {
		if val > 0 {
			count++
		}
		if count == 5 {
			break
		}
	}
	return count
}
1075046
func Calculate(s1, s2 string) float64 {

	// Avoid returning NaN
	if utf8.RuneCountInString(s1) == 0 || utf8.RuneCountInString(s2) == 0 {
		return 0
	}

	s1, s2 = sort(strings.ToLower(s1), strings.ToLower(s2))

	// m as `matching characters`
	// t as `transposition`
	// l as `the length of common prefix at the start of the string up to a maximum of 4 characters`.
	// See more: https://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance
	var m, t, l float64

	window := window(s2)

	runes1 := bytes.Runes([]byte(s1))
	runes2 := bytes.Runes([]byte(s2))

	for i := 0; i < len(runes1); i++ {
		// Exact match
		if runes1[i] == runes2[i] {
			m++
			// Common prefix limiter
			if i == int(l) && i <= commonPrefixLimiter {
				l++
			}
		} else if strings.Contains(s2, string(runes1[i])) {

			// The character is also considered matching if the amount of characters between the occurances in s1 and s2
			// is less than match window
			c := closestIndex(runes2, runes1[i], i)

			gap := math.Abs(float64(c - i))

			//debug:
			//fmt.Println("searched rune", string(runes1[i]), "with starting index", i, "from word", string(runes2), "-- closest index was", c)
			//fmt.Println("string", s2, "contains", string(runes1[i]))

			if gap <= window {
				m++
				// Check if transposition is in reach of window
				for k := i; k < len(runes1); k++ {
					if closestIndex(runes2, runes1[k], i) <= i {
						t++
					}
				}
			}
		}
	}

	score := score(m, t, float64(len(runes1)), float64(len(runes2)))
	distance := score + (l * weight * (1 - score))

	if math.IsNaN(distance) {
		return 0
	}

	//debug:
	//fmt.Println("- score:", score)
	//fmt.Println("- transpositions:", t)
	//fmt.Println("- matches:", m)
	//fmt.Println("- window:", window)
	//fmt.Println("- l:", l)
	//fmt.Println("- distance:", distance)

	return distance
}
672987
func (r *Rule) score() int {
	score := 0
	// wilcard rules are less specific
	if utils.SliceContainsStr(r.Resources, Wildcard) {
		score -= 4
	} else if len(r.Resources) == 1 {
		// rules that match specific resource are more specific than
		// fields that match several resources
		score += 2
	}
	// rules that have wilcard verbs are less specific
	if utils.SliceContainsStr(r.Verbs, Wildcard) {
		score -= 2
	}
	// rules that supply 'where' or 'actions' are more specific
	// having 'where' or 'actions' is more important than
	// whether the rules are wildcard or not, so here we have +8 vs
	// -4 and -2 score penalty for wildcards in resources and verbs
	if len(r.Where) > 0 {
		score += 8
	}
	// rules featuring actions are more specific
	if len(r.Actions) > 0 {
		score += 8
	}
	return score
}
1148713
func (s *UpdateByQueryService) Stats(stats ...string) *UpdateByQueryService {
	s.stats = append(s.stats, stats...)
	return s
}
1238253
func Sum(xs []float64) float64 {
	sum := 0.0
	for _, x := range xs {
		sum += x
	}
	return sum
}
1183222
func (e *Store) calculateTTL(obj runtime.Object, defaultTTL int64, update bool) (ttl uint64, err error) {
	// TODO: validate this is assertion is still valid.

	// etcd may return a negative TTL for a node if the expiration has not
	// occurred due to server lag - we will ensure that the value is at least
	// set.
	if defaultTTL < 0 {
		defaultTTL = 1
	}
	ttl = uint64(defaultTTL)
	if e.TTLFunc != nil {
		ttl, err = e.TTLFunc(obj, ttl, update)
	}
	return ttl, err
}
1259635
func calculateF(b uint, epsilon float64) uint {
	f := uint(math.Ceil(math.Log(2 * float64(b) / epsilon)))
	f = f / 8
	if f <= 0 {
		f = 1
	}
	return f
}
1224734
func (bal Balance2) Rate(rate int8) Input {
	if bal.L == nil || bal.R == nil {
		panic("Balance2 expects L and R to not be nil")
	}
	CheckRate(rate)
	(&bal).defaults()
	return NewInput("Balance2", rate, 0, 2, bal.L, bal.R, bal.Pos, bal.Level)
}
586876
func (s Scores) Validate() error {
	if s.ArticleID == 0 {
		return NewValidationError(errors.New("Articls sxtract has no articls id"))
	}

	return nil
}
726600
func (p *polynomial) evaluate(x uint8) uint8 {
	// Special case the origin
	if x == 0 {
		return p.coefficients[0]
	}

	// Compute the polynomial value using Horner's method.
	degree := len(p.coefficients) - 1
	out := p.coefficients[degree]
	for i := degree - 1; i >= 0; i-- {
		coeff := p.coefficients[i]
		out = add(mult(out, x), coeff)
	}
	return out
}
916975
func (s *sumFloat64) Accumulate(v interface{}) (interface{}, error) {
	c := v.(*Cell)
	l := c.L
	if l == nil {
		return nil, fmt.Errorf("not a valid literal it cell %v", c)
	}
	iv, err := l.Float64()
	if err != nil {
		return s.state, err
	}
	s.state += iv
	return s.state, nil
}
477621
func (m *Metrics) Add(r *Result) {
	m.init()

	m.Requests++
	m.StatusCodes[strconv.Itoa(int(r.Code))]++
	m.BytesOut.Total += r.BytesOut
	m.BytesIn.Total += r.BytesIn

	m.Latencies.Add(r.Latency)

	if m.Earliest.IsZero() || m.Earliest.After(r.Timestamp) {
		m.Earliest = r.Timestamp
	}

	if r.Timestamp.After(m.Latest) {
		m.Latest = r.Timestamp
	}

	if end := r.End(); end.After(m.End) {
		m.End = end
	}

	if r.Code >= 200 && r.Code < 400 {
		m.success++
	}

	if r.Error != "" {
		if _, ok := m.errors[r.Error]; !ok {
			m.errors[r.Error] = struct{}{}
			m.Errors = append(m.Errors, r.Error)
		}
	}
}
241072
func (p *blockPrefetcher) calculatePriority(
	basePriority int, action BlockRequestAction) int {
	// A prefetched, non-deep-synced child always gets throttled for
	// now, until we fix the database performance issues.
	if basePriority > throttleRequestPriority && !action.DeepSync() {
		basePriority = throttleRequestPriority
	}
	return basePriority - 1
}
1018553
func (t Result) Float() float64 {
	switch t.Type {
	default:
		return 0
	case True:
		return 1
	case String:
		n, _ := strconv.ParseFloat(t.Str, 64)
		return n
	case Number:
		return t.Num
	}
}
854799
func (a *AllocMetric) ScoreNode(node *Node, name string, score float64) {
	// Create nodeScoreMeta lazily if its the first time or if its a new node
	if a.nodeScoreMeta == nil || a.nodeScoreMeta.NodeID != node.ID {
		a.nodeScoreMeta = &NodeScoreMeta{
			NodeID: node.ID,
			Scores: make(map[string]float64),
		}
	}
	if name == NormScorerName {
		a.nodeScoreMeta.NormScore = score
		// Once we have the normalized score we can push to the heap
		// that tracks top K by normalized score

		// Create the heap if its not there already
		if a.topScores == nil {
			a.topScores = kheap.NewScoreHeap(MaxRetainedNodeScores)
		}
		heap.Push(a.topScores, a.nodeScoreMeta)

		// Clear out this entry because its now in the heap
		a.nodeScoreMeta = nil
	} else {
		a.nodeScoreMeta.Scores[name] = score
	}
}