public static NextHighestPowerOfTwo ( int v ) : int | ||
v | int | |
return | int |
public int CompareTo(object other) { if (other == null) { return(CompareOrdinal(this, null)); } if (other is UnmanagedString us) { return(CompareOrdinal(this, us)); } if (other is string s) { byte[] arr = null; Span <byte> stringAsBytes = stackalloc byte[0]; // relax the compiler var stringAsSpan = s.AsSpan(); var size = (ushort)Encoding.UTF8.GetByteCount(stringAsSpan); if (size <= 256) // allocate on the stack { stringAsBytes = stackalloc byte[size]; } else { var pooledSize = BitUtil.NextHighestPowerOfTwo(size); arr = ArrayPool <byte> .Shared.Rent(pooledSize); stringAsBytes = new Span <byte>(arr, 0, size); } try { Encoding.UTF8.GetBytes(stringAsSpan, stringAsBytes); return(CompareOrdinal(this, stringAsBytes)); } finally { if (arr != null) { ArrayPool <byte> .Shared.Return(arr); } } } throw new ArgumentException($"Unknown type {other.GetType()} for comparison"); }
/// <param name="tableSize"> Size of the hash table, should be a power of two. /// </param> /// <param name="maxChainLength"> Maximum length of each bucket, after which the oldest item inserted is dropped. /// </param> public SimpleStringInterner(int tableSize, int maxChainLength) { cache = new Entry[System.Math.Max(1, BitUtil.NextHighestPowerOfTwo(tableSize))]; this.maxChainLength = System.Math.Max(2, maxChainLength); }