示例#1
0
        public bool Remove(T key)
        {
            unsafe
            {
                fixed(byte *thisPtr = SwiftData)
                {
                    var thisIntPtr = new IntPtr(thisPtr);

                    StructMarshal.Marshaler.RetainNominalData(typeof(SwiftDictionary <T, U>), thisIntPtr, SwiftData.Length);
                    byte *keyBuffer    = stackalloc byte [StructMarshal.Marshaler.Sizeof(typeof(T))];
                    var   keyBufferPtr = new IntPtr(keyBuffer);

                    StructMarshal.Marshaler.ToSwift(typeof(T), key, keyBufferPtr);
                    bool retval = DictPI.DictRemove(thisIntPtr, keyBufferPtr,
                                                    StructMarshal.Marshaler.Metatypeof(typeof(T)),
                                                    StructMarshal.Marshaler.Metatypeof(typeof(U)),
                                                    StructMarshal.Marshaler.ProtocolWitnessof(typeof(ISwiftHashable), typeof(T)));

                    StructMarshal.Marshaler.ReleaseSwiftPointer(typeof(SwiftDictionary <T, U>), thisIntPtr);
                    StructMarshal.Marshaler.ReleaseSwiftPointer(typeof(T), keyBufferPtr);

                    return(retval);
                }
            }
        }
示例#2
0
        public U this [T key] {
            get {
                unsafe {
                    Type retType = typeof(Tuple <U, bool>);

                    fixed(byte *thisPtr = SwiftData)
                    {
                        var thisIntPtr = new IntPtr(thisPtr);

                        byte *retBuff = stackalloc byte [StructMarshal.Marshaler.Sizeof(retType)];
                        var   retPtr  = new IntPtr(retBuff);

                        byte *keyBuffer    = stackalloc byte [StructMarshal.Marshaler.Sizeof(typeof(T))];
                        var   keyBufferPtr = new IntPtr(keyBuffer);

                        StructMarshal.Marshaler.ToSwift(typeof(T), key, keyBufferPtr);

                        DictPI.DictGet(retPtr, thisIntPtr, keyBufferPtr,
                                       StructMarshal.Marshaler.Metatypeof(typeof(T)),
                                       StructMarshal.Marshaler.Metatypeof(typeof(U)),
                                       StructMarshal.Marshaler.ProtocolWitnessof(typeof(ISwiftHashable), typeof(T)));
                        var retTuple = StructMarshal.Marshaler.ToNet <Tuple <U, bool> > (retPtr);

                        StructMarshal.Marshaler.ReleaseSwiftPointer(typeof(T), keyBufferPtr);
                        if (retTuple.Item2)
                        {
                            return(retTuple.Item1);
                        }
                        throw new KeyNotFoundException($"key {key} not found");
                    }
                }
            }

            set {
                unsafe
                {
                    fixed(byte *thisPtr = SwiftData)
                    {
                        var   thisIntPtr   = new IntPtr(thisPtr);
                        byte *keyBuffer    = stackalloc byte [StructMarshal.Marshaler.Sizeof(typeof(T))];
                        var   keyBufferPtr = new IntPtr(keyBuffer);

                        StructMarshal.Marshaler.ToSwift(typeof(T), key, keyBufferPtr);
                        byte *valBuffer    = stackalloc byte [StructMarshal.Marshaler.Sizeof(typeof(U))];
                        var   valBufferPtr = new IntPtr(valBuffer);

                        StructMarshal.Marshaler.ToSwift(typeof(U), value, valBufferPtr);
                        DictPI.DictSet(thisIntPtr, keyBufferPtr, valBufferPtr,
                                       StructMarshal.Marshaler.Metatypeof(typeof(T)),
                                       StructMarshal.Marshaler.Metatypeof(typeof(U)),
                                       StructMarshal.Marshaler.ProtocolWitnessof(typeof(ISwiftHashable), typeof(T)));
                        StructMarshal.Marshaler.ReleaseSwiftPointer(typeof(T), keyBufferPtr);
                        StructMarshal.Marshaler.ReleaseSwiftPointer(typeof(T), valBufferPtr);
                    }
                }
            }
        }
示例#3
0
 public SwiftDictionary(int capacity)
     : this(SwiftNominalCtorArgument.None)
 {
     unsafe
     {
         fixed(byte *retvalData = StructMarshal.Marshaler.PrepareNominal(this))
         {
             DictPI.NewDict(new IntPtr(retvalData), capacity, StructMarshal.Marshaler.Metatypeof(typeof(T)),
                            StructMarshal.Marshaler.Metatypeof(typeof(U)),
                            StructMarshal.Marshaler.ProtocolWitnessof(typeof(ISwiftHashable), typeof(T)));
             StructMarshal.Marshaler.RetainNominalData(typeof(SwiftDictionary <T, U>), retvalData, SwiftData.Length);
         }
     }
 }
示例#4
0
        public void Clear()
        {
            unsafe
            {
                fixed(byte *thisPtr = SwiftData)
                {
                    IntPtr thisIntPtr = new IntPtr(thisPtr);

                    StructMarshal.Marshaler.RetainNominalData(typeof(SwiftDictionary <T, U>), thisIntPtr, SwiftData.Length);
                    DictPI.DictClear(thisIntPtr, StructMarshal.Marshaler.Metatypeof(typeof(T)),
                                     StructMarshal.Marshaler.Metatypeof(typeof(U)),
                                     StructMarshal.Marshaler.ProtocolWitnessof(typeof(ISwiftHashable), typeof(T)));
                    StructMarshal.Marshaler.ReleaseSwiftPointer(typeof(SwiftDictionary <T, U>), thisIntPtr);
                }
            }
        }
示例#5
0
 public static SwiftMetatype GetSwiftMetatype()
 {
     return(DictPI.PIMetadataAccessor_SwiftDictionary(SwiftMetadataRequest.Complete, StructMarshal.Marshaler.Metatypeof(typeof(T)),
                                                      StructMarshal.Marshaler.Metatypeof(typeof(U)),
                                                      StructMarshal.Marshaler.ProtocolWitnessof(typeof(ISwiftHashable), typeof(T))));
 }