unsafe_map.go 4.1 KB

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