Init() public method

public Init ( ) : void
return void
Beispiel #1
0
        public void Init()
        {
            Stack <StackItem> stack = new Stack <StackItem>();
            uint num  = 2016473283u;
            int  num2 = 0;
            int  num3 = _size - 1;

            _order = new int[_size + 1];
            int num4 = 0;

            for (int i = num2; i <= num3; i++)
            {
                _order[i] = num4;
                num4++;
            }
            stack.Push(new StackItem
            {
                p = num2,
                r = num3
            });
            while (stack.Count > 0)
            {
                StackItem stackItem = stack.Pop();
                num2 = stackItem.p;
                num3 = stackItem.r;
                while (num3 > num2 + 10)
                {
                    num = num * 1539415821 + 1;
                    int i = num2 + (int)((long)num % (long)(num3 - num2 + 1));
                    num4         = _order[i];
                    _order[i]    = _order[num2];
                    _order[num2] = num4;
                    i            = num2 - 1;
                    int num5 = num3 + 1;
                    while (true)
                    {
                        i++;
                        if (_leq(_keys[_order[i]], _keys[num4]))
                        {
                            do
                            {
                                num5--;
                            }while (!_leq(_keys[num4], _keys[_order[num5]]));
                            Swap(ref _order[i], ref _order[num5]);
                            if (i >= num5)
                            {
                                break;
                            }
                        }
                    }
                    Swap(ref _order[i], ref _order[num5]);
                    if (i - num2 < num3 - num5)
                    {
                        stack.Push(new StackItem
                        {
                            p = num5 + 1,
                            r = num3
                        });
                        num3 = i - 1;
                    }
                    else
                    {
                        stack.Push(new StackItem
                        {
                            p = num2,
                            r = i - 1
                        });
                        num2 = num5 + 1;
                    }
                }
                for (int i = num2 + 1; i <= num3; i++)
                {
                    num4 = _order[i];
                    int num5 = i;
                    while (num5 > num2 && !_leq(_keys[num4], _keys[_order[num5 - 1]]))
                    {
                        _order[num5] = _order[num5 - 1];
                        num5--;
                    }
                    _order[num5] = num4;
                }
            }
            _max         = _size;
            _initialized = true;
            _heap.Init();
        }
Beispiel #2
0
        public void Init()
        {
            var  stack = new Stack <StackItem>();
            int  p, r, i, j, piv;
            uint seed = 2016473283;

            p      = 0;
            r      = _size - 1;
            _order = new int[_size + 1];
            for (piv = 0, i = p; i <= r; ++piv, ++i)
            {
                _order[i] = piv;
            }

            stack.Push(new StackItem {
                p = p, r = r
            });
            while (stack.Count > 0)
            {
                var top = stack.Pop();
                p = top.p;
                r = top.r;

                while (r > p + 10)
                {
                    seed      = seed * 1539415821 + 1;
                    i         = p + (int)(seed % (r - p + 1));
                    piv       = _order[i];
                    _order[i] = _order[p];
                    _order[p] = piv;
                    i         = p - 1;
                    j         = r + 1;
                    do
                    {
                        do
                        {
                            ++i;
                        } while (!_leq(_keys[_order[i]], _keys[piv]));
                        do
                        {
                            --j;
                        } while (!_leq(_keys[piv], _keys[_order[j]]));
                        Swap(ref _order[i], ref _order[j]);
                    } while (i < j);
                    Swap(ref _order[i], ref _order[j]);
                    if (i - p < r - j)
                    {
                        stack.Push(new StackItem {
                            p = j + 1, r = r
                        });
                        r = i - 1;
                    }
                    else
                    {
                        stack.Push(new StackItem {
                            p = p, r = i - 1
                        });
                        p = j + 1;
                    }
                }
                for (i = p + 1; i <= r; ++i)
                {
                    piv = _order[i];
                    for (j = i; j > p && !_leq(_keys[piv], _keys[_order[j - 1]]); --j)
                    {
                        _order[j] = _order[j - 1];
                    }
                    _order[j] = piv;
                }
            }

#if DEBUG
            p = 0;
            r = _size - 1;
            for (i = p; i < r; ++i)
            {
                Debug.Assert(_leq(_keys[_order[i + 1]], _keys[_order[i]]), "Wrong sort");
            }
#endif

            _max         = _size;
            _initialized = true;
            _heap.Init();
        }