public bool FetchNext() { //bRead must be false if (streamReader.EndOfStream) { return(false); } currentThisinh.FromString(streamReader.ReadLine()); bRead = true; return(bRead); }
public int [] Distribute(string sfi, string[] sfo, int numfile) { int n = 0; int i, q, r; int m, mh; int k = 0; int[] numrun = new int[numfile]; ThiSinh b; StreamReader fi = new StreamReader(new FileStream(sfi, FileMode.Open, FileAccess.Read)); while ((n < m_nSize) && (!fi.EndOfStream)) { m_arrThiSinh[n] = new ThiSinh(); m_arrThiSinh[n].FromString(fi.ReadLine()); //Console.WriteLine(m_arrThiSinh[n].ToString()); n++; } m = min(m_nSize, n); mh = m / 2; i = mh - 1; while (i >= 0) { shift <ThiSinh>(m_arrThiSinh, i, m - 1, ThiSinh.CompareID); i--; } StreamWriter[] fo = new StreamWriter[numfile]; for (int f = 0; f < numfile; f++) { fo[f] = new StreamWriter(new FileStream(sfo[f], FileMode.Create, FileAccess.Write, FileShare.ReadWrite)); numrun[f] = 0; } q = m - 1; while (!fi.EndOfStream) { b = new ThiSinh(); b.FromString(fi.ReadLine()); //Console.WriteLine(b.ToString()); n++; fo[k].WriteLine(m_arrThiSinh[0].ToString()); if (ThiSinh.CompareID(m_arrThiSinh[0], b) < 0) { m_arrThiSinh[0] = b; shift <ThiSinh>(m_arrThiSinh, 0, q, ThiSinh.CompareID); } else { m_arrThiSinh[0] = m_arrThiSinh[q]; shift <ThiSinh>(m_arrThiSinh, 0, q - 1, ThiSinh.CompareID); m_arrThiSinh[q] = b; if (q < mh) { shift <ThiSinh>(m_arrThiSinh, q, m - 1, ThiSinh.CompareID); } q = q - 1; if (q < 0) { q = m - 1; numrun[k]++; k++; if (k >= numfile) { k -= numfile; } } } } r = m - 1; while (q >= 0) { fo[k].WriteLine(m_arrThiSinh[0].ToString()); m_arrThiSinh[0] = m_arrThiSinh[q]; shift <ThiSinh>(m_arrThiSinh, 0, q - 1, ThiSinh.CompareID); m_arrThiSinh[q] = m_arrThiSinh[r]; r = r - 1; if (q < mh) { shift <ThiSinh>(m_arrThiSinh, q, r, ThiSinh.CompareID); } q = q - 1; } numrun[k]++; k++; if (k >= numfile) { k -= numfile; } while (r >= 0) { fo[k].WriteLine(m_arrThiSinh[0].ToString()); m_arrThiSinh[0] = m_arrThiSinh[r]; r = r - 1; shift <ThiSinh>(m_arrThiSinh, 0, r, ThiSinh.CompareID); } numrun[k]++; k++; if (k >= numfile) { k -= numfile; } for (int f = 0; f < numfile; f++) { //Console.WriteLine("Tap tin {0} co {1} run", sfo[f], numrun[f]); fo[f].Close(); } fi.Close(); return(numrun); }