// 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

// 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 , 
}