github.com/moontrade/nogc@v0.1.7/collections/tree/art_cgo_tinygo.go (about) 1 //go:build tinygo && (darwin || linux || unix) && !tinygo.wasm 2 // +build tinygo 3 // +build darwin linux unix 4 // +build !tinygo.wasm 5 6 package tree 7 8 /* 9 #include "art.h" 10 #include <stdlib.h> 11 */ 12 import "C" 13 import ( 14 "github.com/moontrade/nogc" 15 "unsafe" 16 ) 17 18 type Tree C.art_tree 19 20 type Leaf C.art_leaf 21 22 func (l *Leaf) Data() nogc.Pointer { 23 return *(*nogc.Pointer)(unsafe.Pointer(l)) 24 } 25 func (l *Leaf) Key() nogc.FatPointer { 26 return nogc.FatPointerOf( 27 nogc.Pointer(uintptr(unsafe.Pointer(l))+unsafe.Sizeof(uintptr(0))+4), 28 uintptr(*(*uint32)(unsafe.Pointer(uintptr(unsafe.Pointer(l)) + unsafe.Sizeof(uintptr(0)))))) 29 } 30 31 //go:nosplit 32 //go:noescape 33 func New() (*Tree, int) { 34 tree := C.calloc(1, C.ulong(unsafe.Sizeof(C.art_tree{}))) 35 return (*Tree)(tree), int(C.art_tree_init((*C.art_tree)(tree))) 36 } 37 38 //go:nosplit 39 //go:noescape 40 func (t *Tree) Free() { 41 C.art_tree_destroy((*C.art_tree)(t)) 42 } 43 44 func (t *Tree) Size() int { 45 return int(*(*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(t)) + unsafe.Sizeof(uintptr(0))))) 46 } 47 48 type _string struct { 49 Data, Len uintptr 50 } 51 52 type _bytes struct { 53 Data, Len, Cap uintptr 54 } 55 56 //go:nosplit 57 //go:noescape 58 func (t *Tree) Insert(key nogc.Pointer, size int, value nogc.Pointer) nogc.Pointer { 59 return nogc.Pointer(C.art_insert((*C.art_tree)(t), (*C.uchar)(unsafe.Pointer(key)), C.int(size), unsafe.Pointer(value))) 60 } 61 62 func (t *Tree) InsertBytes(key nogc.Bytes, value nogc.Pointer) nogc.Pointer { 63 return t.Insert(key.Pointer, key.Len(), value) 64 } 65 66 func (t *Tree) InsertString(key string, value nogc.Pointer) nogc.Pointer { 67 k := (*_string)(unsafe.Pointer(&key)) 68 return t.Insert(nogc.Pointer(k.Data), int(k.Len), value) 69 } 70 71 func (t *Tree) InsertSlice(key []byte, value nogc.Pointer) nogc.Pointer { 72 k := (*_bytes)(unsafe.Pointer(&key)) 73 return t.Insert(nogc.Pointer(k.Data), int(k.Len), value) 74 } 75 76 //go:nosplit 77 //go:noescape 78 func (t *Tree) InsertNoReplace(key nogc.Pointer, size int, value nogc.Pointer) nogc.Pointer { 79 return nogc.Pointer(C.art_insert_no_replace((*C.art_tree)(t), (*C.uchar)(unsafe.Pointer(key)), C.int(size), unsafe.Pointer(value))) 80 } 81 82 func (t *Tree) InsertNoReplaceBytes(key nogc.Bytes, value nogc.Pointer) nogc.Pointer { 83 return t.InsertNoReplace(key.Pointer, key.Len(), value) 84 } 85 86 func (t *Tree) InsertNoReplaceString(key string, value nogc.Pointer) nogc.Pointer { 87 k := (*_string)(unsafe.Pointer(&key)) 88 return t.InsertNoReplace(nogc.Pointer(k.Data), int(k.Len), value) 89 } 90 91 func (t *Tree) InsertNoReplaceSlice(key []byte, value nogc.Pointer) nogc.Pointer { 92 k := (*_bytes)(unsafe.Pointer(&key)) 93 return t.InsertNoReplace(nogc.Pointer(k.Data), int(k.Len), value) 94 } 95 96 //go:nosplit 97 //go:noescape 98 func (t *Tree) Delete(key nogc.Pointer, size int) nogc.Pointer { 99 return nogc.Pointer(C.art_delete((*C.art_tree)(t), (*C.uchar)(unsafe.Pointer(key)), C.int(size))) 100 } 101 102 func (t *Tree) DeleteBytes(key nogc.Bytes) nogc.Pointer { 103 return t.Delete(key.Pointer, key.Len()) 104 } 105 106 //go:nosplit 107 //go:noescape 108 func (t *Tree) Find(key nogc.Pointer, size int) nogc.Pointer { 109 return nogc.Pointer(C.art_search((*C.art_tree)(t), (*C.uchar)(unsafe.Pointer(key)), C.int(size))) 110 } 111 112 func (t *Tree) FindBytes(key nogc.Bytes) nogc.Pointer { 113 return t.Find(key.Pointer, key.Len()) 114 } 115 116 //go:nosplit 117 //go:noescape 118 func (t *Tree) Minimum() nogc.Pointer { 119 return nogc.Pointer(unsafe.Pointer(C.art_minimum((*C.art_tree)(t)))) 120 } 121 122 //go:nosplit 123 //go:noescape 124 func (t *Tree) Maximum() nogc.Pointer { 125 return nogc.Pointer(unsafe.Pointer(C.art_maximum((*C.art_tree)(t)))) 126 }