// Copyright 2016 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.

package pprof

import (
	
	
	
	
)

type label struct {
	key   string
	value string
}

// LabelSet is a set of labels.
type LabelSet struct {
	list []label
}

// labelContextKey is the type of contextKeys used for profiler labels.
type labelContextKey struct{}

func labelValue( context.Context) labelMap {
	,  := .Value(labelContextKey{}).(*labelMap)
	if  == nil {
		return labelMap{}
	}
	return *
}

// labelMap is the representation of the label set held in the context type.
// This is an initial implementation, but it will be replaced with something
// that admits incremental immutable modification more efficiently.
type labelMap struct {
	LabelSet
}

// String satisfies Stringer and returns key, value pairs in a consistent
// order.
func ( *labelMap) () string {
	if  == nil {
		return ""
	}
	 := make([]string, 0, len(.list))

	for ,  := range .list {
		 = append(, fmt.Sprintf("%q:%q", .key, .value))
	}

	slices.Sort()
	return "{" + strings.Join(, ", ") + "}"
}

// WithLabels returns a new [context.Context] with the given labels added.
// A label overwrites a prior label with the same key.
func ( context.Context,  LabelSet) context.Context {
	 := labelValue()
	return context.WithValue(, labelContextKey{}, &labelMap{mergeLabelSets(.LabelSet, )})
}

func mergeLabelSets(,  LabelSet) LabelSet {
	if len(.list) == 0 {
		return 
	} else if len(.list) == 0 {
		return 
	}

	,  := 0, 0
	 := make([]label, 0, len(.list))
	for  < len(.list) &&  < len(.list) {
		switch strings.Compare(.list[].key, .list[].key) {
		case -1: // left key < right key
			 = append(, .list[])
			++
		case 1: // right key < left key
			 = append(, .list[])
			++
		case 0: // keys are equal, right value overwrites left value
			 = append(, .list[])
			++
			++
		}
	}

	// Append the remaining elements
	 = append(, .list[:]...)
	 = append(, .list[:]...)

	return LabelSet{list: }
}

// Labels takes an even number of strings representing key-value pairs
// and makes a [LabelSet] containing them.
// A label overwrites a prior label with the same key.
// Currently only the CPU and goroutine profiles utilize any labels
// information.
// See https://golang.org/issue/23458 for details.
func ( ...string) LabelSet {
	if len()%2 != 0 {
		panic("uneven number of arguments to pprof.Labels")
	}
	 := make([]label, 0, len()/2)
	 := true
	for  := 0; +1 < len();  += 2 {
		 = append(, label{key: [], value: [+1]})
		 =  && ( < 2 || [] > [-2])
	}
	if ! {
		// slow path: keys are unsorted, contain duplicates, or both
		slices.SortStableFunc(, func(,  label) int {
			return strings.Compare(.key, .key)
		})
		 := make([]label, 0, len())
		for ,  := range  {
			if  == 0 || .key != [-1].key {
				 = append(, )
			} else {
				[len()-1] = 
			}
		}
		 = 
	}
	return LabelSet{list: }
}

// Label returns the value of the label with the given key on ctx, and a boolean indicating
// whether that label exists.
func ( context.Context,  string) (string, bool) {
	 := labelValue()
	for ,  := range .list {
		if .key ==  {
			return .value, true
		}
	}
	return "", false
}

// ForLabels invokes f with each label set on the context.
// The function f should return true to continue iteration or false to stop iteration early.
func ( context.Context,  func(,  string) bool) {
	 := labelValue()
	for ,  := range .list {
		if !(.key, .value) {
			break
		}
	}
}