Esempio n. 1
0
        public bool TryGetValue(long target, out SkipListNode <T> node)
        {
            node = null;

            var cur = _header;

            while (cur != null)
            {
                while (cur.Right != null && cur.Right.Value < target)
                {
                    cur = cur.Right;
                }

                if (cur.Right != null && cur.Right.Value == target)
                {
                    node = cur.Right;
                    while (node.Down != null)
                    {
                        node = node.Down;
                    }
                    return(true);
                }

                cur = cur.Down;
            }

            return(false);
        }
Esempio n. 2
0
        public void Add(long target, T obj)
        {
            var rLevel = 1;

            while (rLevel <= _level && _random.Next(2) == 0)
            {
                ++rLevel;
            }

            if (rLevel > _level)
            {
                _level = rLevel;

                _header = Fetch().Init(target, obj, null, _header);
            }

            SkipListNode <T> cur = _header, last = null;

            for (var l = _level; l >= 1; --l)
            {
                while (cur.Right != null && cur.Right.Value < target)
                {
                    cur = cur.Right;
                }

                if (l <= rLevel)
                {
                    cur.Right = Fetch().Init(target, obj, cur.Right, null);

                    if (last != null)
                    {
                        last.Down = cur.Right;
                    }

                    last = cur.Right;
                }

                cur = cur.Down;
            }
        }
Esempio n. 3
0
        private void Recycle(SkipListNode <T> node)
        {
            node.Obj = default;

            _pool.Enqueue(node);
        }