NextHighestPowerOfTwo() public static method

returns the next highest power of two, or the current value if it's already a power of two or zero
public static NextHighestPowerOfTwo ( int v ) : int
v int
return int
示例#1
0
            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);
 }