Exemple #1
0
        // This merges a bunch of temporary flat files
        // @param files
        // @param output file
        // @return The number of lines sorted. (P. Beaudoin)

        public static int MergeSortedFiles(List <FileStream> aFiles, FileStream aOutputFile, IComparer <long> aComparer)
        {
            IComparer <BinaryFileBuffer> lComp = Comparer <BinaryFileBuffer> .Create(new Comparison <BinaryFileBuffer>((BinaryFileBuffer x, BinaryFileBuffer y) =>
            {
                long x_peek = Convert.ToInt64(x.Peek());
                long y_peek = Convert.ToInt64(y.Peek());
                int lResult = aComparer.Compare(x_peek, y_peek);
                return(x_peek.CompareTo(y_peek));// Result;
            }));

            PriorityQueue <BinaryFileBuffer> lPq = new PriorityQueue <BinaryFileBuffer>(lComp, 11);

            foreach (FileStream lFile in aFiles)
            {
                BinaryFileBuffer lBfb = new BinaryFileBuffer(lFile);
                lPq.Enqueue(lBfb); //add
            }

            StreamWriter lBufWriter  = new StreamWriter(new BufferedStream(aOutputFile));
            int          lRowCounter = 0;

            try {
                while (lPq.Count > 0)
                {
                    BinaryFileBuffer lBfb  = lPq.Dequeue();
                    string           lLine = lBfb.Pop();
                    lBufWriter.WriteLine(lLine);
                    ++lRowCounter;
                    if (lBfb.Empty)
                    {
                        lBfb.BufReader.Close();
                        lBfb.OriginalFile.Close();// we don't need you anymore
                    }
                    else
                    {
                        lPq.Enqueue(lBfb); // add it back
                    }
                }
            } finally {
                lBufWriter.Close();
                //foreach (BinaryFileBuffer lBfb in lPq)
                //{
                //    lBfb.Close();
                //}
            }
            return(lRowCounter);
        }
        // This merges a bunch of temporary flat files
        // @param files
        // @param output file
        // @return The number of lines sorted. (P. Beaudoin)
        public static int MergeSortedFiles(List<FileStream> aFiles, FileStream aOutputFile, IComparer<long> aComparer)
        {
            IComparer<BinaryFileBuffer> lComp = Comparer<BinaryFileBuffer>.Create(new Comparison<BinaryFileBuffer>((BinaryFileBuffer x, BinaryFileBuffer y) =>
            {
                long x_peek = Convert.ToInt64(x.Peek());
                long y_peek = Convert.ToInt64(y.Peek());
                int lResult = aComparer.Compare(x_peek, y_peek);
                return x_peek.CompareTo(y_peek);// Result;
            }));

            PriorityQueue<BinaryFileBuffer> lPq = new PriorityQueue<BinaryFileBuffer>(lComp, 11);

            foreach (FileStream lFile in aFiles) {
                BinaryFileBuffer lBfb = new BinaryFileBuffer(lFile);
                lPq.Enqueue(lBfb); //add
            }

            StreamWriter lBufWriter = new StreamWriter(new BufferedStream(aOutputFile));
            int lRowCounter = 0;

            try {
                while (lPq.Count > 0) {
                    BinaryFileBuffer lBfb = lPq.Dequeue();
                    string lLine = lBfb.Pop();
                    lBufWriter.WriteLine(lLine);
                    ++lRowCounter;
                    if (lBfb.Empty) {
                        lBfb.BufReader.Close();
                        lBfb.OriginalFile.Close();// we don't need you anymore
                    } else {
                        lPq.Enqueue(lBfb); // add it back
                    }
                }
            } finally {
                lBufWriter.Close();
                //foreach (BinaryFileBuffer lBfb in lPq)
                //{
                //    lBfb.Close();
                //}
            }
            return lRowCounter;
        }