Exemple #1
0
 public static void main(string[] args)
 {
     ulong[] bounds = new ulong[] {
         PACKED_1_START,
         PACKED_1_END,
         PACKED_2_START,
         PACKED_2_END,
         PACKED_3_START,
         PACKED_3_END,
         PACKED_4_START,
         PACKED_4_END,
         PACKED_5_START,
         PACKED_5_END,
         PACKED_6_START,
         PACKED_6_END,
         PACKED_7_START,
         PACKED_7_END,
     };
     for (int n = 0; n < bounds.Length; n++)
     {
         for (ulong l = bounds[n] - 1; l < bounds[n] + 2; l++)
         {
             long abs = Math.Abs((long)l);
             Console.WriteLine(String.Format("number {0} {1} {2} bits: {3} switch: {4} - {5}"
                                             , l.ToString("X")
                                             , l
                                             , Fns.numberOfLeadingZeros(abs)
                                             , bitsneeded((long)l)
                                             , switchon((long)l)
                                             , abs));
         }
     }
 }
Exemple #2
0
 public static IDictionary <Type, IDictionary <String, WriteHandler> > installHandler(IDictionary <Type, IDictionary <String, WriteHandler> > map
                                                                                      , Type cls
                                                                                      , String tag
                                                                                      , WriteHandler handler)
 {
     map[cls] = Fns.soloMap(tag, handler);
     return(map);
 }
Exemple #3
0
 public static int switchon(long l)
 {
     if (l > 0)
     {
         return(Fns.numberOfLeadingZeros(l));
     }
     else
     {
         return(Fns.numberOfLeadingZeros(~l));
     }
 }
Exemple #4
0
 public static int bitsneeded(long l)
 {
     if (l > 0)
     {
         return(65 - Fns.numberOfLeadingZeros(l));
     }
     else
     {
         return(65 - Fns.numberOfLeadingZeros(~l));
     }
 }
Exemple #5
0
 public void writeRawFloat(float f)
 {
     writeRawInt32(Fns.SingleToInt32Bits(f));
 }
Exemple #6
0
        private static org.fressian.handlers.ILookup <Type, IDictionary <String, WriteHandler> > initExtendedWriteHandlers()
        {
            IDictionary <Type, IDictionary <String, WriteHandler> > handlers = new Dictionary <Type, IDictionary <String, WriteHandler> >();
            var intHandler = new Action <Writer, object>((w, o) => w.writeInt(o));

            installHandler(handlers, typeof(IList <object>), "list", new GenericWriteHandler <object>(new Action <Writer, object>((w, o) => w.writeList(o))));
            installHandler(handlers, typeof(DateTime), "inst", new GenericWriteHandler <object>(new Action <Writer, object>((w, o) =>
            {
                w.writeTag("inst", 1);
                w.writeInt(getTime(((DateTime)o)));
            })));
            installHandler(handlers, typeof(ISet <object>), "set", new GenericWriteHandler <object>(new Action <Writer, object>((w, o) =>
            {
                w.writeTag("set", 1);
                w.writeList(((ISet <object>)o).ToList());
            })));
            installHandler(handlers, typeof(IDictionary <object, object>), "map", new GenericWriteHandler <object>(new Action <Writer, object>((w, o) =>
            {
                w.writeTag("map", 1);
                IDictionary <Object, Object> m = (IDictionary <object, object>)o;
                IList <object> l = new List <object>();
                foreach (var e in m)
                {
                    l.Add(e.Key);
                    l.Add(e.Value);
                }
                w.writeList(l);
            })));

            installHandler(handlers, typeof(BigInteger), "bigint", new GenericWriteHandler <object>(new Action <Writer, object>((w, o) =>
            {
                BigInteger b = (BigInteger)o;
                w.writeTag("bigint", 1);
                w.writeBytes(b.ToByteArray());
            })));

            installHandler(handlers, typeof(decimal), "bigdec", new GenericWriteHandler <object>(new Action <Writer, object>((w, o) =>
            {
                decimal d        = (decimal)o;
                var unscaledVals = Fns.UnscaledValues(d);
                w.writeTag("bigdec", 2);
                w.writeBytes(unscaledVals.Item1);
                w.writeInt(unscaledVals.Item2);
            })));

            installHandler(handlers, typeof(Regex), "regex", new GenericWriteHandler <object>(new Action <Writer, object>((w, o) =>
            {
                Regex re = (Regex)o;
                w.writeTag("regex", 1);
                w.writeString(re.ToString());
            })));

            installHandler(handlers, typeof(Uri), "uri", new GenericWriteHandler <object>(new Action <Writer, object>((w, o) =>
            {
                Uri uri = (Uri)o;
                w.writeTag("uri", 1);
                w.writeString(uri.ToString());
            })));

            installHandler(handlers, typeof(Guid), "uuid", new GenericWriteHandler <object>(new Action <Writer, object>((w, o) =>
            {
                Guid uuid = (Guid)o;
                w.writeTag("uuid", 1);
                w.writeBytes(Fns.UUIDtoByteArray(uuid));
            })));

            return(new InheritanceLookup <IDictionary <String, WriteHandler> >(new MapLookup <Type, IDictionary <String, WriteHandler> >(handlers)));
        }
Exemple #7
0
        private static IDictionary <object, ReadHandler> initExtendedReadHandlers()
        {
            IDictionary <object, ReadHandler> handlers = new Dictionary <object, ReadHandler>();

            handlers["set"] = new GenericReadHandler <ISet <object> >(new Func <Reader, Object, int, ISet <object> >((r, t, c) =>
            {
                ISet <object> s = new HashSet <object>();
                s.UnionWith((IList <object>)r.readObject());
                return(s);
            }));

            handlers["map"] = new GenericReadHandler <IDictionary <object, object> >(new Func <Reader, Object, int, IDictionary <object, object> >((r, t, c) =>
            {
                IDictionary <object, object> m = new Dictionary <object, object>();
                //FF - java impl casted to RandomAccess for which there is no c# equivilent
                //List kvs = (List) (RandomAccess) r.readObject();
                IList <object> kvs = (IList <object>)r.readObject();
                for (int i = 0; i < kvs.Count; i += 2)
                {
                    if (kvs[i] == null)
                    {
                        m[Handlers.Null.Value] = kvs[i + 1];
                    }
                    else
                    {
                        m[kvs[i]] = kvs[i + 1];
                    }

                    //m[kvs[i]] = kvs[i + 1];
                }
                return(m);
            }));

            handlers["int[]"] = new GenericReadHandler <int[]>(new Func <Reader, Object, int, int[]>((r, t, c) =>
            {
                int size     = Fns.intCast(r.readInt());
                int[] result = new int[size];
                for (int n = 0; n < size; n++)
                {
                    result[n] = Fns.intCast(r.readInt());
                }
                return(result);
            }));

            handlers["long[]"] = new GenericReadHandler <long[]>(new Func <Reader, Object, int, long[]>((r, t, c) =>
            {
                int size      = Fns.intCast(r.readInt());
                long[] result = new long[size];
                for (int n = 0; n < size; n++)
                {
                    result[n] = r.readInt();
                }
                return(result);
            }));

            handlers["float[]"] = new GenericReadHandler <float[]>(new Func <Reader, Object, int, float[]>((r, t, c) =>
            {
                int size       = Fns.intCast(r.readInt());
                float[] result = new float[size];
                for (int n = 0; n < size; n++)
                {
                    result[n] = r.readFloat();
                }
                return(result);
            }));

            handlers["boolean[]"] = new GenericReadHandler <bool[]>(new Func <Reader, Object, int, bool[]>((r, t, c) =>
            {
                int size      = Fns.intCast(r.readInt());
                bool[] result = new bool[size];
                for (int n = 0; n < size; n++)
                {
                    result[n] = r.readBoolean();
                }
                return(result);
            }));

            handlers["double[]"] = new GenericReadHandler <double[]>(new Func <Reader, Object, int, double[]>((r, t, c) =>
            {
                int size        = Fns.intCast(r.readInt());
                double[] result = new double[size];
                for (int n = 0; n < size; n++)
                {
                    result[n] = r.readDouble();
                }
                return(result);
            }));

            handlers["Object[]"] = new GenericReadHandler <Object[]>(new Func <Reader, Object, int, Object[]>((r, t, c) =>
            {
                int size        = Fns.intCast(r.readInt());
                Object[] result = new Object[size];
                for (int n = 0; n < size; n++)
                {
                    result[n] = r.readObject();
                }
                return(result);
            }));

            handlers["uuid"] = new GenericReadHandler <object>(new Func <Reader, Object, int, object>((r, t, c) =>
            {
                byte[] buf = (byte[])r.readObject();
                if (buf.Length != 16)
                {
                    throw new System.IO.IOException("Invalid uuid buffer size: " + buf.Length);
                }
                //FF ?? Guid is a struct, but now is boxed to an object
                return(Fns.byteArrayToUUID(buf));
            }));

            handlers["regex"] = new GenericReadHandler <Regex>(new Func <Reader, Object, int, Regex>((r, t, c) =>
            {
                return(new System.Text.RegularExpressions.Regex((String)r.readObject()));
            }));

            handlers["uri"] = new GenericReadHandler <Uri>(new Func <Reader, Object, int, Uri>((r, t, c) =>
            {
                try
                {
                    return(new Uri((String)r.readObject()));
                }
                catch (UriFormatException e)
                {
                    throw new ApplicationException("Invalid Uri string!", e);
                }
            }));

            handlers["bigint"] = new GenericReadHandler <BigInteger>(new Func <Reader, Object, int, BigInteger>((r, t, c) =>
            {
                return(new System.Numerics.BigInteger((byte[])r.readObject()));
            }));

            handlers["bigdec"] = new GenericReadHandler <decimal>(new Func <Reader, Object, int, decimal>((r, t, c) =>
            {
                var d = (byte[])r.readObject();
                var s = (int)r.readInt();
                return(Fns.DecimalValueFrom(d, s));
            }));

            handlers["inst"] = new GenericReadHandler <DateTime>(new Func <Reader, Object, int, DateTime>((r, t, c) =>
            {
                return(toDateTime(r.readInt()));
            }));

            return(ImmutableDictionary.CreateRange(handlers));
        }