Example #1
0
        public StringMapper(Dictionary<String, String> input)
        {
            //copy the dictionary so it can't be modified. Only a shallow copy but Strings are immutable anyway
            this.data = new Dictionary<String, String>(input);

            //Desired result is something like "(?<=\\s|^)((Kappa)|(FailFish))(?=\\s|$)"
            StringBuilder regexBuilder = new StringBuilder();
            //In order to ensure to not match Kappa to Kappa! or KappaRoss we need to ensure that before and after the match
            //is either a whitespace (\s) or a start (^) / end ($) of line.
            //However we do not want to capture the whitespaces since it would complicate our dictionary lookups:
            //http://stackoverflow.com/questions/3926451/how-to-match-but-not-capture-part-of-a-regex
            regexBuilder.Append(@"(?<=\s|^)(");
            foreach (var entry in input.Keys)
            {
                regexBuilder.Append("(");
                regexBuilder.Append(entry);
                regexBuilder.Append(")|");
            }

            if(!input.IsEmpty())
                regexBuilder.Remove(regexBuilder.Length - 1, 1);
            regexBuilder.Append(@")(?=\s|$)");
            regex = new Regex(regexBuilder.ToString(), RegexOptions.Compiled);
        }
        private static Dictionary<string, DiffIndex> ApplyIndexAutoReplacements(DiffTable diff, ITable tab, Dictionary<string, Index> dictionary)
        {
            List<string> oldOnly = diff.Indices.Keys.Where(n => !dictionary.ContainsKey(n)).ToList();
            List<string> newOnly = dictionary.Keys.Where(n => !diff.Indices.ContainsKey(n)).ToList();

            if (oldOnly.Count == 0 || newOnly.Count == 0)
                return diff.Indices;

            Dictionary<string, string> replacements = new Dictionary<string, string>();
            foreach (var o in oldOnly)
            {
                var oldIx = diff.Indices[o];

                var nIx = newOnly.FirstOrDefault(n =>
                {
                    var newIx = dictionary[n];
                    if (oldIx.IsPrimary && newIx is PrimaryClusteredIndex)
                        return true;

                    if (oldIx.IsPrimary || newIx is PrimaryClusteredIndex)
                        return false;

                    if (oldIx.IsUnique != (newIx is UniqueIndex))
                        return false;

                    if (oldIx.ViewName != null || (newIx is UniqueIndex) && ((UniqueIndex)newIx).ViewName != null)
                        return false;

                    var news = newIx.Columns.Select(c => diff.Columns.TryGetC(c.Name)?.Name).NotNull().ToHashSet();

                    if (!news.SetEquals(oldIx.Columns))
                        return false;

                    var uix = newIx as UniqueIndex;
                    if (uix != null && uix.Where != null && !oldIx.IndexName.EndsWith(StringHashEncoder.Codify(uix.Where)))
                        return false;

                    return true;
                });

                if (nIx != null)
                {
                    replacements.Add(o, nIx);
                    newOnly.Remove(nIx);
                }
            }

            if (replacements.IsEmpty())
                return diff.Indices;

            return diff.Indices.SelectDictionary(on => replacements.TryGetC(on) ?? on, dif => dif);
        }
		/// <exception cref="NGit.Errors.TransportException"></exception>
		private void VerifyPrerequisites()
		{
			if (prereqs.IsEmpty())
			{
				return;
			}
			RevWalk rw = new RevWalk(transport.local);
			try
			{
				RevFlag PREREQ = rw.NewFlag("PREREQ");
				RevFlag SEEN = rw.NewFlag("SEEN");
				IDictionary<ObjectId, string> missing = new Dictionary<ObjectId, string>();
				IList<RevObject> commits = new AList<RevObject>();
				foreach (KeyValuePair<ObjectId, string> e in prereqs.EntrySet())
				{
					ObjectId p = e.Key;
					try
					{
						RevCommit c = rw.ParseCommit(p);
						if (!c.Has(PREREQ))
						{
							c.Add(PREREQ);
							commits.AddItem(c);
						}
					}
					catch (MissingObjectException)
					{
						missing.Put(p, e.Value);
					}
					catch (IOException err)
					{
						throw new TransportException(transport.uri, MessageFormat.Format(JGitText.Get().cannotReadCommit
							, p.Name), err);
					}
				}
				if (!missing.IsEmpty())
				{
					throw new MissingBundlePrerequisiteException(transport.uri, missing);
				}
				foreach (Ref r in transport.local.GetAllRefs().Values)
				{
					try
					{
						rw.MarkStart(rw.ParseCommit(r.GetObjectId()));
					}
					catch (IOException)
					{
					}
				}
				// If we cannot read the value of the ref skip it.
				int remaining = commits.Count;
				try
				{
					RevCommit c;
					while ((c = rw.Next()) != null)
					{
						if (c.Has(PREREQ))
						{
							c.Add(SEEN);
							if (--remaining == 0)
							{
								break;
							}
						}
					}
				}
				catch (IOException err)
				{
					throw new TransportException(transport.uri, JGitText.Get().cannotReadObject, err);
				}
				if (remaining > 0)
				{
					foreach (RevObject o in commits)
					{
						if (!o.Has(SEEN))
						{
							missing.Put(o, prereqs.Get(o));
						}
					}
					throw new MissingBundlePrerequisiteException(transport.uri, missing);
				}
			}
			finally
			{
				rw.Release();
			}
		}
        private ReadOnlyCollection<Node> FixupEvaluationOrder(ReadOnlyCollection<Node> seq)
        {
            var q_evalOrders = new Dictionary<Node, ReadOnlyCollection<IILOp>>();
            seq.ForEach(q => q_evalOrders[q] = q.CSharpEvaluationOrder().Where(n => n != null)
                .Select(n => _map.GetOrDefault(n)).Where(op => op != null).ToReadOnly());
            var evalOrder = q_evalOrders.SelectMany(kvp => kvp.Value).ToReadOnly();

            var violatedDeps = new Dictionary<IILOp, ReadOnlyCollection<IILOp>>();
            evalOrder.ForEach((op, i_op) => violatedDeps.Add(op, evalOrder.Where(
                (oop, i_oop) => i_op > i_oop && op.Offset < oop.Offset).ToReadOnly()));
            violatedDeps.RemoveElements(kvp => kvp.Value.IsEmpty());

            if (violatedDeps.IsEmpty())
            {
                return seq;
            }
            else
            {
                var fixt = seq.ToList();

                foreach (var op in violatedDeps.Keys)
                {
                    if (op is Call)
                    {
                        var call = op.AssertCast<Call>();
                        if (call.Method.IsGetter()) /* implemented */ {}
                        else if (call.Method.IsSetter()) throw AssertionHelper.Fail();
                        else throw AssertionHelper.Fail();
                    }
                    else if (op is New) /* presume that ctors are stateless */ {}
                    else if (op is Ldloc) /* implemented */ {}
                    else if (op is Ldarg) /* implemented */ {}
                    else if (op is Ldelem) throw AssertionHelper.Fail();
                    else if (op is Ldfld) /* implemented */ {}
                    else if (op is Ldloca) /* implemented */ {}
                    else if (op is Ldarga) /* implemented */ {}
                    else if (op is Ldelema) throw AssertionHelper.Fail();
                    else if (op is Ldflda) /* implemented */ {}
                    else if (op is Ldind) throw AssertionHelper.Fail();
                    else if (op is Ldftn) throw AssertionHelper.Fail();
                    else if (op is Stloc) throw AssertionHelper.Fail();
                    else if (op is Starg) throw AssertionHelper.Fail();
                    else if (op is Stelem) throw AssertionHelper.Fail();
                    else if (op is Stfld) throw AssertionHelper.Fail();
                    else if (op is Stind) throw AssertionHelper.Fail();
                    // ops that neither read nor write from anywhere except stack
                    // can be freely skipped so we have to consider only a dozen of ops
                    else continue;

                    if (op is Ldloc || op is Ldloca)
                    {
                        var ldloc = op as Ldloc;
                        var ldloca = op as Ldloca;
                        var loc_il = ldloc != null ? ldloc.Loc :
                            ldloca != null ? ldloca.Loc : ((Func<ILocalVar>)(() => { throw AssertionHelper.Fail(); }))();

                        var violations = violatedDeps[op];
                        if (violations.OfType<Stloc>().Any(stloc => stloc.Index == loc_il.Index))
                        {
                            var loc_sym = _symbols.ResolveLocal(loc_il.Index);
                            var expr_ldloc = _mapOp(op).AssertCast<Ref>();
                            (expr_ldloc.Sym == loc_sym).AssertTrue();

                            var locName = loc_il.Source.DebugInfo == null ? ("loc" + loc_il.Index) :
                                !loc_il.Source.DebugInfo.LocalNames.ContainsKey(loc_il.Index) ? ("loc" + loc_il.Index) :
                                loc_il.Source.DebugInfo.LocalNames[loc_il.Index];
                            var bufLocName = Seq.Nats.Select(i => locName + "__CF$" + i.ToString("0000")).First(name1 => _symbols.Locals.None(loc1 => loc1.Name == name1));
                            var bufLoc = _symbols.IntroduceLocal(bufLocName, loc_il.Type);

                            var startOfViolations = q_evalOrders.Keys.First(q => Set.Intersect(q_evalOrders[q], violations).IsNotEmpty());
                            q_evalOrders[startOfViolations].Except(violations).AssertEmpty();
                            var insertionPoint = fixt.IndexOf(startOfViolations);
                            fixt.Insert(insertionPoint, new Assign(new Ref(bufLoc), new Ref(loc_sym)));
                            expr_ldloc.Parent.Children.ReplaceElements(expr_ldloc, new Ref(bufLoc));
                        }
                    }

                    if (op is Ldarg || op is Ldarga)
                    {
                        var ldarg = op as Ldarg;
                        var ldarga = op as Ldarga;
                        var arg_il = ldarg != null ? ldarg.Arg :
                            ldarga != null ? ldarga.Arg : ((Func<ParameterInfo>)(() => { throw AssertionHelper.Fail(); }))();
                        var arg_index = ldarg != null ? ldarg.Index :
                            ldarga != null ? ldarga.Index : ((Func<int>)(() => { throw AssertionHelper.Fail(); }))();

                        var violations = violatedDeps[op];
                        if (violations.OfType<Starg>().Any(starg => starg.Index == arg_index))
                        {
                            var arg_sym = _symbols.ResolveParam(arg_index);
                            var expr_ldarg = _mapOp(op).AssertCast<Ref>();
                            (expr_ldarg.Sym == arg_sym).AssertTrue();

                            var argName = arg_sym.Name;
                            var bufLocName = Seq.Nats.Select(i => argName + "__CF$" + i.ToString("0000")).First(name1 => _symbols.Locals.None(loc => loc.Name == name1));
                            var bufLoc = _symbols.IntroduceLocal(bufLocName, null);

                            var startOfViolations = q_evalOrders.Keys.First(q => Set.Intersect(q_evalOrders[q], violations).IsNotEmpty());
                            q_evalOrders[startOfViolations].Except(violations).AssertEmpty();
                            var insertionPoint = fixt.IndexOf(startOfViolations);
                            fixt.Insert(insertionPoint, new Assign(new Ref(bufLoc), new Ref(arg_sym)));
                            expr_ldarg.Children.ReplaceElements(expr_ldarg, new Ref(bufLoc));
                        }
                    }

                    if (op is Ldfld || op is Ldflda)
                    {
                        var ldfld = op as Ldfld;
                        var ldflda = op as Ldflda;
                        var fld = ldfld != null ? ldfld.Field :
                            ldflda != null ? ldflda.Field : ((Func<FieldInfo>)(() => { throw AssertionHelper.Fail(); }))();

                        var violations = violatedDeps[op];
                        if (violations.OfType<Stfld>().Any(stfld => stfld.Field == fld &&
                            ((Func<bool>)(() =>
                            {
                                var stfld_fld = _mapOp(stfld).AssertCast<Assign>().Lhs.AssertCast<Fld>();
                                var ldfld_fld = _mapOp(op).AssertCast<Fld>();
                                return stfld_fld.This.Equiv(ldfld_fld.This);
                            }))()))
                        {
                            var expr_ldfld = _mapOp(op).AssertCast<Fld>();
                            (expr_ldfld.Field == fld).AssertTrue();

                            var fldName = fld.Name;
                            var bufLocName = Seq.Nats.Select(i => fldName + "__CF$" + i.ToString("0000")).First(name1 => _symbols.Locals.None(loc => loc.Name == name1));
                            var bufLoc = _symbols.IntroduceLocal(bufLocName, null);

                            var startOfViolations = q_evalOrders.Keys.First(q => Set.Intersect(q_evalOrders[q], violations).IsNotEmpty());
                            q_evalOrders[startOfViolations].Except(violations).AssertEmpty();
                            var insertionPoint = fixt.IndexOf(startOfViolations);
                            fixt.Insert(insertionPoint, new Assign(new Ref(bufLoc), new Fld(fld, expr_ldfld.This)));
                            expr_ldfld.Children.ReplaceElements(expr_ldfld, new Ref(bufLoc));
                        }
                    }

                    if (op is Call)
                    {
                        var callGet = op.AssertCast<Call>();
                        callGet.Method.IsGetter().AssertTrue();
                        var violations = violatedDeps[callGet];
                        if (violations.OfType<Call>().Any(callSet =>
                        {
                            if (!callSet.Method.IsSetter()) return false;
                            if (!(callSet.Method.EnclosingProperty() == callGet.Method.EnclosingProperty())) return false;

                            // todo. verify that both calls reference the same property
                            // and have the same This (use Dump comparison and add a todo)
                            throw new NotImplementedException();
                        }))
                        {
                            var prop = callGet.Method.EnclosingProperty().AssertNotNull();
                            var expr_callGet = _mapOp(callGet);
                            // todo. verify that it references the "prop" property

                            var rawName = callGet.Method.Name;
                            var bufLocName = Seq.Nats.Select(i => rawName + "__CF$" + i.ToString("0000")).First(name1 => _symbols.Locals.None(loc => loc.Name == name1));
                            var bufLoc = _symbols.IntroduceLocal(bufLocName, null);

                            var startOfViolations = q_evalOrders.Keys.First(q => Set.Intersect(q_evalOrders[q], violations).IsNotEmpty());
                            q_evalOrders[startOfViolations].Except(violations).AssertEmpty();
                            var insertionPoint = fixt.IndexOf(startOfViolations);
                            fixt.Insert(insertionPoint, ((Func<Expression>)(() =>
                            {
                                throw new NotImplementedException();
                            }))());
                            expr_callGet.Children.ReplaceElements(expr_callGet, new Ref(bufLoc));
                        }
                    }
                }

                return fixt.ToReadOnly();
            }
        }
        private string chooseEncounterName()
        {
            string encounterName = null;

            Dictionary<string, int> damageCountMap = new Dictionary<string, int>();
            string targetId;
            foreach (RiftAction riftAction in this.inProgressEncounterActions)
            {
                bool includeAction = RiftActionFilterFactory.getDamageTypes().Contains(riftAction.ActionType);

                if (includeAction)
                {
                    targetId = riftAction.TargetId;
                    RiftEntity target = getEntity(targetId);
                    if ((target != null) && (target.OwnerId == null) && (!target.Relationship.IsPartyMember))
                    {
                        int newValue = riftAction.SpellValue;
                        String targetName = target.Name;

                        int sumValue = damageCountMap.ContainsKey(targetName) ? damageCountMap[targetName] : 0;
                        if (sumValue != 0)
                        {
                            newValue += sumValue;
                        }
                        if(damageCountMap.ContainsKey(targetName))
                            damageCountMap[targetName] = newValue;
                        else
                            damageCountMap.Add(targetName, newValue);}
                    }
                }

            if (!damageCountMap.IsEmpty())
            {
                String maxDamageName = null;
                int maxDamage = 0;

                foreach (var entry in damageCountMap)
                {
                    int entryDamage = entry.Value;
                    if (maxDamage < entryDamage)
                    {
                        maxDamage = entryDamage;
                        maxDamageName = entry.Key;
                    }
                }

                if (!string.IsNullOrEmpty(maxDamageName))
                {
                    encounterName = maxDamageName;
                }
            }

            if (encounterName == null)
            {
                encounterName = "Encounter";
            }

            return encounterName;
        }