/// <summary>Implements a map-reduce operation.</summary> /// <typeparam name="TSource">Specifies the type of the source elements.</typeparam> /// <typeparam name="TMapped">Specifies the type of the mapped elements.</typeparam> /// <typeparam name="TKey">Specifies the type of the element keys.</typeparam> /// <typeparam name="TResult">Specifies the type of the results.</typeparam> /// <param name="source">The source elements.</param> /// <param name="map">A function used to get the target data from a source element.</param> /// <param name="keySelector">A function used to get a key from the target data.</param> /// <param name="reduce">A function used to reduce a group of elements.</param> /// <returns>The result elements of the reductions.</returns> public static ParallelQuery <TResult> MapReduce <TSource, TMapped, TKey, TResult>( this ParallelQuery <TSource> source, Func <TSource, TMapped> map, Func <TMapped, TKey> keySelector, Func <IGrouping <TKey, TMapped>, TResult> reduce) { return(ParallelEnumerable.Select <TSource, TMapped>(source, map).GroupBy <TMapped, TKey>(keySelector) .Select <IGrouping <TKey, TMapped>, TResult>(reduce)); }
public static float GetVolumeOfIntersection(BoundingSphere node, ParallelQuery <BoundingSphere> allNodes) { return(ParallelEnumerable.Sum( ParallelEnumerable.Select( allNodes, b => BoundingSphere.IntersectionVolume(node, b)) )); }
static void Ex4Task1_PLINQAsParallel() { var q = ParallelEnumerable.Select( ParallelEnumerable.OrderBy( ParallelEnumerable.Where(employeeData.AsParallel(), x => x.EmployeeID % 2 == 0), x => x.EmployeeID), x => PayrollServices.GetEmployeeInfo(x)) .ToList(); foreach (var e in q) { Console.WriteLine(e); } }
/// <param name="context">Context used when localizing to reference current localization</param> /// <param name="usedGuids"></param> /// <param name="shouldClean"></param> /// <param name="shouldExpand"></param> /// <param name="pathOk">Path is an acceptable filename for a new localization file</param> /// <param name="fileLocationOk">Path is an acceptable location from which to import an existing localization file</param> public LocalizationEngine(GetFilePath getFilePath, IEnumerable <Project.TData.LocalizerSetData> sets, ILocalizationContext context, Func <HashSet <Id <LocalizedText> > > usedGuids, Func <string, bool> shouldClean, Func <FileInfo, bool> pathOk, Func <string, bool> fileLocationOk, UpToDateFile.BackEnd backEnd, DirectoryInfo origin) { m_context = context; m_usedGuids = usedGuids; ShouldClean = shouldClean; Func <IEnumerable <Tuple <Id <FileInProject>, DocumentPath> >, IEnumerable <ILocalizationFile> > load = files => { //return files.Select(file => LocalizationFile.Load(file, MakeSerializer(file.Name), backend)); var filesAndSerializer = files.Select(f => new { Id = f.Item1, Path = f.Item2, Serializer = MakeSerializer(f.Item2.AbsolutePath, f.Item1) }).ToList(); return(ParallelEnumerable.Select(filesAndSerializer.AsParallel(), fs => LocalizationFile.Load(fs.Path, fs.Id, fs.Serializer, backEnd))); }; Func <DirectoryInfo, LocalizationFile> makeEmpty = path => LocalizationFile.MakeNew(path, MakeSerializer, pathOk, backEnd, origin); m_localizers = new ProjectElementList <ILocalizationFile>(getFilePath, fileLocationOk.Bottleneck(), load, makeEmpty); m_localizationSets = sets.ToHashSet(); }
/// <summary> /// /// </summary> /// <param name="context"></param> /// <param name="data"></param> /// <param name="conversationNodeFactory"></param> /// <param name="domainNodeFactory"></param> /// <param name="initialContent">Represents the current contents of the file. Reference is not held. A copy is made.</param> /// <param name="projectFile"></param> /// <param name="serializer"></param> /// <param name="conversationSerializer"></param> /// <param name="conversationSerializerDeserializerFactory"></param> /// <param name="domainSerializer"></param> /// <param name="pluginsConfig"></param> /// <param name="audioCustomization"></param> public Project(ILocalizationContext context, TData data, INodeFactory conversationNodeFactory, INodeFactory domainNodeFactory, MemoryStream initialData, FileInfo projectFile, ISerializer <TData> serializer, ISerializer <TConversationData> conversationSerializer, ConversationSerializerDeserializerFactory conversationSerializerDeserializerFactory, ISerializer <TDomainData> domainSerializer, PluginsConfig pluginsConfig, Func <IAudioProviderCustomization> audioCustomization, UpToDateFile.BackEnd backend) { AssertUniqueFileIds(data); AssertUniqueLocalizationSetNames(data); AssertUniqueFilePaths(data); m_upToDateFileBackend = backend; Action <Stream> saveTo = stream => { Write(GetFilePath, Conversations.Select(x => (x.Id)), LocalizationFiles.Select(x => (x.Id)), DomainFiles.Select(x => (x.Id)), AudioFiles.Select(x => (x.Id)), Localizer.LocalizationSets, stream, m_serializer); }; m_file = new SaveableFileNotUndoable(initialData, projectFile, saveTo, backend); ConversationNodeFactory = conversationNodeFactory; DomainNodeFactory = domainNodeFactory; m_serializer = serializer; ConversationSerializer = conversationSerializer; m_conversationSerializerFactory = conversationSerializerDeserializerFactory; m_domainSerializer = domainSerializer; m_filePaths = data.Conversations.Concat(data.Localizations).Concat(data.Domains).Concat(data.Audios).ToDictionary(f => f.Id, f => f.Path); IEnumerable <Id <FileInProject> > conversationIds = data.Conversations.Select(f => f.Id); IEnumerable <Id <FileInProject> > localizerIds = data.Localizations.Select(f => f.Id); IEnumerable <Id <FileInProject> > domainIds = data.Domains.Select(f => f.Id); IEnumerable <Id <FileInProject> > audioIds = data.Audios.Select(f => f.Id); m_audioProvider = new AudioProvider(new FileInfo(projectFile.FullName), GetFilePath, s => CheckFolder(s, Origin), this, audioCustomization()); using (m_audioProvider.SuppressUpdates()) { { m_audioProvider.AudioFiles.Load(audioIds); } { //Dictionary<IDynamicEnumParameter, DynamicEnumParameter.Source> domainEnumSource = new Dictionary<IDynamicEnumParameter, DynamicEnumParameter.Source>(); Func <IDynamicEnumParameter, object, DynamicEnumParameter.Source> getDomainEnumSource = (k, o) => { return(null); //Nothing should need a source (but the system will ask anyway) //if (!domainEnumSource.ContainsKey(k)) //domainEnumSource[k] = new DynamicEnumParameter.Source(); //return domainEnumSource[k]; }; m_domainDataSource = new DomainDomain(pluginsConfig); Func <IEnumerable <Tuple <Id <FileInProject>, DocumentPath> >, IEnumerable <IDomainFile> > loader = paths => { var result = DomainFile.Load(paths, m_domainDataSource, DomainSerializerDeserializer.Make, DomainNodeFactory, () => DomainUsage, getDomainEnumSource, backend).Evaluate(); result.ForAll(a => a.ConversationDomainModified += ConversationDatasourceModified); return(result); }; Func <DirectoryInfo, DomainFile> makeEmpty = path => DomainFile.CreateEmpty(path, m_domainDataSource, m_domainSerializer, pathOk, DomainNodeFactory, () => DomainUsage, getDomainEnumSource, backend, Origin); m_domainFiles = new ProjectElementList <IDomainFile>(GetFilePath, s => CheckFolder(s, Origin), loader, makeEmpty); m_domainFiles.Load(domainIds); } m_conversationDataSource = new ConversationDataSource(m_domainFiles.Select(df => df.Data)); if (m_conversationDataSource.DomainErrors.Any()) { MessageBox.Show(string.Join("\n", m_conversationDataSource.DomainErrors.Select(error => error.Message))); } { m_localizer = new LocalizationEngine(GetFilePath, data.LocalizationSets, context, UsedLocalizations, ShouldContract, pathOk, s => CheckFolder(s, Origin), backend, Origin); m_localizer.Localizers.Load(localizerIds); context.CurrentLocalization.Value = m_localizer.LocalizationSets.FirstOrDefault() ?? Project.TData.LocalizerSetData.Empty; m_localizer.LocalizationSetsChanged += () => { m_file.Change(); }; } { GenerateAudio audio = (c) => { return(m_audioProvider.Generate(new AudioGenerationParameters(c.File.File, this.File.File))); }; //This can be called from multiple threads simultaneously and in arbitrary orders by design of //ConversationDataSource and the underlying ConstantTypeSet and DynamicEnumParameter.Source Func <IDynamicEnumParameter, object, DynamicEnumParameter.Source> getSource = (localEnum, newSourceID) => { return(m_conversationDataSource.GetSource(localEnum.TypeId, newSourceID)); }; Func <IEnumerable <Tuple <Id <FileInProject>, DocumentPath> >, IEnumerable <IConversationFile> > loadConversations = files => { ISerializerDeserializer <XmlGraphData <NodeUIData, ConversationEditorData> > conversationSerializerDeserializer = m_conversationSerializerFactory(m_conversationDataSource); // _/ _/ _/ X _/ X _/ //return files.Select(file => ConversationFile.Load(file, ConversationNodeFactory, conversationSerializerDeserializer, audio, getSource, m_audioProvider, backend)); //TODO: AUDIO: This is ok as long as we're not using audio parameters at all return(ParallelEnumerable.Select(files.AsParallel(), file => ConversationFile.Load(file.Item1, file.Item2, ConversationNodeFactory, conversationSerializerDeserializer, audio, getSource, m_audioProvider, backend))); }; Func <DirectoryInfo, ConversationFile> makeEmpty = path => ConversationFile.CreateEmpty(path, this, ConversationNodeFactory, audio, getSource, m_audioProvider, backend, Origin); m_conversations = new ProjectElementList <IConversationFile>(GetFilePath, s => CheckFolder(s, Origin), loadConversations, makeEmpty); m_conversations.Load(conversationIds); } RefreshCallbacks(m_conversations, m_actionsOnRefreshCallbacksConversations); RefreshCallbacks(m_audioProvider.AudioFiles, m_actionsOnRefreshCallbacksAudios); RefreshCallbacks(m_localizer.Localizers, m_actionsOnRefreshCallbacksLocalizations); RefreshCallbacks(m_domainFiles, m_actionsOnRefreshCallbacksDomains); m_conversations.GotChanged += GotChanged; m_audioProvider.AudioFiles.GotChanged += GotChanged; m_localizer.Localizers.GotChanged += GotChanged; m_domainFiles.GotChanged += GotChanged; m_conversations.GotChanged += () => RefreshCallbacks(m_conversations, m_actionsOnRefreshCallbacksConversations); m_audioProvider.AudioFiles.GotChanged += () => RefreshCallbacks(m_audioProvider.AudioFiles, m_actionsOnRefreshCallbacksAudios); m_localizer.Localizers.GotChanged += () => RefreshCallbacks(m_localizer.Localizers, m_actionsOnRefreshCallbacksLocalizations); m_domainFiles.GotChanged += () => RefreshCallbacks(m_domainFiles, m_actionsOnRefreshCallbacksDomains); m_domainFiles.GotChanged += ConversationDatasourceModified; Action <IDomainFile> MaybeConversationDatasourceModified = file => { if (!file.File.Changed()) { ConversationDatasourceModified(); } }; m_domainFiles.Added += argument => { argument.File.SaveStateChanged += () => MaybeConversationDatasourceModified(argument); }; m_domainFiles.Reloaded += (_, argument) => { argument.File.SaveStateChanged += () => MaybeConversationDatasourceModified(argument); }; m_domainFiles.ForAll(d => d.File.SaveStateChanged += () => MaybeConversationDatasourceModified(d)); //m_domainFiles.Added += argument => { argument.File.SaveStateChanged += () => { if (!argument.File.Changed) ReloadDatasource(); }; }; //m_domainFiles.Reloaded += (from, to) => { to.File.SaveStateChanged += () => { if (!to.File.Changed) ReloadDatasource(); }; }; //m_domainFiles.ForAll(d => d.File.SaveStateChanged += () => { if (!d.File.Changed) ReloadDatasource(); }); m_domainFiles.Added += file => m_filePaths[file.Id] = DocumentPath.FromAbsolutePath(file.File.File.FullName, Origin); m_conversations.Added += file => m_filePaths[file.Id] = DocumentPath.FromAbsolutePath(file.File.File.FullName, Origin); m_audioProvider.AudioFiles.Added += file => m_filePaths[file.Id] = DocumentPath.FromAbsolutePath(file.File.File.FullName, Origin); m_localizer.Localizers.Added += file => m_filePaths[file.Id] = DocumentPath.FromAbsolutePath(file.File.File.FullName, Origin); m_domainFiles.Removed += file => m_filePaths.Remove(file.Id); m_conversations.Removed += file => m_filePaths.Remove(file.Id); m_audioProvider.AudioFiles.Removed += file => m_filePaths.Remove(file.Id); m_localizer.Localizers.Removed += file => m_filePaths.Remove(file.Id); //Files being moved is setup in RefreshCallbacks() DomainUsage = new DomainUsage(this); m_audioProvider.UpdateUsage(); } }