// geofilt - The distance filter http://wiki.apache.org/solr/SpatialSearch
// Output example: fq={!geofilt pt=45.15,-93.85 sfield=store d=5}
496104
func (q *Query) Geofilt(latitude, longitude float64, sfield string, distance float64) {
	q.params.Add("fq", fmt.Sprintf("{!geofilt pt=%#v,%#v sfield=%s d=%#v}", latitude, longitude, sfield, distance))
}
686259
func (m *resolversMap) Range(f func(key string, value resolver.Resolver) bool) {
	m.data.Range(func(k, v interface{}) bool {
		key, ok := k.(string)
		if !ok {
			return false
		}
		value, ok := v.(resolver.Resolver)
		if !ok {
			return false
		}
		return f(key, value)
	})
}
286347
func (q *QueryFeedback) DecodeToRanges(isIndex bool) ([]*ranger.Range, error) {
	ranges := make([]*ranger.Range, 0, len(q.Feedback))
	for _, val := range q.Feedback {
		low, high := *val.Lower, *val.Upper
		var lowVal, highVal []types.Datum
		if isIndex {
			var err error
			// As we do not know the origin length, just use a custom value here.
			lowVal, err = codec.DecodeRange(low.GetBytes(), 4)
			if err != nil {
				return nil, errors.Trace(err)
			}
			highVal, err = codec.DecodeRange(high.GetBytes(), 4)
			if err != nil {
				return nil, errors.Trace(err)
			}
		} else {
			_, lowInt, err := codec.DecodeInt(val.Lower.GetBytes())
			if err != nil {
				return nil, errors.Trace(err)
			}
			_, highInt, err := codec.DecodeInt(val.Upper.GetBytes())
			if err != nil {
				return nil, errors.Trace(err)
			}
			lowVal = []types.Datum{types.NewIntDatum(lowInt)}
			highVal = []types.Datum{types.NewIntDatum(highInt)}
		}
		ranges = append(ranges, &(ranger.Range{
			LowVal:      lowVal,
			HighVal:     highVal,
			HighExclude: true,
		}))
	}
	return ranges, nil
}
946502
func (a Aggregations) GeoDistance(name string) (*AggregationBucketRangeItems, bool) {
	if raw, found := a[name]; found {
		agg := new(AggregationBucketRangeItems)
		if raw == nil {
			return agg, true
		}
		if err := json.Unmarshal(raw, agg); err == nil {
			return agg, true
		}
	}
	return nil, false
}
280543
func (p *Pattern) geoSineWaves() {
	period := utils.Map(utils.HexVal(p.Hash, 0, 1), 0, 15, 100, 400)
	amplitude := utils.Map(utils.HexVal(p.Hash, 1, 1), 0, 15, 30, 100)
	waveWidth := utils.Map(utils.HexVal(p.Hash, 2, 1), 0, 15, 3, 30)

	p.Svg.SetHeight(int(waveWidth * 36))
	p.Svg.SetWidth(int(period))

	for i := 0; i <= 35; i++ {
		val := utils.HexVal(p.Hash, i, 1)
		opacity := utils.Opacity(val)
		fill := utils.FillColor(val)
		xOffset := (period / 4) * 0.7

		styles := make(map[string]interface{})
		styles["fill"] = "none"
		styles["stroke"] = fill
		styles["style"] = map[string]string{"opacity": fmt.Sprintf("%v", opacity), "stroke-width": fmt.Sprintf("%vpx", waveWidth)}

		str := fmt.Sprintf("M0 %v C %v 0, %v 0, %v %v S %v %v, %v %v S %v 0, %v, %v", amplitude, xOffset, period/2-xOffset, period/2, amplitude, period-xOffset, amplitude*2, period, amplitude, period*1.5-xOffset, period*1.5, amplitude)

		style := make(map[string]interface{})

		style["transform"] = fmt.Sprintf("translate(-%v, %v)", period/4, (waveWidth*float64(i))-(amplitude*1.5))
		p.Svg.Path(str, utils.Merge(styles, style))

		style["transform"] = fmt.Sprintf("translate(-%v, %v)", period/4, (waveWidth*float64(i))-(amplitude*1.5)+waveWidth*36)
		p.Svg.Path(str, utils.Merge(styles, style))
	}
}
397884
func (c xlsxCalcChainCollection) Filter(fn func(v xlsxCalcChainC) bool) []xlsxCalcChainC {
	results := make([]xlsxCalcChainC, 0)
	for _, v := range c {
		if fn(v) {
			results = append(results, v)
		}
	}
	return results
}
443419
func distanceFromSegmentSquared(a, b, point []float64) float64 {
	x := a[0]
	y := a[1]
	dx := b[0] - x
	dy := b[1] - y

	if dx != 0 || dy != 0 {
		t := ((point[0]-x)*dx + (point[1]-y)*dy) / (dx*dx + dy*dy)

		if t > 1 {
			x = b[0]
			y = b[1]
		} else if t > 0 {
			x += dx * t
			y += dy * t
		}
	}

	dx = point[0] - x
	dy = point[1] - y

	return dx*dx + dy*dy
}
1205651
func (a Aggregations) GeoBounds(name string) (*AggregationGeoBoundsMetric, bool) {
	if raw, found := a[name]; found {
		agg := new(AggregationGeoBoundsMetric)
		if raw == nil {
			return agg, true
		}
		if err := json.Unmarshal(*raw, agg); err == nil {
			return agg, true
		}
	}
	return nil, false
}
276090
func (q *Query) AddFilter(f Filter) *Query {
	if q.Filters == nil {
		q.Filters = make([]Filter, 0)
	}
	q.Filters = append(q.Filters, f)
	return q
}
623311
func (q *GeoBoundingBoxQuery) Field(field string) *GeoBoundingBoxQuery {
	q.options["field"] = field
	return q
}
799342
func (q *GeoDistanceQuery) Boost(boost float32) *GeoDistanceQuery {
	q.options["boost"] = boost
	return q
}
1259332
func (rs *Results) Filter(fn func(Result) bool) *Results {
	results := make(Results, 0)
	for _, result := range *rs {
		if fn(result) {
			results = append(results, result)
		}
	}
	return &results
}
488043
func (l *Line) DistanceFrom(point *Point) float64 {
	// yes duplicate code, but saw a 15% performance increase by removing the function call
	// return math.Sqrt(l.SquaredDistanceFrom(point))
	x := l.a[0]
	y := l.a[1]
	dx := l.b[0] - x
	dy := l.b[1] - y

	if dx != 0 || dy != 0 {
		t := ((point[0]-x)*dx + (point[1]-y)*dy) / (dx*dx + dy*dy)

		if t > 1 {
			x = l.b[0]
			y = l.b[1]
		} else if t > 0 {
			x += dx * t
			y += dy * t
		}
	}

	dx = point[0] - x
	dy = point[1] - y

	return math.Sqrt(dx*dx + dy*dy)
}
1597
func (p *Path) DistanceFrom(point *Point) float64 {
	return math.Sqrt(p.SquaredDistanceFrom(point))
}
442569
func (s GeoDistanceSort) GeoHashes(geohashes ...string) GeoDistanceSort {
	s.geohashes = append(s.geohashes, geohashes...)
	return s
}
397958
func DistanceFromSegment(a, b, point orb.Point) float64 {
	return math.Sqrt(DistanceFromSegmentSquared(a, b, point))
}
1107374
func (x Vector) Filter(f func(float64) bool) Vector {
	y := make(Vector, 0, len(x))

	for _, v := range x {
		if !f(v) {
			y = append(y, v)
		}
	}

	return y
}