diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-05 10:03:08 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-05 21:01:58 -0700 |
commit | 0ff1d420f21101a92d8da888d491860cf0cf16cc (patch) | |
tree | 3e5715b4e4fd6619f2edddf5bab315cb9a125598 | |
parent | 53d7fbb73869eb5defa1ca5c52b26abd346b13b9 (diff) |
containers: s/Cmp/Compare/ to match the standard library
Go 1.18 added net/netip.Addr.Compare, and Go 1.20 added time.Time.Compare.
-rw-r--r-- | lib/btrfs/btrfsprim/misc.go | 8 | ||||
-rw-r--r-- | lib/btrfs/btrfsprim/uuid.go | 2 | ||||
-rw-r--r-- | lib/btrfs/btrfstree/ops.go | 2 | ||||
-rw-r--r-- | lib/btrfs/btrfstree/root.go | 2 | ||||
-rw-r--r-- | lib/btrfs/btrfstree/types_node.go | 4 | ||||
-rw-r--r-- | lib/btrfs/btrfsvol/addr.go | 4 | ||||
-rw-r--r-- | lib/btrfs/btrfsvol/chunk.go | 8 | ||||
-rw-r--r-- | lib/btrfs/btrfsvol/devext.go | 6 | ||||
-rw-r--r-- | lib/btrfs/btrfsvol/lvm.go | 10 | ||||
-rw-r--r-- | lib/btrfs/io4_fs.go | 2 | ||||
-rw-r--r-- | lib/btrfsprogs/btrfsinspect/print_addrspace.go | 4 | ||||
-rw-r--r-- | lib/btrfsprogs/btrfsinspect/rebuildmappings/fuzzymatchsums.go | 6 | ||||
-rw-r--r-- | lib/btrfsprogs/btrfsinspect/rebuildmappings/physicalsums.go | 4 | ||||
-rw-r--r-- | lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go | 22 | ||||
-rw-r--r-- | lib/btrfsprogs/btrfsutil/broken_btree.go | 2 | ||||
-rw-r--r-- | lib/containers/intervaltree.go | 16 | ||||
-rw-r--r-- | lib/containers/ordered.go | 10 | ||||
-rw-r--r-- | lib/containers/ordered_test.go | 13 | ||||
-rw-r--r-- | lib/containers/rbtree.go | 4 | ||||
-rw-r--r-- | lib/containers/set.go | 2 |
20 files changed, 72 insertions, 59 deletions
diff --git a/lib/btrfs/btrfsprim/misc.go b/lib/btrfs/btrfsprim/misc.go index 22939bf..da661f6 100644 --- a/lib/btrfs/btrfsprim/misc.go +++ b/lib/btrfs/btrfsprim/misc.go @@ -44,14 +44,14 @@ func (key Key) Mm() Key { return key } -func (a Key) Cmp(b Key) int { - if d := containers.NativeCmp(a.ObjectID, b.ObjectID); d != 0 { +func (a Key) Compare(b Key) int { + if d := containers.NativeCompare(a.ObjectID, b.ObjectID); d != 0 { return d } - if d := containers.NativeCmp(a.ItemType, b.ItemType); d != 0 { + if d := containers.NativeCompare(a.ItemType, b.ItemType); d != 0 { return d } - return containers.NativeCmp(a.Offset, b.Offset) + return containers.NativeCompare(a.Offset, b.Offset) } var _ containers.Ordered[Key] = Key{} diff --git a/lib/btrfs/btrfsprim/uuid.go b/lib/btrfs/btrfsprim/uuid.go index 0103ee4..232ab58 100644 --- a/lib/btrfs/btrfsprim/uuid.go +++ b/lib/btrfs/btrfsprim/uuid.go @@ -47,7 +47,7 @@ func (uuid UUID) Format(f fmt.State, verb rune) { fmtutil.FormatByteArrayStringer(uuid, uuid[:], f, verb) } -func (a UUID) Cmp(b UUID) int { +func (a UUID) Compare(b UUID) int { for i := range a { if d := int(a[i]) - int(b[i]); d != 0 { return d diff --git a/lib/btrfs/btrfstree/ops.go b/lib/btrfs/btrfstree/ops.go index 537773a..cdacef9 100644 --- a/lib/btrfs/btrfstree/ops.go +++ b/lib/btrfs/btrfstree/ops.go @@ -481,7 +481,7 @@ func KeySearch(fn func(btrfsprim.Key) int) func(btrfsprim.Key, uint32) int { // TreeLookup implements the 'Trees' interface. func (fs TreeOperatorImpl) TreeLookup(treeID btrfsprim.ObjID, key btrfsprim.Key) (Item, error) { - item, err := fs.TreeSearch(treeID, KeySearch(key.Cmp)) + item, err := fs.TreeSearch(treeID, KeySearch(key.Compare)) if err != nil { err = fmt.Errorf("item with key=%v: %w", key, err) } diff --git a/lib/btrfs/btrfstree/root.go b/lib/btrfs/btrfstree/root.go index 6ec45b5..319904b 100644 --- a/lib/btrfs/btrfstree/root.go +++ b/lib/btrfs/btrfstree/root.go @@ -30,7 +30,7 @@ func RootItemSearchFn(treeID btrfsprim.ObjID) func(btrfsprim.Key, uint32) int { ObjectID: treeID, ItemType: btrfsitem.ROOT_ITEM_KEY, Offset: 0, - }.Cmp(key) + }.Compare(key) } } diff --git a/lib/btrfs/btrfstree/types_node.go b/lib/btrfs/btrfstree/types_node.go index a26215b..d9d7118 100644 --- a/lib/btrfs/btrfstree/types_node.go +++ b/lib/btrfs/btrfstree/types_node.go @@ -507,11 +507,11 @@ func ReadNode[Addr ~int64](fs diskio.File[Addr], sb Superblock, addr Addr, exp N if nodeRef.Data.Head.NumItems == 0 { errs = append(errs, fmt.Errorf("has no items")) } else { - if minItem, _ := nodeRef.Data.MinItem(); exp.MinItem.OK && exp.MinItem.Val.Cmp(minItem) > 0 { + if minItem, _ := nodeRef.Data.MinItem(); exp.MinItem.OK && exp.MinItem.Val.Compare(minItem) > 0 { errs = append(errs, fmt.Errorf("expected minItem>=%v but node has minItem=%v", exp.MinItem, minItem)) } - if maxItem, _ := nodeRef.Data.MaxItem(); exp.MaxItem.OK && exp.MaxItem.Val.Cmp(maxItem) < 0 { + if maxItem, _ := nodeRef.Data.MaxItem(); exp.MaxItem.OK && exp.MaxItem.Val.Compare(maxItem) < 0 { errs = append(errs, fmt.Errorf("expected maxItem<=%v but node has maxItem=%v", exp.MaxItem, maxItem)) } diff --git a/lib/btrfs/btrfsvol/addr.go b/lib/btrfs/btrfsvol/addr.go index 94320ef..655f4e9 100644 --- a/lib/btrfs/btrfsvol/addr.go +++ b/lib/btrfs/btrfsvol/addr.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com> +// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com> // // SPDX-License-Identifier: GPL-2.0-or-later @@ -50,7 +50,7 @@ func (a QualifiedPhysicalAddr) Add(b AddrDelta) QualifiedPhysicalAddr { } } -func (a QualifiedPhysicalAddr) Cmp(b QualifiedPhysicalAddr) int { +func (a QualifiedPhysicalAddr) Compare(b QualifiedPhysicalAddr) int { if d := int(a.Dev - b.Dev); d != 0 { return d } diff --git a/lib/btrfs/btrfsvol/chunk.go b/lib/btrfs/btrfsvol/chunk.go index 5f1baa8..08a0b2b 100644 --- a/lib/btrfs/btrfsvol/chunk.go +++ b/lib/btrfs/btrfsvol/chunk.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com> +// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com> // // SPDX-License-Identifier: GPL-2.0-or-later @@ -27,7 +27,7 @@ type ChunkMapping = chunkMapping // return -1 if 'a' is wholly to the left of 'b' // return 0 if there is some overlap between 'a' and 'b' // return 1 if 'a is wholly to the right of 'b' -func (a chunkMapping) cmpRange(b chunkMapping) int { +func (a chunkMapping) compareRange(b chunkMapping) int { switch { case a.LAddr.Add(a.Size) <= b.LAddr: // 'a' is wholly to the left of 'b'. @@ -44,7 +44,7 @@ func (a chunkMapping) cmpRange(b chunkMapping) int { func (a chunkMapping) union(rest ...chunkMapping) (chunkMapping, error) { // sanity check for _, chunk := range rest { - if a.cmpRange(chunk) != 0 { + if a.compareRange(chunk) != 0 { return chunkMapping{}, fmt.Errorf("chunks don't overlap") } } @@ -79,7 +79,7 @@ func (a chunkMapping) union(rest ...chunkMapping) (chunkMapping, error) { } ret.PAddrs = maps.Keys(paddrs) sort.Slice(ret.PAddrs, func(i, j int) bool { - return ret.PAddrs[i].Cmp(ret.PAddrs[j]) < 0 + return ret.PAddrs[i].Compare(ret.PAddrs[j]) < 0 }) // figure out the flags (.Flags) for _, chunk := range chunks { diff --git a/lib/btrfs/btrfsvol/devext.go b/lib/btrfs/btrfsvol/devext.go index e8e5446..037021c 100644 --- a/lib/btrfs/btrfsvol/devext.go +++ b/lib/btrfs/btrfsvol/devext.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com> +// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com> // // SPDX-License-Identifier: GPL-2.0-or-later @@ -23,7 +23,7 @@ type devextMapping struct { // return -1 if 'a' is wholly to the left of 'b' // return 0 if there is some overlap between 'a' and 'b' // return 1 if 'a is wholly to the right of 'b' -func (a devextMapping) cmpRange(b devextMapping) int { +func (a devextMapping) compareRange(b devextMapping) int { switch { case a.PAddr.Add(a.Size) <= b.PAddr: // 'a' is wholly to the left of 'b'. @@ -40,7 +40,7 @@ func (a devextMapping) cmpRange(b devextMapping) int { func (a devextMapping) union(rest ...devextMapping) (devextMapping, error) { // sanity check for _, ext := range rest { - if a.cmpRange(ext) != 0 { + if a.compareRange(ext) != 0 { return devextMapping{}, fmt.Errorf("devexts don't overlap") } } diff --git a/lib/btrfs/btrfsvol/lvm.go b/lib/btrfs/btrfsvol/lvm.go index 1cb1ded..b83e9cd 100644 --- a/lib/btrfs/btrfsvol/lvm.go +++ b/lib/btrfs/btrfsvol/lvm.go @@ -143,7 +143,7 @@ func (lv *LogicalVolume[PhysicalVolume]) addMapping(m Mapping, dryRun bool) erro SizeLocked: m.SizeLocked, Flags: m.Flags, } - logicalOverlaps := lv.logical2physical.SearchRange(newChunk.cmpRange) + logicalOverlaps := lv.logical2physical.SearchRange(newChunk.compareRange) var err error newChunk, err = newChunk.union(logicalOverlaps...) if err != nil { @@ -158,7 +158,7 @@ func (lv *LogicalVolume[PhysicalVolume]) addMapping(m Mapping, dryRun bool) erro SizeLocked: m.SizeLocked, Flags: m.Flags, } - physicalOverlaps := lv.physical2logical[m.PAddr.Dev].SearchRange(newExt.cmpRange) + physicalOverlaps := lv.physical2logical[m.PAddr.Dev].SearchRange(newExt.compareRange) newExt, err = newExt.union(physicalOverlaps...) if err != nil { return fmt.Errorf("(%p).AddMapping: %w", lv, err) @@ -261,7 +261,7 @@ func (lv *LogicalVolume[PhysicalVolume]) Mappings() []Mapping { func (lv *LogicalVolume[PhysicalVolume]) Resolve(laddr LogicalAddr) (paddrs containers.Set[QualifiedPhysicalAddr], maxlen AddrDelta) { node := lv.logical2physical.Search(func(chunk chunkMapping) int { - return chunkMapping{LAddr: laddr, Size: 1}.cmpRange(chunk) + return chunkMapping{LAddr: laddr, Size: 1}.compareRange(chunk) }) if node == nil { return nil, 0 @@ -281,7 +281,7 @@ func (lv *LogicalVolume[PhysicalVolume]) Resolve(laddr LogicalAddr) (paddrs cont func (lv *LogicalVolume[PhysicalVolume]) ResolveAny(laddr LogicalAddr, size AddrDelta) (LogicalAddr, QualifiedPhysicalAddr) { node := lv.logical2physical.Search(func(chunk chunkMapping) int { - return chunkMapping{LAddr: laddr, Size: size}.cmpRange(chunk) + return chunkMapping{LAddr: laddr, Size: size}.compareRange(chunk) }) if node == nil { return -1, QualifiedPhysicalAddr{0, -1} @@ -291,7 +291,7 @@ func (lv *LogicalVolume[PhysicalVolume]) ResolveAny(laddr LogicalAddr, size Addr func (lv *LogicalVolume[PhysicalVolume]) UnResolve(paddr QualifiedPhysicalAddr) LogicalAddr { node := lv.physical2logical[paddr.Dev].Search(func(ext devextMapping) int { - return devextMapping{PAddr: paddr.Addr, Size: 1}.cmpRange(ext) + return devextMapping{PAddr: paddr.Addr, Size: 1}.compareRange(ext) }) if node == nil { return -1 diff --git a/lib/btrfs/io4_fs.go b/lib/btrfs/io4_fs.go index 799c865..fce9c76 100644 --- a/lib/btrfs/io4_fs.go +++ b/lib/btrfs/io4_fs.go @@ -152,7 +152,7 @@ func (sv *Subvolume) LoadFullInode(inode btrfsprim.ObjID) (*FullInode, error) { XAttrs: make(map[string]string), } items, err := sv.FS.TreeSearchAll(sv.TreeID, func(key btrfsprim.Key, _ uint32) int { - return containers.NativeCmp(inode, key.ObjectID) + return containers.NativeCompare(inode, key.ObjectID) }) if err != nil { val.Errs = append(val.Errs, err) diff --git a/lib/btrfsprogs/btrfsinspect/print_addrspace.go b/lib/btrfsprogs/btrfsinspect/print_addrspace.go index a8b992e..e85e055 100644 --- a/lib/btrfsprogs/btrfsinspect/print_addrspace.go +++ b/lib/btrfsprogs/btrfsinspect/print_addrspace.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com> +// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com> // // SPDX-License-Identifier: GPL-2.0-or-later @@ -46,7 +46,7 @@ func PrintLogicalSpace(out io.Writer, fs *btrfs.FS) { func PrintPhysicalSpace(out io.Writer, fs *btrfs.FS) { mappings := fs.LV.Mappings() sort.Slice(mappings, func(i, j int) bool { - return mappings[i].PAddr.Cmp(mappings[j].PAddr) < 0 + return mappings[i].PAddr.Compare(mappings[j].PAddr) < 0 }) var prevDev btrfsvol.DeviceID = 0 diff --git a/lib/btrfsprogs/btrfsinspect/rebuildmappings/fuzzymatchsums.go b/lib/btrfsprogs/btrfsinspect/rebuildmappings/fuzzymatchsums.go index 6b75d84..4724c12 100644 --- a/lib/btrfsprogs/btrfsinspect/rebuildmappings/fuzzymatchsums.go +++ b/lib/btrfsprogs/btrfsinspect/rebuildmappings/fuzzymatchsums.go @@ -26,7 +26,7 @@ type fuzzyRecord struct { N int } -func (a fuzzyRecord) Cmp(b fuzzyRecord) int { +func (a fuzzyRecord) Compare(b fuzzyRecord) int { switch { case a.N < b.N: return -1 @@ -148,12 +148,12 @@ func (l *lowestN[T]) Insert(v T) { switch { case len(l.Dat) < l.N: l.Dat = append(l.Dat, v) - case v.Cmp(l.Dat[0]) < 0: + case v.Compare(l.Dat[0]) < 0: l.Dat[0] = v default: return } sort.Slice(l.Dat, func(i, j int) bool { - return l.Dat[i].Cmp(l.Dat[j]) < 0 + return l.Dat[i].Compare(l.Dat[j]) < 0 }) } diff --git a/lib/btrfsprogs/btrfsinspect/rebuildmappings/physicalsums.go b/lib/btrfsprogs/btrfsinspect/rebuildmappings/physicalsums.go index 0806a63..da22fbf 100644 --- a/lib/btrfsprogs/btrfsinspect/rebuildmappings/physicalsums.go +++ b/lib/btrfsprogs/btrfsinspect/rebuildmappings/physicalsums.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com> +// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com> // // SPDX-License-Identifier: GPL-2.0-or-later @@ -34,7 +34,7 @@ func ListUnmappedPhysicalRegions(fs *btrfs.FS) map[btrfsvol.DeviceID][]PhysicalR pos := make(map[btrfsvol.DeviceID]btrfsvol.PhysicalAddr) mappings := fs.LV.Mappings() sort.Slice(mappings, func(i, j int) bool { - return mappings[i].PAddr.Cmp(mappings[j].PAddr) < 0 + return mappings[i].PAddr.Compare(mappings[j].PAddr) < 0 }) for _, mapping := range mappings { if pos[mapping.PAddr.Dev] < mapping.PAddr.Addr { diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go index ebca2bd..ee5950d 100644 --- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go +++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/rebuild.go @@ -36,11 +36,11 @@ type keyAndTree struct { TreeID btrfsprim.ObjID } -func (a keyAndTree) Cmp(b keyAndTree) int { - if d := containers.NativeCmp(a.TreeID, b.TreeID); d != 0 { +func (a keyAndTree) Compare(b keyAndTree) int { + if d := containers.NativeCompare(a.TreeID, b.TreeID); d != 0 { return d } - return a.Key.Cmp(b.Key) + return a.Key.Compare(b.Key) } func (o keyAndTree) String() string { @@ -155,7 +155,7 @@ func (o *rebuilder) Rebuild(_ctx context.Context) error { itemQueue := maps.Keys(o.itemQueue) o.itemQueue = make(containers.Set[keyAndTree]) sort.Slice(itemQueue, func(i, j int) bool { - return itemQueue[i].Cmp(itemQueue[j]) < 0 + return itemQueue[i].Compare(itemQueue[j]) < 0 }) var progress itemStats progress.D = len(itemQueue) @@ -596,7 +596,7 @@ func (o *rebuilder) _want(ctx context.Context, treeID btrfsprim.ObjID, wantKey s } if key, _, ok := o.rebuilt.Tree(ctx, treeID).Items(ctx).Search(func(key btrfsprim.Key, _ keyio.ItemPtr) int { key.Offset = 0 - return tgt.Cmp(key) + return tgt.Compare(key) }); ok { return key, true } @@ -608,7 +608,7 @@ func (o *rebuilder) _want(ctx context.Context, treeID btrfsprim.ObjID, wantKey s } wants := make(containers.Set[btrfsvol.LogicalAddr]) o.rebuilt.Tree(ctx, treeID).PotentialItems(ctx).Subrange( - func(k btrfsprim.Key, _ keyio.ItemPtr) int { k.Offset = 0; return tgt.Cmp(k) }, + func(k btrfsprim.Key, _ keyio.ItemPtr) int { k.Offset = 0; return tgt.Compare(k) }, func(_ btrfsprim.Key, v keyio.ItemPtr) bool { wants.InsertFrom(o.rebuilt.Tree(ctx, treeID).LeafToRoots(ctx, v.Node)) return true @@ -649,7 +649,7 @@ func (o *rebuilder) _wantOff(ctx context.Context, treeID btrfsprim.ObjID, wantKe } wants := make(containers.Set[btrfsvol.LogicalAddr]) o.rebuilt.Tree(ctx, treeID).PotentialItems(ctx).Subrange( - func(k btrfsprim.Key, _ keyio.ItemPtr) int { return tgt.Cmp(k) }, + func(k btrfsprim.Key, _ keyio.ItemPtr) int { return tgt.Compare(k) }, func(_ btrfsprim.Key, v keyio.ItemPtr) bool { wants.InsertFrom(o.rebuilt.Tree(ctx, treeID).LeafToRoots(ctx, v.Node)) return true @@ -674,7 +674,7 @@ func (o *rebuilder) _wantFunc(ctx context.Context, treeID btrfsprim.ObjID, wantK o.rebuilt.Tree(ctx, treeID).Items(ctx).Subrange( func(key btrfsprim.Key, _ keyio.ItemPtr) int { key.Offset = 0 - return tgt.Cmp(key) + return tgt.Compare(key) }, func(_ btrfsprim.Key, itemPtr keyio.ItemPtr) bool { if fn(itemPtr) { @@ -693,7 +693,7 @@ func (o *rebuilder) _wantFunc(ctx context.Context, treeID btrfsprim.ObjID, wantK } wants := make(containers.Set[btrfsvol.LogicalAddr]) o.rebuilt.Tree(ctx, treeID).PotentialItems(ctx).Subrange( - func(k btrfsprim.Key, _ keyio.ItemPtr) int { k.Offset = 0; return tgt.Cmp(k) }, + func(k btrfsprim.Key, _ keyio.ItemPtr) int { k.Offset = 0; return tgt.Compare(k) }, func(k btrfsprim.Key, v keyio.ItemPtr) bool { if fn(v) { wants.InsertFrom(o.rebuilt.Tree(ctx, treeID).LeafToRoots(ctx, v.Node)) @@ -735,9 +735,9 @@ func (o *rebuilder) _walkRange( items.Subrange( func(runKey btrfsprim.Key, _ keyio.ItemPtr) int { switch { - case min.Cmp(runKey) < 0: + case min.Compare(runKey) < 0: return 1 - case max.Cmp(runKey) > 0: + case max.Compare(runKey) > 0: return -1 default: return 0 diff --git a/lib/btrfsprogs/btrfsutil/broken_btree.go b/lib/btrfsprogs/btrfsutil/broken_btree.go index f0b298e..8261119 100644 --- a/lib/btrfsprogs/btrfsutil/broken_btree.go +++ b/lib/btrfsprogs/btrfsutil/broken_btree.go @@ -194,7 +194,7 @@ func (bt *brokenTrees) rawTreeWalk(root btrfstree.TreeRoot, cacheEntry treeIndex } func (bt *brokenTrees) TreeLookup(treeID btrfsprim.ObjID, key btrfsprim.Key) (btrfstree.Item, error) { - item, err := bt.TreeSearch(treeID, btrfstree.KeySearch(key.Cmp)) + item, err := bt.TreeSearch(treeID, btrfstree.KeySearch(key.Compare)) if err != nil { err = fmt.Errorf("item with key=%v: %w", key, err) } diff --git a/lib/containers/intervaltree.go b/lib/containers/intervaltree.go index 16bc916..b7ff866 100644 --- a/lib/containers/intervaltree.go +++ b/lib/containers/intervaltree.go @@ -12,11 +12,11 @@ func (ival intervalKey[K]) ContainsFn(fn func(K) int) bool { return fn(ival.Min) >= 0 && fn(ival.Max) <= 0 } -func (a intervalKey[K]) Cmp(b intervalKey[K]) int { - if d := a.Min.Cmp(b.Min); d != 0 { +func (a intervalKey[K]) Compare(b intervalKey[K]) int { + if d := a.Min.Compare(b.Min); d != 0 { return d } - return a.Max.Cmp(b.Max) + return a.Max.Compare(b.Max) } type intervalValue[K Ordered[K], V any] struct { @@ -39,19 +39,19 @@ func (t *IntervalTree[K, V]) keyFn(v intervalValue[K, V]) intervalKey[K] { func (t *IntervalTree[K, V]) attrFn(node *RBNode[intervalValue[K, V]]) { max := t.MaxFn(node.Value.Val) - if node.Left != nil && node.Left.Value.SpanOfChildren.Max.Cmp(max) > 0 { + if node.Left != nil && node.Left.Value.SpanOfChildren.Max.Compare(max) > 0 { max = node.Left.Value.SpanOfChildren.Max } - if node.Right != nil && node.Right.Value.SpanOfChildren.Max.Cmp(max) > 0 { + if node.Right != nil && node.Right.Value.SpanOfChildren.Max.Compare(max) > 0 { max = node.Right.Value.SpanOfChildren.Max } node.Value.SpanOfChildren.Max = max min := t.MinFn(node.Value.Val) - if node.Left != nil && node.Left.Value.SpanOfChildren.Min.Cmp(min) < 0 { + if node.Left != nil && node.Left.Value.SpanOfChildren.Min.Compare(min) < 0 { min = node.Left.Value.SpanOfChildren.Min } - if node.Right != nil && node.Right.Value.SpanOfChildren.Min.Cmp(min) < 0 { + if node.Right != nil && node.Right.Value.SpanOfChildren.Min.Compare(min) < 0 { min = node.Right.Value.SpanOfChildren.Min } node.Value.SpanOfChildren.Min = min @@ -98,7 +98,7 @@ func (t *IntervalTree[K, V]) Max() (K, bool) { } func (t *IntervalTree[K, V]) Lookup(k K) (V, bool) { - return t.Search(k.Cmp) + return t.Search(k.Compare) } func (t *IntervalTree[K, V]) Search(fn func(K) int) (V, bool) { diff --git a/lib/containers/ordered.go b/lib/containers/ordered.go index d918149..aa2c94d 100644 --- a/lib/containers/ordered.go +++ b/lib/containers/ordered.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com> +// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com> // // SPDX-License-Identifier: GPL-2.0-or-later @@ -9,7 +9,7 @@ import ( ) type _Ordered[T any] interface { - Cmp(T) int + Compare(T) int } type Ordered[T _Ordered[T]] _Ordered[T] @@ -18,7 +18,7 @@ type NativeOrdered[T constraints.Ordered] struct { Val T } -func NativeCmp[T constraints.Ordered](a, b T) int { +func NativeCompare[T constraints.Ordered](a, b T) int { switch { case a < b: return -1 @@ -29,8 +29,8 @@ func NativeCmp[T constraints.Ordered](a, b T) int { } } -func (a NativeOrdered[T]) Cmp(b NativeOrdered[T]) int { - return NativeCmp(a.Val, b.Val) +func (a NativeOrdered[T]) Compare(b NativeOrdered[T]) int { + return NativeCompare(a.Val, b.Val) } var _ Ordered[NativeOrdered[int]] = NativeOrdered[int]{} diff --git a/lib/containers/ordered_test.go b/lib/containers/ordered_test.go new file mode 100644 index 0000000..4f0b2e6 --- /dev/null +++ b/lib/containers/ordered_test.go @@ -0,0 +1,13 @@ +// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com> +// +// SPDX-License-Identifier: GPL-2.0-or-later + +package containers_test + +import ( + "net/netip" + + "git.lukeshu.com/btrfs-progs-ng/lib/containers" +) + +var _ containers.Ordered[netip.Addr] = netip.Addr{} diff --git a/lib/containers/rbtree.go b/lib/containers/rbtree.go index 0430123..1fdb799 100644 --- a/lib/containers/rbtree.go +++ b/lib/containers/rbtree.go @@ -111,7 +111,7 @@ func (node *RBNode[V]) search(fn func(V) int) (exact, nearest *RBNode[V]) { func (t *RBTree[K, V]) exactKey(key K) func(V) int { return func(val V) int { valKey := t.KeyFn(val) - return key.Cmp(valKey) + return key.Compare(valKey) } } @@ -341,7 +341,7 @@ func (t *RBTree[K, V]) Insert(val V) { switch { case parent == nil: t.root = node - case key.Cmp(t.KeyFn(parent.Value)) < 0: + case key.Compare(t.KeyFn(parent.Value)) < 0: parent.Left = node default: parent.Right = node diff --git a/lib/containers/set.go b/lib/containers/set.go index b2af494..0d9202c 100644 --- a/lib/containers/set.go +++ b/lib/containers/set.go @@ -32,7 +32,7 @@ func (o Set[T]) EncodeJSON(w io.Writer) error { var zero T switch (any(zero)).(type) { case _Ordered[T]: - less = func(a, b T) bool { return cast[_Ordered[T]](a).Cmp(b) < 0 } + less = func(a, b T) bool { return cast[_Ordered[T]](a).Compare(b) < 0 } // This is the constraints.Ordered list case string: less = func(a, b T) bool { return cast[string](a) < cast[string](b) } |