Exemple #1
0
        public void CreateOnListReturnsSet()
        {
            object[]  items = new object[] { 1, "a" };
            ArrayList a     = new ArrayList(items);

            IPersistentSet m = PersistentHashSet.createWithCheck(a);

            Expect(m.count(), EqualTo(2));
            Expect(m.contains(1));
            Expect(m.contains("a"));
            Expect(m.contains(3), False);
        }
Exemple #2
0
        public void CreateOnISeqReturnsSet()
        {
            object[]       items = new object[] { 1, "a" };
            ArrayList      a     = new ArrayList(items);
            ISeq           s     = PersistentList.create(a).seq();
            IPersistentSet m     = PersistentHashSet.create(s);

            Expect(m.count(), EqualTo(2));
            Expect(m.contains(1));
            Expect(m.contains("a"));
            Expect(m.contains(3), False);
        }
        public static bool setEquals(IPersistentSet s1, object obj)
        {
            // I really can't do what the Java version does.
            // It casts to a Set.  No such thing here.  We'll use IPersistentSet instead.


            if (s1 == obj)
            {
                return(true);
            }

#if CLR2
            // No System.Collections.Generic.ISet<T>
#else
            ISet <Object> is2 = obj as ISet <Object>;
            if (is2 != null)
            {
                if (is2.Count != s1.count())
                {
                    return(false);
                }
                foreach (Object elt in is2)
                {
                    if (!s1.contains(elt))
                    {
                        return(false);
                    }
                }
                return(true);
            }
#endif

            IPersistentSet s2 = obj as IPersistentSet;
            if (s2 == null)
            {
                return(false);
            }

            if (s2.count() != s1.count())
            {
                return(false);
            }

            for (ISeq seq = s2.seq(); seq != null; seq = seq.next())
            {
                if (!s1.contains(seq.first()))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #4
0
        internal void EmitLetFnInits(CljILGen ilg, LocalBuilder localBuilder, ObjExpr objx, IPersistentSet letFnLocals)
        {
            if (_typeBuilder != null)
            {
                // Full compile
                ilg.Emit(OpCodes.Castclass, _typeBuilder);

                for (ISeq s = RT.keys(Closes); s != null; s = s.next())
                {
                    LocalBinding lb = (LocalBinding)s.first();
                    if (letFnLocals.contains(lb))
                    {
                        FieldBuilder fb;
                        _closedOverFieldsMap.TryGetValue(lb, out fb);

                        Type primt = lb.PrimitiveType;
                        ilg.Emit(OpCodes.Dup);  // this
                        if (primt != null)
                        {
                            objx.EmitUnboxedLocal(ilg, lb);
                            ilg.Emit(OpCodes.Stfld, fb);
                        }
                        else
                        {
                            objx.EmitLocal(ilg, lb);
                            ilg.Emit(OpCodes.Stfld, fb);
                        }
                    }
                }
                ilg.Emit(OpCodes.Pop);
            }
        }
        public static bool setEquals(IPersistentSet s1, object obj)
        {
            // I really can't do what the Java version does.
            // It casts to a Set.  No such thing here.  We'll use IPersistentSet instead.


            if (s1 == obj)
            {
                return(true);
            }

            if (obj is ISet <Object> is2)
            {
                if (is2.Count != s1.count())
                {
                    return(false);
                }
                foreach (Object elt in is2)
                {
                    if (!s1.contains(elt))
                    {
                        return(false);
                    }
                }
                return(true);
            }

            if (!(obj is IPersistentSet s2))
            {
                return(false);
            }

            if (s2.count() != s1.count())
            {
                return(false);
            }

            for (ISeq seq = s2.seq(); seq != null; seq = seq.next())
            {
                if (!s1.contains(seq.first()))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #6
0
        public static Expr Parse(ParserContext pcon, IPersistentMap form)
        {
            ParserContext pconToUse = pcon.EvalOrExpr();

            bool           keysConstant          = true;
            bool           valsConstant          = true;
            bool           allConstantKeysUnique = true;
            IPersistentSet constantKeys          = PersistentHashSet.EMPTY;

            IPersistentVector keyvals = PersistentVector.EMPTY;

            for (ISeq s = RT.seq(form); s != null; s = s.next())
            {
                IMapEntry e = (IMapEntry)s.first();
                Expr      k = Compiler.Analyze(pconToUse, e.key());
                Expr      v = Compiler.Analyze(pconToUse, e.val());
                keyvals = (IPersistentVector)keyvals.cons(k);
                keyvals = (IPersistentVector)keyvals.cons(v);
                if (k is LiteralExpr)
                {
                    object kval = k.Eval();
                    if (constantKeys.contains(kval))
                    {
                        allConstantKeysUnique = false;
                    }
                    else
                    {
                        constantKeys = (IPersistentSet)constantKeys.cons(kval);
                    }
                }
                else
                {
                    keysConstant = false;
                }
                if (!(v is LiteralExpr))
                {
                    valsConstant = false;
                }
            }

            Expr ret = new MapExpr(keyvals);

            if (form is IObj iobjForm && iobjForm.meta() != null)
            {
                return(Compiler.OptionallyGenerateMetaInit(pcon, form, ret));
            }
Exemple #7
0
        public void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
        {
            bool           allKeysConstant       = true;
            bool           allConstantKeysUnique = true;
            IPersistentSet constantKeys          = PersistentHashSet.EMPTY;

            for (int i = 0; i < _keyvals.count(); i += 2)
            {
                Expr k = (Expr)_keyvals.nth(i);
                if (k is LiteralExpr)
                {
                    object kval = k.Eval();
                    if (constantKeys.contains(kval))
                    {
                        allConstantKeysUnique = false;
                    }
                    else
                    {
                        constantKeys = (IPersistentSet)constantKeys.cons(kval);
                    }
                }
                else
                {
                    allKeysConstant = false;
                }
            }

            MethodExpr.EmitArgsAsArray(_keyvals, objx, ilg);

            if ((allKeysConstant && allConstantKeysUnique) || (_keyvals.count() <= 2))
            {
                ilg.EmitCall(Compiler.Method_RT_mapUniqueKeys);
            }
            else
            {
                ilg.EmitCall(Compiler.Method_RT_map);
            }

            if (rhc == RHC.Statement)
            {
                ilg.Emit(OpCodes.Pop);
            }
        }
        public static bool IsAssemblyExportable(Assembly assembly, IPersistentSet checkedAssemblies)
        {
            if (checkedAssemblies.contains(assembly))
            {
                return(true);
            }

            foreach (var referencedAssembly in assembly.GetReferencedAssemblies())
            {
                if (referencedAssembly.Name == "UnityEditor" || referencedAssembly.Name == "Assembly-CSharp-Editor")
                {
                    return(false);
                }

                if (!IsAssemblyExportable(Assembly.Load(referencedAssembly), (IPersistentSet)checkedAssemblies.cons(assembly)))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #9
0
        /// <summary>
        /// Is one value preferred over another?
        /// </summary>
        /// <param name="x">The first dispatch value.</param>
        /// <param name="y">The second dispatch value.</param>
        /// <returns><value>true</value> if <paramref name="x"/> is preferred over <paramref name="y"/></returns>
        private bool Prefers(object x, object y)
        {
            IPersistentSet xprefs = (IPersistentSet)PreferTable.valAt(x);

            if (xprefs != null && xprefs.contains(y))
            {
                return(true);
            }
            for (ISeq ps = RT.seq(_parents.invoke(y)); ps != null; ps = ps.next())
            {
                if (Prefers(x, ps.first()))
                {
                    return(true);
                }
            }
            for (ISeq ps = RT.seq(_parents.invoke(x)); ps != null; ps = ps.next())
            {
                if (Prefers(ps.first(), y))
                {
                    return(true);
                }
            }
            return(false);
        }
Exemple #10
0
        internal void EmitLetFnInits(CljILGen ilg, LocalBuilder localBuilder, ObjExpr objx, IPersistentSet letFnLocals)
        {
            if (_typeBuilder != null)
            {
                // Full compile
                ilg.Emit(OpCodes.Castclass, _typeBuilder);

                for (ISeq s = RT.keys(Closes); s != null; s = s.next())
                {
                    LocalBinding lb = (LocalBinding)s.first();
                    if (letFnLocals.contains(lb))
                    {
                        FieldBuilder fb;
                        _closedOverFieldsMap.TryGetValue(lb, out fb);

                        Type primt = lb.PrimitiveType;
                        ilg.Emit(OpCodes.Dup);  // this
                        if (primt != null)
                        {
                            objx.EmitUnboxedLocal(ilg, lb);
                            ilg.MaybeEmitVolatileOp(IsVolatile(lb));
                            ilg.Emit(OpCodes.Stfld, fb);
                        }
                        else
                        {
                            objx.EmitLocal(ilg, lb);
                            ilg.MaybeEmitVolatileOp(IsVolatile(lb));
                            ilg.Emit(OpCodes.Stfld, fb);
                        }
                    }
                }
                ilg.Emit(OpCodes.Pop);
            }
        }
Exemple #11
0
        public static Expr Parse(ParserContext pcon, IPersistentMap form)
        {
            ParserContext pconToUse = pcon.EvalOrExpr();

            bool           keysConstant          = true;
            bool           valsConstant          = true;
            bool           allConstantKeysUnique = true;
            IPersistentSet constantKeys          = PersistentHashSet.EMPTY;

            IPersistentVector keyvals = PersistentVector.EMPTY;

            for (ISeq s = RT.seq(form); s != null; s = s.next())
            {
                IMapEntry e = (IMapEntry)s.first();
                Expr      k = Compiler.Analyze(pconToUse, e.key());
                Expr      v = Compiler.Analyze(pconToUse, e.val());
                keyvals = (IPersistentVector)keyvals.cons(k);
                keyvals = (IPersistentVector)keyvals.cons(v);
                if (k is LiteralExpr)
                {
                    object kval = k.Eval();
                    if (constantKeys.contains(kval))
                    {
                        allConstantKeysUnique = false;
                    }
                    else
                    {
                        constantKeys = (IPersistentSet)constantKeys.cons(kval);
                    }
                }
                else
                {
                    keysConstant = false;
                }
                if (!(v is LiteralExpr))
                {
                    valsConstant = false;
                }
            }

            Expr ret = new MapExpr(keyvals);

            IObj iobjForm = form as IObj;

            if (iobjForm != null && iobjForm.meta() != null)
            {
                return(Compiler.OptionallyGenerateMetaInit(pcon, form, ret));
            }
            //else if (constant)
            //{
            // This 'optimzation' works, mostly, unless you have nested map values.
            // The nested map values do not participate in the constants map, so you end up with the code to create the keys.
            // Result: huge duplication of keyword creation.  3X increase in init time to the REPL.
            //    //IPersistentMap m = PersistentHashMap.EMPTY;
            //    //for (int i = 0; i < keyvals.length(); i += 2)
            //    //    m = m.assoc(((LiteralExpr)keyvals.nth(i)).Val, ((LiteralExpr)keyvals.nth(i + 1)).Val);
            //    //return new ConstantExpr(m);
            //    return ret;
            //}
            else if (keysConstant)
            {
                // TBD: Add more detail to exception thrown below.
                if (!allConstantKeysUnique)
                {
                    throw new ArgumentException("Duplicate constant keys in map");
                }
                if (valsConstant)
                {
                    // This 'optimzation' works, mostly, unless you have nested map values.
                    // The nested map values do not participate in the constants map, so you end up with the code to create the keys.
                    // Result: huge duplication of keyword creation.  3X increase in init time to the REPL.
                    //IPersistentMap m = PersistentHashMap.EMPTY;
                    //for (int i = 0; i < keyvals.length(); i += 2)
                    //    m = m.assoc(((LiteralExpr)keyvals.nth(i)).Val, ((LiteralExpr)keyvals.nth(i + 1)).Val);
                    //return new ConstantExpr(m);
                    return(ret);
                }
                else
                {
                    return(ret);
                }
            }
            else
            {
                return(ret);
            }
        }
Exemple #12
0
        internal Expression GenLetFnInits(GenContext context, ParameterExpression parm, ObjExpr objx, IPersistentSet letFnLocals)
        {
            ParameterExpression cvtParm = Expression.Parameter(_compiledType,"cvt");
            Expression initExpr = Expression.Assign(cvtParm,Expression.Convert(parm, _compiledType));

            List<Expression> exprs = new List<Expression>();
            exprs.Add(initExpr);

            for (ISeq s = RT.keys(_closes); s != null; s = s.next())
            {
                LocalBinding lb = (LocalBinding)s.first();
                if (letFnLocals.contains(lb))
                {
                    FieldBuilder fb;
                    _closedOverFieldsMap.TryGetValue(lb,out fb);

                    Type primt = lb.PrimitiveType;
                    Expression init = primt != null ? objx.GenUnboxedLocal(context, lb) : objx.GenLocal(context,lb);

                    exprs.Add(Expression.Assign(Expression.Field(_thisParam,fb), init));
                }
            }

            return Expression.Block(new ParameterExpression[] { cvtParm }, exprs);
        }