| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Text; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | |
| | | 7 | | namespace DeltaSharp.Util.Hashtable; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Specialized hashtable to store landmarks and respective collisions |
| | | 11 | | /// Because we hash at every 8 bytes, we can store the address as multiple of 8 |
| | | 12 | | /// To improve memory usage, there are specialized implementations according to data size |
| | | 13 | | /// Byte-sized indices can address 255 complete 8-byte blocks, including sources |
| | | 14 | | /// up to 2047 bytes long with a partial final block. |
| | | 15 | | /// </summary> |
| | | 16 | | internal readonly ref struct Hashtable64 |
| | | 17 | | { |
| | | 18 | | internal const int BytesPerBlock = 8; |
| | 0 | 19 | | public int BlockSize => BytesPerBlock; |
| | | 20 | | |
| | | 21 | | private readonly Hashtable64_8 _hashtable8; |
| | | 22 | | private readonly Hashtable64_16 _hashtable16; |
| | | 23 | | #if DELTASHARP_HASHTABLE24 |
| | | 24 | | private readonly Hashtable64_24 _hashtable24; |
| | | 25 | | #endif |
| | | 26 | | private readonly Hashtable64_32 _hashtable32; |
| | | 27 | | private readonly byte _hashtable; |
| | | 28 | | |
| | | 29 | | internal static byte GetIndexWidth(int sourceLength) |
| | | 30 | | { |
| | | 31 | | // Zero is the empty-slot sentinel; stored block indices are one-based. |
| | 6930 | 32 | | if (sourceLength / BytesPerBlock <= byte.MaxValue) |
| | 3910 | 33 | | return 8; |
| | 3020 | 34 | | if (sourceLength < ushort.MaxValue * BytesPerBlock) |
| | 2930 | 35 | | return 16; |
| | | 36 | | #if DELTASHARP_HASHTABLE24 |
| | 45 | 37 | | if (sourceLength < 0xff_ff_ff * BytesPerBlock) |
| | 40 | 38 | | return 24; |
| | | 39 | | #endif |
| | 50 | 40 | | return 32; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | public Hashtable64(ReadOnlySpan<byte> data) |
| | | 44 | | { |
| | 2766 | 45 | | _hashtable = GetIndexWidth(data.Length); |
| | 2766 | 46 | | switch (_hashtable) |
| | | 47 | | { |
| | 3144 | 48 | | case 8: _hashtable8 = new Hashtable64_8(data); break; |
| | 2340 | 49 | | case 16: _hashtable16 = new Hashtable64_16(data); break; |
| | | 50 | | #if DELTASHARP_HASHTABLE24 |
| | 24 | 51 | | case 24: _hashtable24 = new Hashtable64_24(data); break; |
| | | 52 | | #endif |
| | 12 | 53 | | default: _hashtable32 = new Hashtable64_32(data); break; |
| | | 54 | | } |
| | 12 | 55 | | } |
| | | 56 | | |
| | | 57 | | public int Match(ReadOnlySpan<byte> data) |
| | | 58 | | { |
| | 2016854 | 59 | | switch (_hashtable) |
| | | 60 | | { |
| | 159064 | 61 | | case 8: return _hashtable8.Match(data); |
| | 1482634 | 62 | | case 16: return _hashtable16.Match(data); |
| | | 63 | | #if DELTASHARP_HASHTABLE24 |
| | 187578 | 64 | | case 24: return _hashtable24.Match(data); |
| | | 65 | | #endif |
| | 187578 | 66 | | default: return _hashtable32.Match(data); |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | public int More(int pos) |
| | | 71 | | { |
| | 104131352 | 72 | | switch (_hashtable) |
| | | 73 | | { |
| | 335688 | 74 | | case 8: return _hashtable8.More(pos); |
| | 103349600 | 75 | | case 16: return _hashtable16.More(pos); |
| | | 76 | | #if DELTASHARP_HASHTABLE24 |
| | 223032 | 77 | | case 24: return _hashtable24.More(pos); |
| | | 78 | | #endif |
| | 223032 | 79 | | default: return _hashtable32.More(pos); |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | |
| | | 84 | | |
| | | 85 | | public void Dispose() |
| | | 86 | | { |
| | 2766 | 87 | | switch (_hashtable) |
| | | 88 | | { |
| | 3144 | 89 | | case 8: _hashtable8.Dispose(); break; |
| | 2340 | 90 | | case 16: _hashtable16.Dispose(); break; |
| | | 91 | | #if DELTASHARP_HASHTABLE24 |
| | 24 | 92 | | case 24: _hashtable24.Dispose(); break; |
| | | 93 | | #endif |
| | 12 | 94 | | default: _hashtable32.Dispose(); break; |
| | | 95 | | } |
| | 12 | 96 | | } |
| | | 97 | | } |