Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/09/12 11:42:08 (12 years ago)
Author:
gkronber
Message:

#1733 updated alglib sources to version 3.4.0 of alglib and updated project and plugin references

Location:
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0
Files:
1 edited
1 copied
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/3.4.0/ALGLIB-3.4.0/ap.cs

    r4977 r7294  
    33Copyright (c) 2003-2009 Sergey Bochkanov (ALGLIB project).
    44
    5 >>> LICENSE >>>
     5>>> SOURCE LICENSE >>>
    66This program is free software; you can redistribute it and/or modify
    77it under the terms of the GNU General Public License as published by
     
    1616A copy of the GNU General Public License is available at
    1717http://www.fsf.org/licensing/licenses
    18 
    1918>>> END OF LICENSE >>>
    2019*************************************************************************/
     
    258257
    259258        /****************************************************************
     259        prints formatted complex
     260        ****************************************************************/
     261        public static string format(complex a, int _dps)
     262        {
     263            int dps = Math.Abs(_dps);
     264            string fmt = _dps>=0 ? "F" : "E";
     265            string fmtx = String.Format("{{0:"+fmt+"{0}}}", dps);
     266            string fmty = String.Format("{{0:"+fmt+"{0}}}", dps);
     267            string result = String.Format(fmtx, a.x) + (a.y >= 0 ? "+" : "-") + String.Format(fmty, Math.Abs(a.y)) + "i";
     268            result = result.Replace(',', '.');
     269            return result;
     270        }
     271
     272        /****************************************************************
    260273        prints formatted array
    261274        ****************************************************************/
     
    287300        prints formatted array
    288301        ****************************************************************/
    289         public static string format(double[] a, int dps)
    290         {
    291             string fmt = String.Format("{{0:F{0}}}", dps);
     302        public static string format(double[] a, int _dps)
     303        {
     304            int dps = Math.Abs(_dps);
     305            string sfmt = _dps >= 0 ? "F" : "E";
     306            string fmt = String.Format("{{0:" + sfmt + "{0}}}", dps);
    292307            string[] result = new string[len(a)];
    293308            int i;
     
    303318        prints formatted array
    304319        ****************************************************************/
    305         public static string format(complex[] a, int dps)
    306         {
    307             string fmtx = String.Format("{{0:F{0}}}", dps);
    308             string fmty = String.Format("{{0:F{0}}}", dps);
     320        public static string format(complex[] a, int _dps)
     321        {
     322            int dps = Math.Abs(_dps);
     323            string fmt = _dps >= 0 ? "F" : "E";
     324            string fmtx = String.Format("{{0:"+fmt+"{0}}}", dps);
     325            string fmty = String.Format("{{0:"+fmt+"{0}}}", dps);
    309326            string[] result = new string[len(a)];
    310327            int i;
     
    586603
    587604    }
    588 }
     605   
     606    /********************************************************************
     607    serializer object (should not be used directly)
     608    ********************************************************************/
     609    public class serializer
     610    {
     611        enum SMODE { DEFAULT, ALLOC, TO_STRING, FROM_STRING };
     612        private const int SER_ENTRIES_PER_ROW = 5;
     613        private const int SER_ENTRY_LENGTH    = 11;
     614       
     615        private SMODE mode;
     616        private int entries_needed;
     617        private int entries_saved;
     618        private int bytes_asked;
     619        private int bytes_written;
     620        private int bytes_read;
     621        private char[] out_str;
     622        private char[] in_str;
     623       
     624        public serializer()
     625        {
     626            mode = SMODE.DEFAULT;
     627            entries_needed = 0;
     628            bytes_asked = 0;
     629        }
     630
     631        public void alloc_start()
     632        {
     633            entries_needed = 0;
     634            bytes_asked = 0;
     635            mode = SMODE.ALLOC;
     636        }
     637
     638        public void alloc_entry()
     639        {
     640            if( mode!=SMODE.ALLOC )
     641                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
     642            entries_needed++;
     643        }
     644
     645        private int get_alloc_size()
     646        {
     647            int rows, lastrowsize, result;
     648           
     649            // check and change mode
     650            if( mode!=SMODE.ALLOC )
     651                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
     652           
     653            // if no entries needes (degenerate case)
     654            if( entries_needed==0 )
     655            {
     656                bytes_asked = 1;
     657                return bytes_asked;
     658            }
     659           
     660            // non-degenerate case
     661            rows = entries_needed/SER_ENTRIES_PER_ROW;
     662            lastrowsize = SER_ENTRIES_PER_ROW;
     663            if( entries_needed%SER_ENTRIES_PER_ROW!=0 )
     664            {
     665                lastrowsize = entries_needed%SER_ENTRIES_PER_ROW;
     666                rows++;
     667            }
     668           
     669            // calculate result size
     670            result  = ((rows-1)*SER_ENTRIES_PER_ROW+lastrowsize)*SER_ENTRY_LENGTH;
     671            result +=  (rows-1)*(SER_ENTRIES_PER_ROW-1)+(lastrowsize-1);
     672            result += rows*2;
     673            bytes_asked = result;
     674            return result;
     675        }
     676
     677        public void sstart_str()
     678        {
     679            int allocsize = get_alloc_size();
     680           
     681            // check and change mode
     682            if( mode!=SMODE.ALLOC )
     683                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
     684            mode = SMODE.TO_STRING;
     685           
     686            // other preparations
     687            out_str = new char[allocsize];
     688            entries_saved = 0;
     689            bytes_written = 0;
     690        }
     691
     692        public void ustart_str(string s)
     693        {
     694            // check and change mode
     695            if( mode!=SMODE.DEFAULT )
     696                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
     697            mode = SMODE.FROM_STRING;
     698           
     699            in_str = s.ToCharArray();
     700            bytes_read = 0;
     701        }
     702
     703        public void serialize_bool(bool v)
     704        {
     705            if( mode!=SMODE.TO_STRING )
     706                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
     707            bool2str(v, out_str, ref bytes_written);
     708            entries_saved++;
     709            if( entries_saved%SER_ENTRIES_PER_ROW!=0 )
     710            {
     711                out_str[bytes_written] = ' ';
     712                bytes_written++;
     713            }
     714            else
     715            {
     716                out_str[bytes_written+0] = '\r';
     717                out_str[bytes_written+1] = '\n';
     718                bytes_written+=2;
     719            }           
     720        }
     721
     722        public void serialize_int(int v)
     723        {
     724            if( mode!=SMODE.TO_STRING )
     725                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
     726            int2str(v, out_str, ref bytes_written);
     727            entries_saved++;
     728            if( entries_saved%SER_ENTRIES_PER_ROW!=0 )
     729            {
     730                out_str[bytes_written] = ' ';
     731                bytes_written++;
     732            }
     733            else
     734            {
     735                out_str[bytes_written+0] = '\r';
     736                out_str[bytes_written+1] = '\n';
     737                bytes_written+=2;
     738            }
     739        }
     740
     741        public void serialize_double(double v)
     742        {
     743            if( mode!=SMODE.TO_STRING )
     744                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
     745            double2str(v, out_str, ref bytes_written);
     746            entries_saved++;
     747            if( entries_saved%SER_ENTRIES_PER_ROW!=0 )
     748            {
     749                out_str[bytes_written] = ' ';
     750                bytes_written++;
     751            }
     752            else
     753            {
     754                out_str[bytes_written+0] = '\r';
     755                out_str[bytes_written+1] = '\n';
     756                bytes_written+=2;
     757            }
     758        }
     759
     760        public bool unserialize_bool()
     761        {
     762            if( mode!=SMODE.FROM_STRING )
     763                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
     764            return str2bool(in_str, ref bytes_read);
     765        }
     766
     767        public int unserialize_int()
     768        {
     769            if( mode!=SMODE.FROM_STRING )
     770                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
     771            return str2int(in_str, ref bytes_read);
     772        }
     773
     774        public double unserialize_double()
     775        {
     776            if( mode!=SMODE.FROM_STRING )
     777                throw new alglib.alglibexception("ALGLIB: internal error during (un)serialization");
     778            return str2double(in_str, ref bytes_read);
     779        }
     780
     781        public void stop()
     782        {
     783        }
     784
     785        public string get_string()
     786        {
     787            return new string(out_str, 0, bytes_written);
     788        }
     789
     790
     791        /************************************************************************
     792        This function converts six-bit value (from 0 to 63)  to  character  (only
     793        digits, lowercase and uppercase letters, minus and underscore are used).
     794
     795        If v is negative or greater than 63, this function returns '?'.
     796        ************************************************************************/
     797        private static char[] _sixbits2char_tbl = new char[64]{
     798                '0', '1', '2', '3', '4', '5', '6', '7',
     799                '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
     800                'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
     801                'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
     802                'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
     803                'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
     804                'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
     805                'u', 'v', 'w', 'x', 'y', 'z', '-', '_' };
     806        private static char sixbits2char(int v)
     807        {
     808            if( v<0 || v>63 )
     809                return '?';
     810            return _sixbits2char_tbl[v];
     811        }
     812       
     813        /************************************************************************
     814        This function converts character to six-bit value (from 0 to 63).
     815
     816        This function is inverse of ae_sixbits2char()
     817        If c is not correct character, this function returns -1.
     818        ************************************************************************/
     819        private static int[] _char2sixbits_tbl = new int[128] {
     820            -1, -1, -1, -1, -1, -1, -1, -1,
     821            -1, -1, -1, -1, -1, -1, -1, -1,
     822            -1, -1, -1, -1, -1, -1, -1, -1,
     823            -1, -1, -1, -1, -1, -1, -1, -1,
     824            -1, -1, -1, -1, -1, -1, -1, -1,
     825            -1, -1, -1, -1, -1, 62, -1, -1,
     826             0,  1,  2,  3,  4,  5,  6,  7,
     827             8,  9, -1, -1, -1, -1, -1, -1,
     828            -1, 10, 11, 12, 13, 14, 15, 16,
     829            17, 18, 19, 20, 21, 22, 23, 24,
     830            25, 26, 27, 28, 29, 30, 31, 32,
     831            33, 34, 35, -1, -1, -1, -1, 63,
     832            -1, 36, 37, 38, 39, 40, 41, 42,
     833            43, 44, 45, 46, 47, 48, 49, 50,
     834            51, 52, 53, 54, 55, 56, 57, 58,
     835            59, 60, 61, -1, -1, -1, -1, -1 };
     836        private static int char2sixbits(char c)
     837        {
     838            return (c>=0 && c<127) ? _char2sixbits_tbl[c] : -1;
     839        }
     840       
     841        /************************************************************************
     842        This function converts three bytes (24 bits) to four six-bit values
     843        (24 bits again).
     844
     845        src         array
     846        src_offs    offset of three-bytes chunk
     847        dst         array for ints
     848        dst_offs    offset of four-ints chunk
     849        ************************************************************************/
     850        private static void threebytes2foursixbits(byte[] src, int src_offs, int[] dst, int dst_offs)
     851        {
     852            dst[dst_offs+0] =  src[src_offs+0] & 0x3F;
     853            dst[dst_offs+1] = (src[src_offs+0]>>6) | ((src[src_offs+1]&0x0F)<<2);
     854            dst[dst_offs+2] = (src[src_offs+1]>>4) | ((src[src_offs+2]&0x03)<<4);
     855            dst[dst_offs+3] =  src[src_offs+2]>>2;
     856        }
     857
     858        /************************************************************************
     859        This function converts four six-bit values (24 bits) to three bytes
     860        (24 bits again).
     861
     862        src         pointer to four ints
     863        src_offs    offset of the chunk
     864        dst         pointer to three bytes
     865        dst_offs    offset of the chunk
     866        ************************************************************************/
     867        private static void foursixbits2threebytes(int[] src, int src_offs, byte[] dst, int dst_offs)
     868        {
     869            dst[dst_offs+0] =      (byte)(src[src_offs+0] | ((src[src_offs+1]&0x03)<<6));
     870            dst[dst_offs+1] = (byte)((src[src_offs+1]>>2) | ((src[src_offs+2]&0x0F)<<4));
     871            dst[dst_offs+2] = (byte)((src[src_offs+2]>>4) |  (src[src_offs+3]<<2));
     872        }
     873
     874        /************************************************************************
     875        This function serializes boolean value into buffer
     876
     877        v           boolean value to be serialized
     878        buf         buffer, at least 11 characters wide
     879        offs        offset in the buffer
     880       
     881        after return from this function, offs points to the char's past the value
     882        being read.
     883        ************************************************************************/
     884        private static void bool2str(bool v, char[] buf, ref int offs)
     885        {
     886            char c = v ? '1' : '0';
     887            int i;
     888            for(i=0; i<SER_ENTRY_LENGTH; i++)
     889                buf[offs+i] = c;
     890            offs += SER_ENTRY_LENGTH;
     891        }
     892
     893        /************************************************************************
     894        This function unserializes boolean value from buffer
     895
     896        buf         buffer which contains value; leading spaces/tabs/newlines are
     897                    ignored, traling spaces/tabs/newlines are treated as  end  of
     898                    the boolean value.
     899        offs        offset in the buffer
     900       
     901        after return from this function, offs points to the char's past the value
     902        being read.
     903
     904        This function raises an error in case unexpected symbol is found
     905        ************************************************************************/
     906        private static bool str2bool(char[] buf, ref int offs)
     907        {
     908            bool was0, was1;
     909            string emsg = "ALGLIB: unable to read boolean value from stream";
     910           
     911            was0 = false;
     912            was1 = false;
     913            while( buf[offs]==' ' || buf[offs]=='\t' || buf[offs]=='\n' || buf[offs]=='\r' )
     914                offs++;
     915            while( buf[offs]!=' ' && buf[offs]!='\t' && buf[offs]!='\n' && buf[offs]!='\r' && buf[offs]!=0 )
     916            {
     917                if( buf[offs]=='0' )
     918                {
     919                    was0 = true;
     920                    offs++;
     921                    continue;
     922                }
     923                if( buf[offs]=='1' )
     924                {
     925                    was1 = true;
     926                    offs++;
     927                    continue;
     928                }
     929                throw new alglib.alglibexception(emsg);
     930            }
     931            if( (!was0) && (!was1) )
     932                throw new alglib.alglibexception(emsg);
     933            if( was0 && was1 )
     934                throw new alglib.alglibexception(emsg);
     935            return was1 ? true : false;
     936        }
     937
     938        /************************************************************************
     939        This function serializes integer value into buffer
     940
     941        v           integer value to be serialized
     942        buf         buffer, at least 11 characters wide
     943        offs        offset in the buffer
     944       
     945        after return from this function, offs points to the char's past the value
     946        being read.
     947
     948        This function raises an error in case unexpected symbol is found
     949        ************************************************************************/
     950        private static void int2str(int v, char[] buf, ref int offs)
     951        {
     952            int i;
     953            byte[] _bytes = System.BitConverter.GetBytes((int)v);
     954            byte[]  bytes = new byte[9];
     955            int[] sixbits = new int[12];
     956            byte c;
     957           
     958            //
     959            // copy v to array of bytes, sign extending it and
     960            // converting to little endian order. Additionally,
     961            // we set 9th byte to zero in order to simplify
     962            // conversion to six-bit representation
     963            //
     964            if( !System.BitConverter.IsLittleEndian )
     965                System.Array.Reverse(_bytes);
     966            c = v<0 ? (byte)0xFF : (byte)0x00;
     967            for(i=0; i<sizeof(int); i++)
     968                bytes[i] = _bytes[i];
     969            for(i=sizeof(int); i<8; i++)
     970                bytes[i] = c;
     971            bytes[8] = 0;
     972           
     973            //
     974            // convert to six-bit representation, output
     975            //
     976            // NOTE: last 12th element of sixbits is always zero, we do not output it
     977            //
     978            threebytes2foursixbits(bytes, 0, sixbits, 0);
     979            threebytes2foursixbits(bytes, 3, sixbits, 4);
     980            threebytes2foursixbits(bytes, 6, sixbits, 8);       
     981            for(i=0; i<SER_ENTRY_LENGTH; i++)
     982                buf[offs+i] = sixbits2char(sixbits[i]);
     983            offs += SER_ENTRY_LENGTH;
     984        }
     985
     986        /************************************************************************
     987        This function unserializes integer value from string
     988
     989        buf         buffer which contains value; leading spaces/tabs/newlines are
     990                    ignored, traling spaces/tabs/newlines are treated as  end  of
     991                    the integer value.
     992        offs        offset in the buffer
     993       
     994        after return from this function, offs points to the char's past the value
     995        being read.
     996
     997        This function raises an error in case unexpected symbol is found
     998        ************************************************************************/
     999        private static int str2int(char[] buf, ref int offs)
     1000        {
     1001            string emsg =       "ALGLIB: unable to read integer value from stream";
     1002            string emsg3264 =   "ALGLIB: unable to read integer value from stream (value does not fit into 32 bits)";
     1003            int[] sixbits = new int[12];
     1004            byte[] bytes = new byte[9];
     1005            byte[] _bytes = new byte[sizeof(int)];
     1006            int sixbitsread, i;
     1007            byte c;
     1008           
     1009            //
     1010            // 1. skip leading spaces
     1011            // 2. read and decode six-bit digits
     1012            // 3. set trailing digits to zeros
     1013            // 4. convert to little endian 64-bit integer representation
     1014            // 5. check that we fit into int
     1015            // 6. convert to big endian representation, if needed
     1016            //
     1017            sixbitsread = 0;
     1018            while( buf[offs]==' ' || buf[offs]=='\t' || buf[offs]=='\n' || buf[offs]=='\r' )
     1019                offs++;
     1020            while( buf[offs]!=' ' && buf[offs]!='\t' && buf[offs]!='\n' && buf[offs]!='\r' && buf[offs]!=0 )
     1021            {
     1022                int d;
     1023                d = char2sixbits(buf[offs]);
     1024                if( d<0 || sixbitsread>=SER_ENTRY_LENGTH )
     1025                    throw new alglib.alglibexception(emsg);
     1026                sixbits[sixbitsread] = d;
     1027                sixbitsread++;
     1028                offs++;
     1029            }
     1030            if( sixbitsread==0 )
     1031                throw new alglib.alglibexception(emsg);
     1032            for(i=sixbitsread; i<12; i++)
     1033                sixbits[i] = 0;
     1034            foursixbits2threebytes(sixbits, 0, bytes, 0);
     1035            foursixbits2threebytes(sixbits, 4, bytes, 3);
     1036            foursixbits2threebytes(sixbits, 8, bytes, 6);
     1037            c = (bytes[sizeof(int)-1] & 0x80)!=0 ? (byte)0xFF : (byte)0x00;
     1038            for(i=sizeof(int); i<8; i++)
     1039                if( bytes[i]!=c )
     1040                    throw new alglib.alglibexception(emsg3264);
     1041            for(i=0; i<sizeof(int); i++)
     1042                _bytes[i] = bytes[i];       
     1043            if( !System.BitConverter.IsLittleEndian )
     1044                System.Array.Reverse(_bytes);
     1045            return System.BitConverter.ToInt32(_bytes,0);
     1046        }   
     1047       
     1048       
     1049        /************************************************************************
     1050        This function serializes double value into buffer
     1051
     1052        v           double value to be serialized
     1053        buf         buffer, at least 11 characters wide
     1054        offs        offset in the buffer
     1055       
     1056        after return from this function, offs points to the char's past the value
     1057        being read.
     1058        ************************************************************************/
     1059        private static void double2str(double v, char[] buf, ref int offs)
     1060        {
     1061            int i;
     1062            int[] sixbits = new int[12];
     1063            byte[] bytes = new byte[9];
     1064
     1065            //
     1066            // handle special quantities
     1067            //
     1068            if( System.Double.IsNaN(v) )
     1069            {
     1070                buf[offs+0] = '.';
     1071                buf[offs+1] = 'n';
     1072                buf[offs+2] = 'a';
     1073                buf[offs+3] = 'n';
     1074                buf[offs+4] = '_';
     1075                buf[offs+5] = '_';
     1076                buf[offs+6] = '_';
     1077                buf[offs+7] = '_';
     1078                buf[offs+8] = '_';
     1079                buf[offs+9] = '_';
     1080                buf[offs+10] = '_';
     1081                offs += SER_ENTRY_LENGTH;
     1082                return;
     1083            }
     1084            if( System.Double.IsPositiveInfinity(v) )
     1085            {
     1086                buf[offs+0] = '.';
     1087                buf[offs+1] = 'p';
     1088                buf[offs+2] = 'o';
     1089                buf[offs+3] = 's';
     1090                buf[offs+4] = 'i';
     1091                buf[offs+5] = 'n';
     1092                buf[offs+6] = 'f';
     1093                buf[offs+7] = '_';
     1094                buf[offs+8] = '_';
     1095                buf[offs+9] = '_';
     1096                buf[offs+10] = '_';
     1097                offs += SER_ENTRY_LENGTH;
     1098                return;
     1099            }
     1100            if( System.Double.IsNegativeInfinity(v) )
     1101            {
     1102                buf[offs+0] = '.';
     1103                buf[offs+1] = 'n';
     1104                buf[offs+2] = 'e';
     1105                buf[offs+3] = 'g';
     1106                buf[offs+4] = 'i';
     1107                buf[offs+5] = 'n';
     1108                buf[offs+6] = 'f';
     1109                buf[offs+7] = '_';
     1110                buf[offs+8] = '_';
     1111                buf[offs+9] = '_';
     1112                buf[offs+10] = '_';
     1113                offs += SER_ENTRY_LENGTH;
     1114                return;
     1115            }
     1116           
     1117            //
     1118            // process general case:
     1119            // 1. copy v to array of chars
     1120            // 2. set 9th byte to zero in order to simplify conversion to six-bit representation
     1121            // 3. convert to little endian (if needed)
     1122            // 4. convert to six-bit representation
     1123            //    (last 12th element of sixbits is always zero, we do not output it)
     1124            //
     1125            byte[] _bytes = System.BitConverter.GetBytes((double)v);
     1126            if( !System.BitConverter.IsLittleEndian )
     1127                System.Array.Reverse(_bytes);
     1128            for(i=0; i<sizeof(double); i++)
     1129                bytes[i] = _bytes[i];
     1130            for(i=sizeof(double); i<9; i++)
     1131                bytes[i] = 0;
     1132            threebytes2foursixbits(bytes, 0, sixbits, 0);
     1133            threebytes2foursixbits(bytes, 3, sixbits, 4);
     1134            threebytes2foursixbits(bytes, 6, sixbits, 8);
     1135            for(i=0; i<SER_ENTRY_LENGTH; i++)
     1136                buf[offs+i] = sixbits2char(sixbits[i]);
     1137            offs += SER_ENTRY_LENGTH;
     1138        }
     1139
     1140        /************************************************************************
     1141        This function unserializes double value from string
     1142
     1143        buf         buffer which contains value; leading spaces/tabs/newlines are
     1144                    ignored, traling spaces/tabs/newlines are treated as  end  of
     1145                    the double value.
     1146        offs        offset in the buffer
     1147       
     1148        after return from this function, offs points to the char's past the value
     1149        being read.
     1150
     1151        This function raises an error in case unexpected symbol is found
     1152        ************************************************************************/
     1153        private static double str2double(char[] buf, ref int offs)
     1154        {
     1155            string emsg = "ALGLIB: unable to read double value from stream";
     1156            int[] sixbits = new int[12];
     1157            byte[]  bytes = new byte[9];
     1158            byte[] _bytes = new byte[sizeof(double)];
     1159            int sixbitsread, i;
     1160           
     1161           
     1162            //
     1163            // skip leading spaces
     1164            //
     1165            while( buf[offs]==' ' || buf[offs]=='\t' || buf[offs]=='\n' || buf[offs]=='\r' )
     1166                offs++;
     1167           
     1168             
     1169            //
     1170            // Handle special cases
     1171            //
     1172            if( buf[offs]=='.' )
     1173            {
     1174                string s = new string(buf, offs, SER_ENTRY_LENGTH);
     1175                if( s==".nan_______" )
     1176                {
     1177                    offs += SER_ENTRY_LENGTH;
     1178                    return System.Double.NaN;
     1179                }
     1180                if( s==".posinf____" )
     1181                {
     1182                    offs += SER_ENTRY_LENGTH;
     1183                    return System.Double.PositiveInfinity;
     1184                }
     1185                if( s==".neginf____" )
     1186                {
     1187                    offs += SER_ENTRY_LENGTH;
     1188                    return System.Double.NegativeInfinity;
     1189                }
     1190                throw new alglib.alglibexception(emsg);
     1191            }
     1192           
     1193            //
     1194            // General case:
     1195            // 1. read and decode six-bit digits
     1196            // 2. check that all 11 digits were read
     1197            // 3. set last 12th digit to zero (needed for simplicity of conversion)
     1198            // 4. convert to 8 bytes
     1199            // 5. convert to big endian representation, if needed
     1200            //
     1201            sixbitsread = 0;
     1202            while( buf[offs]!=' ' && buf[offs]!='\t' && buf[offs]!='\n' && buf[offs]!='\r' && buf[offs]!=0 )
     1203            {
     1204                int d;
     1205                d = char2sixbits(buf[offs]);
     1206                if( d<0 || sixbitsread>=SER_ENTRY_LENGTH )
     1207                    throw new alglib.alglibexception(emsg);
     1208                sixbits[sixbitsread] = d;
     1209                sixbitsread++;
     1210                offs++;
     1211            }
     1212            if( sixbitsread!=SER_ENTRY_LENGTH )
     1213                throw new alglib.alglibexception(emsg);
     1214            sixbits[SER_ENTRY_LENGTH] = 0;
     1215            foursixbits2threebytes(sixbits, 0, bytes, 0);
     1216            foursixbits2threebytes(sixbits, 4, bytes, 3);
     1217            foursixbits2threebytes(sixbits, 8, bytes, 6);
     1218            for(i=0; i<sizeof(double); i++)
     1219                _bytes[i] = bytes[i];       
     1220            if( !System.BitConverter.IsLittleEndian )
     1221                System.Array.Reverse(_bytes);       
     1222            return System.BitConverter.ToDouble(_bytes,0);
     1223        }
     1224    }}
Note: See TracChangeset for help on using the changeset viewer.