demo: qsort
This commit is contained in:
@@ -36,7 +36,8 @@ go install -v ./...
|
|||||||
|
|
||||||
The `_demo` directory contains our demos (it start with `_` to prevent the `go` command from compiling it):
|
The `_demo` directory contains our demos (it start with `_` to prevent the `go` command from compiling it):
|
||||||
|
|
||||||
* genints: closure usage in llgo
|
* [qsort](_demo/qsort): call C function with callback
|
||||||
|
* [genints](_demo/genints): closure usage in llgo
|
||||||
|
|
||||||
### How to run demos
|
### How to run demos
|
||||||
|
|
||||||
|
|||||||
17
_demo/qsort/qsort.go
Normal file
17
_demo/qsort/qsort.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/goplus/llgo/c"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a := [...]int{100, 8, 23, 2, 7}
|
||||||
|
c.Qsort(c.Pointer(&a), 5, unsafe.Sizeof(0), func(a, b c.Pointer) c.Int {
|
||||||
|
return c.Int(*(*int)(a) - *(*int)(b))
|
||||||
|
})
|
||||||
|
for _, v := range a {
|
||||||
|
c.Printf(c.Str("%d\n"), v)
|
||||||
|
}
|
||||||
|
}
|
||||||
3
c/c.go
3
c/c.go
@@ -71,3 +71,6 @@ func Printf(format *Char, __llgo_va_list ...any) Int
|
|||||||
|
|
||||||
//go:linkname Fprintf C.fprintf
|
//go:linkname Fprintf C.fprintf
|
||||||
func Fprintf(fp FilePtr, format *Char, __llgo_va_list ...any) Int
|
func Fprintf(fp FilePtr, format *Char, __llgo_va_list ...any) Int
|
||||||
|
|
||||||
|
//go:linkname Qsort C.qsort
|
||||||
|
func Qsort(base Pointer, count, elem uintptr, compar func(a, b Pointer) Int)
|
||||||
|
|||||||
Reference in New Issue
Block a user