aboutsummaryrefslogtreecommitdiffstats
path: root/src/bindings/mono/eina_mono/eina_hash.cs
blob: 8b3c1e7db55477ea68e204a50d81eb2b0ff53d4f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#pragma warning disable 1591

using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;

using static Eina.TraitFunctions;
using static Eina.IteratorNativeFunctions;
using static Eina.HashNativeFunctions;
using Eina.Callbacks;

namespace Eina
{

[StructLayout(LayoutKind.Sequential)]
public struct HashTupleNative
{
    public IntPtr key;
    public IntPtr data;
    public uint   key_length;
}

public static class HashNativeFunctions
{
    [DllImport(efl.Libs.Eina)] public static extern IntPtr
        eina_hash_new(IntPtr key_length_cb, IntPtr key_cmp_cb, IntPtr key_hash_cb, IntPtr data_free_cb, int buckets_power_size);

    [DllImport(efl.Libs.Eina)] public static extern void
        eina_hash_free_cb_set(IntPtr hash, IntPtr data_free_cb);

    [DllImport(efl.Libs.Eina)] public static extern IntPtr
        eina_hash_string_djb2_new(IntPtr data_free_cb);

    [DllImport(efl.Libs.Eina)] public static extern IntPtr
        eina_hash_string_superfast_new(IntPtr data_free_cb);

    [DllImport(efl.Libs.Eina)] public static extern IntPtr
        eina_hash_string_small_new(IntPtr data_free_cb);

    [DllImport(efl.Libs.Eina)] public static extern IntPtr
        eina_hash_int32_new(IntPtr data_free_cb);

    [DllImport(efl.Libs.Eina)] public static extern IntPtr
        eina_hash_int64_new(IntPtr data_free_cb);

    [DllImport(efl.Libs.Eina)] public static extern IntPtr
        eina_hash_pointer_new(IntPtr data_free_cb);

    [DllImport(efl.Libs.Eina)] public static extern IntPtr
        eina_hash_stringshared_new(IntPtr data_free_cb);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
        eina_hash_add(IntPtr hash, IntPtr key, IntPtr data);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
        eina_hash_direct_add(IntPtr hash, IntPtr key, IntPtr data);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
        eina_hash_del(IntPtr hash, IntPtr key, IntPtr data);

    [DllImport(efl.Libs.Eina)] public static extern IntPtr
        eina_hash_find(IntPtr hash, IntPtr key);

    [DllImport(efl.Libs.Eina)] public static extern IntPtr
        eina_hash_modify(IntPtr hash, IntPtr key, IntPtr data);

    [DllImport(efl.Libs.Eina)] public static extern IntPtr
        eina_hash_set(IntPtr hash, IntPtr key, IntPtr data);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
        eina_hash_move(IntPtr hash, IntPtr old_key, IntPtr new_key);

    [DllImport(efl.Libs.Eina)] public static extern void
        eina_hash_free(IntPtr hash);

    [DllImport(efl.Libs.Eina)] public static extern void
        eina_hash_free_buckets(IntPtr hash);

    [DllImport(efl.Libs.Eina)] public static extern int
        eina_hash_population(IntPtr hash);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
        eina_hash_add_by_hash(IntPtr hash, IntPtr key, int key_length, int key_hash, IntPtr data);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
        eina_hash_direct_add_by_hash(IntPtr hash, IntPtr key, int key_length, int key_hash, IntPtr data);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
        eina_hash_del_by_key_hash(IntPtr hash, IntPtr key, int key_length, int key_hash);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
        eina_hash_del_by_key(IntPtr hash, IntPtr key);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
        eina_hash_del_by_data(IntPtr hash, IntPtr data);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] public static extern bool
        eina_hash_del_by_hash(IntPtr hash, IntPtr key, int key_length, int key_hash, IntPtr data);

    [DllImport(efl.Libs.Eina)] public static extern IntPtr
        eina_hash_find_by_hash(IntPtr hash, IntPtr key, int key_length, int key_hash);

    [DllImport(efl.Libs.Eina)] public static extern IntPtr
        eina_hash_modify_by_hash(IntPtr hash, IntPtr key, int key_length, int key_hash, IntPtr data);

    [DllImport(efl.Libs.Eina)] public static extern IntPtr
        eina_hash_iterator_key_new(IntPtr hash);

    [DllImport(efl.Libs.Eina)] public static extern IntPtr
        eina_hash_iterator_data_new(IntPtr hash);

    [DllImport(efl.Libs.Eina)] public static extern IntPtr
        eina_hash_iterator_tuple_new(IntPtr hash);

    [DllImport(efl.Libs.Eina)] public static extern void
        eina_hash_foreach(IntPtr hash, IntPtr func, IntPtr fdata);


    [DllImport(efl.Libs.Eina)] public static extern void
        eina_hash_list_append(IntPtr hash, IntPtr key, IntPtr data);
    [DllImport(efl.Libs.Eina)] public static extern void
        eina_hash_list_prepend(IntPtr hash, IntPtr key, IntPtr data);
    [DllImport(efl.Libs.Eina)] public static extern void
        eina_hash_list_remove(IntPtr hash, IntPtr key, IntPtr data);

    [DllImport(efl.Libs.Eina)] public static extern int
        eina_hash_superfast(string key, int len);

    [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
        eina_hash_iterator_ptr_key_wrapper_new_custom_export_mono(IntPtr hash);
}

public class Hash<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IDisposable
{
    public IntPtr Handle {get; set;} = IntPtr.Zero;
    public bool Own {get; set;}
    public bool OwnKey {get; set;}
    public bool OwnValue {get; set;}

    public int Count
    {
        get
        {
            return Population();
        }
    }


    private void InitNew()
    {
        Handle = EinaHashNew<TKey>();
        SetOwn(true);
        SetOwnKey(true);
        SetOwnValue(true);
    }

    public Hash()
    {
        InitNew();
    }

    public Hash(IntPtr handle, bool own)
    {
        Handle = handle;
        SetOwnership(own);
    }

    public Hash(IntPtr handle, bool own, bool ownKey, bool ownValue)
    {
        Handle = handle;
        SetOwnership(own, ownKey, ownValue);
    }

    ~Hash()
    {
        Dispose(false);
    }

    protected virtual void Dispose(bool disposing)
    {
        IntPtr h = Handle;
        Handle = IntPtr.Zero;
        if (h == IntPtr.Zero)
        {
            return;
        }

        if (Own)
        {
            if (disposing)
            {
                eina_hash_free(h);
            }
            else
            {
                Efl.Eo.Globals.ThreadSafeFreeCbExec(eina_hash_free, h);
            }
        }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    public void Free()
    {
        Dispose();
    }

    public IntPtr Release()
    {
        IntPtr h = Handle;
        Handle = IntPtr.Zero;
        return h;
    }

    public void SetOwn(bool own)
    {
        Own = own;
    }

    public void SetOwnKey(bool ownKey)
    {
        OwnKey = ownKey;
    }

    public void SetOwnValue(bool ownValue)
    {
        OwnValue = ownValue;

        if (ownValue)
        {
            eina_hash_free_cb_set(Handle, EinaFreeCb<TValue>());
        }
    }

    public void SetOwnership(bool ownAll)
    {
        SetOwn(ownAll);
        SetOwnKey(ownAll);
        SetOwnValue(ownAll);
    }

    public void SetOwnership(bool own, bool ownKey, bool ownValue)
    {
        SetOwn(own);
        SetOwnKey(ownKey);
        SetOwnValue(ownValue);
    }

    public void UnSetFreeCb()
    {
        eina_hash_free_cb_set(Handle, IntPtr.Zero);
    }

    public bool AddNew(TKey key, TValue val)
    {
        IntPtr gchnk = CopyNativeObject(key, ForceRefKey<TKey>());
        IntPtr nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
        IntPtr gchnv = CopyNativeObject(val, false);
        IntPtr nv = GetNativePtr<TValue>(gchnv, false);
        var r = eina_hash_add(Handle, nk, nv);
        FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
        FreeNativeIndirection<TValue>(gchnv, false);
        return r;
    }

    public void Add(TKey key, TValue val)
    {
        Set(key, val);
    }

    public bool DelByKey(TKey key)
    {
        IntPtr gchnk = CopyNativeObject(key, ForceRefKey<TKey>());
        IntPtr nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
        var r = eina_hash_del_by_key(Handle, nk);
        FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
        // NativeFreeRef<TKey>(nk, OwnKey && r);
        return r;
    }

    public bool DelByValue(TValue val)
    {
        IntPtr gchnv = CopyNativeObject(val, false);
        IntPtr nv = GetNativePtr<TValue>(gchnv, false);
        var r = eina_hash_del_by_data(Handle, nv);
        FreeNativeIndirection<TValue>(gchnv, false);
        return r;
    }

    public void Remove(TKey key)
    {
        DelByKey(key);
    }

    public TValue Find(TKey key)
    {
        var gchnk = CopyNativeObject<TKey>(key, ForceRefKey<TKey>());
        var nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
        var found = eina_hash_find(Handle, nk);
        //NativeFreeRef<TKey>(nk);
        FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
        if (found == IntPtr.Zero)
        {
            throw new KeyNotFoundException();
        }

        return NativeToManaged<TValue>(IndirectNative<TValue>(found, false));
    }

    public bool TryGetValue(TKey key, out TValue val)
    {
        var gchnk = CopyNativeObject<TKey>(key, ForceRefKey<TKey>());
        var nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
        var found = eina_hash_find(Handle, nk);
        FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
        if (found == IntPtr.Zero)
        {
            val = default(TValue);
            return false;
        }

        val = NativeToManaged<TValue>(IndirectNative<TValue>(found, false));
        return true;
    }

    public bool ContainsKey(TKey key)
    {
        var gchnk = CopyNativeObject<TKey>(key, ForceRefKey<TKey>());
        var nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
        // var nk = ManagedToNativeAllocRef(key);
        var found = eina_hash_find(Handle, nk);
        // NativeFreeRef<TKey>(nk);
        FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
        return found != IntPtr.Zero;
    }

    public bool Modify(TKey key, TValue val)
    {
        var gchnk = CopyNativeObject<TKey>(key, ForceRefKey<TKey>());
        var nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
        var gchnv = CopyNativeObject<TValue>(val, false);
        var nv = GetNativePtr<TValue>(gchnv, false);
        var old = eina_hash_modify(Handle, nk, nv);
        FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
        // NativeFreeRef<TKey>(nk);
        if (old == IntPtr.Zero)
        {
            NativeFree<TValue>(nv);
            return false;
        }

        if (OwnValue)
        {
            NativeFree<TValue>(old);
        }

        return true;
    }

    private static bool ForceRefKey<T>()
    {
        return (!typeof(T).IsValueType) && (typeof(T) != typeof(string));
    }

    private static IntPtr CopyNativeObject<T>(T value, bool forceRef)
    {
        if (!IsEflObject(typeof(T)) && forceRef)
        {
            GCHandle gch = GCHandle.Alloc(new byte[Marshal.SizeOf<T>()], GCHandleType.Pinned);
            IntPtr pin = gch.AddrOfPinnedObject();

            ManagedToNativeCopyTo(value, pin);

            return GCHandle.ToIntPtr(gch);
        }
        else if (IsEflObject(typeof(T)) && forceRef)
        {
            GCHandle gch = GCHandle.Alloc(new byte[Marshal.SizeOf<IntPtr>()], GCHandleType.Pinned);
            IntPtr pin = gch.AddrOfPinnedObject();

            ManagedToNativeCopyTo(value, pin);

            return GCHandle.ToIntPtr(gch);
        }
        else
        {
            return ManagedToNativeAlloc(value);
        }
    }

    private static IntPtr GetNativePtr<T>(IntPtr gchptr, bool forceRef)
    {
        if (forceRef)
        {
            GCHandle gch = GCHandle.FromIntPtr(gchptr);
            IntPtr pin = gch.AddrOfPinnedObject();

            return pin;
        }
        else
        {
            return gchptr;
        }
    }

    private static void FreeNativeIndirection<T>(IntPtr gchptr, bool forceRef)
    {
        if (forceRef)
        {
            GCHandle gch = GCHandle.FromIntPtr(gchptr);
            gch.Free();
        }
    }

    private static IntPtr IndirectNative<T>(IntPtr ptr, bool forceRef)
    {
        if (forceRef)
        {
            IntPtr val = Marshal.ReadIntPtr(ptr);
            return val;
        }
        else
        {
            return ptr;
        }
    }

    public void Set(TKey key, TValue val)
    {
        IntPtr gchnk = CopyNativeObject(key, ForceRefKey<TKey>());
        IntPtr nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());

        IntPtr gchnv = CopyNativeObject(val, false);
        IntPtr nv = GetNativePtr<TValue>(gchnv, false);
        IntPtr old = eina_hash_set(Handle, nk, nv);
        FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
        FreeNativeIndirection<TValue>(gchnv, false);
        if (OwnValue || old != IntPtr.Zero)
        {
            NativeFree<TValue>(old);
        }
    }

    public TValue this[TKey key]
    {
        get
        {
            return Find(key);
        }
        set
        {
            Set(key, value);
        }
    }

    public bool Move(TKey key_old, TKey key_new)
    {
        IntPtr gchnko = CopyNativeObject(key_old, ForceRefKey<TKey>());
        IntPtr nko = GetNativePtr<TKey>(gchnko, ForceRefKey<TKey>());
        IntPtr gchnk = CopyNativeObject(key_new, ForceRefKey<TKey>());
        IntPtr nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
        // var nk_old = ManagedToNativeAllocRef(key_old);
        // var nk_new = ManagedToNativeAllocRef(key_new, true);
        var r = eina_hash_move(Handle, nko, nk);
        FreeNativeIndirection<TKey>(gchnko, ForceRefKey<TKey>());
        FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
        // NativeFreeRef<TKey>(nk_old, OwnKey && r);
        // NativeFreeRef<TKey>(nk_new, !r);
        return r;
    }

    public void FreeBuckets()
    {
        eina_hash_free_buckets(Handle);
    }

    public int Population()
    {
        return eina_hash_population(Handle);
    }

    public Eina.Iterator<TKey> Keys()
    {
        return new Eina.Iterator<TKey>(EinaHashIteratorKeyNew<TKey>(Handle), true, false);
    }

    public Eina.Iterator<TValue> Values()
    {
        return new Eina.Iterator<TValue>(eina_hash_iterator_data_new(Handle), true, false);
    }

    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
    {
        IntPtr itr = eina_hash_iterator_tuple_new(Handle);
        try
        {
            for (IntPtr tuplePtr; eina_iterator_next(itr, out tuplePtr);)
            {
                var tuple = Marshal.PtrToStructure<Eina.HashTupleNative>(tuplePtr);
                IntPtr ikey = IndirectNative<TKey>(tuple.key, ForceRefKey<TKey>());
                var key = NativeToManaged<TKey>(ikey);
                var val = NativeToManaged<TValue>(tuple.data);
                yield return new KeyValuePair<TKey, TValue>(key, val);
            }
        }
        finally
        {
            eina_iterator_free(itr);
        }
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}

}