github.com/puellanivis/breton@v0.2.16/lib/sort/int.go (about)

     1  package sort
     2  
     3  import (
     4  	"math/bits"
     5  	"sort"
     6  )
     7  
     8  // IntSlice attaches the methods of sort.Interface to []int, sorting in increasing order.
     9  type IntSlice []int
    10  
    11  // Len implements sort.Interface.
    12  func (p IntSlice) Len() int { return len(p) }
    13  
    14  // Less implements sort.Interface.
    15  func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
    16  
    17  // Swap implements sort.Interface.
    18  func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
    19  
    20  func cmpInt(x, y int) int {
    21  	if x == y {
    22  		return 0
    23  	}
    24  
    25  	if x < y {
    26  		return -1
    27  	}
    28  
    29  	return +1
    30  }
    31  
    32  // Compare implements Comparer.
    33  func (p IntSlice) Compare(i, j int) int {
    34  	return cmpInt(p[i], p[j])
    35  }
    36  
    37  // CompareFunc implements Comparer.
    38  func (p IntSlice) CompareFunc(x interface{}) func(int) int {
    39  	e := x.(int)
    40  	return func(i int) int {
    41  		return cmpInt(p[i], e)
    42  	}
    43  }
    44  
    45  // RadixRange implements RadixInterface.
    46  func (p IntSlice) RadixRange() (int, int) {
    47  	allBits := int(^1)
    48  	var anyBits int
    49  	for _, v := range p {
    50  		anyBits |= v
    51  		allBits &= v
    52  	}
    53  	bitMask := anyBits &^ allBits
    54  
    55  	end := uintMSB - bits.TrailingZeros(uint(bitMask))
    56  
    57  	if bitMask < 0 {
    58  		return 0, end
    59  	}
    60  
    61  	return bits.LeadingZeros(uint(bitMask)), end
    62  }
    63  
    64  // RadixFunc implements RadixInterface.
    65  func (p IntSlice) RadixFunc(r int) RadixTest {
    66  	if r == 0 {
    67  		return func(i int) bool {
    68  			return p[i] >= 0
    69  		}
    70  	}
    71  
    72  	mask := int(1) << uint(uintMSB-r)
    73  	return func(i int) bool {
    74  		return p[i]&mask != 0
    75  	}
    76  }
    77  
    78  // Sort is a convenience method.
    79  func (p IntSlice) Sort() { radix(p) }
    80  
    81  // Radix is a convenience method.
    82  func (p IntSlice) Radix() { radix(p) }
    83  
    84  // Search is a convenience method.
    85  func (p IntSlice) Search(x int) int { return SearchInts(p, x) }
    86  
    87  // SearchFor is a convenience method.
    88  func (p IntSlice) SearchFor(x interface{}) int { return SearchInts(p, x.(int)) }
    89  
    90  // Ints sorts a slice of ints in increasing order.
    91  func Ints(a []int) { radix(IntSlice(a)) }
    92  
    93  // SearchInts searches for x in a sorted slice of ints and returns the index as specified by sort.Search.
    94  // The return value is the index to insert x if x is not present (it could be len(a)).
    95  // The slice must be sorted in ascending order.
    96  func SearchInts(a []int, x int) int {
    97  	return sort.Search(len(a), func(i int) bool { return a[i] >= x })
    98  }
    99  
   100  // IntsAreSorted tests whether a slice of ints is sorted in increasing order.
   101  func IntsAreSorted(a []int) bool { return sort.IsSorted(IntSlice(a)) }
   102  
   103  // Int64Slice attaches the methods of sort.Interface to []int64, sorting in increasing order.
   104  type Int64Slice []int64
   105  
   106  // Len implements sort.Interface.
   107  func (p Int64Slice) Len() int { return len(p) }
   108  
   109  // Less implements sort.Interface.
   110  func (p Int64Slice) Less(i, j int) bool { return p[i] < p[j] }
   111  
   112  // Swap implements sort.Interface.
   113  func (p Int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
   114  
   115  func cmpInt64(x, y int64) int {
   116  	if x == y {
   117  		return 0
   118  	}
   119  
   120  	if x < y {
   121  		return -1
   122  	}
   123  
   124  	return +1
   125  }
   126  
   127  // Compare implements Comparer.
   128  func (p Int64Slice) Compare(i, j int) int {
   129  	return cmpInt64(p[i], p[j])
   130  }
   131  
   132  // CompareFunc implements Comparer.
   133  func (p Int64Slice) CompareFunc(x interface{}) func(int) int {
   134  	e := x.(int64)
   135  	return func(i int) int {
   136  		return cmpInt64(p[i], e)
   137  	}
   138  }
   139  
   140  // RadixRange implements RadixInterface.
   141  func (p Int64Slice) RadixRange() (int, int) {
   142  	allBits := int64(^1)
   143  	var anyBits int64
   144  	for _, v := range p {
   145  		anyBits |= v
   146  		allBits &= v
   147  	}
   148  	bitMask := anyBits &^ allBits
   149  
   150  	end := 63 - bits.TrailingZeros64(uint64(bitMask))
   151  
   152  	if bitMask < 0 {
   153  		return 0, end
   154  	}
   155  
   156  	return bits.LeadingZeros64(uint64(bitMask)), end
   157  }
   158  
   159  // RadixFunc implements RadixInterface.
   160  func (p Int64Slice) RadixFunc(r int) RadixTest {
   161  	if r == 0 {
   162  		return func(i int) bool {
   163  			return p[i] >= 0
   164  		}
   165  	}
   166  
   167  	mask := int64(1) << uint(63-r)
   168  	return func(i int) bool {
   169  		return p[i]&mask != 0
   170  	}
   171  }
   172  
   173  // Sort is a convenience method.
   174  func (p Int64Slice) Sort() { radix(p) }
   175  
   176  // Radix is a convenience method.
   177  func (p Int64Slice) Radix() { radix(p) }
   178  
   179  // Search is a convenience method.
   180  func (p Int64Slice) Search(x int64) int { return SearchInt64s(p, x) }
   181  
   182  // SearchFor is a convenience method.
   183  func (p Int64Slice) SearchFor(x interface{}) int { return SearchInt64s(p, x.(int64)) }
   184  
   185  // Int64s sorts a slice of int64s in increasing order.
   186  func Int64s(a []int64) { radix(Int64Slice(a)) }
   187  
   188  // SearchInt64s searches for x in a sorted slice of int64s and returns the index as specified by sort.Search.
   189  // The return value is the index to insert x if x is not present (it could be len(a)).
   190  // The slice must be sorted in ascending order.
   191  func SearchInt64s(a []int64, x int64) int {
   192  	return sort.Search(len(a), func(i int) bool { return a[i] >= x })
   193  }
   194  
   195  // Int64sAreSorted tests whether a slice of int64s is sorted in increasing order.
   196  func Int64sAreSorted(a []int64) bool { return sort.IsSorted(Int64Slice(a)) }
   197  
   198  // Int32Slice attaches the methods of sort.Interface to []int32, sorting in increasing order.
   199  type Int32Slice []int32
   200  
   201  // Len implements sort.Interface.
   202  func (p Int32Slice) Len() int { return len(p) }
   203  
   204  // Less implements sort.Interface.
   205  func (p Int32Slice) Less(i, j int) bool { return p[i] < p[j] }
   206  
   207  // Swap implements sort.Interface.
   208  func (p Int32Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
   209  
   210  func cmpInt32(x, y int32) int {
   211  	if x == y {
   212  		return 0
   213  	}
   214  
   215  	if x < y {
   216  		return -1
   217  	}
   218  
   219  	return +1
   220  }
   221  
   222  // Compare implements Comparer.
   223  func (p Int32Slice) Compare(i, j int) int {
   224  	return cmpInt32(p[i], p[j])
   225  }
   226  
   227  // CompareFunc implements Comparer.
   228  func (p Int32Slice) CompareFunc(x interface{}) func(int) int {
   229  	e := x.(int32)
   230  	return func(i int) int {
   231  		return cmpInt32(p[i], e)
   232  	}
   233  }
   234  
   235  // RadixRange implements RadixInterface.
   236  func (p Int32Slice) RadixRange() (int, int) {
   237  	allBits := int32(^1)
   238  	var anyBits int32
   239  	for _, v := range p {
   240  		anyBits |= v
   241  		allBits &= v
   242  	}
   243  	bitMask := anyBits &^ allBits
   244  
   245  	end := 31 - bits.TrailingZeros32(uint32(bitMask))
   246  
   247  	if bitMask < 0 {
   248  		return 0, end
   249  	}
   250  
   251  	return bits.LeadingZeros32(uint32(bitMask)), end
   252  }
   253  
   254  // RadixFunc implements RadixInterface.
   255  func (p Int32Slice) RadixFunc(r int) RadixTest {
   256  	if r == 0 {
   257  		return func(i int) bool {
   258  			return p[i] >= 0
   259  		}
   260  	}
   261  
   262  	mask := int32(1) << uint(31-r)
   263  	return func(i int) bool {
   264  		return p[i]&mask != 0
   265  	}
   266  }
   267  
   268  // Sort is a convenience method.
   269  func (p Int32Slice) Sort() { radix(p) }
   270  
   271  // Radix is a convenience method.
   272  func (p Int32Slice) Radix() { radix(p) }
   273  
   274  // Search is a convenience method.
   275  func (p Int32Slice) Search(x int32) int { return SearchInt32s(p, x) }
   276  
   277  // SearchFor is a convenience method.
   278  func (p Int32Slice) SearchFor(x interface{}) int { return SearchInt32s(p, x.(int32)) }
   279  
   280  // Int32s sorts a slice of int32s in increasing order.
   281  func Int32s(a []int32) { radix(Int32Slice(a)) }
   282  
   283  // SearchInt32s searches for x in a sorted slice of int32s and returns the index as specified by sort.Search.
   284  // The return value is the index to insert x if x is not present (it could be len(a)).
   285  // The slice must be sorted in ascending order.
   286  func SearchInt32s(a []int32, x int32) int {
   287  	return sort.Search(len(a), func(i int) bool { return a[i] >= x })
   288  }
   289  
   290  // Int32sAreSorted tests whether a slice of int32s is sorted in increasing order.
   291  func Int32sAreSorted(a []int32) bool { return sort.IsSorted(Int32Slice(a)) }
   292  
   293  // Int16Slice attaches the methods of sort.Interface to []int16, sorting in increasing order.
   294  type Int16Slice []int16
   295  
   296  // Len implements sort.Interface.
   297  func (p Int16Slice) Len() int { return len(p) }
   298  
   299  // Less implements sort.Interface.
   300  func (p Int16Slice) Less(i, j int) bool { return p[i] < p[j] }
   301  
   302  // Swap implements sort.Interface.
   303  func (p Int16Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
   304  
   305  func cmpInt16(x, y int16) int {
   306  	if x == y {
   307  		return 0
   308  	}
   309  
   310  	if x < y {
   311  		return -1
   312  	}
   313  
   314  	return +1
   315  }
   316  
   317  // Compare implements Comparer.
   318  func (p Int16Slice) Compare(i, j int) int {
   319  	return cmpInt16(p[i], p[j])
   320  }
   321  
   322  // CompareFunc implements Comparer.
   323  func (p Int16Slice) CompareFunc(x interface{}) func(int) int {
   324  	e := x.(int16)
   325  	return func(i int) int {
   326  		return cmpInt16(p[i], e)
   327  	}
   328  }
   329  
   330  // RadixRange implements RadixInterface.
   331  func (p Int16Slice) RadixRange() (int, int) {
   332  	allBits := int16(^1)
   333  	var anyBits int16
   334  	for _, v := range p {
   335  		anyBits |= v
   336  		allBits &= v
   337  	}
   338  	bitMask := anyBits &^ allBits
   339  
   340  	end := 15 - bits.TrailingZeros16(uint16(bitMask))
   341  
   342  	if bitMask < 0 {
   343  		return 0, end
   344  	}
   345  
   346  	return bits.LeadingZeros16(uint16(bitMask)), end
   347  }
   348  
   349  // RadixFunc implements RadixInterface.
   350  func (p Int16Slice) RadixFunc(r int) RadixTest {
   351  	if r == 0 {
   352  		return func(i int) bool {
   353  			return p[i] >= 0
   354  		}
   355  	}
   356  
   357  	mask := int16(1) << uint(15-r)
   358  	return func(i int) bool {
   359  		return p[i]&mask != 0
   360  	}
   361  }
   362  
   363  // Sort is a convenience method.
   364  func (p Int16Slice) Sort() { radix(p) }
   365  
   366  // Radix is a convenience method.
   367  func (p Int16Slice) Radix() { radix(p) }
   368  
   369  // Search is a convenience method.
   370  func (p Int16Slice) Search(x int16) int { return SearchInt16s(p, x) }
   371  
   372  // SearchFor is a convenience method.
   373  func (p Int16Slice) SearchFor(x interface{}) int { return SearchInt16s(p, x.(int16)) }
   374  
   375  // Int16s sorts a slice of int16s in increasing order.
   376  func Int16s(a []int16) { radix(Int16Slice(a)) }
   377  
   378  // SearchInt16s searches for x in a sorted slice of int16s and returns the index as specified by sort.Search.
   379  // The return value is the index to insert x if x is not present (it could be len(a)).
   380  // The slice must be sorted in ascending order.
   381  func SearchInt16s(a []int16, x int16) int {
   382  	return sort.Search(len(a), func(i int) bool { return a[i] >= x })
   383  }
   384  
   385  // Int16sAreSorted tests whether a slice of int16s is sorted in increasing order.
   386  func Int16sAreSorted(a []int16) bool { return sort.IsSorted(Int16Slice(a)) }
   387  
   388  // Int8Slice attaches the methods of sort.Interface to []int8, sorting in increasing order.
   389  type Int8Slice []int8
   390  
   391  // Len implements sort.Interface.
   392  func (p Int8Slice) Len() int { return len(p) }
   393  
   394  // Less implements sort.Interface.
   395  func (p Int8Slice) Less(i, j int) bool { return p[i] < p[j] }
   396  
   397  // Swap implements sort.Interface.
   398  func (p Int8Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
   399  
   400  func cmpInt8(x, y int8) int {
   401  	if x == y {
   402  		return 0
   403  	}
   404  
   405  	if x < y {
   406  		return -1
   407  	}
   408  
   409  	return +1
   410  }
   411  
   412  // Compare implements Comparer.
   413  func (p Int8Slice) Compare(i, j int) int {
   414  	return cmpInt8(p[i], p[j])
   415  }
   416  
   417  // CompareFunc implements Comparer.
   418  func (p Int8Slice) CompareFunc(x interface{}) func(int) int {
   419  	e := x.(int8)
   420  	return func(i int) int {
   421  		return cmpInt8(p[i], e)
   422  	}
   423  }
   424  
   425  // RadixRange implements RadixInterface.
   426  func (p Int8Slice) RadixRange() (int, int) {
   427  	allBits := int8(^1)
   428  	var anyBits int8
   429  	for _, v := range p {
   430  		anyBits |= v
   431  		allBits &= v
   432  	}
   433  	bitMask := anyBits &^ allBits
   434  
   435  	end := 7 - bits.TrailingZeros8(uint8(bitMask))
   436  
   437  	if bitMask < 0 {
   438  		return 0, end
   439  	}
   440  
   441  	return bits.LeadingZeros8(uint8(bitMask)), end
   442  }
   443  
   444  // RadixFunc implements RadixInterface.
   445  func (p Int8Slice) RadixFunc(r int) RadixTest {
   446  	if r == 0 {
   447  		return func(i int) bool {
   448  			return p[i] >= 0
   449  		}
   450  	}
   451  
   452  	mask := int8(1) << uint(7-r)
   453  	return func(i int) bool {
   454  		return p[i]&mask != 0
   455  	}
   456  }
   457  
   458  // Sort is a convenience method.
   459  func (p Int8Slice) Sort() { radix(p) }
   460  
   461  // Radix is a convenience method.
   462  func (p Int8Slice) Radix() { radix(p) }
   463  
   464  // Search is a convenience method.
   465  func (p Int8Slice) Search(x int8) int { return SearchInt8s(p, x) }
   466  
   467  // SearchFor is a convenience method.
   468  func (p Int8Slice) SearchFor(x interface{}) int { return SearchInt8s(p, x.(int8)) }
   469  
   470  // Int8s sorts a slice of int8s in increasing order.
   471  func Int8s(a []int8) { radix(Int8Slice(a)) }
   472  
   473  // SearchInt8s searches for x in a sorted slice of int8s and returns the index as specified by sort.Search.
   474  // The return value is the index to insert x if x is not present (it could be len(a)).
   475  // The slice must be sorted in ascending order.
   476  func SearchInt8s(a []int8, x int8) int {
   477  	return sort.Search(len(a), func(i int) bool { return a[i] >= x })
   478  }
   479  
   480  // Int8sAreSorted tests whether a slice of int8s is sorted in increasing order.
   481  func Int8sAreSorted(a []int8) bool { return sort.IsSorted(Int8Slice(a)) }