All pastes #640652 Raw Edit

Something

public text v1 · immutable
#640652 ·published 2007-07-31 15:31 UTC
rendered paste body
[DllImport("kernel32", SetLastError = true)]
        static extern unsafe bool ReadFile(
              IntPtr hFile,                       // handle to file
              void* pBuffer,                      // data buffer
              int NumberOfBytesToRead,            // number of bytes to read
              int* pNumberOfBytesRead,            // number of bytes read
              int Overlapped                      // overlapped buffer
        );

static void Main(string[] args)
        {
            int lowestSoFar = int.MaxValue;

            for (int time = 0; time < 5000; time++)
            {

                Stopwatch total = new Stopwatch();
                total.Start();

            FileStream sentenceFile = File.Open("../sentences.txt", FileMode.Open, FileAccess.Read);
            byte[] array = new byte[sentenceFile.Length];

            #region Nums
            int[] nums = new int[(int)'z' + 1];
            nums[(int)'\r'] = -1;
            nums[(int)'a'] = 2;
            nums[(int)'b'] = 2;
            nums[(int)'c'] = 2;
            nums[(int)'d'] = 3;
            nums[(int)'e'] = 3;
            nums[(int)'f'] = 3;
            nums[(int)'g'] = 4;
            nums[(int)'h'] = 4;
            nums[(int)'i'] = 4;
            nums[(int)'j'] = 5;
            nums[(int)'k'] = 5;
            nums[(int)'l'] = 5;
            nums[(int)'m'] = 6;
            nums[(int)'n'] = 6;
            nums[(int)'o'] = 6;
            nums[(int)'p'] = 7;
            nums[(int)'q'] = 7;
            nums[(int)'r'] = 7;
            nums[(int)'s'] = 7;
            nums[(int)'t'] = 8;
            nums[(int)'u'] = 8;
            nums[(int)'v'] = 8;
            nums[(int)'w'] = 9;
            nums[(int)'x'] = 9;
            nums[(int)'y'] = 9;
            nums[(int)'z'] = 9;
            nums[(int)' '] = 0;
            #endregion

            #region Codes
            char[][] codes = new char[(byte)'z' + 1][];
            codes[(byte)'a'] = new char[1] { '2' };
            codes[(byte)'b'] = new char[2] { '2', '2' };
            codes[(byte)'c'] = new char[3] { '2', '2', '2' };
            codes[(byte)'d'] = new char[1] { '3' };
            codes[(byte)'e'] = new char[2] { '3', '3' };
            codes[(byte)'f'] = new char[3] { '3', '3', '3' };
            codes[(byte)'g'] = new char[1] { '4' };
            codes[(byte)'h'] = new char[2] { '4', '4' };
            codes[(byte)'i'] = new char[3] { '4', '4', '4' };
            codes[(byte)'j'] = new char[1] { '5' };
            codes[(byte)'k'] = new char[2] { '5', '5' };
            codes[(byte)'l'] = new char[3] { '5', '5', '5' };
            codes[(byte)'m'] = new char[1] { '6' };
            codes[(byte)'n'] = new char[2] { '6', '6' };
            codes[(byte)'o'] = new char[3] { '6', '6', '6' };
            codes[(byte)'p'] = new char[1] { '7' };
            codes[(byte)'q'] = new char[2] { '7', '7' };
            codes[(byte)'r'] = new char[3] { '7', '7', '7' };
            codes[(byte)'s'] = new char[4] { '7', '7', '7', '7' };
            codes[(byte)'t'] = new char[1] { '8' };
            codes[(byte)'u'] = new char[2] { '8', '8' };
            codes[(byte)'v'] = new char[3] { '8', '8', '8' };
            codes[(byte)'w'] = new char[1] { '9' };
            codes[(byte)'x'] = new char[2] { '9', '9' };
            codes[(byte)'y'] = new char[3] { '9', '9', '9' };
            codes[(byte)'z'] = new char[4] { '9', '9', '9', '9' };
            codes[(byte)' '] = new char[1] { '0' };
            #endregion

            int[] lengths = new int[(byte)'z' + 1];

            for (int i = 0; i < codes.Length; i++)
            {
                if (codes[i] != null)
                    lengths[i] = codes[i].Length;
            }

            int lastKey = -1;
            int thisKey = -1;
            byte letter = byte.MinValue;

            unsafe
            {
                char[] bresult = new char[sentenceFile.Length * 4];
                int bytes = 0;

                fixed (byte* p = array)
                {
                    int n = 0;
                    bool done = ReadFile(sentenceFile.Handle, p, (int)sentenceFile.Length, &n, 0);

                    for (int c = 0; c < array.Length; c++)
                    {
                        letter = p[c];

                        if (letter == 13)
                        {
                            bresult[bytes++] = (char)'\n';
                            c++;
                            lastKey = -1;
                           // continue;
                        }
                        else
                        {
                            thisKey = nums[letter];

                            if (thisKey == lastKey)
                                bresult[bytes++] = (char)' ';

                            char[] b = codes[letter];
                            b.CopyTo(bresult, bytes);
                            bytes += lengths[letter];

                            lastKey = thisKey;
                        }
                    }
                }

                Console.WriteLine(bresult, 0, bytes);
            }

            sentenceFile.Close();


            total.Stop();

            if (total.ElapsedMilliseconds < lowestSoFar)
            {
                Console.WriteLine("Took: " + total.ElapsedMilliseconds);
                lowestSoFar = (int)total.ElapsedMilliseconds;
            }

            }

        }