예제 #1
0
파일: IntPair.cs 프로젝트: edwardt/study
 public static void QSortIntPair(IntPair[] arr, int l, int r)
 {
     int i = l;
     int j = r;
     int x = arr[(i + j) / 2].Second;
     IntPair tmp;
     do
     {
         while (arr[i].Second > x)
             i++;
         while (arr[j].Second < x)
             j--;
         if (i <= j)
         {
             tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp;
             i++; j--;
         }
     } while (i < j);
     if (i < r)
         QSortIntPair(arr, i, r);
     if (l < j)
         QSortIntPair(arr, l, j);
 }
예제 #2
0
        /// <summary>
        /// Adds weights into flowgraph to favour connections that are in a larger 
        /// group of connections disabled by the same barrier
        /// </summary>
        /// <param name="g"></param>
        public static void AddWeight(Graph g)
        {
            IEnumerator<Vertex> vertexIter = g.vertices.GetEnumerator();
            Hashtable weights = new Hashtable();
            int max_weight = 0;
            int min_weight = 0xFFFFFFF;
            while (vertexIter.MoveNext())
            {
                Vertex v = vertexIter.Current;
                for (int i = 0; i < v.forward.Count; i++)
                {
                    Edge e = (Edge)v.forward[i];
                    if (e.ExtraInfo != null)
                    {
                        CILInstruction ins = ((DelayedAction)e.ExtraInfo).SourceInstruction;
                        if (weights[ins.ID] == null)
                            weights.Add(ins.ID, 1);
                        else
                        {
                            weights[ins.ID] = (int)weights[ins.ID] + 1;
                            if (max_weight < (int)weights[ins.ID])
                                max_weight = (int)weights[ins.ID];
                        }
                    }
                }
            }

            // print out the list of instruction usage with counters
            // first copy them to array to sort
            if (weights.Count == 0)
                return;
            IntPair[] arr = new IntPair[weights.Count];
            IDictionaryEnumerator iter = weights.GetEnumerator();
            int ix = 0;
            while (iter.MoveNext())
            {
                arr[ix++] = new IntPair((int)iter.Key, (int)iter.Value);
                if (min_weight > (int)iter.Value)
                    min_weight = (int)iter.Value;
            }
            IntPair.QSortIntPair(arr, 0, weights.Count - 1);

            StreamWriter fout = new StreamWriter("weightlog.tmp");
            for (int i = 0; i < arr.Length; i++)
                fout.WriteLine("{0} {1}", arr[i].First, arr[i].Second);

            int MAX_CAPACITY = 50;
            double scale;
            if (max_weight != min_weight)
                scale = (min_weight * MAX_CAPACITY) / (max_weight - min_weight);
            else
                scale = 0;
            // assigns the weights
            vertexIter = g.vertices.GetEnumerator();
            while (vertexIter.MoveNext())
            {
                Vertex v = vertexIter.Current;
                for (int i = 0; i < v.forward.Count; i++)
                {
                    Edge e = (Edge)v.forward[i];
                    if (e.ExtraInfo != null)
                    {
                        CILInstruction ins = ((DelayedAction)e.ExtraInfo).SourceInstruction;
                        e.capacity = (int)((1.0 * max_weight / (int)weights[ins.ID] - 1) * scale) + 1;
                        fout.Write("{0} ", e.capacity);
                    }
                }
            }
            fout.Close();
        }