public static DataHolderTableMapping[] CreateDataHolderTableMappings(
            Dictionary<TableDefinition, List<DataHolderDefinition>> tableDataHolderMap,
            DataHolderDefinition[] dataHolderDefinitions
            )
        {
            // find exact mappings and split them up into as small parts as possible
            // this way we will only read the data from tables that is actually
            // mapped to DataHolders and we will read every cell only once

            var tablesByHolder = new Dictionary<DataHolderDefinition, List<TableDefinition>>();
            foreach (var pair in tableDataHolderMap)
            {
                if (pair.Value == null)
                {
                    log.Warn("Table-definition \"{0}\" has no used columns (and can possibly be removed from the config).");
                }
                else
                {
                    pair.Key.DataHolderDefinitions = pair.Value.ToArray();
                    foreach (var dataHolder in pair.Key.DataHolderDefinitions)
                    {
                        tablesByHolder.GetOrCreate(dataHolder).Add(pair.Key);
                    }
                }
            }

            var mappings = new List<DataHolderTableMapping>();
            var allDefs = new HashSet<DataHolderDefinition>(tablesByHolder.Keys.ToArray());

            var dataHolders = new HashSet<DataHolderDefinition>();
            var tables = new HashSet<TableDefinition>();
            foreach (var holder in tablesByHolder.Keys)
            {
                if (AddTables(allDefs, holder, tablesByHolder, dataHolders, tables))
                {
                    var mapping = new DataHolderTableMapping(dataHolders.ToArray(), tables.ToArray());
                    mappings.Add(mapping);
                    dataHolders.Clear();
                    tables.Clear();
                }
            }

            foreach (var table in tableDataHolderMap.Keys)
            {
                foreach (var field in table.ColumnDefinitions)
                {
                    if (field is IDataFieldBase)
                    {
                        var holderDef = ((IDataFieldBase)field).DataHolderDefinition;
                        if (!dataHolders.Contains(holderDef))
                        {
                            var mapping = mappings.Find(map => map.TableDefinitions.Contains(table));
                            var tableHolders = mapping.DataHolderDefinitions;
                            mapping.DataHolderDefinitions = new DataHolderDefinition[tableHolders.Length + 1];
                            Array.Copy(tableHolders, mapping.TableDefinitions, tableHolders.Length);
                            mapping.DataHolderDefinitions[tableHolders.Length] = holderDef;
                        }
                    }
                }
            }

            return mappings.ToArray();
        }
Example #2
0
		/// <summary>
		/// Creates the graph of all quests and their relations
		/// </summary>
		private static void CreateQuestRelationGraph()
		{
			var groups = new Dictionary<int, List<uint>>();
			foreach (var quest in Templates)
			{
				if (quest != null)
				{
					if (quest.Id == 10068)
					{
						quest.ToString();
					}
					if (quest.ExclusiveGroup != 0)
					{
						groups.GetOrCreate(quest.ExclusiveGroup).AddUnique(quest.Id);
					}
					else if (quest.NextQuestId != 0)
					{
						var nextQuest = GetTemplate((uint)Math.Abs(quest.NextQuestId));
						if (nextQuest == null)
						{
							ContentMgr.OnInvalidDBData("NextQuestId {0} is invalid in: {1}", quest.NextQuestId, quest);
						}
						else
						{
							if (quest.NextQuestId > 0)
							{
								nextQuest.ReqAllFinishedQuests.AddUnique(quest.Id);
							}
							else
							{
								nextQuest.ReqAllActiveQuests.AddUnique(quest.Id);
							}
						}
					}
					if (quest.PreviousQuestId != 0)
					{
						//var prevQuest = GetTemplate((uint)Math.Abs(quest.PreviousQuestId));
						//if (prevQuest == null)
						//{
						//    ContentHandler.OnInvalidDBData("PreviousQuestId {0} is invalid in: {1}", quest.PreviousQuestId, quest);
						//}
						//else
						if (quest.PreviousQuestId > 0)
						{
							quest.ReqAllFinishedQuests.AddUnique((uint)quest.PreviousQuestId);
						}
						else
						{
							quest.ReqAllActiveQuests.AddUnique((uint)-quest.PreviousQuestId);
						}
					}
					if (quest.FollowupQuestId != 0)
					{
						// follow up quest requires this one to be finished before it can be taken
						var followupQuest = GetTemplate(quest.FollowupQuestId);
						if (followupQuest != null)
						{
							followupQuest.ReqAllFinishedQuests.AddUnique(quest.Id);
						}
					}
				}
			}

			foreach (var group in groups)
			{
				foreach (var qid in group.Value)
				{
					var quest = GetTemplate(qid);
					foreach (var qid2 in group.Value)
					{
						if (qid2 != qid)
						{
							if (group.Key > 0)
							{
								quest.ReqUndoneQuests.AddUnique(qid2);
							}
						}
					}

					if (quest.NextQuestId != 0)
					{
						var nextQuest = GetTemplate((uint)Math.Abs(quest.NextQuestId));
						if (nextQuest == null)
						{
							ContentMgr.OnInvalidDBData("NextQuestId {0} is invalid in: {1}", quest.NextQuestId, quest);
						}
						else
						{
							if (group.Key > 0)
							{
								nextQuest.ReqAllFinishedQuests.AddUnique(quest.Id);
							}
							else
							{
								nextQuest.ReqAnyFinishedQuests.AddUnique(quest.Id);
							}
						}
					}
				}
			}
		}
Example #3
0
		static void AddMapping(LightDBDefinitionSet defs, TableDefinition[] defaultTables, SimpleFlatFieldDefinition fieldDef,
			Dictionary<string, List<SimpleDataColumn>> mappedFields, IFlatDataFieldAccessor accessor, MemberInfo member)
		{
			var column = fieldDef.Column;

			var tables = defs.EnsureTables(fieldDef.Table, defaultTables);
			object defaultValue;
			if (!String.IsNullOrEmpty(fieldDef.DefaultStringValue))
			{
				defaultValue = StringParser.Parse(fieldDef.DefaultStringValue, member.GetVariableType());
			}
			else
			{
				if (String.IsNullOrEmpty(column))
				{
					return;
				}
				defaultValue = null;
			}

			foreach (var table in tables)
			{
				var dataHolders = defs.m_tableDataHolderMap.GetOrCreate(table);
				if (!dataHolders.Contains(accessor.DataHolderDefinition))
				{
					dataHolders.Add(accessor.DataHolderDefinition);
				}

				var mappedFieldMap = mappedFields.GetOrCreate(table.Name);

				SimpleDataColumn dataColumn;
                var dbAttrs = member.GetCustomAttributes<DBAttribute>();
                var attr = dbAttrs.Where(attribute => attribute is PersistentAttribute).FirstOrDefault() as PersistentAttribute;

				if (String.IsNullOrEmpty(column))
				{
					// use default value
					mappedFieldMap.Add(dataColumn = new SimpleDataColumn(fieldDef.Name, defaultValue));
				}
				else
				{
					dataColumn = mappedFieldMap.Find((cmpField) => cmpField.ColumnName == column);
					if (dataColumn == null)
					{
                        var type = member.GetActualType();
                        if (attr != null)
                            type = attr.ReadType ?? type;
                        var reader = Converters.GetReader(type);
                        mappedFieldMap.Add(dataColumn = new SimpleDataColumn(column, reader));
					}
				}

				dataColumn.FieldList.Add(accessor);
			}
		}
Example #4
0
        private void CreateProxies(
            ProxyBuilder proxyBuilder,
            Action<TypeCodeGenInfo, TypeDefinition> onTypeGenerated,
            Func<TypeCodeGenInfo, bool> typeIsGeneratedPredicate = null)
        {
            typeIsGeneratedPredicate = typeIsGeneratedPredicate ?? (x => true);
            var generatedTypeDict = new Dictionary<TypeCodeGenInfo, TypeDefinition>();

            foreach (var typeInfo in this.clientTypeInfoDict.Values.Where(typeIsGeneratedPredicate))
            {
                generatedTypeDict.GetOrCreate(typeInfo,
                    () =>
                        CreateProxy(proxyBuilder, onTypeGenerated, typeInfo, generatedTypeDict));
            }
        }
Example #5
0
        private TypeDefinition CreateProxy(ProxyBuilder proxyBuilder,
            Action<TypeCodeGenInfo, TypeDefinition> onTypeGenerated,
            TypeCodeGenInfo typeInfo,
            Dictionary<TypeCodeGenInfo, TypeDefinition> generatedTypeDict)
        {
            var targetType = typeInfo.TransformedType;
            var name = targetType.Name;

            TypeDefinition baseTypeDef = null;
            var tt = typeInfo.TransformedType;
            var rt = typeInfo.TransformedType as ResourceType;
            if (rt != null && rt.UriBaseType != null && rt.UriBaseType != rt)
            {
                var baseTypeInfo = this.clientTypeInfoDict[tt.BaseType];
                baseTypeDef = generatedTypeDict.GetOrCreate(baseTypeInfo,
                    () =>
                        CreateProxy(proxyBuilder,
                            onTypeGenerated,
                            baseTypeInfo,
                            generatedTypeDict));
            }
            var proxyType = proxyBuilder.CreateProxyType(name, typeInfo.InterfaceType.WrapAsEnumerable(), baseTypeDef);

            if (onTypeGenerated != null)
                onTypeGenerated(typeInfo, proxyType);

            return proxyType;
        }
 public void SpecifiedValueIsIgnoredStoredWhenKeyIsPresent()
 {
     var dict = new Dictionary<string, string>();
     dict["foo"] = "bar";
     Assert.AreEqual("bar", dict.GetOrCreate("foo", "ignored"));
 }
 public void SpecifiedValueIsReturnedAndStoredWhenKeyIsMissing()
 {
     var dict = new Dictionary<string,string>();
     Assert.AreEqual("bar", dict.GetOrCreate("foo", "bar"));
     Assert.AreEqual("bar", dict["foo"]);
 }
 public void WithExistingKeyValueIsReturnedAndDelegateNotCalled()
 {
     var dict = new Dictionary<string, string>();
     dict["foo"] = "bar";
     Assert.AreEqual("bar", dict.GetOrCreate("foo", () => { throw new Exception(); }));
 }
 public void WithMissingKeyDelegateIsCalledAndResultStoredAndReturned()
 {
     var dict = new Dictionary<string, string>();
     Assert.AreEqual("bar", dict.GetOrCreate("foo", () => "bar"));
     Assert.AreEqual("bar", dict["foo"]);            
 }
Example #10
0
 public void MissingValueIsCreatedAndStoredWithConstructorConstraint()
 {
     var dict = new Dictionary<string, List<int>>();
     var list = dict.GetOrCreate("foo");
     Assert.AreSame(list, dict["foo"]);
 }
Example #11
0
 public void ExistingValueIsReturnedWithConstructorConstraint()
 {
     var dict = new Dictionary<string, int>();
     dict["foo"] = 5000;
     Assert.AreEqual(5000, dict.GetOrCreate("foo"));
 }
Example #12
0
		private static void CreateGraph()
		{
			var groups = new Dictionary<int, List<uint>>();
			foreach (var quest in Templates)
			{
				if (quest != null)
				{
					if (quest.ExclusiveGroup != 0)
					{
						groups.GetOrCreate(quest.ExclusiveGroup).Add(quest.Id);
					}
					else if (quest.NextQuestId != 0)
					{
						var nextQuest = GetTemplate((uint)Math.Abs(quest.NextQuestId));
						if (nextQuest == null)
						{
							ContentHandler.OnInvalidDBData("NextQuestId {0} is invalid in: {1}", quest.NextQuestId, quest);
						}
						else
						{
							if (quest.NextQuestId > 0)
							{
								nextQuest.ReqAllFinishedQuests.Add(quest.Id);
							}
							else
							{
								nextQuest.ReqAllActiveQuests.Add(quest.Id);
							}
						}
					}
					if (quest.PreviousQuestId != 0)
					{
						//var prevQuest = GetTemplate((uint)Math.Abs(quest.PreviousQuestId));
						//if (prevQuest == null)
						//{
						//    ContentHandler.OnInvalidDBData("PreviousQuestId {0} is invalid in: {1}", quest.PreviousQuestId, quest);
						//}
						//else
						if (quest.PreviousQuestId > 0)
						{
							quest.ReqAllFinishedQuests.Add((uint)quest.PreviousQuestId);
						}
						else
						{
							quest.ReqAllActiveQuests.Add((uint)-quest.PreviousQuestId);
						}
					}
				}
			}

			foreach (var group in groups)
			{
				foreach (var qid in group.Value)
				{
					var quest = GetTemplate(qid);
					foreach (var qid2 in group.Value)
					{
						if (qid2 != qid)
						{
							if (group.Key > 0)
							{
								quest.ReqUndoneQuests.Add(qid2);
							}
						}
					}

					if (quest.NextQuestId != 0)
					{
						var nextQuest = GetTemplate((uint)Math.Abs(quest.NextQuestId));
						if (nextQuest == null)
						{
							ContentHandler.OnInvalidDBData("NextQuestId {0} is invalid in: {1}", quest.NextQuestId, quest);
						}
						else
						{
							if (group.Key > 0)
							{
								nextQuest.ReqAllFinishedQuests.Add(quest.Id);
							}
							else
							{
								nextQuest.ReqAnyFinishedQuests.Add(quest.Id);
							}
						}
					}
				}
			}
		}
Example #13
0
		static void AddMapping(LightDBDefinitionSet defs, TableDefinition[] defaultTables, SimpleFlatFieldDefinition fieldDef,
			Dictionary<string, List<SimpleDataColumn>> mappedFields, IFlatDataFieldAccessor accessor, MemberInfo member)
		{
			var column = fieldDef.Column;

			var tables = defs.EnsureTables(fieldDef.Table, defaultTables);
			object defaultValue;
			if (!String.IsNullOrEmpty(fieldDef.DefaultStringValue))
			{
				defaultValue = Utility.Parse(fieldDef.DefaultStringValue, member.GetVariableType());
			}
			else
			{
				if (String.IsNullOrEmpty(column))
				{
					return;
				}
				defaultValue = null;
			}

			if (accessor.DataHolderDefinition.Type.Name.EndsWith("TrainerEntry"))
			{
				defs.ToString();
			}

			foreach (var table in tables)
			{
				var dataHolders = defs.m_tableDataHolderMap.GetOrCreate(table);
				if (!dataHolders.Contains(accessor.DataHolderDefinition))
				{
					dataHolders.Add(accessor.DataHolderDefinition);
				}

				var mappedFieldMap = mappedFields.GetOrCreate(table.Name);

				SimpleDataColumn dataColumn;

				if (String.IsNullOrEmpty(column))
				{
					// use default value
					mappedFieldMap.Add(dataColumn = new SimpleDataColumn(fieldDef.Name, defaultValue));
				}
				else
				{
					dataColumn = mappedFieldMap.Find((cmpField) => cmpField.ColumnName == column);
					if (dataColumn == null)
					{
						mappedFieldMap.Add(dataColumn = new SimpleDataColumn(column, Converters.GetReader(member.GetActualType())));
					}
				}

				dataColumn.FieldList.Add(accessor);
			}
		}
Example #14
0
        public static Module matmul()
        {
            Type u16 = new Type { Name = TypeName.U16 }, u32 = new Type { Name = TypeName.U32 }, u64 = new Type { Name = TypeName.U64 };
            Type f32 = new Type { Name = TypeName.F32 }, f64 = new Type { Name = TypeName.F64 }, pred = new Type { Name = TypeName.Pred };

            var regs = new Dictionary<String, Reg>();
            Func<String, Reg> reg_u32 = name => new Reg{Name = name, Type = u32};
            Func<String, Reg> reg_f32 = name => new Reg{Name = name, Type = f32};
            Func<int, Reg> rh = i => regs.GetOrCreate(String.Format("%rh{0}", i), name => new Reg{Name = name, Type = u16});
            Func<int, Reg> r = i => regs.GetOrCreate(String.Format("%r{0}", i), name => new Reg{Name = name, Type = u32});
            Func<int, Reg> rd = i => regs.GetOrCreate(String.Format("%rd{0}", i), name => new Reg{Name = name, Type = u64});
            Func<int, Reg> f = i => regs.GetOrCreate(String.Format("%f{0}", i), name => new Reg{Name = name, Type = f32});
            Func<int, Reg> fd = i => regs.GetOrCreate(String.Format("%fd{0}", i), name => new Reg{Name = name, Type = f64});
            Func<int, Reg> p = i => regs.GetOrCreate(String.Format("%p{0}", i), name => new Reg{Name = name, Type = pred});

            var module = new Module(SoftwareIsa.PTX_14, HardwareIsa.SM_13);
            Func<String, Var> param_align4_b8_12 = name => new Var{Name = name, Space = space.param, Alignment = 4, Type = new Type{Name = TypeName.B8, Dims = new []{12}}};
            Var a = param_align4_b8_12("A"), b = param_align4_b8_12("B"), c = param_align4_b8_12("C");
            var kernel = module.AddEntry("MatMulKernel", a, b, c);
            var ptx = kernel.Stmts;

            Func<String, Label> label = name => new Label{Name = name};
            Label loop_body = label("$LoopBody"), after_loop = label("$AfterLoop"), exit = label("$Exit");
            Reg a_width = reg_u32("a_width"), a_height = reg_u32("a_height"), a_raw = reg_u32("a_raw");
            Reg b_width = reg_u32("b_width"), b_height = reg_u32("b_height"), b_raw = reg_u32("b_raw");
            Reg c_width = reg_u32("c_width"), c_height = reg_u32("c_height"), c_raw = reg_u32("c_raw");
            Reg row = reg_u32("row"), col = reg_u32("col"), cvalue = reg_f32("cvalue"), dim = reg_u32("dim");
            Reg a_offset = reg_u32("a_offset"), a_offset_lo = reg_u32("a_offset_lo"), a_offset_stride = reg_u32("a_offset_stride"), a_offset_hi = reg_u32("a_offset_hi");
            Reg b_offset = reg_u32("b_offset"), b_offset_lo = reg_u32("b_offset_lo"), b_offset_stride = reg_u32("b_offset_stride"), b_offset_hi = reg_u32("b_offset_hi");

            ptx.Add(new Comment{Text = Environment.NewLine});
            ptx.Add(new Comment{Text = "int row = blockIdx.y * blockDim.y + threadIdx.y;"});
            ptx.Add(new Comment{Text = "int col = blockIdx.x * blockDim.x + threadIdx.x;"});
            ptx.Add(new mov{type = u16, d = rh(1), a = new ctaid().mod(Mod.X)});
            ptx.Add(new mov{type = u16, d = rh(2), a = new ntid().mod(Mod.X)});
            ptx.Add(new mul{type = u16, mode = mulm.wide, d = r(1), a = rh(1), b = rh(2)});
            ptx.Add(new mov{type = u16, d = rh(3), a = new ctaid().mod(Mod.Y)});
            ptx.Add(new mov{type = u16, d = rh(4), a = new ntid().mod(Mod.Y)});
            ptx.Add(new mul{type = u16, mode = mulm.wide, d = r(2), a = rh(3), b = rh(4)});
            ptx.Add(new cvt{dtype = u32, atype = u16, d = r(3), a = new tid().mod(Mod.X)});
            ptx.Add(new add{type = u32, d = col, a = r(3), b = r(1)});
            ptx.Add(new cvt{dtype = u32, atype = u16, d = r(5), a = new tid().mod(Mod.Y)});
            ptx.Add(new add{type = u32, d = row, a = r(5), b = r(2)});

            ptx.Add(new Comment{Text = Environment.NewLine});
            ptx.Add(new Comment{Text = "if (A.height <= row || B.width <= col) return;"});
            ptx.Add(new ld{ss = space.param, type = u32, d = b_width, a = b + 0});
            ptx.Add(new ld{ss = space.param, type = u32, d = a_height, a = a + 4});
            ptx.Add(new setp{cmpop = cmp.le, type = u32, p = p(6), a = a_height, b = row});
            ptx.Add(new setp{cmpop = cmp.le, type = u32, p = p(7), a = b_width, b = col});
            ptx.Add(new or{type = pred, d = p(1), a = p(6), b = p(7)});
            ptx.Add(new bra{Guard = p(1), tgt = exit});

            ptx.Add(new Comment{Text = Environment.NewLine});
            ptx.Add(new Comment{Text = "float Cvalue = 0;"});
            ptx.Add(new mov{type = f32, d = cvalue, a = (Const)0f});

            ptx.Add(new Comment{Text = Environment.NewLine});
            ptx.Add(new Comment{Text = "for (int dim = 0; dim < A.width; ++dim)"});
            ptx.Add(new ld{ss = space.param, type = u32, d = a_width, a = a + 0});
            ptx.Add(new mov{type = u32, d = dim, a = (Const)0});
            ptx.Add(new setp{cmpop = cmp.le, type = u32, p = new Modded{Mod = Mod.Couple, Embedded = {p(2), p(8)}}, a = a_width, b = dim});
            ptx.Add(new bra{Guard = p(8).mod(Mod.Not), tgt = after_loop});

            ptx.Add(new Comment{Text = Environment.NewLine});
            ptx.Add(new Comment{Text = "Cvalue += A.elements[row * A.width + dim] * B.elements[dim * B.width + col];"});
            ptx.Add(new ld{ss = space.param, type = u32, d = a_raw, a = a + 8});
            ptx.Add(new mul{mode = mulm.lo, type = u32, d = r(18), a = a_width, b = row});
            ptx.Add(new mul{mode = mulm.lo, type = u32, d = a_offset_lo, a = r(18), b = (Const)4});
            ptx.Add(new add{type = u32, d = a_offset, a = a_offset_lo, b = a_raw});
            ptx.Add(new add{type = u32, d = r(21), a = r(18), b = a_width});
            ptx.Add(new mul{mode = mulm.lo, type = u32, d = r(25), a = r(21), b = (Const)4});
            ptx.Add(new add{type = u32, d = a_offset_hi, a = r(25), b = a_raw});
            ptx.Add(new ld{ss = space.param, type = u32, d = b_raw, a = b + 8});
            ptx.Add(new mul{mode = mulm.lo, type = u32, d = b_offset_lo, a = col, b = (Const)4});
            ptx.Add(new add{type = u32, d = b_offset, a = b_offset_lo, b = b_raw});
            ptx.Add(new mul{mode = mulm.lo, type = u32, d = b_offset_stride, a = b_width, b = (Const)4});

            ptx.Add(new Comment{Text = Environment.NewLine});
            ptx.Add(new Comment{Text = "Cvalue += A.elements[row * A.width + dim] * B.elements[dim * B.width + col];"});
            ptx.Add(loop_body);
            ptx.Add(new ld{ss = space.global, type = f32, d = f(2), a = a_offset});
            ptx.Add(new ld{ss = space.global, type = f32, d = f(3), a = b_offset});
            ptx.Add(new mad{type = f32, d = cvalue, a = f(3), b = f(2), c = cvalue});
            ptx.Add(new add{type = u32, d = a_offset, a = a_offset, b = (Const)4});
            ptx.Add(new add{type = u32, d = b_offset, a = b_offset, b = b_offset_stride});
            ptx.Add(new setp{cmpop = cmp.ne, type = u32, p = p(3), a = a_offset, b = a_offset_hi});
            ptx.Add(new bra{Guard = p(3), tgt = loop_body});
            ptx.Add(new bra{uni = true, tgt = after_loop});

            ptx.Add(new Comment{Text = Environment.NewLine});
            ptx.Add(new Comment{Text = "C.elements[row * C.width + col] = Cvalue;"});
            ptx.Add(after_loop);
            ptx.Add(new ld{ss = space.param, type = u32, d = c_raw, a = c + 8});
            ptx.Add(new ld{ss = space.param, type = u32, d = c_width, a = c + 0});
            ptx.Add(new mul{mode = mulm.lo, type = u32, d = r(32), a = c_width, b = row});
            ptx.Add(new add{type = u32, d = r(33), a = col, b = r(32)});
            ptx.Add(new mul{mode = mulm.lo, type = u32, d = r(34), a = r(33), b = (Const)4});
            ptx.Add(new add{type = u32, d = r(35), a = c_raw, b = r(34)});
            ptx.Add(new st{ss = space.global, type = f32, a = r(35), b = cvalue});

            ptx.Add(new Comment{Text = Environment.NewLine});
            ptx.Add(exit);
            ptx.Add(new exit());

            return module;
        }