Beispiel #1
0
        public static ushort EncToIdx(ulong[] encWords, int count)
        {
            uint hashcode = Hashing.Hashcode64(encWords[0]);

            for (int i = 1; i < count; i++)
            {
                hashcode = 31 * hashcode + Hashing.Hashcode64(encWords[i]);
            }
            uint idx = hashcode % SIZE;

            if (encSymbs == null)
            {
                encSymbs     = new ulong[SIZE][];
                encSymbsIdxs = new ushort[SIZE];
            }

            ulong[] storedEncs = encSymbs[idx];
            if (storedEncs != null && storedEncs.Length == count && Array.IsPrefix(storedEncs, encWords))
            {
                return(encSymbsIdxs[idx]);
            }

            byte[] bytes   = Decode(encWords, count);
            ushort symbIdx = SymbObj.BytesToIdx(bytes);

            if (storedEncs == null)
            {
                encSymbs[idx]     = Array.Take(encWords, count);
                encSymbsIdxs[idx] = symbIdx;
            }

            return(symbIdx);
        }
Beispiel #2
0
        public static ushort EncToIdx(ulong encWord1, ulong encWord2)
        {
            uint hashcode1 = Hashing.Hashcode64(encWord1);
            uint hashcode2 = Hashing.Hashcode64(encWord2);
            uint idx       = (31 * hashcode1 + hashcode2) % SIZE;

            ulong storedEnc1 = encSymbs2[2 * idx];
            ulong storedEnc2 = encSymbs2[2 * idx + 1];

            if (storedEnc1 == encWord1 & storedEnc2 == encWord2)
            {
                return(encSymbsIdxs2[idx]);
            }

            byte[] bytes   = Decode(encWord1, encWord2);
            ushort symbIdx = SymbObj.BytesToIdx(bytes);

            if (storedEnc1 == 0)
            {
                encSymbs2[2 * idx]     = encWord1;
                encSymbs2[2 * idx + 1] = encWord2;
                encSymbsIdxs2[idx]     = symbIdx;
            }

            return(symbIdx);
        }
Beispiel #3
0
        public static ushort EncToIdx(ulong encWord1, ulong encWord2, ulong encWord3)
        {
            uint hashcode1 = Hashing.Hashcode64(encWord1);
            uint hashcode2 = Hashing.Hashcode64(encWord2);
            uint hashcode3 = Hashing.Hashcode64(encWord3);
            uint idx       = (31 * 31 * hashcode1 + 31 * hashcode2 + hashcode3) % SIZE;

            if (encSymbs3 == null)
            {
                encSymbs3     = new ulong[3 * SIZE];
                encSymbsIdxs3 = new  ushort[SIZE];
            }

            ulong storedEnc1 = encSymbs3[3 * idx];
            ulong storedEnc2 = encSymbs3[3 * idx + 1];
            ulong storedEnc3 = encSymbs3[3 * idx + 2];

            if (storedEnc1 == encWord1 & storedEnc2 == encWord2 & storedEnc3 == encWord3)
            {
                return(encSymbsIdxs3[idx]);
            }

            byte[] bytes   = Decode(encWord1, encWord2, encWord3);
            ushort symbIdx = SymbObj.BytesToIdx(bytes);

            if (storedEnc1 == 0)
            {
                encSymbs3[3 * idx]     = encWord1;
                encSymbs3[3 * idx + 1] = encWord2;
                encSymbs3[3 * idx + 2] = encWord3;
                encSymbsIdxs3[idx]     = symbIdx;
            }

            return(symbIdx);
        }
Beispiel #4
0
        static long ParseSymbOrTaggedObj(Token[] tokens, long offset, out Obj var)
        {
            int     length  = tokens.Length;
            SymbObj symbObj = (SymbObj)tokens[offset].value;
            int     symbIdx = symbObj.GetSymbId();

            if (++offset < length)
            {
                if (tokens[offset].type == TokenType.OpenPar)
                {
                    Obj innerObj;
                    if (IsRecord(tokens, offset))
                    {
                        offset = ParseRec(tokens, offset, out innerObj);
                    }
                    else
                    {
                        offset = ParseInnerObjOrTuple(tokens, offset, out innerObj);
                    }
                    if (offset >= 0)
                    {
                        var = new TaggedObj(symbIdx, innerObj);
                    }
                    else
                    {
                        var = null;
                    }
                    return(offset);
                }
            }
            var = symbObj;
            return(offset);
        }
Beispiel #5
0
        public static Obj ParseSymb(Obj obj)
        {
            string str = obj.GetString();
            int    id  = SymbTable.StrToIdx(str);

            return(SymbObj.Get(id));
        }
Beispiel #6
0
        //////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////////

        public static void Write(DataWriter writer, int fieldSymbId, BinaryTable[] tables, bool flipCols, int indentation, bool indentFirstLine, bool writeSeparator)
        {
            string baseWs  = new string(Array.Repeat(' ', indentation));
            string entryWs = new string(Array.Repeat(' ', indentation + 2));

            int count = 0;

            for (int i = 0; i < tables.Length; i++)
            {
                count += tables[i].Size();
            }

            if (indentFirstLine)
            {
                writer.Write(baseWs);
            }
            writer.Write(SymbObj.IdxToStr(fieldSymbId));
            writer.Write(": [");

            if (count > 0)
            {
                writer.Write("\n");

                int written = 0;
                for (int i = 0; i < tables.Length; i++)
                {
                    BinaryTable      table   = tables[i];
                    SurrObjMapper    mapper1 = table.mapper1;
                    SurrObjMapper    mapper2 = table.mapper2;
                    BinaryTable.Iter it      = table.GetIter();
                    while (!it.Done())
                    {
                        writer.Write(entryWs);
                        Obj obj1 = mapper1(it.Get1());
                        Obj obj2 = mapper2(it.Get2());
                        if (flipCols)
                        {
                            Obj tmp = obj1;
                            obj1 = obj2;
                            obj2 = tmp;
                        }
                        ObjPrinter.PrintNoFlush(obj1, writer);
                        writer.Write(", ");
                        ObjPrinter.PrintNoFlush(obj2, writer);
                        written++;
                        writer.Write(written < count || count == 1 ? ";\n" : "\n");
                        it.Next();
                    }
                }
                Debug.Assert(written == count);

                writer.Write(baseWs);
            }

            writer.Write(writeSeparator ? "],\n" : "]\n");
        }
Beispiel #7
0
 static public Obj Fetch(Obj target)
 {
     if (attachments.ContainsKey(target))
     {
         return(Builder.CreateTaggedObj(SymbObj.JustSymbId, attachments[target]));
     }
     else
     {
         return(SymbObj.Get(SymbObj.NothingSymbId));
     }
 }
Beispiel #8
0
        ////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////

        public static Obj At(bool[] array, int size, long idx)
        {
            if (idx < size)
            {
                return(SymbObj.Get(array[(int)idx]));
            }
            else
            {
                throw ErrorHandler.SoftFail();
            }
        }
Beispiel #9
0
        public static Obj FileWrite_P(Obj fname, Obj data, object env)
        {
            string fnameStr = fname.GetString();

            byte[] bytes = data.GetByteArray();
            try {
                File.WriteAllBytes(fnameStr, bytes);
                return(SymbObj.Get(SymbTable.TrueSymbId));
            }
            catch {
                return(SymbObj.Get(SymbTable.FalseSymbId));
            }
        }
Beispiel #10
0
        public static Obj GetChar_P(object env)
        {
            int ch = Console.Read();

            if (ch != -1)
            {
                return(new TaggedObj(SymbTable.JustSymbId, IntObj.Get(ch)));
            }
            else
            {
                return(SymbObj.Get(SymbTable.NothingSymbId));
            }
        }
Beispiel #11
0
        public static Obj FileRead_P(Obj fname, object env)
        {
            string fnameStr = fname.GetString();

            try {
                byte[] content  = File.ReadAllBytes(fnameStr);
                Obj    bytesObj = Builder.BuildConstIntSeq(content);
                return(new TaggedObj(SymbTable.JustSymbId, bytesObj));
            }
            catch {
                return(SymbObj.Get(SymbTable.NothingSymbId));
            }
        }
Beispiel #12
0
        static public Obj Fetch(Obj target)
        {
            Obj attachment;

            if (attachments.TryGetValue(target, out attachment))
            {
                return(new TaggedObj(SymbTable.JustSymbId, attachment));
            }
            else
            {
                return(SymbObj.Get(SymbTable.NothingSymbId));
            }
        }
Beispiel #13
0
        public static void Write(DataWriter writer, int fieldSymbId, UnaryTable[] tables, int indentation, bool indentFirstLine, bool writeSeparator)
        {
            string baseWs  = new string(Array.Repeat(' ', indentation));
            string entryWs = new string(Array.Repeat(' ', indentation + 2));

            int count = 0;

            for (int i = 0; i < tables.Length; i++)
            {
                count += tables[i].Size();
            }

            if (indentFirstLine)
            {
                writer.Write(baseWs);
            }
            writer.Write(SymbObj.IdxToStr(fieldSymbId));
            writer.Write(": [");

            if (count > 0)
            {
                writer.Write("\n");

                int written = 0;
                for (int i = 0; i < tables.Length; i++)
                {
                    UnaryTable      table  = tables[i];
                    SurrObjMapper   mapper = table.mapper;
                    UnaryTable.Iter it     = table.GetIter();
                    while (!it.Done())
                    {
                        writer.Write(entryWs);
                        Obj obj = mapper(it.Get());
                        ObjPrinter.PrintNoFlush(obj, writer);
                        written++;
                        writer.Write(written < count ? ",\n" : "\n");
                        it.Next();
                    }
                }
                Debug.Assert(written == count);

                writer.Write(baseWs);
            }

            writer.Write(writeSeparator ? "],\n" : "]\n");
        }
Beispiel #14
0
        //////////////////////////////////////////////////////////////////////////////

        public override uint Hashcode()
        {
            if (hcode == Hashing.NULL_HASHCODE)
            {
                ushort[] fieldIds = GetFieldIds();
                ulong    code     = 0;
                for (int i = 0; i < fieldIds.Length; i++)
                {
                    code += Hashing.Hashcode(SymbObj.Get(fieldIds[i]).Hashcode(), LookupField(fieldIds[i]).Hashcode());
                }
                hcode = Hashing.Hashcode(SymbObj.Get(GetTagId()).Hashcode(), Hashing.Hashcode64(code));
                if (hcode == Hashing.NULL_HASHCODE)
                {
                    hcode++;
                }
            }
            return(hcode);
        }
Beispiel #15
0
        static long ReadSymbol(byte[] text, uint length, long offset, Token token)
        {
            long i = offset;

            while (++i < length)
            {
                byte ch = text[i];
                if (ch == '_')
                {
                    if (++i == length)
                    {
                        return(-i - 1);
                    }
                    ch = text[i];
                    if (!Char.IsLower((char)ch) & !Char.IsDigit((char)ch))
                    {
                        return(-i - 1);
                    }
                }
                else if (!Char.IsLower((char)ch) & !Char.IsDigit((char)ch))
                {
                    break;
                }
            }

            if (token != null)
            {
                long   len   = i - offset;
                char[] chars = new char[len];
                for (int j = 0; j < len; j++)
                {
                    chars[j] = (char)text[offset + j];
                }

                token.offset = offset;
                token.length = len;
                token.type   = TokenType.Symbol;
                token.value  = SymbObj.Get(SymbTable.StrToIdx(new string(chars)));
            }

            return(i);
        }
Beispiel #16
0
        public static ushort EncToIdx(ulong encWord)
        {
            uint hashcode = Hashing.Hashcode64(encWord);
            uint idx      = hashcode % SIZE;

            ulong storedEnc = encSymbs1[idx];

            if (storedEnc == encWord)
            {
                return(encSymbsIdxs1[idx]);
            }

            byte[] bytes   = Decode(encWord);
            ushort symbIdx = SymbObj.BytesToIdx(bytes);

            if (storedEnc == 0)
            {
                encSymbs1[idx]     = encWord;
                encSymbsIdxs1[idx] = symbIdx;
            }

            return(symbIdx);
        }
Beispiel #17
0
        //////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////////

        static int SymbolOrder(SymbObj obj1, SymbObj obj2)
        {
            string str1 = obj1.stringRepr;
            string str2 = obj2.stringRepr;
            int    len1 = str1.Length;
            int    len2 = str2.Length;

            if (len1 != len2)
            {
                return(len1 < len2 ? -1 : 1);
            }

            for (int i = 0; i < len1; i++)
            {
                char ch1 = str1[i];
                char ch2 = str2[i];
                if (ch1 != ch2)
                {
                    return(ch1 < ch2 ? -1 : 1);
                }
            }

            return(0);
        }
Beispiel #18
0
        static long ReadRecEntry(Token[] tokens, long offset, State state)
        {
            int length = tokens.Length;

            if (offset >= length || tokens[offset].type != TokenType.Symbol)
            {
                return(-offset - 1);
            }
            SymbObj label = (SymbObj)tokens[offset++].value;

            if (offset >= length || tokens[offset].type != TokenType.Colon)
            {
                return(-offset - 1);
            }
            Obj[] entry = new Obj[2];
            offset = ParseObj(tokens, offset + 1, out entry[1]);
            if (offset < 0)
            {
                return(offset);
            }
            entry[0] = label;
            state.Store(entry, 2);
            return(offset);
        }
Beispiel #19
0
        //////////////////////////////////////////////////////////////////////////////

        public override bool HasField(ushort fieldId)
        {
            return(Contains1(SymbObj.Get(fieldId)));
        }
Beispiel #20
0
 override public Obj AsObj()
 {
     return(SymbObj.Get(id));
 }
Beispiel #21
0
 public override NeSeqObj Append(bool value)
 {
     return(Append(SymbObj.Get(value)));
 }
Beispiel #22
0
 public override Obj LookupField(ushort fieldId)
 {
     return(Lookup(SymbObj.Get(fieldId)));
 }
Beispiel #23
0
 protected override Obj CreateTaggedObj(ushort tagId, Obj obj)
 {
     return(ConvertGenericTaggedValue(SymbObj.Get(tagId), obj));
 }
Beispiel #24
0
 public override uint Hashcode()
 {
     return(Hashing.Hashcode(SymbObj.Get(GetTagId()).Hashcode(), obj.Hashcode()));
 }
Beispiel #25
0
        //////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////////

        public static void Write(DataWriter writer, int fieldSymbId, ColumnBase[] columns, bool flipCols, int indentation, bool indentFirstLine, bool writeSeparator)
        {
            string baseWs  = new string(Array.Repeat(' ', indentation));
            string entryWs = new string(Array.Repeat(' ', indentation + 2));

            int count = 0;

            for (int i = 0; i < columns.Length; i++)
            {
                count += columns[i].Size();
            }

            if (indentFirstLine)
            {
                writer.Write(baseWs);
            }
            writer.Write(SymbObj.IdxToStr(fieldSymbId));
            writer.Write(": [");

            if (count > 0)
            {
                writer.Write("\n");

                int written = 0;
                for (int i = 0; i < columns.Length; i++)
                {
                    ColumnBase    col    = columns[i];
                    SurrObjMapper mapper = col.mapper;

                    if (col is IntColumn)
                    {
                        IntColumn      intCol = (IntColumn)col;
                        IntColumn.Iter it     = intCol.GetIter();
                        while (!it.Done())
                        {
                            writer.Write(entryWs);
                            Obj  key   = mapper(it.GetIdx());
                            long value = it.GetValue();
                            if (flipCols)
                            {
                                writer.Write(value);
                                writer.Write(", ");
                                ObjPrinter.PrintNoFlush(key, writer);
                                written++;
                                writer.Write(written == 1 | written < count ? ";\n" : "\n");
                            }
                            else
                            {
                                ObjPrinter.PrintNoFlush(key, writer);
                                writer.Write(" -> ");
                                writer.Write(value);
                                written++;
                                writer.Write(written < count ? ",\n" : "\n");
                            }
                            it.Next();
                        }
                    }
                    else if (col is FloatColumn)
                    {
                        FloatColumn      floatCol = (FloatColumn)col;
                        FloatColumn.Iter it       = floatCol.GetIter();
                        while (!it.Done())
                        {
                            writer.Write(entryWs);
                            Obj    key   = mapper(it.GetIdx());
                            double value = it.GetValue();
                            if (flipCols)
                            {
                                writer.Write(FloatObjPrinter.Print(value));
                                writer.Write(", ");
                                ObjPrinter.PrintNoFlush(key, writer);
                                written++;
                                writer.Write(written == 1 | written < count ? ";\n" : "\n");
                            }
                            else
                            {
                                ObjPrinter.PrintNoFlush(key, writer);
                                writer.Write(" -> ");
                                writer.Write(FloatObjPrinter.Print(value));
                                written++;
                                writer.Write(written < count ? ",\n" : "\n");
                            }
                            it.Next();
                        }
                    }
                    else
                    {
                        ObjColumn      objCol = (ObjColumn)col;
                        ObjColumn.Iter it     = objCol.GetIter();
                        while (!it.Done())
                        {
                            writer.Write(entryWs);
                            Obj key   = mapper(it.GetIdx());
                            Obj value = it.GetValue();
                            if (flipCols)
                            {
                                ObjPrinter.PrintNoFlush(value, writer);
                                writer.Write(", ");
                                ObjPrinter.PrintNoFlush(key, writer);
                                written++;
                                writer.Write(written == 1 | written < count ? ";\n" : "\n");
                            }
                            else
                            {
                                ObjPrinter.PrintNoFlush(key, writer);
                                writer.Write(" -> ");
                                ObjPrinter.PrintNoFlush(value, writer);
                                written++;
                                writer.Write(written < count ? ",\n" : "\n");
                            }
                            it.Next();
                        }
                    }
                }
                Debug.Assert(written == count);

                writer.Write(baseWs);
            }

            writer.Write(writeSeparator ? "],\n" : "]\n");
        }
Beispiel #26
0
 public override uint Hashcode()
 {
     return(Hashing.Hashcode(SymbObj.Get(GetTagId()).Hashcode(), IntObj.Hashcode(GetInnerLong())));
 }