Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CharacterMap{T}"/> class.
        /// </summary>
        /// <param name="maps">The states.</param>
        /// <exception cref="ArgumentNullException"></exception>
        public CharacterMap(IEnumerable <KeyValuePair <char, T> > maps)
        {
            if (maps is null)
            {
                ThrowHelper.ArgumentNullException(nameof(maps));
            }
            var charSet = new HashSet <char>();
            int maxChar = 0;

            foreach (var map in maps)
            {
                var openingChar = map.Key;
                charSet.Add(openingChar);

                if (openingChar < 128)
                {
                    maxChar = Math.Max(maxChar, openingChar);

                    if (openingChar == 0)
                    {
                        ThrowHelper.ArgumentOutOfRangeException("Null is not a valid opening character.", nameof(maps));
                    }
                }
                else
                {
                    nonAsciiMap ??= new Dictionary <uint, T>();
                }
            }

            OpeningCharacters = charSet.ToArray();
            Array.Sort(OpeningCharacters);

            asciiMap = new T[maxChar + 1];

            foreach (var state in maps)
            {
                char openingChar = state.Key;
                if (openingChar < 128)
                {
                    asciiMap[openingChar] ??= state.Value;
                    isOpeningCharacter.Set(openingChar);
                }
                else if (!nonAsciiMap !.ContainsKey(openingChar))
                {
                    nonAsciiMap[openingChar] = state.Value;
                }
            }

#if NETCOREAPP3_1_OR_GREATER
            if (nonAsciiMap is null)
            {
                long bitmap_0_3 = 0;
                long bitmap_4_7 = 0;

                foreach (char openingChar in OpeningCharacters)
                {
                    int position = (openingChar >> 4) | ((openingChar & 0x0F) << 3);
                    if (position < 64)
                    {
                        bitmap_0_3 |= 1L << position;
                    }
                    else
                    {
                        bitmap_4_7 |= 1L << (position - 64);
                    }
                }

                _asciiBitmap = Vector128.Create(bitmap_0_3, bitmap_4_7).AsByte();
            }
#endif
        }