unsafe_map.go (3975B)
1 package reflect2 2 3 import ( 4 "reflect" 5 "unsafe" 6 ) 7 8 type UnsafeMapType struct { 9 unsafeType 10 pKeyRType unsafe.Pointer 11 pElemRType unsafe.Pointer 12 } 13 14 func newUnsafeMapType(cfg *frozenConfig, type1 reflect.Type) MapType { 15 return &UnsafeMapType{ 16 unsafeType: *newUnsafeType(cfg, type1), 17 pKeyRType: unpackEFace(reflect.PtrTo(type1.Key())).data, 18 pElemRType: unpackEFace(reflect.PtrTo(type1.Elem())).data, 19 } 20 } 21 22 func (type2 *UnsafeMapType) IsNil(obj interface{}) bool { 23 if obj == nil { 24 return true 25 } 26 objEFace := unpackEFace(obj) 27 assertType("Type.IsNil argument 1", type2.ptrRType, objEFace.rtype) 28 return type2.UnsafeIsNil(objEFace.data) 29 } 30 31 func (type2 *UnsafeMapType) UnsafeIsNil(ptr unsafe.Pointer) bool { 32 if ptr == nil { 33 return true 34 } 35 return *(*unsafe.Pointer)(ptr) == nil 36 } 37 38 func (type2 *UnsafeMapType) LikePtr() bool { 39 return true 40 } 41 42 func (type2 *UnsafeMapType) Indirect(obj interface{}) interface{} { 43 objEFace := unpackEFace(obj) 44 assertType("MapType.Indirect argument 1", type2.ptrRType, objEFace.rtype) 45 return type2.UnsafeIndirect(objEFace.data) 46 } 47 48 func (type2 *UnsafeMapType) UnsafeIndirect(ptr unsafe.Pointer) interface{} { 49 return packEFace(type2.rtype, *(*unsafe.Pointer)(ptr)) 50 } 51 52 func (type2 *UnsafeMapType) Key() Type { 53 return type2.cfg.Type2(type2.Type.Key()) 54 } 55 56 func (type2 *UnsafeMapType) MakeMap(cap int) interface{} { 57 return packEFace(type2.ptrRType, type2.UnsafeMakeMap(cap)) 58 } 59 60 func (type2 *UnsafeMapType) UnsafeMakeMap(cap int) unsafe.Pointer { 61 m := makeMapWithSize(type2.rtype, cap) 62 return unsafe.Pointer(&m) 63 } 64 65 func (type2 *UnsafeMapType) SetIndex(obj interface{}, key interface{}, elem interface{}) { 66 objEFace := unpackEFace(obj) 67 assertType("MapType.SetIndex argument 1", type2.ptrRType, objEFace.rtype) 68 keyEFace := unpackEFace(key) 69 assertType("MapType.SetIndex argument 2", type2.pKeyRType, keyEFace.rtype) 70 elemEFace := unpackEFace(elem) 71 assertType("MapType.SetIndex argument 3", type2.pElemRType, elemEFace.rtype) 72 type2.UnsafeSetIndex(objEFace.data, keyEFace.data, elemEFace.data) 73 } 74 75 func (type2 *UnsafeMapType) UnsafeSetIndex(obj unsafe.Pointer, key unsafe.Pointer, elem unsafe.Pointer) { 76 mapassign(type2.rtype, *(*unsafe.Pointer)(obj), key, elem) 77 } 78 79 func (type2 *UnsafeMapType) TryGetIndex(obj interface{}, key interface{}) (interface{}, bool) { 80 objEFace := unpackEFace(obj) 81 assertType("MapType.TryGetIndex argument 1", type2.ptrRType, objEFace.rtype) 82 keyEFace := unpackEFace(key) 83 assertType("MapType.TryGetIndex argument 2", type2.pKeyRType, keyEFace.rtype) 84 elemPtr := type2.UnsafeGetIndex(objEFace.data, keyEFace.data) 85 if elemPtr == nil { 86 return nil, false 87 } 88 return packEFace(type2.pElemRType, elemPtr), true 89 } 90 91 func (type2 *UnsafeMapType) GetIndex(obj interface{}, key interface{}) interface{} { 92 objEFace := unpackEFace(obj) 93 assertType("MapType.GetIndex argument 1", type2.ptrRType, objEFace.rtype) 94 keyEFace := unpackEFace(key) 95 assertType("MapType.GetIndex argument 2", type2.pKeyRType, keyEFace.rtype) 96 elemPtr := type2.UnsafeGetIndex(objEFace.data, keyEFace.data) 97 return packEFace(type2.pElemRType, elemPtr) 98 } 99 100 func (type2 *UnsafeMapType) UnsafeGetIndex(obj unsafe.Pointer, key unsafe.Pointer) unsafe.Pointer { 101 return mapaccess(type2.rtype, *(*unsafe.Pointer)(obj), key) 102 } 103 104 func (type2 *UnsafeMapType) Iterate(obj interface{}) MapIterator { 105 objEFace := unpackEFace(obj) 106 assertType("MapType.Iterate argument 1", type2.ptrRType, objEFace.rtype) 107 return type2.UnsafeIterate(objEFace.data) 108 } 109 110 type UnsafeMapIterator struct { 111 *hiter 112 pKeyRType unsafe.Pointer 113 pElemRType unsafe.Pointer 114 } 115 116 func (iter *UnsafeMapIterator) HasNext() bool { 117 return iter.key != nil 118 } 119 120 func (iter *UnsafeMapIterator) Next() (interface{}, interface{}) { 121 key, elem := iter.UnsafeNext() 122 return packEFace(iter.pKeyRType, key), packEFace(iter.pElemRType, elem) 123 } 124 125 func (iter *UnsafeMapIterator) UnsafeNext() (unsafe.Pointer, unsafe.Pointer) { 126 key := iter.key 127 elem := iter.value 128 mapiternext(iter.hiter) 129 return key, elem 130 }