go mysort 源码

  • 2022-07-15
  • 浏览 (1149)

golang mysort 代码

文件路径:/src/cmd/compile/internal/test/testdata/mysort/mysort.go

// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Generic sort function, tested with two different pointer types.

package mysort

import (
	"fmt"
)

type LessConstraint[T any] interface {
	Less(T) bool
}

//go:noinline
func Sort[T LessConstraint[T]](x []T) {
	n := len(x)
	for i := 1; i < n; i++ {
		for j := i; j > 0 && x[j].Less(x[j-1]); j-- {
			x[j], x[j-1] = x[j-1], x[j]
		}
	}
}

type MyInt struct {
	Value int
}

func (a *MyInt) Less(b *MyInt) bool {
	return a.Value < b.Value
}

//go:noinline
func F() {
	sl1 := []*MyInt{&MyInt{4}, &MyInt{3}, &MyInt{8}, &MyInt{7}}
	Sort(sl1)
	fmt.Printf("%v %v %v %v\n", sl1[0], sl1[1], sl1[2], sl1[3])
}

相关信息

go 源码目录

相关文章

go go_android_exec 源码

go argposition_test 源码

go badsym_test 源码

go errors_test 源码

go ptr_test 源码

go err1 源码

go err2 源码

go err4 源码

go issue11097a 源码

go issue11097b 源码

0  赞