github.com/jfcg/sorty@v1.2.0/sortyI.go (about)

     1  /*	Copyright (c) 2019, Serhat Şevki Dinçer.
     2  	This Source Code Form is subject to the terms of the Mozilla Public
     3  	License, v. 2.0. If a copy of the MPL was not distributed with this
     4  	file, You can obtain one at http://mozilla.org/MPL/2.0/.
     5  */
     6  
     7  package sorty
     8  
     9  import "unsafe"
    10  
    11  // IsSortedI returns 0 if ar is sorted in ascending order,
    12  // otherwise it returns i > 0 with ar[i] < ar[i-1]
    13  func IsSortedI(ar []int) int {
    14  	s := unsafe.Sizeof(int(0))
    15  	if s == 4 {
    16  		return IsSortedI4(*(*[]int32)(unsafe.Pointer(&ar)))
    17  	}
    18  	if s == 8 {
    19  		return IsSortedI8(*(*[]int64)(unsafe.Pointer(&ar)))
    20  	}
    21  	panic("sorty: IsSortedI: hw word size unknown")
    22  }
    23  
    24  // IsSortedU returns 0 if ar is sorted in ascending order,
    25  // otherwise it returns i > 0 with ar[i] < ar[i-1]
    26  func IsSortedU(ar []uint) int {
    27  	s := unsafe.Sizeof(uint(0))
    28  	if s == 4 {
    29  		return IsSortedU4(*(*[]uint32)(unsafe.Pointer(&ar)))
    30  	}
    31  	if s == 8 {
    32  		return IsSortedU8(*(*[]uint64)(unsafe.Pointer(&ar)))
    33  	}
    34  	panic("sorty: IsSortedU: hw word size unknown")
    35  }
    36  
    37  // IsSortedP returns 0 if ar is sorted in ascending order,
    38  // otherwise it returns i > 0 with ar[i] < ar[i-1]
    39  func IsSortedP(ar []uintptr) int {
    40  	s := unsafe.Sizeof(uintptr(0))
    41  	if s == 4 {
    42  		return IsSortedU4(*(*[]uint32)(unsafe.Pointer(&ar)))
    43  	}
    44  	if s == 8 {
    45  		return IsSortedU8(*(*[]uint64)(unsafe.Pointer(&ar)))
    46  	}
    47  	panic("sorty: IsSortedP: hw pointer size unknown")
    48  }
    49  
    50  // SortI concurrently sorts ar in ascending order.
    51  func SortI(ar []int) {
    52  	s := unsafe.Sizeof(int(0))
    53  	if s == 4 {
    54  		SortI4(*(*[]int32)(unsafe.Pointer(&ar)))
    55  		return
    56  	}
    57  	if s == 8 {
    58  		SortI8(*(*[]int64)(unsafe.Pointer(&ar)))
    59  		return
    60  	}
    61  	panic("sorty: SortI: hw word size unknown")
    62  }
    63  
    64  // SortU concurrently sorts ar in ascending order.
    65  func SortU(ar []uint) {
    66  	s := unsafe.Sizeof(uint(0))
    67  	if s == 4 {
    68  		SortU4(*(*[]uint32)(unsafe.Pointer(&ar)))
    69  		return
    70  	}
    71  	if s == 8 {
    72  		SortU8(*(*[]uint64)(unsafe.Pointer(&ar)))
    73  		return
    74  	}
    75  	panic("sorty: SortU: hw word size unknown")
    76  }
    77  
    78  // SortP concurrently sorts ar in ascending order.
    79  func SortP(ar []uintptr) {
    80  	s := unsafe.Sizeof(uintptr(0))
    81  	if s == 4 {
    82  		SortU4(*(*[]uint32)(unsafe.Pointer(&ar)))
    83  		return
    84  	}
    85  	if s == 8 {
    86  		SortU8(*(*[]uint64)(unsafe.Pointer(&ar)))
    87  		return
    88  	}
    89  	panic("sorty: SortP: hw pointer size unknown")
    90  }