Beispiel #1
0
 public SetDatum VisitDictionary(DictionaryDatum d)
 {
     return(SetDatum.UnionAll
            (
                d.Values.Select(i => SetDatum.Union(i.Key.Visit(this), i.Value.Visit(this)))
            ));
 }
Beispiel #2
0
        private int CompareDictionary(DictionaryDatum x, DictionaryDatum y)
        {
            int pos = 0;

            while (true)
            {
                if (x.Count == pos && y.Count == pos)
                {
                    return(0);
                }
                if (x.Count == pos)
                {
                    return(-1);
                }
                if (y.Count == pos)
                {
                    return(1);
                }
                KeyValuePair <Datum, Datum> xkvp = x[pos];
                KeyValuePair <Datum, Datum> ykvp = y[pos];
                int itemResult = this.Compare(xkvp.Key, ykvp.Key);
                if (itemResult != 0)
                {
                    return(itemResult);
                }
                itemResult = this.Compare(xkvp.Value, ykvp.Value);
                if (itemResult != 0)
                {
                    return(itemResult);
                }
                ++pos;
            }
        }
Beispiel #3
0
 public bool VisitDictionary(DictionaryDatum d)
 {
     bw.Write(B_DICTIONARY);
     bw.Write(d.Count);
     foreach (KeyValuePair <Datum, Datum> kvp in d.Values)
     {
         kvp.Key.Visit(this);
         kvp.Value.Visit(this);
     }
     return(true);
 }
        public State VisitDictionary(State state, DictionaryDatum d)
        {
            State s = state;

            foreach (KeyValuePair <Datum, Datum> pair in d)
            {
                s = pair.Key.Visit(this, s);
                s = pair.Value.Visit(this, s);
            }
            return(s);
        }
Beispiel #5
0
        public static void WriteDatum(this BinaryWriter w, Datum d)
        {
            SymbolCollector sc      = new SymbolCollector();
            SetDatum        symbols = d.Visit(sc);

            MutableBoxCollector mc           = new MutableBoxCollector();
            SetDatum            mutableBoxes = d.Visit(mc);

            DictionaryDatum symbolMap         = DictionaryDatum.Empty;
            ListDatum       uninternedSymbols = ListDatum.Empty;
            ListDatum       internedSymbols   = ListDatum.Empty;
            DictionaryDatum mutableBoxMap     = DictionaryDatum.Empty;

            foreach (SymbolDatum s in symbols.Cast <SymbolDatum>())
            {
                if (s.IsInterned)
                {
                    internedSymbols = internedSymbols.Add(s);
                }
                else
                {
                    uninternedSymbols = uninternedSymbols.Add(s);
                }
            }

            w.Write(uninternedSymbols.Count);
            foreach (int i in Enumerable.Range(0, uninternedSymbols.Count))
            {
                symbolMap = symbolMap.Add(uninternedSymbols[i], new IntDatum((BigInteger)(~i)));
            }

            w.Write(internedSymbols.Count);
            foreach (int i in Enumerable.Range(0, internedSymbols.Count))
            {
                symbolMap = symbolMap.Add(internedSymbols[i], new IntDatum((BigInteger)i));
                w.Write(((SymbolDatum)(internedSymbols[i])).Name);
            }

            w.Write(mutableBoxes.Count);
            foreach (int i in Enumerable.Range(0, mutableBoxes.Count))
            {
                mutableBoxMap = mutableBoxMap.Add(mutableBoxes[i], new IntDatum((BigInteger)i));
            }

            BinaryWriteVisitor v = new BinaryWriteVisitor(w, symbolMap, mutableBoxMap);

            foreach (int i in Enumerable.Range(0, mutableBoxes.Count))
            {
                ((MutableBoxDatum)mutableBoxes[i]).Content.Visit(v);
            }

            d.Visit(v);
        }
        public SetDatum VisitDictionary(DictionaryDatum d)
        {
            SetDatum result = SetDatum.Empty;

            foreach (KeyValuePair <Datum, Datum> kvp in d)
            {
                result = SetDatum.Union(kvp.Key.Visit(this), result);
                result = SetDatum.Union(kvp.Value.Visit(this), result);
            }

            return(result);
        }
 public SetDatum VisitMutableBox(MutableBoxDatum d)
 {
     if (visited.ContainsKey(d))
     {
         return((SetDatum)(visited[d]));
     }
     else if (visiting.Contains(d))
     {
         return(SetDatum.Empty);
     }
     else
     {
         visiting = SetDatum.Union(visiting, SetDatum.Singleton(d));
         SetDatum result = SetDatum.Union(d.Content.Visit(this), SetDatum.Singleton(d));
         visiting = SetDatum.Difference(visiting, SetDatum.Singleton(d));
         visited  = visited.Add(d, result);
         return(result);
     }
 }
 private bool EqualDictionary(DictionaryDatum x, DictionaryDatum y)
 {
     if (x.Count != y.Count)
     {
         return(false);
     }
     foreach (int i in Enumerable.Range(0, x.Count))
     {
         var kx = x[i];
         var ky = y[i];
         if (!Equals(kx.Key, ky.Key))
         {
             return(false);
         }
         if (!Equals(kx.Value, ky.Value))
         {
             return(false);
         }
     }
     return(true);
 }
 public static IEnumerable <JoinResult <Datum, bool, Datum> > RightJoin(SetDatum left, DictionaryDatum right)
 {
     return(right.Keys.Select(k => new JoinResult <Datum, bool, Datum>(k, left.Contains(k), right[k])));
 }
 public static IEnumerable <JoinResult <Datum, Option <Datum>, Nothing> > RightJoin(DictionaryDatum left, SetDatum right)
 {
     return(right.Select(k => new JoinResult <Datum, Option <Datum>, Nothing>(k, left.TryGet(k), Nothing.Value)));
 }
 public static IEnumerable <JoinResult <Datum, Option <Datum>, Datum> > RightJoin(DictionaryDatum left, DictionaryDatum right)
 {
     return(right.Keys.Select(k => new JoinResult <Datum, Option <Datum>, Datum>(k, left.TryGet(k), right[k])));
 }
 public static IEnumerable <JoinResult <Datum, Nothing, Option <Datum> > > LeftJoin(SetDatum left, DictionaryDatum right)
 {
     return(left.Select(k => new JoinResult <Datum, Nothing, Option <Datum> >(k, Nothing.Value, right.TryGet(k))));
 }
 public static IEnumerable <JoinResult <Datum, Datum, bool> > LeftJoin(DictionaryDatum left, SetDatum right)
 {
     return(left.Keys.Select(k => new JoinResult <Datum, Datum, bool>(k, left[k], right.Contains(k))));
 }
Beispiel #14
0
 public BinaryWriteVisitor(BinaryWriter bw, DictionaryDatum symbolMap, DictionaryDatum mutableBoxMap)
 {
     this.bw            = bw;
     this.symbolMap     = symbolMap;
     this.mutableBoxMap = mutableBoxMap;
 }
 public MutableBoxCollector()
 {
     this.visiting = SetDatum.Empty;
     this.visited  = DictionaryDatum.Empty;
 }
 public State(SetDatum boxesSeen, DictionaryDatum boxesReferenced, int nextBoxNumber)
 {
     BoxesSeen       = boxesSeen;
     BoxesReferenced = boxesReferenced;
     NextBoxNumber   = nextBoxNumber;
 }
 public string VisitDictionary(DictionaryDatum d)
 {
     return("{" + string.Join(", ", d.Select(kvp => kvp.Key.Visit(this) + " => " + kvp.Value.Visit(this))) + "}");
 }
        public static IEnumerable <JoinResult <Datum, bool, Option <Datum> > > FullJoin(SetDatum left, DictionaryDatum right)
        {
            SetDatum keys = SetDatum.Union(left, right.Keys);

            return(keys.Select(k => new JoinResult <Datum, bool, Option <Datum> >(k, left.Contains(k), right.TryGet(k))));
        }
Beispiel #19
0
        public static Datum ReadDatum(this BinaryReader r)
        {
            int       uninternedSymbolCount = r.ReadInt32();
            ListDatum uninternedSymbols     = ListDatum.Empty;

            for (int i = 0; i < uninternedSymbolCount; ++i)
            {
                uninternedSymbols = uninternedSymbols.Add(new SymbolDatum());
            }
            int       internedSymbolCount = r.ReadInt32();
            ListDatum internedSymbols     = ListDatum.Empty;

            for (int i = 0; i < internedSymbolCount; ++i)
            {
                string name = r.ReadString();
                internedSymbols = internedSymbols.Add(new SymbolDatum(name));
            }
            int       mutableBoxCount = r.ReadInt32();
            ListDatum mutableBoxes    = ListDatum.Empty;

            for (int i = 0; i < mutableBoxCount; ++i)
            {
                mutableBoxes = mutableBoxes.Add(new MutableBoxDatum(NullDatum.Value));
            }

            Func <Datum> read = null;

            read = delegate()
            {
                byte typeId = r.ReadByte();
                switch (typeId)
                {
                case B_NULL:
                    return(NullDatum.Value);

                case B_BOOLEAN_FALSE:
                    return(BooleanDatum.False);

                case B_BOOLEAN_TRUE:
                    return(BooleanDatum.True);

                case B_CHAR:
                {
                    char ch = r.ReadChar();
                    return(new CharDatum(ch));
                }

                case B_STRING:
                {
                    string str = r.ReadString();
                    return(new StringDatum(str));
                }

                case B_INT:
                {
                    BigInteger b = r.ReadBigInteger();
                    return(new IntDatum(b));
                }

                case B_FLOAT:
                {
                    double f = r.ReadDouble();
                    return(new FloatDatum(f));
                }

                case B_BYTE_ARRAY:
                {
                    int    nBytes    = r.ReadInt32();
                    byte[] buf       = new byte[nBytes];
                    int    bytesRead = r.Read(buf, 0, nBytes);
                    if (bytesRead != nBytes)
                    {
                        throw new EndOfStreamException();
                    }
                    return(ByteArrayDatum.FromByteArray(buf));
                }

                case B_SYMBOL:
                {
                    int index = r.ReadInt32();
                    if (index < 0)
                    {
                        return(uninternedSymbols[~index]);
                    }
                    else
                    {
                        return(internedSymbols[index]);
                    }
                }

                case B_LIST:
                {
                    int       count = r.ReadInt32();
                    ListDatum l     = ListDatum.Empty;
                    foreach (int i in Enumerable.Range(0, count))
                    {
                        l = l.Add(read());
                    }
                    return(l);
                }

                case B_SET:
                {
                    int      count = r.ReadInt32();
                    SetDatum s     = SetDatum.Empty;
                    foreach (int i in Enumerable.Range(0, count))
                    {
                        s = s.Add(read());
                    }
                    return(s);
                }

                case B_DICTIONARY:
                {
                    int             count = r.ReadInt32();
                    DictionaryDatum d     = DictionaryDatum.Empty;
                    foreach (int i in Enumerable.Range(0, count))
                    {
                        Datum k = read();
                        Datum v = read();
                        d = d.Add(k, v);
                    }
                    return(d);
                }

                case B_MUTABLE_BOX:
                {
                    int index = r.ReadInt32();
                    return(mutableBoxes[index]);
                }

                case B_RATIONAL:
                {
                    BigInteger numerator   = r.ReadBigInteger();
                    BigInteger denominator = r.ReadBigInteger();
                    return(new RationalDatum(new BigRational(numerator, denominator)));
                }

                case B_GUID:
                {
                    byte[] buf       = new byte[16];
                    int    bytesRead = r.Read(buf, 0, 16);
                    if (bytesRead < 16)
                    {
                        throw new EndOfStreamException();
                    }
                    return(new GuidDatum(new Guid(buf)));
                }

                default:
                    throw new FormatException("Unknown type id");
                }
            };

            for (int j = 0; j < mutableBoxCount; ++j)
            {
                ((MutableBoxDatum)mutableBoxes[j]).Content = read();
            }

            return(read());
        }
        public static IEnumerable <JoinResult <Datum, Datum, Datum> > InnerJoin(DictionaryDatum left, DictionaryDatum right)
        {
            SetDatum keys = SetDatum.Intersection(left.Keys, right.Keys);

            return(keys.Select(k => new JoinResult <Datum, Datum, Datum>(k, left[k], right[k])));
        }
Beispiel #21
0
 public string VisitDictionary(DictionaryDatum d)
 {
     return("d(" + string.Join(",", d.Values.Select(kvp => kvp.Key.Visit(this) + ":" + kvp.Value.Visit(this))) + ")");
 }