This repository was archived by the owner on Mar 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsort_generic.go
More file actions
66 lines (59 loc) · 1.69 KB
/
Copy pathsort_generic.go
File metadata and controls
66 lines (59 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2014 The Semver Package Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build amd64,go1.17 386,go1.16 purego,!mips,!mips64,!ppc64,!s390x !amd64,!386,!mips,!mips64,!ppc64,!s390x
package semver
// magnitudeAwareKey is part of twoFieldKey below, and returns small figures verbatim,
// else signals magnitudes in a way susceptible to sorting.
// Parameter 'x' must be non-negative.
func magnitudeAwareKey(x int32) uint8 {
if x <= 11 {
return uint8(x)
}
// For all larger numbers, store the number of bytes +11.
if x <= 0xffff {
if x <= 0xff {
return 12
}
return 13
}
if x <= 0xffffff {
return 14
}
return 15
}
// twoFieldKeyGonly is part of multikeyRadixSort and derives a key from two fields in 'v'.
// The order established by the keys is ascending but not total:
// fields with great values map to a low-resolution key.
// Fields must be non-negative.
//
// This is the Go-only implementation, available for benchmarks on architectures
// that otherwise used an optimized variant.
func twoFieldKey(v *[14]int32, fieldAdjustment uint64, keyIndex uint8) uint8 {
off := int32(fieldAdjustment)
n1 := magnitudeAwareKey(v[keyIndex]+off) << 4
if n1 >= (12 << 4) {
return n1
}
off = int32(fieldAdjustment >> 32)
return (n1 | magnitudeAwareKey(v[keyIndex+1]+off))
}
func (p VersionPtrs) isSorted(skipFields uint) bool {
if len(p) < 2 || skipFields > maxKeyIndex {
return true
}
previous := p[0]
for _, ptr := range p {
if previous == nil {
if ptr != nil {
return false
}
continue
}
if compare(previous, ptr, skipFields) > 0 {
return false
}
previous = ptr
}
return true
}