/*
 *
 * Copyright 2021 gRPC 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 resolver

import (
	
	
	
)

type addressMapEntry[ any] struct {
	addr  Address
	value 
}

// AddressMap is an AddressMapV2[any].  It will be deleted in an upcoming
// release of grpc-go.
//
// Deprecated: use the generic AddressMapV2 type instead.
type AddressMap = AddressMapV2[any]

// AddressMapV2 is a map of addresses to arbitrary values taking into account
// Attributes.  BalancerAttributes are ignored, as are Metadata and Type.
// Multiple accesses may not be performed concurrently.  Must be created via
// NewAddressMap; do not construct directly.
type AddressMapV2[ any] struct {
	// The underlying map is keyed by an Address with fields that we don't care
	// about being set to their zero values. The only fields that we care about
	// are `Addr`, `ServerName` and `Attributes`. Since we need to be able to
	// distinguish between addresses with same `Addr` and `ServerName`, but
	// different `Attributes`, we cannot store the `Attributes` in the map key.
	//
	// The comparison operation for structs work as follows:
	//  Struct values are comparable if all their fields are comparable. Two
	//  struct values are equal if their corresponding non-blank fields are equal.
	//
	// The value type of the map contains a slice of addresses which match the key
	// in their `Addr` and `ServerName` fields and contain the corresponding value
	// associated with them.
	m map[Address]addressMapEntryList[]
}

func ( *Address) Address {
	return Address{Addr: .Addr, ServerName: .ServerName}
}

type addressMapEntryList[ any] []*addressMapEntry[]

// NewAddressMap creates a new AddressMapV2[any].
//
// Deprecated: use the generic NewAddressMapV2 constructor instead.
func () *AddressMap {
	return NewAddressMapV2[any]()
}

// NewAddressMapV2 creates a new AddressMapV2.
func [ any]() *AddressMapV2[] {
	return &AddressMapV2[]{m: make(map[Address]addressMapEntryList[])}
}

// find returns the index of addr in the addressMapEntry slice, or -1 if not
// present.
func ( addressMapEntryList[]) ( Address) int {
	for ,  := range  {
		// Attributes are the only thing to match on here, since `Addr` and
		// `ServerName` are already equal.
		if .addr.Attributes.Equal(.Attributes) {
			return 
		}
	}
	return -1
}

// Get returns the value for the address in the map, if present.
func ( *AddressMapV2[]) ( Address) ( ,  bool) {
	 := toMapKey(&)
	 := .m[]
	if  := .find();  != -1 {
		return [].value, true
	}
	return , false
}

// Set updates or adds the value to the address in the map.
func ( *AddressMapV2[]) ( Address,  ) {
	 := toMapKey(&)
	 := .m[]
	if  := .find();  != -1 {
		[].value = 
		return
	}
	.m[] = append(, &addressMapEntry[]{addr: , value: })
}

// Delete removes addr from the map.
func ( *AddressMapV2[]) ( Address) {
	 := toMapKey(&)
	 := .m[]
	 := .find()
	if  == -1 {
		return
	}
	if len() == 1 {
		 = nil
	} else {
		copy([:], [+1:])
		 = [:len()-1]
	}
	.m[] = 
}

// Len returns the number of entries in the map.
func ( *AddressMapV2[]) () int {
	 := 0
	for ,  := range .m {
		 += len()
	}
	return 
}

// Keys returns a slice of all current map keys.
func ( *AddressMapV2[]) () []Address {
	 := make([]Address, 0, .Len())
	for ,  := range .m {
		for ,  := range  {
			 = append(, .addr)
		}
	}
	return 
}

// Values returns a slice of all current map values.
func ( *AddressMapV2[]) () [] {
	 := make([], 0, .Len())
	for ,  := range .m {
		for ,  := range  {
			 = append(, .value)
		}
	}
	return 
}

type endpointMapKey string

// EndpointMap is a map of endpoints to arbitrary values keyed on only the
// unordered set of address strings within an endpoint. This map is not thread
// safe, thus it is unsafe to access concurrently. Must be created via
// NewEndpointMap; do not construct directly.
type EndpointMap[ any] struct {
	endpoints map[endpointMapKey]endpointData[]
}

type endpointData[ any] struct {
	// decodedKey stores the original key to avoid decoding when iterating on
	// EndpointMap keys.
	decodedKey Endpoint
	value      
}

// NewEndpointMap creates a new EndpointMap.
func [ any]() *EndpointMap[] {
	return &EndpointMap[]{
		endpoints: make(map[endpointMapKey]endpointData[]),
	}
}

// encodeEndpoint returns a string that uniquely identifies the unordered set of
// addresses within an endpoint.
func ( Endpoint) endpointMapKey {
	 := make([]string, 0, len(.Addresses))
	// base64 encoding the address strings restricts the characters present
	// within the strings. This allows us to use a delimiter without the need of
	// escape characters.
	for ,  := range .Addresses {
		 = append(, base64.StdEncoding.EncodeToString([]byte(.Addr)))
	}
	sort.Strings()
	// " " should not appear in base64 encoded strings.
	return endpointMapKey(strings.Join(, " "))
}

// Get returns the value for the address in the map, if present.
func ( *EndpointMap[]) ( Endpoint) ( ,  bool) {
	,  := .endpoints[encodeEndpoint()]
	if  {
		return .value, true
	}
	return , false
}

// Set updates or adds the value to the address in the map.
func ( *EndpointMap[]) ( Endpoint,  ) {
	 := encodeEndpoint()
	.endpoints[] = endpointData[]{
		decodedKey: Endpoint{Addresses: .Addresses},
		value:      ,
	}
}

// Len returns the number of entries in the map.
func ( *EndpointMap[]) () int {
	return len(.endpoints)
}

// Keys returns a slice of all current map keys, as endpoints specifying the
// addresses present in the endpoint keys, in which uniqueness is determined by
// the unordered set of addresses. Thus, endpoint information returned is not
// the full endpoint data (drops duplicated addresses and attributes) but can be
// used for EndpointMap accesses.
func ( *EndpointMap[]) () []Endpoint {
	 := make([]Endpoint, 0, len(.endpoints))
	for ,  := range .endpoints {
		 = append(, .decodedKey)
	}
	return 
}

// Values returns a slice of all current map values.
func ( *EndpointMap[]) () [] {
	 := make([], 0, len(.endpoints))
	for ,  := range .endpoints {
		 = append(, .value)
	}
	return 
}

// Delete removes the specified endpoint from the map.
func ( *EndpointMap[]) ( Endpoint) {
	 := encodeEndpoint()
	delete(.endpoints, )
}