github.com/metaworking/channeld@v0.7.3/pkg/channeld/util.go (about)

     1  package channeld
     2  
     3  import (
     4  	"hash/maphash"
     5  	"net"
     6  	"strings"
     7  	"sync"
     8  
     9  	"github.com/metaworking/channeld/pkg/common"
    10  )
    11  
    12  type LocalhostAddr struct {
    13  	NetworkName string
    14  }
    15  
    16  func (addr *LocalhostAddr) Network() string {
    17  	return addr.NetworkName
    18  }
    19  
    20  func (addr *LocalhostAddr) Addr() string {
    21  	return "localhost"
    22  }
    23  
    24  func GetIP(addr net.Addr) string {
    25  	switch addr := addr.(type) {
    26  	case *net.UDPAddr:
    27  		return addr.IP.String()
    28  	case *net.TCPAddr:
    29  		return addr.IP.String()
    30  	}
    31  	return strings.Split(addr.String(), ":")[0]
    32  }
    33  
    34  func GetNextId(m *map[uint32]interface{}, start uint32, min uint32, max uint32) (uint32, bool) {
    35  	for i := min; i <= max; i++ {
    36  		if _, exists := (*m)[start]; !exists {
    37  			return start, true
    38  		}
    39  		if start < max {
    40  			start++
    41  		} else {
    42  			start = min
    43  		}
    44  	}
    45  
    46  	return 0, false
    47  }
    48  
    49  func GetNextIdSync(m *sync.Map, start common.ChannelId, min common.ChannelId, max common.ChannelId) (common.ChannelId, bool) {
    50  	for i := min; i <= max; i++ {
    51  		if _, exists := m.Load(start); !exists {
    52  			return start, true
    53  		}
    54  		if start < max {
    55  			start++
    56  		} else {
    57  			start = min
    58  		}
    59  	}
    60  
    61  	return 0, false
    62  }
    63  
    64  type UintId interface {
    65  	common.ChannelId | ConnectionId | EntityId | uint32
    66  }
    67  type MapRead[K comparable, V any] interface {
    68  	Load(key K) (value V, ok bool)
    69  }
    70  
    71  func GetNextIdTyped[K UintId, V any](m MapRead[K, V], start K, min K, max K) (K, bool) {
    72  	for i := min; i <= max; i++ {
    73  		if _, exists := m.Load(start); !exists {
    74  			return start, true
    75  		}
    76  		if start < max {
    77  			start++
    78  		} else {
    79  			start = min
    80  		}
    81  	}
    82  
    83  	return 0, false
    84  }
    85  
    86  func UintIdHasher[T UintId]() func(maphash.Seed, T) uint64 {
    87  	return func(_ maphash.Seed, id T) uint64 {
    88  		return uint64(id)
    89  	}
    90  }
    91  
    92  func HashString(s string) uint32 {
    93  	hash := uint32(17)
    94  	for c := range s {
    95  		hash = hash*31 + uint32(c)
    96  	}
    97  	return hash
    98  }
    99  
   100  func Pointer[K any](val K) *K {
   101  	return &val
   102  }
   103  
   104  // Returns a map of all the keys in thisMap that are not in otherMap
   105  func Difference[K comparable, V any](thisMap map[K]V, otherMap map[K]V) map[K]V {
   106  	result := make(map[K]V)
   107  	for k, v := range thisMap {
   108  		if _, exists := otherMap[k]; !exists {
   109  			result[k] = v
   110  		}
   111  	}
   112  	return result
   113  }
   114  
   115  func CopyArray[FROM UintId, TO UintId](arr []FROM) []TO {
   116  	result := make([]TO, len(arr))
   117  	for i, v := range arr {
   118  		result[i] = TO(v)
   119  	}
   120  	return result
   121  }