// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Implements methods to filter samples from profiles.

package profile

import 

// FilterSamplesByName filters the samples in a profile and only keeps
// samples where at least one frame matches focus but none match ignore.
// Returns true is the corresponding regexp matched at least one sample.
func ( *Profile) (, ,  *regexp.Regexp) (, ,  bool) {
	 := make(map[uint64]bool)
	 := make(map[uint64]bool)
	for ,  := range .Location {
		if  != nil && .matchesName() {
			 = true
			[.ID] = false
		} else if  == nil || .matchesName() {
			 = true
			[.ID] = true
		}
		if  != nil && .matchesName() {
			 = true
			.Line = .unmatchedLines()
			if len(.Line) == 0 {
				[.ID] = true
			}
		}
	}

	 := make([]*Sample, 0, len(.Sample))
	for ,  := range .Sample {
		if focusedAndNotIgnored(.Location, ) {
			if len() > 0 {
				var  []*Location
				for ,  := range .Location {
					if ![.ID] {
						 = append(, )
					}
				}
				if len() == 0 {
					// Remove sample with no locations (by not adding it to s).
					continue
				}
				.Location = 
			}
			 = append(, )
		}
	}
	.Sample = 

	return
}

// matchesName reports whether the function name or file in the
// location matches the regular expression.
func ( *Location) ( *regexp.Regexp) bool {
	for ,  := range .Line {
		if  := .Function;  != nil {
			if .MatchString(.Name) {
				return true
			}
			if .MatchString(.Filename) {
				return true
			}
		}
	}
	return false
}

// unmatchedLines returns the lines in the location that do not match
// the regular expression.
func ( *Location) ( *regexp.Regexp) []Line {
	var  []Line
	for ,  := range .Line {
		if  := .Function;  != nil {
			if .MatchString(.Name) {
				continue
			}
			if .MatchString(.Filename) {
				continue
			}
		}
		 = append(, )
	}
	return 
}

// focusedAndNotIgnored looks up a slice of ids against a map of
// focused/ignored locations. The map only contains locations that are
// explicitly focused or ignored. Returns whether there is at least
// one focused location but no ignored locations.
func focusedAndNotIgnored( []*Location,  map[uint64]bool) bool {
	var  bool
	for ,  := range  {
		if ,  := [.ID];  {
			if  {
				// Found focused location. Must keep searching in case there
				// is an ignored one as well.
				 = true
			} else {
				// Found ignored location. Can return false right away.
				return false
			}
		}
	}
	return 
}

// TagMatch selects tags for filtering
type TagMatch func(key, val string, nval int64) bool

// FilterSamplesByTag removes all samples from the profile, except
// those that match focus and do not match the ignore regular
// expression.
func ( *Profile) (,  TagMatch) (,  bool) {
	 := make([]*Sample, 0, len(.Sample))
	for ,  := range .Sample {
		,  := focusedSample(, , )
		 =  || 
		 =  || 
		if  && ! {
			 = append(, )
		}
	}
	.Sample = 
	return
}

// focusedSample checks a sample against focus and ignore regexps.
// Returns whether the focus/ignore regexps match any tags.
func focusedSample( *Sample, ,  TagMatch) (,  bool) {
	 =  == nil
	for ,  := range .Label {
		for ,  := range  {
			if  != nil && (, , 0) {
				 = true
			}
			if ! && (, , 0) {
				 = true
			}
		}
	}
	for ,  := range .NumLabel {
		for ,  := range  {
			if  != nil && (, "", ) {
				 = true
			}
			if ! && (, "", ) {
				 = true
			}
		}
	}
	return , 
}