gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/maps/unsafe/unsafe.go (about)

     1  package unsafe
     2  
     3  import "unsafe"
     4  
     5  type iter struct {
     6  	key         unsafe.Pointer
     7  	elem        unsafe.Pointer
     8  	t           unsafe.Pointer
     9  	h           unsafe.Pointer
    10  	buckets     unsafe.Pointer
    11  	bptr        unsafe.Pointer
    12  	overflow    unsafe.Pointer
    13  	oldoverflow unsafe.Pointer
    14  	startBucket uintptr
    15  	offset      uint8
    16  	wrapped     bool
    17  	B           uint8
    18  	i           uint8
    19  	bucket      uintptr
    20  	checkBucket uintptr
    21  }
    22  
    23  type Iter[K comparable, V any] iter
    24  
    25  //go:linkname mapiterinit runtime.mapiterinit
    26  func mapiterinit(t unsafe.Pointer, h unsafe.Pointer, it *iter)
    27  
    28  //go:linkname mapiternext runtime.mapiternext
    29  func mapiternext(it *iter)
    30  
    31  func NewIter[K comparable, V any](m map[K]V) *Iter[K, V] {
    32  	mi := any(m)
    33  	hi := new(iter)
    34  	mapiterinit(*(*unsafe.Pointer)(unsafe.Pointer(&mi)), *(*unsafe.Pointer)(unsafe.Pointer(&m)), hi)
    35  	return (*Iter[K, V])(hi)
    36  }
    37  
    38  func (i *Iter[K, V]) Next() {
    39  	mapiternext((*iter)(i))
    40  }
    41  
    42  func (i *Iter[K, V]) Has() bool {
    43  	return i.key != nil
    44  }
    45  
    46  func (i *Iter[K, V]) Key() K {
    47  	return *(*K)(i.key)
    48  }
    49  
    50  func (i *Iter[K, V]) Value() V {
    51  	return *(*V)(i.elem)
    52  }