Example #1
0
        /// <summary>
        /// Determines whether the specified Object is equal to the current Object.
        /// </summary>
        /// <param name="obj">The Object to compare with the current Object.</param>
        /// <returns><value>true</value> if the specified Object is equal to the current Object;
        /// otherwise, <value>false</value>.
        /// </returns>
        public override bool Equals(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.

            IPersistentSet s = obj as IPersistentSet;

            if (s == null)
            {
                return(false);
            }

            if (s.count() != count() || s.GetHashCode() != GetHashCode())
            {
                return(false);
            }

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

            return(true);
        }
Example #2
0
        public void CreateOnEmptyListReturnsEmptySet()
        {
            ArrayList      a = new ArrayList();
            IPersistentSet m = PersistentHashSet.createWithCheck(a);

            Expect(m.count(), EqualTo(0));
        }
Example #3
0
 public CaseExpr( IPersistentMap sourceSpan, LocalBindingExpr expr, int shift, int mask, int low, int high, Expr defaultExpr,
     SortedDictionary<int, Expr> tests, Dictionary<int, Expr> thens, Keyword switchType, Keyword testType, IPersistentSet skipCheck)
 {
     _sourceSpan = sourceSpan;
     _expr = expr;
     _shift = shift;
     _mask = mask;
     //_low = low;
     //_high = high;
     _defaultExpr = defaultExpr;
     _tests = tests;
     _thens = thens;
     if (switchType != _compactKey && switchType != _sparseKey)
         throw new ArgumentException("Unexpected switch type: " + switchType);
     //_switchType = switchType;
     if (testType != _intKey && testType != _hashEquivKey && testType != _hashIdentityKey)
         throw new ArgumentException("Unexpected test type: " + testType);
     _testType = testType;
     _skipCheck = skipCheck;
     ICollection<Expr> returns = new List<Expr>(thens.Values);
     returns.Add(defaultExpr);
     _returnType = Compiler.MaybeClrType(returns);
     if (RT.count(skipCheck) > 0 && RT.booleanCast(RT.WarnOnReflectionVar.deref()))
     {
         RT.errPrintWriter().WriteLine("Performance warning, {0}:{1} - hash collision of some case test constants; if selected, those entries will be tested sequentially.",
             Compiler.SourcePathVar.deref(),RT.get(sourceSpan,RT.StartLineKey));
     }
 }
Example #4
0
 public virtual bool Add(object o)
 {
     if (link != null)
     {
         IPersistent obj = (IPersistent) o;
         if (link.IndexOf(obj) >= 0)
         {
             return false;
         }
         if (link.Size == BTREE_THRESHOLD)
         {
             Set = Storage.CreateSet();
             for (int i = 0, n = link.Size; i < n; i++)
             {
                 Set.Add(link.GetRaw(i));
             }
             link = null;
             Modify();
             Set.Add(obj);
         }
         else
         {
             Modify();
             link.Add(obj);
         }
         return true;
     }
     else
     {
         return Set.Add(o);
     }
 }
Example #5
0
        public static Expr Parse(ParserContext pcon, IPersistentSet form)
        {
            ParserContext pconToUse = pcon.EvEx();
            bool constant = true;

            IPersistentVector keys = PersistentVector.EMPTY;
            for (ISeq s = RT.seq(form); s != null; s = s.next())
            {
                object e = s.first();
                Expr expr = Compiler.Analyze(pconToUse, e);
                keys = (IPersistentVector)keys.cons(expr);
                if (!(expr is LiteralExpr))
                    constant = false;
            }
            Expr ret = new SetExpr(keys);
            if (form is IObj && ((IObj)form).meta() != null)
                return Compiler.OptionallyGenerateMetaInit(pcon, form, ret);
            else if (constant)
            {
                IPersistentSet set = PersistentHashSet.EMPTY;
                for (int i = 0; i < keys.count(); i++)
                {
                    LiteralExpr ve = (LiteralExpr)keys.nth(i);
                    set = (IPersistentSet)set.cons(ve.Val);
                }
                return new ConstantExpr(set);
            }
            else
                return ret;
        }
Example #6
0
        public void CreateOnEmptyISeqReturnsEmptySet()
        {
            object[]       items = new object[] { };
            ArrayList      a     = new ArrayList(items);
            ISeq           s     = PersistentList.create(a).seq();
            IPersistentSet m     = PersistentHashSet.create(s);

            Expect(m.count(), EqualTo(0));
        }
Example #7
0
 public static Expr Parse(IPersistentSet form)
 {
     IPersistentVector keys = PersistentVector.EMPTY;
     for (ISeq s = RT.seq(form); s != null; s = s.next())
     {
         object e = s.first();
         keys = (IPersistentVector)keys.cons(Compiler.GenerateAST(e));
     }
     Expr ret = new SetExpr(keys);
     return Compiler.OptionallyGenerateMetaInit(form, ret);
 }
        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);
        }
Example #9
0
 internal ScalableSet(StorageImpl storage, int initialSize)
     : base(storage)
 {
     if (initialSize <= BTREE_THRESHOLD)
     {
         link = storage.CreateLink(initialSize);
     }
     else
     {
         Set = storage.CreateSet();
     }
 }
Example #10
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);
        }
Example #11
0
        public static Expr Parse(IPersistentSet form)
        {
            IPersistentVector keys = PersistentVector.EMPTY;

            for (ISeq s = RT.seq(form); s != null; s = s.next())
            {
                object e = s.first();
                keys = (IPersistentVector)keys.cons(Compiler.GenerateAST(e));
            }
            Expr ret = new SetExpr(keys);

            return(Compiler.OptionallyGenerateMetaInit(form, ret));
        }
Example #12
0
        private static List <CustomAttributeBuilder> CreateCustomAttributeBuilders(IMapEntry me)
        {
            Type           t     = (Type)me.key();
            IPersistentSet inits = (IPersistentSet)me.val();

            List <CustomAttributeBuilder> builders = new List <CustomAttributeBuilder>(inits.count());

            for (ISeq s = RT.seq(inits); s != null; s = s.next())
            {
                IPersistentMap init = (IPersistentMap)s.first();
                builders.Add(CreateCustomAttributeBuilder(t, init));
            }

            return(builders);
        }
        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);
        }
Example #14
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);
        }
Example #16
0
        public static Expr Parse(ParserContext pcon, IPersistentSet form)
        {
            ParserContext pconToUse = pcon.EvalOrExpr();
            bool          constant  = true;

            IPersistentVector keys = PersistentVector.EMPTY;

            for (ISeq s = RT.seq(form); s != null; s = s.next())
            {
                object e    = s.first();
                Expr   expr = Compiler.Analyze(pconToUse, e);
                keys = (IPersistentVector)keys.cons(expr);
                if (!(expr is LiteralExpr))
                {
                    constant = false;
                }
            }
            Expr ret      = new SetExpr(keys);
            IObj iobjForm = form as IObj;

            if (iobjForm != null && iobjForm.meta() != null)
            {
                return(Compiler.OptionallyGenerateMetaInit(pcon, form, ret));
            }
            else if (constant)
            {
                IPersistentSet set = PersistentHashSet.EMPTY;
                for (int i = 0; i < keys.count(); i++)
                {
                    LiteralExpr ve = (LiteralExpr)keys.nth(i);
                    set = (IPersistentSet)set.cons(ve.Val);
                }
                return(new ConstantExpr(set));
            }
            else
            {
                return(ret);
            }
        }
Example #17
0
        public static Expr Parse(ParserContext pcon, IPersistentSet form)
        {
            ParserContext pconToUse = pcon.EvalOrExpr();
            bool          constant  = true;

            IPersistentVector keys = PersistentVector.EMPTY;

            for (ISeq s = RT.seq(form); s != null; s = s.next())
            {
                object e    = s.first();
                Expr   expr = Compiler.Analyze(pconToUse, e);
                keys = (IPersistentVector)keys.cons(expr);
                if (!(expr is LiteralExpr))
                {
                    constant = false;
                }
            }
            Expr ret = new SetExpr(keys);

            if (form is IObj iobjForm && iobjForm.meta() != null)
            {
                return(Compiler.OptionallyGenerateMetaInit(pcon, form, ret));
            }
Example #18
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);
        }
Example #19
0
        private static Expression GenerateSetExpr(IPersistentSet set)
        {
            Expression[] args = new Expression[set.count()];
            int i = 0;
            for (ISeq s = RT.seq(set); s != null; s = s.next(), i++)
                args[i] = MaybeBox(Generate(s.first()));

            Expression argArray = Expression.NewArrayInit(typeof(object), args);

            Expression ret = Expression.Call(Method_CGen_MakeSet, argArray);
            ret = OptionallyGenerateMetaInit(set, ret);

            return ret;
        }
Example #20
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);
            }
        }
Example #21
0
 internal Expression GenLetFnInits(GenContext context, ParameterExpression parm, FnExpr fn, IPersistentSet leFnLocals)
 {
     // fn is the enclosing IFn, not this.
     throw new NotImplementedException();
 }
Example #22
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);
        }
Example #23
0
 internal Expression GenLetFnInits(GenContext context, ParameterExpression parm, FnExpr fn, IPersistentSet leFnLocals)
 {
     // TODO: Implement this!!!!!!!!!!!!!!!!
     // fn is the enclosing IFn, not this.
     throw new NotImplementedException();
 }
Example #24
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);
            }
        }