@@ -684,7 +684,7 @@ const (
684
684
// to generate hashes and check for equality of key values.
685
685
//
686
686
// It is implemented as an Hash Array Mapped Trie.
687
- type Map [K comparable , V any ] struct {
687
+ type Map [K , V any ] struct {
688
688
size int // total number of key/value pairs
689
689
root mapNode [K , V ] // root node of trie
690
690
hasher Hasher [K ] // hasher implementation
@@ -693,7 +693,7 @@ type Map[K comparable, V any] struct {
693
693
// NewMap returns a new instance of Map. If hasher is nil, a default hasher
694
694
// implementation will automatically be chosen based on the first key added.
695
695
// Default hasher implementations only exist for int, string, and byte slice types.
696
- func NewMap [K comparable , V any ](hasher Hasher [K ]) * Map [K , V ] {
696
+ func NewMap [K , V any ](hasher Hasher [K ]) * Map [K , V ] {
697
697
return & Map [K , V ]{
698
698
hasher : hasher ,
699
699
}
@@ -814,12 +814,12 @@ func (m *Map[K, V]) Iterator() *MapIterator[K, V] {
814
814
}
815
815
816
816
// MapBuilder represents an efficient builder for creating Maps.
817
- type MapBuilder [K comparable , V any ] struct {
817
+ type MapBuilder [K , V any ] struct {
818
818
m * Map [K , V ] // current state
819
819
}
820
820
821
821
// NewMapBuilder returns a new instance of MapBuilder.
822
- func NewMapBuilder [K comparable , V any ](hasher Hasher [K ]) * MapBuilder [K , V ] {
822
+ func NewMapBuilder [K , V any ](hasher Hasher [K ]) * MapBuilder [K , V ] {
823
823
return & MapBuilder [K , V ]{m : NewMap [K , V ](hasher )}
824
824
}
825
825
@@ -863,7 +863,7 @@ func (b *MapBuilder[K, V]) Iterator() *MapIterator[K, V] {
863
863
}
864
864
865
865
// mapNode represents any node in the map tree.
866
- type mapNode [K comparable , V any ] interface {
866
+ type mapNode [K , V any ] interface {
867
867
get (key K , shift uint , keyHash uint32 , h Hasher [K ]) (value V , ok bool )
868
868
set (key K , value V , shift uint , keyHash uint32 , h Hasher [K ], mutable bool , resized * bool ) mapNode [K , V ]
869
869
delete (key K , shift uint , keyHash uint32 , h Hasher [K ], mutable bool , resized * bool ) mapNode [K , V ]
@@ -876,7 +876,7 @@ var _ mapNode[string, any] = (*mapValueNode[string, any])(nil)
876
876
var _ mapNode [string , any ] = (* mapHashCollisionNode [string , any ])(nil )
877
877
878
878
// mapLeafNode represents a node that stores a single key hash at the leaf of the map tree.
879
- type mapLeafNode [K comparable , V any ] interface {
879
+ type mapLeafNode [K , V any ] interface {
880
880
mapNode [K , V ]
881
881
keyHashValue () uint32
882
882
}
@@ -887,7 +887,7 @@ var _ mapLeafNode[string, any] = (*mapHashCollisionNode[string, any])(nil)
887
887
// mapArrayNode is a map node that stores key/value pairs in a slice.
888
888
// Entries are stored in insertion order. An array node expands into a bitmap
889
889
// indexed node once a given threshold size is crossed.
890
- type mapArrayNode [K comparable , V any ] struct {
890
+ type mapArrayNode [K , V any ] struct {
891
891
entries []mapEntry [K , V ]
892
892
}
893
893
@@ -989,7 +989,7 @@ func (n *mapArrayNode[K, V]) delete(key K, shift uint, keyHash uint32, h Hasher[
989
989
// mapBitmapIndexedNode represents a map branch node with a variable number of
990
990
// node slots and indexed using a bitmap. Indexes for the node slots are
991
991
// calculated by counting the number of set bits before the target bit using popcount.
992
- type mapBitmapIndexedNode [K comparable , V any ] struct {
992
+ type mapBitmapIndexedNode [K , V any ] struct {
993
993
bitmap uint32
994
994
nodes []mapNode [K , V ]
995
995
}
@@ -1028,7 +1028,7 @@ func (n *mapBitmapIndexedNode[K, V]) set(key K, value V, shift uint, keyHash uin
1028
1028
if exists {
1029
1029
newNode = n .nodes [idx ].set (key , value , shift + mapNodeBits , keyHash , h , mutable , resized )
1030
1030
} else {
1031
- newNode = newMapValueNode [ K , V ] (keyHash , key , value )
1031
+ newNode = newMapValueNode (keyHash , key , value )
1032
1032
}
1033
1033
1034
1034
// Convert to a hash-array node once we exceed the max bitmap size.
@@ -1135,7 +1135,7 @@ func (n *mapBitmapIndexedNode[K, V]) delete(key K, shift uint, keyHash uint32, h
1135
1135
1136
1136
// mapHashArrayNode is a map branch node that stores nodes in a fixed length
1137
1137
// array. Child nodes are indexed by their index bit segment for the current depth.
1138
- type mapHashArrayNode [K comparable , V any ] struct {
1138
+ type mapHashArrayNode [K , V any ] struct {
1139
1139
count uint // number of set nodes
1140
1140
nodes [mapNodeSize ]mapNode [K , V ] // child node slots, may contain empties
1141
1141
}
@@ -1231,14 +1231,14 @@ func (n *mapHashArrayNode[K, V]) delete(key K, shift uint, keyHash uint32, h Has
1231
1231
// mapValueNode represents a leaf node with a single key/value pair.
1232
1232
// A value node can be converted to a hash collision leaf node if a different
1233
1233
// key with the same keyHash is inserted.
1234
- type mapValueNode [K comparable , V any ] struct {
1234
+ type mapValueNode [K , V any ] struct {
1235
1235
keyHash uint32
1236
1236
key K
1237
1237
value V
1238
1238
}
1239
1239
1240
1240
// newMapValueNode returns a new instance of mapValueNode.
1241
- func newMapValueNode [K comparable , V any ](keyHash uint32 , key K , value V ) * mapValueNode [K , V ] {
1241
+ func newMapValueNode [K , V any ](keyHash uint32 , key K , value V ) * mapValueNode [K , V ] {
1242
1242
return & mapValueNode [K , V ]{
1243
1243
keyHash : keyHash ,
1244
1244
key : key ,
@@ -1303,7 +1303,7 @@ func (n *mapValueNode[K, V]) delete(key K, shift uint, keyHash uint32, h Hasher[
1303
1303
1304
1304
// mapHashCollisionNode represents a leaf node that contains two or more key/value
1305
1305
// pairs with the same key hash. Single pairs for a hash are stored as value nodes.
1306
- type mapHashCollisionNode [K comparable , V any ] struct {
1306
+ type mapHashCollisionNode [K , V any ] struct {
1307
1307
keyHash uint32 // key hash for all entries
1308
1308
entries []mapEntry [K , V ]
1309
1309
}
@@ -1409,7 +1409,7 @@ func (n *mapHashCollisionNode[K, V]) delete(key K, shift uint, keyHash uint32, h
1409
1409
1410
1410
// mergeIntoNode merges a key/value pair into an existing node.
1411
1411
// Caller must verify that node's keyHash is not equal to keyHash.
1412
- func mergeIntoNode [K comparable , V any ](node mapLeafNode [K , V ], shift uint , keyHash uint32 , key K , value V ) mapNode [K , V ] {
1412
+ func mergeIntoNode [K , V any ](node mapLeafNode [K , V ], shift uint , keyHash uint32 , key K , value V ) mapNode [K , V ] {
1413
1413
idx1 := (node .keyHashValue () >> shift ) & mapNodeMask
1414
1414
idx2 := (keyHash >> shift ) & mapNodeMask
1415
1415
@@ -1428,14 +1428,14 @@ func mergeIntoNode[K comparable, V any](node mapLeafNode[K, V], shift uint, keyH
1428
1428
}
1429
1429
1430
1430
// mapEntry represents a single key/value pair.
1431
- type mapEntry [K comparable , V any ] struct {
1431
+ type mapEntry [K , V any ] struct {
1432
1432
key K
1433
1433
value V
1434
1434
}
1435
1435
1436
1436
// MapIterator represents an iterator over a map's key/value pairs. Although
1437
1437
// map keys are not sorted, the iterator's order is deterministic.
1438
- type MapIterator [K comparable , V any ] struct {
1438
+ type MapIterator [K , V any ] struct {
1439
1439
m * Map [K , V ] // source map
1440
1440
1441
1441
stack [32 ]mapIteratorElem [K , V ] // search stack
@@ -1559,7 +1559,7 @@ func (itr *MapIterator[K, V]) first() {
1559
1559
}
1560
1560
1561
1561
// mapIteratorElem represents a node/index pair in the MapIterator stack.
1562
- type mapIteratorElem [K comparable , V any ] struct {
1562
+ type mapIteratorElem [K , V any ] struct {
1563
1563
node mapNode [K , V ]
1564
1564
index int
1565
1565
}
@@ -1573,7 +1573,7 @@ const (
1573
1573
// is determined by the Comparer used by the map.
1574
1574
//
1575
1575
// This map is implemented as a B+tree.
1576
- type SortedMap [K comparable , V any ] struct {
1576
+ type SortedMap [K , V any ] struct {
1577
1577
size int // total number of key/value pairs
1578
1578
root sortedMapNode [K , V ] // root of b+tree
1579
1579
comparer Comparer [K ]
@@ -1582,7 +1582,7 @@ type SortedMap[K comparable, V any] struct {
1582
1582
// NewSortedMap returns a new instance of SortedMap. If comparer is nil then
1583
1583
// a default comparer is set after the first key is inserted. Default comparers
1584
1584
// exist for int, string, and byte slice keys.
1585
- func NewSortedMap [K comparable , V any ](comparer Comparer [K ]) * SortedMap [K , V ] {
1585
+ func NewSortedMap [K , V any ](comparer Comparer [K ]) * SortedMap [K , V ] {
1586
1586
return & SortedMap [K , V ]{
1587
1587
comparer : comparer ,
1588
1588
}
@@ -1705,12 +1705,12 @@ func (m *SortedMap[K, V]) Iterator() *SortedMapIterator[K, V] {
1705
1705
}
1706
1706
1707
1707
// SortedMapBuilder represents an efficient builder for creating sorted maps.
1708
- type SortedMapBuilder [K comparable , V any ] struct {
1708
+ type SortedMapBuilder [K , V any ] struct {
1709
1709
m * SortedMap [K , V ] // current state
1710
1710
}
1711
1711
1712
1712
// NewSortedMapBuilder returns a new instance of SortedMapBuilder.
1713
- func NewSortedMapBuilder [K comparable , V any ](comparer Comparer [K ]) * SortedMapBuilder [K , V ] {
1713
+ func NewSortedMapBuilder [K , V any ](comparer Comparer [K ]) * SortedMapBuilder [K , V ] {
1714
1714
return & SortedMapBuilder [K , V ]{m : NewSortedMap [K , V ](comparer )}
1715
1715
}
1716
1716
@@ -1754,7 +1754,7 @@ func (b *SortedMapBuilder[K, V]) Iterator() *SortedMapIterator[K, V] {
1754
1754
}
1755
1755
1756
1756
// sortedMapNode represents a branch or leaf node in the sorted map.
1757
- type sortedMapNode [K comparable , V any ] interface {
1757
+ type sortedMapNode [K , V any ] interface {
1758
1758
minKey () K
1759
1759
indexOf (key K , c Comparer [K ]) int
1760
1760
get (key K , c Comparer [K ]) (value V , ok bool )
@@ -1766,12 +1766,12 @@ var _ sortedMapNode[string, any] = (*sortedMapBranchNode[string, any])(nil)
1766
1766
var _ sortedMapNode [string , any ] = (* sortedMapLeafNode [string , any ])(nil )
1767
1767
1768
1768
// sortedMapBranchNode represents a branch in the sorted map.
1769
- type sortedMapBranchNode [K comparable , V any ] struct {
1769
+ type sortedMapBranchNode [K , V any ] struct {
1770
1770
elems []sortedMapBranchElem [K , V ]
1771
1771
}
1772
1772
1773
1773
// newSortedMapBranchNode returns a new branch node with the given child nodes.
1774
- func newSortedMapBranchNode [K comparable , V any ](children ... sortedMapNode [K , V ]) * sortedMapBranchNode [K , V ] {
1774
+ func newSortedMapBranchNode [K , V any ](children ... sortedMapNode [K , V ]) * sortedMapBranchNode [K , V ] {
1775
1775
// Fetch min keys for every child.
1776
1776
elems := make ([]sortedMapBranchElem [K , V ], len (children ))
1777
1777
for i , child := range children {
@@ -1914,13 +1914,13 @@ func (n *sortedMapBranchNode[K, V]) delete(key K, c Comparer[K], mutable bool, r
1914
1914
return other
1915
1915
}
1916
1916
1917
- type sortedMapBranchElem [K comparable , V any ] struct {
1917
+ type sortedMapBranchElem [K , V any ] struct {
1918
1918
key K
1919
1919
node sortedMapNode [K , V ]
1920
1920
}
1921
1921
1922
1922
// sortedMapLeafNode represents a leaf node in the sorted map.
1923
- type sortedMapLeafNode [K comparable , V any ] struct {
1923
+ type sortedMapLeafNode [K , V any ] struct {
1924
1924
entries []mapEntry [K , V ]
1925
1925
}
1926
1926
@@ -2035,7 +2035,7 @@ func (n *sortedMapLeafNode[K, V]) delete(key K, c Comparer[K], mutable bool, res
2035
2035
2036
2036
// SortedMapIterator represents an iterator over a sorted map.
2037
2037
// Iteration can occur in natural or reverse order based on use of Next() or Prev().
2038
- type SortedMapIterator [K comparable , V any ] struct {
2038
+ type SortedMapIterator [K , V any ] struct {
2039
2039
m * SortedMap [K , V ] // source map
2040
2040
2041
2041
stack [32 ]sortedMapIteratorElem [K , V ] // search stack
@@ -2223,13 +2223,13 @@ func (itr *SortedMapIterator[K, V]) seek(key K) {
2223
2223
}
2224
2224
2225
2225
// sortedMapIteratorElem represents node/index pair in the SortedMapIterator stack.
2226
- type sortedMapIteratorElem [K comparable , V any ] struct {
2226
+ type sortedMapIteratorElem [K , V any ] struct {
2227
2227
node sortedMapNode [K , V ]
2228
2228
index int
2229
2229
}
2230
2230
2231
2231
// Hasher hashes keys and checks them for equality.
2232
- type Hasher [K comparable ] interface {
2232
+ type Hasher [K any ] interface {
2233
2233
// Computes a hash for key.
2234
2234
Hash (key K ) uint32
2235
2235
@@ -2238,7 +2238,7 @@ type Hasher[K comparable] interface {
2238
2238
}
2239
2239
2240
2240
// NewHasher returns the built-in hasher for a given key type.
2241
- func NewHasher [K comparable ](key K ) Hasher [K ] {
2241
+ func NewHasher [K any ](key K ) Hasher [K ] {
2242
2242
// Attempt to use non-reflection based hasher first.
2243
2243
switch (any (key )).(type ) {
2244
2244
case int , int8 , int16 , int32 , int64 , uint , uint8 , uint16 , uint32 , uint64 , uintptr , string :
@@ -2267,7 +2267,7 @@ func hashString(value string) uint32 {
2267
2267
}
2268
2268
2269
2269
// reflectIntHasher implements a reflection-based Hasher for keys.
2270
- type reflectHasher [K comparable ] struct {}
2270
+ type reflectHasher [K any ] struct {}
2271
2271
2272
2272
// Hash returns a hash for key.
2273
2273
func (h * reflectHasher [K ]) Hash (key K ) uint32 {
@@ -2313,11 +2313,10 @@ func hashUint64(value uint64) uint32 {
2313
2313
}
2314
2314
2315
2315
// defaultHasher implements Hasher.
2316
- type defaultHasher [K comparable ] struct {}
2316
+ type defaultHasher [K any ] struct {}
2317
2317
2318
2318
// Hash returns a hash for key.
2319
2319
func (h * defaultHasher [K ]) Hash (key K ) uint32 {
2320
- // Attempt to use non-reflection based hasher first.
2321
2320
switch x := (any (key )).(type ) {
2322
2321
case int :
2323
2322
return hashUint64 (uint64 (x ))
@@ -2348,13 +2347,13 @@ func (h *defaultHasher[K]) Hash(key K) uint32 {
2348
2347
}
2349
2348
2350
2349
// Equal returns true if a is equal to b. Otherwise returns false.
2351
- // Panics if a and b are not ints .
2350
+ // Panics if a and b are not comparable .
2352
2351
func (h * defaultHasher [K ]) Equal (a , b K ) bool {
2353
- return a == b
2352
+ return any ( a ) == any ( b )
2354
2353
}
2355
2354
2356
2355
// Comparer allows the comparison of two keys for the purpose of sorting.
2357
- type Comparer [K comparable ] interface {
2356
+ type Comparer [K any ] interface {
2358
2357
// Returns -1 if a is less than b, returns 1 if a is greater than b,
2359
2358
// and returns 0 if a is equal to b.
2360
2359
Compare (a , b K ) int
@@ -2363,7 +2362,7 @@ type Comparer[K comparable] interface {
2363
2362
// NewComparer returns the built-in comparer for a given key type.
2364
2363
// Note that only int-ish and string-ish types are supported, despite the 'comparable' constraint.
2365
2364
// Attempts to use other types will result in a panic - users should define their own Comparers for these cases.
2366
- func NewComparer [K comparable ](key K ) Comparer [K ] {
2365
+ func NewComparer [K any ](key K ) Comparer [K ] {
2367
2366
// Attempt to use non-reflection based comparer first.
2368
2367
switch (any (key )).(type ) {
2369
2368
case int , int8 , int16 , int32 , int64 , uint , uint8 , uint16 , uint32 , uint64 , uintptr , string :
@@ -2380,8 +2379,8 @@ func NewComparer[K comparable](key K) Comparer[K] {
2380
2379
panic (fmt .Sprintf ("immutable.NewComparer: must set comparer for %T type" , key ))
2381
2380
}
2382
2381
2383
- // defaultComparer compares two values (int-ish and string-ish types are supported. Implements Comparer.
2384
- type defaultComparer [K comparable ] struct {}
2382
+ // defaultComparer compares two values (int-ish and string-ish types are supported) . Implements Comparer.
2383
+ type defaultComparer [K any ] struct {}
2385
2384
2386
2385
// Compare returns -1 if a is less than b, returns 1 if a is greater than b, and
2387
2386
// returns 0 if a is equal to b. Panic if a or b is not a string or int* type
@@ -2427,7 +2426,7 @@ func defaultCompare[K constraints.Ordered](i, j K) int {
2427
2426
}
2428
2427
2429
2428
// reflectIntComparer compares two values using reflection. Implements Comparer.
2430
- type reflectComparer [K comparable ] struct {}
2429
+ type reflectComparer [K any ] struct {}
2431
2430
2432
2431
// Compare returns -1 if a is less than b, returns 1 if a is greater than b, and
2433
2432
// returns 0 if a is equal to b. Panic if a or b is not an int-ish or string-ish type.
0 commit comments