public unsafe void Add(string str, int value)
        {
            var bytes = Encoding.UTF8.GetBytes(str);

            fixed(byte *buffer = &bytes[0])
            {
                var node = root;

                var p    = buffer;
                var rest = bytes.Length;

                while (rest != 0)
                {
                    var key = AutomataKeyGen.GetKey(ref p, ref rest);

                    if (rest == 0)
                    {
                        node = node.Add(key, value, str);
                    }
                    else
                    {
                        node = node.Add(key);
                    }
                }
            }
        }
            public unsafe AutomataNode SearchNext(ref byte *p, ref int rest)
            {
                var key = AutomataKeyGen.GetKey(ref p, ref rest);

                if (count < 4)
                {
                    // linear search
                    for (int i = 0; i < count; i++)
                    {
                        if (nextKeys[i] == key)
                        {
                            return(nexts[i]);
                        }
                    }
                }
                else
                {
                    // binary search
                    var index = BinarySearch(nextKeys, 0, count, key);
                    if (index >= 0)
                    {
                        return(nexts[index]);
                    }
                }

                return(null);
            }
Пример #3
0
            public AutomataNode SearchNext(ref ReadOnlySpan <byte> value)
            {
                var key = AutomataKeyGen.GetKey(ref value);

                if (count < 4)
                {
                    // linear search
                    for (int i = 0; i < count; i++)
                    {
                        if (nextKeys[i] == key)
                        {
                            return(nexts[i]);
                        }
                    }
                }
                else
                {
                    // binary search
                    var index = BinarySearch(nextKeys, 0, count, key);
                    if (index >= 0)
                    {
                        return(nexts[index]);
                    }
                }

                return(null);
            }
        public void Add(string str, int value)
        {
            ReadOnlySpan <byte> bytes = Encoding.UTF8.GetBytes(str);
            AutomataNode        node  = this.root;

            while (bytes.Length > 0)
            {
                var key = AutomataKeyGen.GetKey(ref bytes);

                if (bytes.Length == 0)
                {
                    node = node.Add(key, value, str);
                }
                else
                {
                    node = node.Add(key);
                }
            }
        }