// Copyright 2013 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package model

import (
	
	
	
)

// A LabelSet is a collection of LabelName and LabelValue pairs.  The LabelSet
// may be fully-qualified down to the point where it may resolve to a single
// Metric in the data store or not.  All operations that occur within the realm
// of a LabelSet can emit a vector of Metric entities to which the LabelSet may
// match.
type LabelSet map[LabelName]LabelValue

// Validate checks whether all names and values in the label set
// are valid.
func ( LabelSet) () error {
	for ,  := range  {
		if !.IsValid() {
			return fmt.Errorf("invalid name %q", )
		}
		if !.IsValid() {
			return fmt.Errorf("invalid value %q", )
		}
	}
	return nil
}

// Equal returns true iff both label sets have exactly the same key/value pairs.
func ( LabelSet) ( LabelSet) bool {
	if len() != len() {
		return false
	}
	for ,  := range  {
		,  := []
		if ! {
			return false
		}
		if  !=  {
			return false
		}
	}
	return true
}

// Before compares the metrics, using the following criteria:
//
// If m has fewer labels than o, it is before o. If it has more, it is not.
//
// If the number of labels is the same, the superset of all label names is
// sorted alphanumerically. The first differing label pair found in that order
// determines the outcome: If the label does not exist at all in m, then m is
// before o, and vice versa. Otherwise the label value is compared
// alphanumerically.
//
// If m and o are equal, the method returns false.
func ( LabelSet) ( LabelSet) bool {
	if len() < len() {
		return true
	}
	if len() > len() {
		return false
	}

	 := make(LabelNames, 0, len()+len())
	for  := range  {
		 = append(, )
	}
	for  := range  {
		 = append(, )
	}
	// It's probably not worth it to de-dup lns.
	sort.Sort()
	for ,  := range  {
		,  := []
		if ! {
			return true
		}
		,  := []
		if ! {
			return false
		}
		if  <  {
			return true
		}
		if  >  {
			return false
		}
	}
	return false
}

// Clone returns a copy of the label set.
func ( LabelSet) () LabelSet {
	 := make(LabelSet, len())
	for ,  := range  {
		[] = 
	}
	return 
}

// Merge is a helper function to non-destructively merge two label sets.
func ( LabelSet) ( LabelSet) LabelSet {
	 := make(LabelSet, len())

	for ,  := range  {
		[] = 
	}

	for ,  := range  {
		[] = 
	}

	return 
}

// Fingerprint returns the LabelSet's fingerprint.
func ( LabelSet) () Fingerprint {
	return labelSetToFingerprint()
}

// FastFingerprint returns the LabelSet's Fingerprint calculated by a faster hashing
// algorithm, which is, however, more susceptible to hash collisions.
func ( LabelSet) () Fingerprint {
	return labelSetToFastFingerprint()
}

// UnmarshalJSON implements the json.Unmarshaler interface.
func ( *LabelSet) ( []byte) error {
	var  map[LabelName]LabelValue
	if  := json.Unmarshal(, &);  != nil {
		return 
	}
	// encoding/json only unmarshals maps of the form map[string]T. It treats
	// LabelName as a string and does not call its UnmarshalJSON method.
	// Thus, we have to replicate the behavior here.
	for  := range  {
		if !.IsValid() {
			return fmt.Errorf("%q is not a valid label name", )
		}
	}
	* = LabelSet()
	return nil
}