-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringCache.cs
More file actions
177 lines (142 loc) · 5.21 KB
/
StringCache.cs
File metadata and controls
177 lines (142 loc) · 5.21 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Text;
using System.Threading;
namespace ecl.Collections {
public partial class StringCache : IEnumerable<string> {
public readonly StringCache Parent;
private Slot _slot;
public StringCache( StringCache parent ) {
Parent = parent;
_slot = new Slot( 16, 13 );
}
public StringCache()
: this( (StringCache)null ) {
}
public int Count => _slot.Count;
public StringCache( IEnumerable<string> str, StringCache parent ) {
HashSet<string> set;
if ( parent != null ) {
Parent = parent;
set = new HashSet<string>();
foreach ( var s in str ) {
if ( parent.Get( s ) == null ) {
set.Add( s );
}
}
} else {
set = new HashSet<string>( str );
}
_slot = set.Count > 0 ? new Slot( set ) : new Slot( 16, 13 );
}
public StringCache( params string[] args )
: this( args, (StringCache)null ) {
}
internal static readonly uint CaseSensitiveSeed = GetRandomUint( 0x15051505 );
private static uint GetRandomUint( int someSeed ) {
#if DEBUG
return (uint)someSeed;
#else
var rnd = new Random( Environment.TickCount ^ someSeed );
return (uint)HashCode.Combine( rnd.Next(), rnd.Next() );
#endif
}
public static int GetOrdinalHashCode( ReadOnlySpan<char> span ) {
uint hash = CaseSensitiveSeed;
foreach ( var ch in span ) {
hash = ( BitOperations.RotateLeft( hash, 5 ) + hash ) ^ ch;
}
return (int)hash;
}
public string Get( ReadOnlySpan<byte> key ) {
if ( key.Length < 256 ) {
Span<char> name = stackalloc char[ 512 ];
if ( Encoding.UTF8.GetChars( key, name ) == key.Length ) {
return Get( name );
}
}
return null;
}
public string GetF( ReadOnlySpan<char> key, int hashCode ) {
return _slot.Find( key, hashCode ) ?? Parent?.GetF( key, hashCode );
}
public string GetF( ReadOnlySpan<char> key ) {
int hashCode = GetOrdinalHashCode( key );
return GetF( key, hashCode );
}
private string Get( ReadOnlySpan<char> key, int hashCode ) {
var slot = Volatile.Read( ref _slot );
int idx = slot.IndexOf( key, hashCode );
return idx >= 0 ? slot[ idx ] : Parent?.Get( key, hashCode );
}
public string Get( ReadOnlySpan<char> key ) {
return Get( key, GetOrdinalHashCode( key ) );
}
public string GetOrAdd( ReadOnlySpan<char> key ) {
var hashCode = GetOrdinalHashCode( key );
return Get( key, hashCode ) ?? Add( key, hashCode );
}
public string GetOrAdd( ReadOnlySpan<byte> key ) {
if ( key.Length < 256 ) {
Span<char> name = stackalloc char[ 512 ];
if ( Encoding.UTF8.GetChars( key, name ) == key.Length ) {
return GetOrAdd( name );
}
}
return null;
}
public string GetOrAdd( string key ) {
var hashCode = GetOrdinalHashCode( key );
return Get( key, hashCode ) ?? Add( key, hashCode );
}
private readonly object _addition = new();
private string Add( ReadOnlySpan<char> key, int hashCode ) {
lock ( _addition ) {
return _slot.Insert( this, key, hashCode, null );
}
}
private string Add( string key, int hashCode ) {
lock ( _addition ) {
return _slot.Insert( this, key, hashCode, key );
}
}
public struct Enumerator : IEnumerator<string> {
private readonly Entry[] _entries;
private readonly int _count;
private int _index;
internal Enumerator( Entry[] entries, int count ) {
_entries = entries;
_count = count;
_index = -1;
}
public bool MoveNext() {
int idx = _index + 1;
if ( idx < _count ) {
_index = idx;
return true;
}
return false;
}
public void Reset() {
_index = -1;
}
public string Current => (uint)_index < (uint)_entries.Length? _entries[ _index ].Key:null;
object IEnumerator.Current => Current;
void IDisposable.Dispose() {
}
}
public Enumerator GetEnumerator() {
return _slot.GetEnumerator();
}
#region Implementation of IEnumerable
IEnumerator<string> IEnumerable<string>.GetEnumerator() {
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
#endregion
}
}