public static SetDatum SymmetricDifference(SetDatum a, SetDatum b) { var idb = a.ToBuilder(); idb.SymmetricExceptWith(b.set); return(new SetDatum(idb.ToImmutable())); }
public static SetDatum Union(SetDatum a, SetDatum b) { var idb = a.ToBuilder(); idb.UnionWith(b.set); return(new SetDatum(idb.ToImmutable())); }
private int CompareSet(SetDatum x, SetDatum 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); } int itemResult = this.Compare(x[pos], y[pos]); if (itemResult != 0) { return(itemResult); } ++pos; } }
public State VisitSet(State state, SetDatum d) { State s = state; foreach (Datum listItem in d) { s = listItem.Visit(this, s); } return(s); }
public bool VisitSet(SetDatum d) { bw.Write(B_SET); bw.Write(d.Count); foreach (Datum i in d.Values) { i.Visit(this); } return(true); }
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); }
private bool EqualSet(SetDatum x, SetDatum y) { if (x.Count != y.Count) { return(false); } foreach (int i in Enumerable.Range(0, x.Count)) { if (!Equals(x[i], y[i])) { return(false); } } return(true); }
public string VisitMutableBox(MutableBoxDatum d) { if (boxReferences.BoxesReferenced.ContainsKey(d)) { if (boxesStarted.Contains(d)) { return("#b[" + boxReferences.BoxesReferenced[d].Visit(this) + "]"); } else { boxesStarted = boxesStarted.Add(d); return("#b[" + boxReferences.BoxesReferenced[d].Visit(this) + "]=" + d.Content.Visit(this)); } } else { return("#b=" + d.Content.Visit(this)); } }
private SetDatum GetKeys() { return(SetDatum.FromEnumerable(dict.Keys)); }
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]))); }
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, 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)))); }
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, 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 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, bool> > LeftJoin(DictionaryDatum left, SetDatum right) { return(left.Keys.Select(k => new JoinResult <Datum, Datum, bool>(k, left[k], right.Contains(k)))); }
public static IEnumerable <JoinResult <Datum, bool, Nothing> > RightJoin(SetDatum left, SetDatum right) { return(right.Select(k => new JoinResult <Datum, bool, Nothing>(k, left.Contains(k), Nothing.Value))); }
public static IEnumerable <JoinResult <Datum, Nothing, bool> > LeftJoin(SetDatum left, SetDatum right) { return(left.Select(k => new JoinResult <Datum, Nothing, bool>(k, Nothing.Value, right.Contains(k)))); }
public static IEnumerable <JoinResult <Datum, bool, bool> > FullJoin(SetDatum left, SetDatum right) { SetDatum keys = SetDatum.Union(left, right); return(keys.Select(k => new JoinResult <Datum, bool, bool>(k, left.Contains(k), right.Contains(k)))); }
public string VisitSet(SetDatum d) { return("w(" + string.Join(",", d.Values.Select(i => i.Visit(this))) + ")"); }
public State(SetDatum boxesSeen, DictionaryDatum boxesReferenced, int nextBoxNumber) { BoxesSeen = boxesSeen; BoxesReferenced = boxesReferenced; NextBoxNumber = nextBoxNumber; }
public string VisitSet(SetDatum d) { return("#s{" + string.Join(" ", d.Select(i => i.Visit(this))) + "}"); }
public DatumToStringVisitor(MutableBoxReferenceCollector.State boxReferences) { this.boxReferences = boxReferences; this.boxesStarted = SetDatum.Empty; }