Exemplo n.º 1
0
        public V insert(K key, V value)
        {
            SkipNode[] update = new SkipNode[MaxLevel];
            var        x      = HEAD;

            for (int i = MaxLevel - 1; i >= 0; i--)
            {
                while (x.levels[i].key.CompareTo(key) < 0)
                {
                    x = x.levels[i];
                }
                update[i] = x;
            }
            x = x.levels[0];
            if (x.key.CompareTo(key) == 0)
            {
                V old = x.value;
                x.value = value;
                return(old);
            }
            else
            {
                var newlevel = newLevel();
                var newnode  = new SkipNode(key, value, newlevel, MaxLevel);
                for (int i = 0; i <= newlevel; i++)
                {
                    newnode.levels[i]   = update[i].levels[i];
                    update[i].levels[i] = newnode;
                }
            }
            return(value);
        }
Exemplo n.º 2
0
        //private int level = 1;

        public void Add(int value)
        {
            int level = 1;
            int r     = rand.Next();

            for (int i = 1; i <= 32; i++)
            {
                if ((r & 1) == 1)
                {
                    level++;
                }
            }
            SkipNode node = new SkipNode(value, level);
            SkipNode current;

            for (int i = 1; i <= level; i++)
            {
                for (current = head; current.next[i] != null; current = current.next[i])
                {
                    if (current.next[i].value > value)
                    {
                        break;
                    }
                }
                node.next[i]    = current.next[i];
                current.next[i] = node;
            }
        }
Exemplo n.º 3
0
        public void Insert(int value)
        {
            SkipNode NodeIn  = new SkipNode(value, Levels + 1);
            SkipNode Current = Head;

            Levels = setNodeLevel(.125, 3);
            for (int i = Levels - 1; i >= 0; i--)
            {
                for (; Current.Next[i] != null; Current = Current.Next[i])
                {
                    if (Current.Next[i].v > value)
                    {
                        break;
                    }
                }
                if (i <= Levels)
                {
                    NodeIn.Next[i]  = Current.Next[i];
                    Current.Next[i] = NodeIn;
                }
            }
            // Determine the level of a new node. Generate a random number R by calling ChooseRandLevel() function
            // Use the setLevel algorithm we discuss in Slide 19 of Skip Lists
            // Insert this node into the skip list(remember skips lists are sorted)
            // And since this is a linked list, remember that you need to start at the head node
        }
Exemplo n.º 4
0
            /// <summary>
            /// 移动到下一个节点
            /// </summary>
            /// <returns>下一个节点是否存在</returns>
            public bool MoveNext()
            {
                if (forward)
                {
                    do
                    {
                        current = current.Level[0].Forward;
                    } while (current != null && current.IsDeleted);
                    return(current != null);
                }

                if (current == null)
                {
                    do
                    {
                        current = sortSet.tail;
                    } while (current != null && current.IsDeleted);
                    return(current != null);
                }

                do
                {
                    current = current.Backward;
                } while (current != null && current.IsDeleted);
                return(current != null);
            }
        /// <summary>
        /// Search for values, given a key
        /// </summary>
        /// <param name="key">Key to be searched</param>
        /// <returns>Value otherwise type default</returns>
        public TValue Search(TKey key)
        {
            Contract.Requires <ArgumentNullException>(key != null);

            SkipNode <TKey, TValue> cursor = header;

            for (int i = 0; i < level; i++)
            {
                SkipNode <TKey, TValue> nextElement = cursor.Links[i];
                while (nextElement.Key.CompareTo(key) == -1)
                {
                    cursor      = nextElement;
                    nextElement = cursor.Links[i];
                }
            }
            //Got previous element of current key in the list
            //So next element must our searched element if list contains that element
            cursor = cursor.Links[0];
            if (cursor.Key.Equals(key))
            {
                return(cursor.Value);
            }
            else
            {
                //Element is not in the list, return default
                return(default(TValue));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 根据分数区间移除区间内的元素
        /// </summary>
        /// <param name="startScore">开始的分数(包含)</param>
        /// <param name="stopScore">结束的分数(包含)</param>
        /// <returns>被删除的元素个数</returns>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="startScore"/>和<paramref name="stopScore"/>区间无效时引发</exception>
        public int RemoveRangeByScore(TScore startScore, TScore stopScore)
        {
            Guard.Requires <ArgumentNullException>(startScore != null);
            Guard.Requires <ArgumentNullException>(stopScore != null);
            Guard.Requires <ArgumentOutOfRangeException>(Compare(startScore, stopScore) <= 0);

            var removed = 0;
            var update  = new SkipNode[maxLevel];
            var cursor  = header;

            for (var i = level - 1; i >= 0; --i)
            {
                while (cursor.Level[i].Forward != null &&
                       Compare(cursor.Level[i].Forward.Score, startScore) < 0)
                {
                    cursor = cursor.Level[i].Forward;
                }
                update[i] = cursor;
            }

            cursor = cursor.Level[0].Forward;

            while (cursor != null &&
                   Compare(cursor.Score, stopScore) <= 0)
            {
                var next = cursor.Level[0].Forward;
                dict.Remove(cursor.Element);
                DeleteNode(cursor, update);
                ++removed;
                cursor = next;
            }

            return(removed);
        }
Exemplo n.º 7
0
        /// <summary>
        /// 根据排名区间移除区间内的元素
        /// </summary>
        /// <param name="startRank">开始的排名(包含),排名以0为底</param>
        /// <param name="stopRank">结束的排名(包含),排名以0为底</param>
        /// <returns>被删除的元素个数</returns>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="startRank"/>和<paramref name="stopRank"/>区间无效时引发</exception>
        public int RemoveRangeByRank(int startRank, int stopRank)
        {
            startRank = Math.Max(startRank, 0);
            Guard.Requires <ArgumentOutOfRangeException>(startRank <= stopRank);

            int traversed = 0, removed = 0;
            var update = new SkipNode[maxLevel];
            var cursor = header;

            for (var i = level - 1; i >= 0; --i)
            {
                while (cursor.Level[i].Forward != null &&
                       (traversed + cursor.Level[i].Span <= startRank))
                {
                    traversed += cursor.Level[i].Span;
                    cursor     = cursor.Level[i].Forward;
                }
                update[i] = cursor;
            }

            cursor = cursor.Level[0].Forward;

            while (cursor != null &&
                   traversed <= stopRank)
            {
                var next = cursor.Level[0].Forward;
                dict.Remove(cursor.Element);
                DeleteNode(cursor, update);
                ++removed;
                ++traversed;
                cursor = next;
            }

            return(removed);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 如果元素存在那么从有序集中删除元素
        /// </summary>
        /// <param name="element">元素</param>
        /// <param name="score">分数</param>
        /// <returns>是否成功</returns>
        private bool Remove(TElement element, TScore score)
        {
            Guard.Requires <ArgumentNullException>(element != null);
            Guard.Requires <ArgumentNullException>(score != null);

            var update = new SkipNode[maxLevel];
            var cursor = header;

            //从跳跃层高到低的进行查找
            for (var i = level - 1; i >= 0; --i)
            {
                while (IsFindNext(cursor.Level[i].Forward, element, score, i))
                {
                    cursor = cursor.Level[i].Forward;
                }

                //将找到的最后一个结点置入需要更新的结点
                update[i] = cursor;
            }

            cursor = update[0].Level[0].Forward;

            if (cursor == null ||
                Compare(cursor.Score, score) != 0 ||
                !cursor.Element.Equals(element))
            {
                return(false);
            }

            dict.Remove(element);
            DeleteNode(cursor, update);
            return(true);
        }
Exemplo n.º 9
0
        /// <summary>
        /// 是否查询下一个元素
        /// </summary>
        /// <param name="node">跳跃结点</param>
        /// <param name="element">元素</param>
        /// <param name="score">分数</param>
        /// <param name="level">层级</param>
        /// <returns>是否查找下一个</returns>
        private bool IsFindNext(SkipNode node, TElement element, TScore score, int level)
        {
            if (node == null)
            {
                return(false);
            }

            var compare = Compare(node.Score, score);

            if (compare < 0 || compare > 0)
            {
                return(compare < 0);
            }

            // 如果层级大于0,说明有可能直接定位到元素2,从而导致bug
            // 所以我们认为层级大于 0 那么值相等依旧返回前序跳跃结点
            //
            // ------------------------------------------
            // |  元素1(分数:50)  | 元素2 (分数:50)
            // ------------------------------------------
            if (level > 0)
            {
                return(false);
            }

            return(!node.Element.Equals(element));
        }
Exemplo n.º 10
0
        /// <summary>
        /// Remove the skip node.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="score">The score.</param>
        /// <returns>Whether is removed the element.</returns>
        private bool Remove(TElement element, TScore score)
        {
            Guard.Requires <ArgumentNullException>(
                element != null,
                $"{nameof(element)} should not be null.");
            Guard.Requires <ArgumentNullException>(
                score != null,
                $"{nameof(score)} should not be null.");

            var update = new SkipNode[maxLevel];
            var cursor = header;

            for (var i = level - 1; i >= 0; --i)
            {
                while (IsFindNext(cursor.Level[i].Forward, element, score, i))
                {
                    cursor = cursor.Level[i].Forward;
                }

                update[i] = cursor;
            }

            cursor = update[0].Level[0].Forward;

            if (cursor == null ||
                Compare(cursor.Score, score) != 0 ||
                !cursor.Element.Equals(element))
            {
                return(false);
            }

            elementMapping.Remove(element);
            DeleteNode(cursor, update);
            return(true);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Determine if you need to find the next node.
        /// </summary>
        /// <param name="node">The skip not</param>
        /// <param name="element">The element.</param>
        /// <param name="score">The element score.</param>
        /// <param name="level">The level.</param>
        /// <returns>True if find next.</returns>
        private bool IsFindNext(SkipNode node, TElement element, TScore score, int level)
        {
            if (node == null)
            {
                return(false);
            }

            var compare = Compare(node.Score, score);

            if (compare < 0 || compare > 0)
            {
                return(compare < 0);
            }

            // If the level is greater than 0, it means that it is possible
            // to directly locate element 2, resulting in a bug.
            // So we think that the level is greater than 0, then the value
            // is equal and still returns the pre-order jump node.
            //
            // ----------------------------------------------------
            // |  element 1(score:50)  | element 2 (score:50)
            // ----------------------------------------------------
            if (level > 0)
            {
                return(false);
            }

            return(!node.Element.Equals(element));
        }
Exemplo n.º 12
0
 /// <summary>
 /// 删除结点关系
 /// </summary>
 /// <param name="cursor">结点</param>
 /// <param name="update">更新结点列表</param>
 private void DeleteNode(SkipNode cursor, SkipNode[] update)
 {
     for (var i = 0; i < level; ++i)
     {
         if (update[i].Level[i].Forward == cursor)
         {
             update[i].Level[i].Span   += cursor.Level[i].Span - 1;
             update[i].Level[i].Forward = cursor.Level[i].Forward;
         }
         else
         {
             update[i].Level[i].Span -= 1;
         }
     }
     if (cursor.Level[0].Forward != null)
     {
         cursor.Level[0].Forward.Backward = cursor.Backward;
     }
     else
     {
         tail = cursor.Backward;
     }
     while (level > 1 && header.Level[level - 1].Forward == null)
     {
         --level;
     }
     cursor.IsDeleted = true;
     --Count;
 }
Exemplo n.º 13
0
        public SkipList(int cap)
        {
            this.cap = cap;
            MaxLevel = SetMaxLevel(cap);

            head         = new SkipNode();
            head.forward = new SkipNode[MaxLevel];
        }
Exemplo n.º 14
0
        /// <summary>
        /// Get the number of elements in the specified score range.
        /// </summary>
        /// <param name="start">The start score.(contain).</param>
        /// <param name="end">The end score.(contain).</param>
        /// <returns>The number of elements in the score range.</returns>
        public int GetRangeCount(TScore start, TScore end)
        {
            Guard.Requires <ArgumentNullException>(
                start != null,
                $"{nameof(start)} should not be null.");
            Guard.Requires <ArgumentNullException>(
                end != null,
                $"{nameof(end)} should not be null.");
            Guard.Requires <ArgumentOutOfRangeException>(
                Compare(start, end) <= 0,
                $"{nameof(start)} should not larger than {nameof(end)}.");

            int      rank = 0, leftRank = 0;
            SkipNode leftCursor = null;

            var isRight = false;
            var cursor  = header;

            do
            {
                for (var i = level - 1; i >= 0; --i)
                {
                    while (cursor.Level[i].Forward != null &&
                           ((!isRight && Compare(cursor.Level[i].Forward.Score, start) < 0) ||
                            (isRight && Compare(cursor.Level[i].Forward.Score, end) <= 0)))
                    {
                        rank  += cursor.Level[i].Span;
                        cursor = cursor.Level[i].Forward;
                    }

                    if (leftCursor != null)
                    {
                        continue;
                    }

                    // First set the skip cursor and ranking of the top leftmost level.
                    // Than the cursor will start to look down.
                    leftCursor = cursor;
                    leftRank   = rank;
                }

                if (isRight)
                {
                    continue;
                }

                cursor = leftCursor;

                var foo = rank;
                rank     = leftRank;
                leftRank = foo;
            }
#pragma warning disable S1121
            while (isRight = !isRight);
#pragma warning restore S1121

            return(Math.Max(0, rank - leftRank));
        }
Exemplo n.º 15
0
        /// <summary>
        /// Insert key value pair in list
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Insert(TKey key, TValue value)
        {
            Contract.Requires <ArgumentNullException>(key != null, "key");

            SkipNode <TKey, TValue>[] update = new SkipNode <TKey, TValue> [maxLevel];
            //Start search for each level from dummy header node
            SkipNode <TKey, TValue> cursor = header;

            //Start from current max initialized header
            for (int i = level; i >= 0; i--)
            {
                //Find the node which is previous node to current code in any level
                while (cursor.Links[i].Key.CompareTo(key) == -1)
                {
                    cursor = cursor.Links[i];
                }
                //Put the predecessor node link in the level of update list
                update[i] = cursor;
            }
            //Check level 0 next whether we already have that element
            cursor = cursor.Links[0];
            if (cursor.Key.CompareTo(key) == 0)
            {
                //Assign new value to corresponding key
                cursor.Value = value;
            }
            else
            {
                //If this is new node, then
                //Find random level for insertion
                int newLevel = GetRandomLevel();
                //New node level is greater then current level
                //Update intermediate nodes
                if (newLevel > level)
                {
                    //This is a special case, where dummy header links aren't initialized yet
                    for (int i = level + 1; i < newLevel; i++)
                    {
                        //These levels of header aren't initialized yet
                        update[i] = header;
                    }
                    //update current level, until this level from bottom header link is initialized
                    level = newLevel;
                }
                //New node which will be inserted into new level, also needs newLevel number of forward edges
                cursor = new SkipNode <TKey, TValue>(newLevel, key, value);
                //Insert the node
                for (int i = 0; i < newLevel; i++)
                {
                    //Update edges of all the levels below to that level
                    //New node is set to successor node its predecessor
                    cursor.Links[i] = update[i].Links[i];
                    //Update forward edges of predecessor to currently inserted node
                    update[i].Links[i] = cursor;
                }
                Count++;
            }
        }
            public bool Equals(SkipNode <TKey, TValue> otherNode)
            {
                NullSkipNode <TKey, TValue> otherNullNode = otherNode as NullSkipNode <TKey, TValue>;

                if (otherNullNode == null)
                {
                    return(false);
                }
                return(true);
            }
Exemplo n.º 17
0
        public override DynValue Visit(SkipNode skipNode)
        {
            if (!Environment.IsInsideLoop)
            {
                throw new RuntimeException("Unexpected 'skip' outside of a loop.", skipNode.Line);
            }

            Environment.LoopStackTop.SkipThisIteration = true;
            return(DynValue.Zero);
        }
 public SLEnumerator(SkipList <T> skipList)
 {
     this.skipList    = skipList;
     this.currentNode = skipList.head;
     while (this.currentNode.Down != null)
     {
         this.currentNode = this.currentNode.Down;
     }
     this.currentCount = 1;
 }
Exemplo n.º 19
0
 private void setUp(K max, K min, int size)
 {
     MaxLevel = (int)(Math.Floor(Math.Log(size) / Math.Log(2)) + 1);
     HEAD     = new SkipNode(min, default(V), MaxLevel - 1, MaxLevel);
     NIL      = new SkipNode(max, default(V), 0, MaxLevel);
     for (int i = 0; i < MaxLevel; i++)
     {
         HEAD.setLevel(NIL, i);
     }
 }
 public bool MoveNext()
 {
     if (currentCount < currentNode.Count)
     {
         currentCount++;
         return(true);
     }
     currentNode  = currentNode.Next;
     currentCount = 1;
     return(currentNode != null);
 }
Exemplo n.º 21
0
        private SkipNode InitNode(int key, T value)
        {
            var node = new SkipNode {
                key   = key,
                value = value
            };
            var level = RandomLevel();

            node.forward = new SkipNode[level];
            return(node);
        }
Exemplo n.º 22
0
        /// <summary>
        /// Enabling an iterator so that we can iterate using a foreach loop
        /// </summary>
        ///
        public IEnumerable <int> Enumerate()
        {
            SkipNode Current = Head.Next[0];

            while (Current != null)
            {
                yield return(Current.v);

                Current = Current.Next[0];
            }
        }
        public override DynValue Visit(SkipNode skipNode)
        {
            if (LoopStack.Count <= 0)
            {
                throw new RuntimeException("Unexpected 'skip' outside of a loop.", skipNode.Line);
            }

            LoopStack.Peek().SkipThisIteration = true;

            return(DynValue.Zero);
        }
Exemplo n.º 24
0
 /// <summary>
 /// 清空SortSet
 /// </summary>
 public void Clear()
 {
     for (var i = 0; i < header.Level.Length; ++i)
     {
         header.Level[i].Span    = 0;
         header.Level[i].Forward = null;
     }
     tail  = null;
     level = 1;
     dict.Clear();
     Count = 0;
 }
Exemplo n.º 25
0
        /// <summary>
        /// Clear the <see cref="SortSet{TElement, TScore}"/>.
        /// </summary>
        public void Clear()
        {
            for (var i = 0; i < header.Level.Length; ++i)
            {
                header.Level[i].Span    = 0;
                header.Level[i].Forward = null;
            }

            tail  = null;
            level = 1;
            Count = 0;
            elementMapping.Clear();
        }
Exemplo n.º 26
0
        /// <summary>
        /// 创建一个有序集
        /// </summary>
        /// <param name="probable">可能出现层数的概率系数(0-1之间的数)</param>
        /// <param name="maxLevel">最大层数</param>
        public SortSet(double probable = PROBABILITY, int maxLevel = 32)
        {
            Guard.Requires <ArgumentOutOfRangeException>(maxLevel > 0);
            Guard.Requires <ArgumentOutOfRangeException>(probable < 1);
            Guard.Requires <ArgumentOutOfRangeException>(probable > 0);

            probability   = probable * 0xFFFF;
            this.maxLevel = maxLevel;
            level         = 1;
            header        = new SkipNode
            {
                Level = new SkipNode.SkipNodeLevel[maxLevel]
            };
        }
Exemplo n.º 27
0
    public void Insert(int key, object data)
    {
        this.Remove(key);

        this.DataMap.Add(key, data);
        SkipNode node = new SkipNode(key);

        int level = this.RandomLevel();

        for (int i = CurLevel; i <= level; i++)
        {
            this.Header.NodeList[i - 1] = new SkipNode(0);
        }
    }
Exemplo n.º 28
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SortSet{TElement, TScore}"/> class.
        /// </summary>
        /// <param name="probable">Probability coefficient of possible number of level(0-1).</param>
        /// <param name="maxLevel">The max level.</param>
        public SortSet(double probable = 0.25, int maxLevel = 32)
        {
            Guard.Requires <ArgumentOutOfRangeException>(probable < 1);
            Guard.Requires <ArgumentOutOfRangeException>(probable > 0);

            maxLevel = Math.Max(1, maxLevel);

            probability   = probable * 0xFFFF;
            level         = 1;
            this.maxLevel = maxLevel;
            header        = new SkipNode
            {
                Level = new SkipNode.SkipNodeLevel[maxLevel],
            };
        }
Exemplo n.º 29
0
        /// <summary>
        /// Get the number of elements in the specified score range.
        /// </summary>
        /// <param name="start">The start score.(contain)</param>
        /// <param name="end">The end score.(contain)</param>
        /// <returns>The number of elements in the score range.</returns>
        public int GetRangeCount(TScore start, TScore end)
        {
            Guard.Requires <ArgumentNullException>(start != null);
            Guard.Requires <ArgumentNullException>(end != null);
            Guard.Requires <ArgumentOutOfRangeException>(Compare(start, end) <= 0);

            int      rank = 0, leftRank = 0;
            SkipNode leftCursor = null;

            var isRight = false;
            var cursor  = header;

            do
            {
                for (var i = level - 1; i >= 0; --i)
                {
                    while (cursor.Level[i].Forward != null &&
                           ((!isRight && Compare(cursor.Level[i].Forward.Score, start) < 0) ||
                            (isRight && Compare(cursor.Level[i].Forward.Score, end) <= 0)))
                    {
                        rank  += cursor.Level[i].Span;
                        cursor = cursor.Level[i].Forward;
                    }

                    if (leftCursor != null)
                    {
                        continue;
                    }

                    // First set the skip cursor and ranking of the top leftmost level.
                    // Than the cursor will start to look down.
                    leftCursor = cursor;
                    leftRank   = rank;
                }

                if (isRight)
                {
                    continue;
                }

                cursor    = leftCursor;
                leftRank ^= (rank ^= leftRank);
                rank     ^= leftRank;
            } while (isRight = !isRight);

            return(Math.Max(0, rank - leftRank));
        }
Exemplo n.º 30
0
        /// <summary>
        /// 获取分数范围内的元素个数
        /// </summary>
        /// <param name="start">起始值(包含)</param>
        /// <param name="end">结束值(包含)</param>
        /// <returns>分数值在<paramref name="start"/>(包含)和<paramref name="end"/>(包含)之间的元素数量</returns>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="start"/>和<paramref name="end"/>区间无效时引发</exception>
        public int GetRangeCount(TScore start, TScore end)
        {
            Guard.Requires <ArgumentNullException>(start != null);
            Guard.Requires <ArgumentNullException>(end != null);
            Guard.Requires <ArgumentOutOfRangeException>(Compare(start, end) <= 0);

            int      rank = 0, leftRank = 0;
            SkipNode leftCursor = null;

            var isRight = false;
            var cursor  = header;

            do
            {
                for (var i = level - 1; i >= 0; --i)
                {
                    while (cursor.Level[i].Forward != null &&
                           ((!isRight && Compare(cursor.Level[i].Forward.Score, start) < 0) ||
                            (isRight && Compare(cursor.Level[i].Forward.Score, end) <= 0)))
                    {
                        rank  += cursor.Level[i].Span;
                        cursor = cursor.Level[i].Forward;
                    }

                    if (leftCursor != null)
                    {
                        continue;
                    }

                    // 设定最左边最高层的跳跃游标和排名
                    // 之后将由这个游标来开始向下查找
                    leftCursor = cursor;
                    leftRank   = rank;
                }

                if (isRight)
                {
                    continue;
                }

                cursor    = leftCursor;
                leftRank ^= (rank ^= leftRank);
                rank     ^= leftRank;
            } while (isRight = !isRight);

            return(Math.Max(0, rank - leftRank));
        }
Exemplo n.º 31
0
        private void BuildBranchNodes(Graph g)
        {
            // lay out the dag nodes in order
            List<Node> Nodes = g.ReversePostOrder();

            // Figure out dominance
            Dictionary<Node, Node> IDOM = g.Dominators(Nodes);

            for( int i=0; i<Nodes.Count; i++ )
            {
                Node n = Nodes[i];
                if (n is LoopNode)
                {
                    // descend into loop nodes
                    BuildBranchNodes((n as LoopNode).SubGraph);
                }
                else
                {
                    LeafNode leaf = n as LeafNode;
                    BasicBlock bl = leaf.Block;

                    // mark break, branch, and continue nodes as such
                    if (bl.LastInstruction is IBranchInstruction)
                    {
                        IBranchInstruction branch = bl.LastInstruction as IBranchInstruction;
                        if (branch.Category == BranchCategory.BREAK_BRANCH)
                            leaf.NodeType = "break";
                        else if (branch.Category == BranchCategory.CONTINUE_BRANCH)
                            leaf.NodeType = "continue";
                        else if (branch.Category == BranchCategory.LOOPSKIP_BRANCH)
                            leaf.NodeType = "node";
                        else if (branch.Category == BranchCategory.SKIP_BRANCH)
                            leaf.NodeType = "skip";
                        else
                            leaf.NodeType = "branch";
                    }
                    else
                    {
                        // mark continue nodes as such
                        if (bl.InnerMostLoop != null && bl.Successors.First() == bl.InnerMostLoop.Header)
                            n.NodeType = "continue";
                    }
                }
            }

            int k = 0;
            while( k < Nodes.Count )
            {
                if( Nodes[k] is LeafNode && Nodes[k].NodeType.Equals("branch") )
                {
                    BranchNode br = new BranchNode((Nodes[k] as LeafNode));

                    List<Node> descendents = new List<Node>(g.ChildrenOf(Nodes[k]));
                    Graph[] branchGraphs = new Graph[2];
                    branchGraphs[0] = br.IfGraph;
                    branchGraphs[1] = br.ElseGraph;

                    for (int k0 = k + 1; k0 < Nodes.Count; k0++)
                    {
                        Node n = Nodes[k0];
                        for (int j = 0; j < descendents.Count; j++)
                        {
                            if (Algorithms.Dominates(descendents[j], n, IDOM))
                                branchGraphs[j].AddNode(n);
                        }
                    }

                    br.OwnedNodes.AddRange(branchGraphs[0].Nodes);
                    br.OwnedNodes.AddRange(branchGraphs[1].Nodes);

                    Graph branchGraph = new Graph();
                    g.CombineNodes(br.OwnedNodes, br, branchGraph);
                    branchGraph.TransferEdgesToSubgraph(branchGraphs[0]);
                    branchGraph.TransferEdgesToSubgraph(branchGraphs[1]);

                    // do this recursively on the if/else branches
                    BuildBranchNodes(br.IfGraph);
                    BuildBranchNodes(br.ElseGraph);

                    // start over
                    k = 0;
                    Nodes = g.ReversePostOrder();
                }
                else if (Nodes[k] is LeafNode && Nodes[k].NodeType.Equals("skip"))
                {
                    Dictionary<Node, Node> PDOM = g.PostDominators(Nodes);

                    // find nodes to put into the sub-graph
                    // these are all the nodes which are skipped
                    //  nodes are skipped if they are post-dominated by the convergence node (which post-dominates the test)
                    Node joinPoint = PDOM[Nodes[k]];
                    Graph branchGraph = new Graph();
                    for (int k0 = k + 1; Nodes[k0] != joinPoint; k0++)
                    {
                        Node n = Nodes[k0];
                        if (Algorithms.Dominates(joinPoint, Nodes[k0], PDOM))
                            branchGraph.AddNode(Nodes[k0]);
                    }

                    SkipNode sk = new SkipNode((Nodes[k] as LeafNode), branchGraph);

                    // now make a graph containing both test node and all skipped nodes
                    //  combine these into one skip node
                    List<Node> ownedNodes = new List<Node>(branchGraph.Nodes);
                    ownedNodes.Add(Nodes[k]);
                    Graph tmpGraph = new Graph();
                    g.CombineNodes(ownedNodes, sk, tmpGraph);
                    tmpGraph.TransferEdgesToSubgraph(branchGraph);

                    // do this recursively on the skipped nodes
                    BuildBranchNodes(sk.BranchGraph);

                    // start over
                    k = 0;
                    Nodes = g.ReversePostOrder();
                }
                else
                {
                    k++;
                }
            }
        }
Exemplo n.º 32
0
 //private int level = 1;
 public void Add(int value)
 {
     int level = 1;
     int r = rand.Next();
     for (int i = 1; i <= 32; i++)
     {
         if ((r & 1) == 1) level++;
     }
     SkipNode node = new SkipNode(value, level);
     SkipNode current;
     for (int i = 1; i <= level; i++)
     {
         for (current = head; current.next[i] != null; current = current.next[i])
         {
             if (current.next[i].value > value) break;
         }
         node.next[i] = current.next[i];
         current.next[i] = node;
     }
 }