示例#1
0
        // the root must not be full when this is called.
        void _Add(_Node t, TKey key, TValue value)
        {
            int i = t.KeyCount - 1;

            if (t.IsLeaf)
            {
                // shift items
                while (i >= 0 && 0 < _comparer.Compare(t.Items[i].Key, key))
                {
                    t.Items[i + 1] = t.Items[i];
                    --i;
                }

                t.Items[i + 1] = new KeyValuePair <TKey, TValue>(key, value);
                ++t.KeyCount;
            }
            else             // is not leaf
            {
                while (i >= 0 && 0 < _comparer.Compare(t.Items[i].Key, key))
                {
                    i--;
                }
                if (t.Children[i + 1].KeyCount == 2 * _minimumDegree - 1)
                {
                    _Split(t, i + 1, t.Children[i + 1]);
                    if (0 > _comparer.Compare(t.Items[i + 1].Key, key))
                    {
                        ++i;
                    }
                }
                _Add(t.Children[i + 1], key, value);
            }
        }
示例#2
0
 public bool Remove(TKey k)
 {
     if (null != _root)
     {
         if (!_Remove(_root, k))
         {
             // if root node has 0 keys collapse the tree by one
             if (0 == _root.KeyCount)
             {
                 _Node tmp = _root;
                 if (_root.IsLeaf)
                 {
                     _root = null;
                 }
                 else
                 {
                     _root = _root.Children[0];
                 }
             }
         }
         --_count;
         return(true);
     }
     return(false);
 }
示例#3
0
        _Node _Add(TKey key, TValue data, _Node t)
        {
            var node = new _Node(key, data);

            if (t == null)
            {
                node.Left = node.Right = null;
                return(node);
            }

            t = _Splay(key, t);
            var cmp = _comparer.Compare(key, t.Key);

            if (cmp < 0)
            {
                node.Left  = t.Left;
                node.Right = t;
                t.Left     = null;
            }
            else if (cmp >= 0)
            {
                node.Right = t.Right;
                node.Left  = t;
                t.Right    = null;
            }
            return(node);
        }
示例#4
0
文件: stack.cs 项目: qmgindi/Au
            /// <summary>
            /// Replaces target with this in parent stack.
            /// Used when moving, to create new parent (this) tab or stack for target and the moved node.
            /// Does not add/remove tree nodes.
            /// </summary>
            void _ReplaceInStack(_Node target)
            {
                if (target._splitter != null)
                {
                    target._SetSplitterEvents(false);
                    _splitter = target._splitter; target._splitter = null;
                    _SetSplitterEvents(true);
                }

                _dockedSize = target._dockedSize;
                if (_dockedSize.IsAuto)
                {
                    _SizeDef = _dockedSize = new GridLength(100, GridUnitType.Star);
                }

                int i      = _index * 2;
                var pstack = Parent._stack;
                var g      = pstack.grid;

                if (pstack.isVertical)
                {
                    Grid.SetRow(_elem, i);
                }
                else
                {
                    Grid.SetColumn(_elem, i);
                }
                g.Children.Remove(target._elem);
                g.Children.Add(_elem);
                _AddRemoveCaptionAndBorder();
                target._AddRemoveCaptionAndBorder();
            }
示例#5
0
        bool _TrySet(_Node t, TKey k, TValue v)
        {           // returns false if k is not present.
            // Find the first key greater than or equal to k
            if (null == t)
            {
                return(false);
            }
            int i = 0;

            while (i < t.KeyCount && 0 < _comparer.Compare(k, t.Items[i].Key))
            {
                i++;
            }

            // If the found key is equal to k, set this node
            var item = t.Items[i];

            if (0 == _comparer.Compare(item.Key, k))
            {
                t.Items[i] = new KeyValuePair <TKey, TValue>(k, v);
                return(true);
            }

            // If the key is not found here and this is a leaf node
            if (t.IsLeaf)
            {
                v = default(TValue);
                return(false);
            }

            // Go to the appropriate child and set it
            return(_TrySet(t.Children[i], k, v));
        }
示例#6
0
        bool _TryGet(_Node t, TKey k, out TValue v)
        {           // returns NULL if k is not present.
            // Find the first key greater than or equal to k
            var i = 0;

            while (i < t.KeyCount && 0 < _comparer.Compare(k, t.Items[i].Key))
            {
                i++;
            }

            // If the found key is equal to k, return this node
            var item = t.Items[i];

            if (0 == _comparer.Compare(item.Key, k))
            {
                v = item.Value;
                return(true);
            }

            // If the key is not found here and this is a leaf node
            if (t.IsLeaf)
            {
                v = default(TValue);
                return(false);
            }

            // Go to the appropriate child
            return(_TryGet(t.Children[i], k, out v));
        }
示例#7
0
        // A function to traverse all nodes in a subtree rooted with this node
        static IEnumerable <KeyValuePair <TKey, TValue> > _EnumNodes(_Node t)
        {
            if (null != t)
            {
                int i;
                for (i = 0; i < t.KeyCount; ++i)
                {
                    // If this is not leaf, then before returning Item[i],
                    // traverse the subtree rooted with child Children[i].
                    if (!t.IsLeaf)
                    {
                        foreach (var item in _EnumNodes(t.Children[i]))
                        {
                            yield return(item);
                        }
                    }
                    yield return(t.Items[i]);
                }

                // report the subtree rooted with last child
                if (!t.IsLeaf)
                {
                    foreach (var item in _EnumNodes(t.Children[i]))
                    {
                        yield return(item);
                    }
                }
            }
        }
示例#8
0
        _Node _SearchInterior(_Node t, TKey k)
        {
            if (null == t)
            {
                return(null);
            }
            // returns NULL if k is not present.
            // Find the first key greater than or equal to k
            var i = 0;

            while (i < t.KeyCount && 0 < _comparer.Compare(k, t.Items[i].Key))
            {
                ++i;
            }
            // If the found key is equal to k, return this node
            if (i >= t.KeyCount)
            {
                return(null);
            }
            if (0 == _comparer.Compare(t.Items[i].Key, k))
            {
                return(t);
            }

            // If the key is not found here and this is a leaf node
            if (t.IsLeaf)
            {
                return(null);
            }

            // Go to the appropriate child
            return(_SearchInterior(t.Children[i], k));
        }
示例#9
0
    private List <_Node> GetAdjacent_Nodes(_Node n)
    {
        List <_Node> temp = new List <_Node>();

        int row = (int)n.Position.y;
        int col = (int)n.Position.x;

        if (row + 1 < GridRows)
        {
            temp.Add(Grid[col][row + 1]);
        }
        if (row - 1 >= 0)
        {
            temp.Add(Grid[col][row - 1]);
        }
        if (col - 1 >= 0)
        {
            temp.Add(Grid[col - 1][row]);
        }
        if (col + 1 < GridCols)
        {
            temp.Add(Grid[col + 1][row]);
        }

        return(temp);
    }
示例#10
0
        /**
         * Deletes i from the tree if it's there
         */
        bool _TryRemove(
            TKey i, _Node t, out _Node x)
        {
            x = null;
            if (t == null)
            {
                return(false);
            }
            t = _Splay(i, t);
            var cmp = _comparer.Compare(i, t.Key);

            if (cmp == 0)
            {                           /* found it */
                if (t.Left == null)
                {
                    x = t.Right;
                }
                else
                {
                    x       = _Splay(i, t.Left);
                    x.Right = t.Right;
                }

                this._size--;
                return(true);
            }
            return(false);                                     /* It wasn't there */
        }
示例#11
0
        // A function to fill child C[idx] which has less than t-1 keys
        void _Fill(_Node t, int idx)
        {
            // If the previous child(C[idx-1]) has more than t-1 keys, borrow a key
            // from that child
            if (idx != 0 && t.Children[idx - 1].KeyCount >= _minimumDegree)
            {
                _BorrowFromPrevious(t, idx);
            }

            // If the next child(C[idx+1]) has more than t-1 keys, borrow a key
            // from that child
            else if (idx != t.KeyCount && t.Children[idx + 1].KeyCount >= _minimumDegree)
            {
                _BorrowFromNext(t, idx);
            }

            // Merge C[idx] with its sibling
            // If C[idx] is the last child, merge it with with its previous sibling
            // Otherwise merge it with its next sibling
            else
            {
                if (idx != t.KeyCount)
                {
                    _Merge(t, idx);
                }
                else if (0 != idx)
                {
                    _Merge(t, idx - 1);
                }
            }
            return;
        }
示例#12
0
        // adds an item (no validation)
        void _Add(TKey key, TValue value)
        {
            // tree is empty?
            if (null == root)
            {
                root          = new _Node(_comparer, _minimumDegree, true);
                root.Items[0] = new KeyValuePair <TKey, TValue>(key, value);                // Insert key
                root.KeyCount = 1;
                return;
            }

            // grow tree if root is full
            if (root.KeyCount == 2 * _minimumDegree - 1)
            {
                _Node newRoot = new _Node(_comparer, _minimumDegree, false);

                newRoot.Children[0] = root;
                newRoot.Split(0, root);
                // figure out which child gets the key (sort)
                int i = 0;
                if (0 > _comparer.Compare(newRoot.Items[0].Key, key))
                {
                    ++i;
                }
                newRoot.Children[i].Insert(key, value);

                root = newRoot;
            }
            else              // just insert
            {
                root.Insert(key, value);
            }
        }
示例#13
0
 public SortedBTreeDictionary(int minimumDegree, IComparer <TKey> comparer)
 {
     _count         = 0;
     _comparer      = comparer ?? Comparer <TKey> .Default;
     _root          = null;
     _minimumDegree = minimumDegree;
 }
示例#14
0
        _Node _Search(TKey x, _Node t)
        {
            if (null == t)
            {
                return(null);
            }
            int c = _comparer.Compare(x, t.Key);

            if (0 > c)
            {
                if (0 == c)                 // should never happen
                {
                    return(t);
                }
                else
                {
                    return(_Search(x, t.Left));
                }
            }
            else
            {
                if (0 == c)
                {
                    return(t);
                }
                else
                {
                    return(_Search(x, t.Right));
                }
            }
        }
示例#15
0
文件: tab.cs 项目: qmgindi/Au
            void _ReorderInTab(_Node target, bool after)
            {
                if (target == this || (after && target.Next == this) || (!after && target.Previous == this))
                {
                    return;
                }
                Remove(); target.AddSibling(this, after);
                int index = 0; foreach (var v in Parent.Children())
                {
                    v._index = index++;
                }
                //to avoid auto-selecting next item when removed active item, we remove all inactive items and then add in new order.
                var tc  = Parent._tab.tc;
                var sel = tc.SelectedItem;
                var a   = tc.Items.OfType <TabItem>().ToArray();

                for (int i = a.Length; --i >= 0;)
                {
                    if (a[i] != sel)
                    {
                        tc.Items.RemoveAt(i);
                    }
                }
                Array.Sort(a, (x, y) => (x.Tag as _Node)._index - (y.Tag as _Node)._index);
                for (int i = 0; i < a.Length; i++)
                {
                    if (a[i] != sel)
                    {
                        tc.Items.Insert(i, a[i]);
                    }
                }
            }
示例#16
0
    public List <_Node> GetNeighbours(_Node node)
    {
        List <_Node> neighbours = new List <_Node> ();

        for (int x = -1; x <= 1; x++)
        {
            for (int y = -1; y <= 1; y++)
            {
                if ((x == 0 && y == 0) || (x == 1 && y == 1) || (x == -1 && y == -1) || (x == 1 && y == -1) || (x == -1 && y == 1))
                {
                    continue;
                }

                int checkX = node.gridX + x;
                int checkY = node.gridY + y;

                if (checkX >= 0 && checkX < gridSizeX && checkY >= 0 && checkY < gridSizeY)
                {
                    neighbours.Add(grid[checkX, checkY]);
                }
            }
        }

        return(neighbours);
    }
示例#17
0
文件: _Split.cs 项目: alexfordc/Au
            /// <summary>
            /// Sets a child to have fixed width or height.
            /// Used for context menu.
            /// </summary>
            internal void SetChildFixedSize(_Node gn, bool fixedSize)
            {
                //gn = _GetChildNotTabbedPanel(gn);
                _AssertIsChild(gn);
                Debug.Assert(IsSplitterVisible);

                _isFraction = !fixedSize;
                _isWidth1   = gn == Child1;
                int w, w1, w2;

                if (IsVerticalSplit)
                {
                    w  = this.Bounds.Width;
                    w1 = Child1.Bounds.Width; w2 = Child2.Bounds.Width;
                }
                else
                {
                    w  = this.Bounds.Height;
                    w1 = Child1.Bounds.Height; w2 = Child2.Bounds.Height;
                }
                if (w < 1)
                {
                    return;
                }
                if (_isFraction)
                {
                    _fraction = (float)w1 / w;
                }
                else
                {
                    _width = _isWidth1 ? w1 : w2;
                }
            }
示例#18
0
    Vector2[] FindPath(Vector2 from, Vector2 to)
    {
        Stopwatch sw = new Stopwatch();

        sw.Start();

        Vector2[] waypoints   = new Vector2[0];
        bool      pathSuccess = false;

        _Node startNode  = grid.NodeFromWorldPoint(from);
        _Node targetNode = grid.NodeFromWorldPoint(to);

        Heap <_Node>    openSet  = new Heap <_Node>(grid.MaxSize);
        HashSet <_Node> closeSet = new HashSet <_Node> ();

        openSet.Add(startNode);

        while (openSet.Count > 0)
        {
            _Node currentNode = openSet.RemoveFirst();
            closeSet.Add(currentNode);

            if (currentNode == targetNode)
            {
                sw.Stop();
                print("Path found: " + sw.ElapsedMilliseconds + " ms");
                pathSuccess = true;
                break;
            }

            foreach (_Node neighbour in grid.GetNeighbours(currentNode))
            {
                if (!neighbour.walkable || closeSet.Contains(neighbour))
                {
                    continue;
                }

                int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour);
                if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
                {
                    neighbour.gCost  = newMovementCostToNeighbour;
                    neighbour.hCost  = GetDistance(neighbour, targetNode);
                    neighbour.parent = currentNode;

                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                }
            }
        }

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }

        return(waypoints);
    }
示例#19
0
文件: _Split.cs 项目: alexfordc/Au
 int _MinimalChildWidthOrHeight(_Node gn)
 {
     if (!gn.IsDocked)
     {
         return(0);
     }
     return(this.IsVerticalSplit ? gn.MinimalWidth : gn.MinimalHeight);
 }
示例#20
0
 public _Node(Vector2 pos, bool walkable)
 {
     Parent           = null;
     Position         = pos;
     DistanceToTarget = -1;
     Cost             = 1;
     Walkable         = walkable;
 }
        // A utility function to left rotate subtree rooted with x
        // See the diagram given above.
        _Node _Rol(_Node x)
        {
            _Node y = x.Right;

            x.Right = y.Left;
            y.Left  = x;
            return(y);
        }
        /* Helper function that allocates a new node with the given key and
         *      null left and right pointers. */
        _Node _CreateNode(TKey key, TValue value)
        {
            var result = new _Node();

            result.Key   = key;
            result.Value = value;
            result.Left  = result.Right = null;
            return(result);
        }
示例#23
0
        public GhostEdge(PaintEventArgs e, PointF end1, PointF end2, _Node node1)
        {
            EdgeColor = Color.Pink;

            End1 = end1;
            End2 = end2;

            Node1     = node1;
            PaintArgs = e;
        }
 public override Task CreateDir(FsPath path)
 {
     while (true)
     {
         _data[path] = new _Node(_StorageKind.Directory);
         if (path.IsRoot)
             return CompletedTask;
         path = path.Parent;
     }
 }
示例#25
0
        // A utility function that returns the index of the first key that is
        // greater than or equal to k
        int _GetIndexOfKey(_Node t, TKey k)
        {
            int idx = 0;

            while (idx < t.KeyCount && 0 > _comparer.Compare(t.Items[idx].Key, k))
            {
                ++idx;
            }
            return(idx);
        }
示例#26
0
 public void CreateDir(FsPath path)
 {
     while (true)
     {
         _data[path] = new _Node(_StorageKind.Directory);
         if (path.IsRoot)
             return;
         path = path.Parent;
     }
 }
示例#27
0
文件: _Split.cs 项目: alexfordc/Au
            /// <summary>
            /// Gets whether a child has fixed width or height.
            /// Used for context menu.
            /// </summary>
            internal bool IsChildFixedSize(_Node gn)
            {
                //gn = _GetChildNotTabbedPanel(gn);
                _AssertIsChild(gn);

                if (_isFraction)
                {
                    return(false);
                }
                return((gn == Child1) == _isWidth1);
            }
        public bool Remove(TKey key)
        {
            _Node temp;

            if (_TryRemove(_root, key, out temp))
            {
                _root = temp;
                return(true);
            }
            return(false);
        }
示例#29
0
 public void initialize()
 {
     gCost  = 0;
     hCost  = 0;
     parent = null;
     link.Clear();
     for (int i = 0; i < neighboor.Count; i++)
     {
         link.Add(neighboor[i]);
     }
 }
示例#30
0
 int _GetBalance(_Node t)
 {
     if (t == null)
     {
         return(0);
     }
     else
     {
         return(_GetHeight(t.Left) - _GetHeight(t.Right));
     }
 }
示例#31
0
        /**
         * @param  {Key} key
         * @return {Node|null}
         */
        public bool Remove(TKey key)
        {
            _Node r;

            if (_TryRemove(key, _root, out r))
            {
                _root = r;
                return(true);
            }
            return(false);
        }
示例#32
0
    public Stack <_Node> FindPath(Vector2 Start, Vector2 End)
    {
        _Node start = new _Node(new Vector2((int)(Start.x / _Node._Node_SIzE), (int)(Start.y / _Node._Node_SIzE)), true);
        _Node end   = new _Node(new Vector2((int)(End.x / _Node._Node_SIzE), (int)(End.y / _Node._Node_SIzE)), true);

        Stack <_Node> Path       = new Stack <_Node>();
        List <_Node>  OpenList   = new List <_Node>();
        List <_Node>  ClosedList = new List <_Node>();
        List <_Node>  adjacencies;
        _Node         current = start;

        // add start _Node to Open List
        OpenList.Add(start);

        while (OpenList.Count != 0 && !ClosedList.Exists(x => x.Position == end.Position))
        {
            current = OpenList[0];
            OpenList.Remove(current);
            ClosedList.Add(current);
            adjacencies = GetAdjacent_Nodes(current);


            foreach (_Node n in adjacencies)
            {
                if (!ClosedList.Contains(n) && n.Walkable)
                {
                    if (!OpenList.Contains(n))
                    {
                        n.Parent           = current;
                        n.DistanceToTarget = Math.Abs(n.Position.x - end.Position.x) + Math.Abs(n.Position.y - end.Position.y);
                        n.Cost             = 1 + n.Parent.Cost;
                        OpenList.Add(n);
                        OpenList = OpenList.OrderBy(_Node => _Node.F).ToList <_Node>();
                    }
                }
            }
        }

        // construct path, if end was not closed return null
        if (!ClosedList.Exists(x => x.Position == end.Position))
        {
            return(null);
        }

        // if all good, return path
        _Node temp = ClosedList[ClosedList.IndexOf(current)];

        while (temp.Parent != start && temp != null)
        {
            Path.Push(temp);
            temp = temp.Parent;
        }
        return(Path);
    }
示例#33
0
 public void Overwrite(FsPath path, byte[] newContents)
 {
     _data[path] = new _Node(_StorageKind.File) {
         RawContents = newContents
     };
 }
示例#34
0
 public void Overwrite(FsPath path, string newContents)
 {
     _data[path] = new _Node(_StorageKind.File) {
         RawContents= DefaultEncoding.GetBytes(newContents)
     };
 }
示例#35
0
 private void _ValidateStorage(FsPath path, _Node storage)
 {
     if (storage.Kind == _StorageKind.Missing)
         throw new FileNotFoundException(string.Format("Could not find file '{0}'.", path.Absolute), path.Absolute);
     if (storage.Kind == _StorageKind.Directory)
         throw new UnauthorizedAccessException(string.Format("Access to the path '{0}' is denied.", path.Absolute));
 }
 public override Task Overwrite(FsPath path, string newContents)
 {
     _data[path] = new _Node(_StorageKind.File)
     {
         RawContents = DefaultEncoding.GetBytes(newContents)
     };
     return CompletedTask;
 }
 public override Task Overwrite(FsPath path, byte[] newContents)
 {
     _data[path] = new _Node(_StorageKind.File)
     {
         RawContents = newContents
     };
     return CompletedTask;
 }
 public void Overwrite(FSPath path, string newContents)
 {
     _data[path] = new _Node(_StorageKind.File) {
         TextContents = newContents
     };
 }