public double Sample(out IntervalSampler childSampler) { // TODO: Implement divide & conquer for speedup double sample = MyUtils.GetRandomDouble(0.0, TotalWeight); double lastLimit = m_min; double lastWeight = 0.0; for (int i = 0; i < m_entries.Count; ++i) { if (m_entries[i].CumulativeWeight >= sample) { childSampler = m_entries[i].Sampler; double weightRange = m_entries[i].CumulativeWeight - lastWeight; double t = (sample - lastWeight) / weightRange; return(t * m_entries[i].UpperLimit + (1.0 - t) * lastLimit); } lastLimit = m_entries[i].UpperLimit; lastWeight = m_entries[i].CumulativeWeight; } System.Diagnostics.Debug.Assert(false, "Shouldn't get here!"); childSampler = null; return(m_max); }
public SamplingEntry(double limit, IntervalSampler sampler, double weight) { UpperLimit = limit; Sampler = sampler; CumulativeWeight = weight; Full = false; }
public Vector3D Sample() { Vector3D output; IntervalSampler sampler = m_sampler; output.X = sampler.Sample(out sampler); if (sampler != null) { output.Y = sampler.Sample(out sampler); } else { output.Y = MyUtils.GetRandomDouble(m_bBox.Min.Y, m_bBox.Max.Y); } if (sampler != null) { output.Z = sampler.Sample(out sampler); } else { output.Z = MyUtils.GetRandomDouble(m_bBox.Min.Z, m_bBox.Max.Z); } System.Diagnostics.Debug.Assert(sampler == null, "Inconsistency in MyBBSetSampler"); return(output); }
public MyBBSetSampler(Vector3D min, Vector3D max) { Vector3D newMax = Vector3D.Max(min, max); Vector3D newMin = Vector3D.Min(min, max); m_bBox = new BoundingBoxD(newMin, newMax); m_sampler = new IntervalSampler(newMin.X, newMax.X, (newMax.Y - newMin.Y) * (newMax.Z - newMin.Z), Base6Directions.Axis.LeftRight); }
public SamplingEntry(SamplingEntry other) { UpperLimit = other.UpperLimit; CumulativeWeight = other.CumulativeWeight; Full = other.Full; if (other.Sampler == null) { Sampler = null; } else { Sampler = new IntervalSampler(other.Sampler, 1.0, clone: true); } }
private IntervalSampler(IntervalSampler other, double t, bool clone) { m_min = other.m_min; m_max = other.m_max; m_axis = other.m_axis; m_weightMult = other.m_weightMult; m_totalWeight = other.m_totalWeight; m_entries = new List <SamplingEntry>(other.m_entries); for (int i = 0; i < other.m_entries.Count; ++i) { m_entries[i] = new SamplingEntry(other.m_entries[i]); } Multiply(t); // If we are not cloning, we are splitting, so we have to multiply the remnant as well if (!clone) { other.Multiply(1.0 - t); } }
public static async Task <List <ListedItem> > ListEntries( StorageFolder rootFolder, StorageFolderWithPath currentStorageFolder, string returnformat, Type sourcePageType, CancellationToken cancellationToken, int countLimit, Func <List <ListedItem>, Task> intermediateAction ) { var sampler = new IntervalSampler(500); var tempList = new List <ListedItem>(); uint count = 0; var firstRound = true; while (true) { IReadOnlyList <IStorageItem> items; uint maxItemsToRetrieve = 300; if (intermediateAction == null) { // without intermediate action increase batches significantly maxItemsToRetrieve = 1000; } else if (firstRound) { maxItemsToRetrieve = 32; firstRound = false; } try { items = await rootFolder.GetItemsAsync(count, maxItemsToRetrieve); if (items == null || items.Count == 0) { break; } } catch (NotImplementedException) { break; } catch (Exception ex) when( ex is UnauthorizedAccessException || ex is FileNotFoundException || (uint)ex.HResult == 0x80070490) // ERROR_NOT_FOUND { // If some unexpected exception is thrown - enumerate this folder file by file - just to be sure items = await EnumerateFileByFile(rootFolder, count, maxItemsToRetrieve); } foreach (var item in items) { if (item.IsOfType(StorageItemTypes.Folder)) { var folder = await AddFolderAsync(item as StorageFolder, currentStorageFolder, returnformat, cancellationToken); if (folder != null) { tempList.Add(folder); } } else { var file = item as StorageFile; var fileEntry = await AddFileAsync(file, currentStorageFolder, returnformat, true, sourcePageType, cancellationToken); if (fileEntry != null) { tempList.Add(fileEntry); } } if (cancellationToken.IsCancellationRequested) { break; } } count += maxItemsToRetrieve; if (countLimit > -1 && count >= countLimit) { break; } if (intermediateAction != null && (items.Count == maxItemsToRetrieve || sampler.CheckNow())) { await intermediateAction(tempList); // clear the temporary list every time we do an intermediate action tempList.Clear(); } } return(tempList); }
public static async Task <List <ListedItem> > ListEntries( string path, string returnformat, IntPtr hFile, WIN32_FIND_DATA findData, AppServiceConnection connection, CancellationToken cancellationToken, List <string> skipItems, int countLimit, Func <List <ListedItem>, Task> intermediateAction ) { var sampler = new IntervalSampler(500); var tempList = new List <ListedItem>(); var hasNextFile = false; var count = 0; do { if (((FileAttributes)findData.dwFileAttributes & FileAttributes.System) != FileAttributes.System || !App.AppSettings.AreSystemItemsHidden) { if (((FileAttributes)findData.dwFileAttributes & FileAttributes.Hidden) != FileAttributes.Hidden || App.AppSettings.AreHiddenItemsVisible) { if (((FileAttributes)findData.dwFileAttributes & FileAttributes.Directory) != FileAttributes.Directory) { var file = await GetFile(findData, path, returnformat, connection, cancellationToken); if (file != null) { if (skipItems?.Contains(file.ItemPath) ?? false) { skipItems.Remove(file.ItemPath); } else { tempList.Add(file); } ++count; } } else if (((FileAttributes)findData.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory) { if (findData.cFileName != "." && findData.cFileName != "..") { var folder = await GetFolder(findData, path, returnformat, cancellationToken); if (folder != null) { if (skipItems?.Contains(folder.ItemPath) ?? false) { skipItems.Remove(folder.ItemPath); } else { tempList.Add(folder); } ++count; } } } } } if (cancellationToken.IsCancellationRequested || count == countLimit) { break; } hasNextFile = FindNextFile(hFile, out findData); if (intermediateAction != null && (count == 32 || sampler.CheckNow())) { await intermediateAction(tempList); // clear the temporary list every time we do an intermediate action tempList.Clear(); } } while (hasNextFile); FindClose(hFile); return(tempList); }
public static async Task <List <ListedItem> > ListEntries( string path, string returnformat, IntPtr hFile, WIN32_FIND_DATA findData, NamedPipeAsAppServiceConnection connection, CancellationToken cancellationToken, int countLimit, Func <List <ListedItem>, Task> intermediateAction, Dictionary <string, BitmapImage> defaultIconPairs = null ) { var sampler = new IntervalSampler(500); var tempList = new List <ListedItem>(); var hasNextFile = false; var count = 0; IUserSettingsService userSettingsService = Ioc.Default.GetService <IUserSettingsService>(); bool showFolderSize = userSettingsService.PreferencesSettingsService.ShowFolderSize; do { var isSystem = ((FileAttributes)findData.dwFileAttributes & FileAttributes.System) == FileAttributes.System; var isHidden = ((FileAttributes)findData.dwFileAttributes & FileAttributes.Hidden) == FileAttributes.Hidden; var startWithDot = findData.cFileName.StartsWith("."); if ((!isHidden || (userSettingsService.PreferencesSettingsService.AreHiddenItemsVisible && (!isSystem || !userSettingsService.PreferencesSettingsService.AreSystemItemsHidden))) && (!startWithDot || userSettingsService.PreferencesSettingsService.ShowDotFiles)) { if (((FileAttributes)findData.dwFileAttributes & FileAttributes.Directory) != FileAttributes.Directory) { var file = await GetFile(findData, path, returnformat, connection, cancellationToken); if (file != null) { if (defaultIconPairs != null) { if (!string.IsNullOrEmpty(file.FileExtension)) { var lowercaseExtension = file.FileExtension.ToLowerInvariant(); if (defaultIconPairs.ContainsKey(lowercaseExtension)) { file.SetDefaultIcon(defaultIconPairs[lowercaseExtension]); } } } tempList.Add(file); ++count; } } else if (((FileAttributes)findData.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory) { if (findData.cFileName != "." && findData.cFileName != "..") { var folder = await GetFolder(findData, path, returnformat, cancellationToken); if (folder != null) { if (defaultIconPairs?.ContainsKey(string.Empty) ?? false) { // Set folder icon (found by empty extension string) folder.SetDefaultIcon(defaultIconPairs[string.Empty]); } tempList.Add(folder); ++count; if (showFolderSize) { folderSizeProvider.UpdateFolder(folder, cancellationToken); } } } } } if (cancellationToken.IsCancellationRequested || count == countLimit) { break; } hasNextFile = FindNextFile(hFile, out findData); if (intermediateAction != null && (count == 32 || sampler.CheckNow())) { await intermediateAction(tempList); // clear the temporary list every time we do an intermediate action tempList.Clear(); } } while (hasNextFile); FindClose(hFile); return(tempList); }
public static async Task <List <ListedItem> > ListEntries( BaseStorageFolder rootFolder, StorageFolderWithPath currentStorageFolder, CancellationToken cancellationToken, int countLimit, Func <List <ListedItem>, Task> intermediateAction, Dictionary <string, BitmapImage> defaultIconPairs = null ) { var sampler = new IntervalSampler(500); var tempList = new List <ListedItem>(); uint count = 0; var firstRound = true; IUserSettingsService userSettingsService = Ioc.Default.GetService <IUserSettingsService>(); while (true) { IReadOnlyList <IStorageItem> items; uint maxItemsToRetrieve = 300; if (intermediateAction == null) { // without intermediate action increase batches significantly maxItemsToRetrieve = 1000; } else if (firstRound) { maxItemsToRetrieve = 32; firstRound = false; } try { items = await rootFolder.GetItemsAsync(count, maxItemsToRetrieve); if (items == null || items.Count == 0) { break; } } catch (NotImplementedException) { break; } catch (Exception ex) when( ex is UnauthorizedAccessException || ex is FileNotFoundException || (uint)ex.HResult == 0x80070490) // ERROR_NOT_FOUND { // If some unexpected exception is thrown - enumerate this folder file by file - just to be sure items = await EnumerateFileByFile(rootFolder, count, maxItemsToRetrieve); } foreach (var item in items) { var startWithDot = item.Name.StartsWith("."); if (!startWithDot || userSettingsService.PreferencesSettingsService.ShowDotFiles) { if (item.IsOfType(StorageItemTypes.Folder)) { var folder = await AddFolderAsync(item.AsBaseStorageFolder(), currentStorageFolder, cancellationToken); if (folder != null) { if (defaultIconPairs?.ContainsKey(string.Empty) ?? false) { folder.SetDefaultIcon(defaultIconPairs[string.Empty]); } tempList.Add(folder); } } else { var fileEntry = await AddFileAsync(item.AsBaseStorageFile(), currentStorageFolder, cancellationToken); if (fileEntry != null) { if (defaultIconPairs != null) { if (!string.IsNullOrEmpty(fileEntry.FileExtension)) { var lowercaseExtension = fileEntry.FileExtension.ToLowerInvariant(); if (defaultIconPairs.ContainsKey(lowercaseExtension)) { fileEntry.SetDefaultIcon(defaultIconPairs[lowercaseExtension]); } } } tempList.Add(fileEntry); } } } if (cancellationToken.IsCancellationRequested) { break; } } count += maxItemsToRetrieve; if (countLimit > -1 && count >= countLimit) { break; } if (intermediateAction != null && (items.Count == maxItemsToRetrieve || sampler.CheckNow())) { await intermediateAction(tempList); // clear the temporary list every time we do an intermediate action tempList.Clear(); } } return(tempList); }
public double Sample(out IntervalSampler childSampler) { // TODO: Implement divide & conquer for speedup double sample = MyUtils.GetRandomDouble(0.0, TotalWeight); double lastLimit = m_min; double lastWeight = 0.0; for (int i = 0; i < m_entries.Count; ++i) { if (m_entries[i].CumulativeWeight >= sample) { childSampler = m_entries[i].Sampler; double weightRange = m_entries[i].CumulativeWeight - lastWeight; double t = (sample - lastWeight) / weightRange; return t * m_entries[i].UpperLimit + (1.0 - t) * lastLimit; } lastLimit = m_entries[i].UpperLimit; lastWeight = m_entries[i].CumulativeWeight; } System.Diagnostics.Debug.Assert(false, "Shouldn't get here!"); childSampler = null; return m_max; }
private IntervalSampler(IntervalSampler other, double t, bool clone) { m_min = other.m_min; m_max = other.m_max; m_axis = other.m_axis; m_weightMult = other.m_weightMult; m_totalWeight = other.m_totalWeight; m_entries = new List<SamplingEntry>(other.m_entries); for (int i = 0; i < other.m_entries.Count; ++i) { m_entries[i] = new SamplingEntry(other.m_entries[i]); } Multiply(t); // If we are not cloning, we are splitting, so we have to multiply the remnant as well if (!clone) { other.Multiply(1.0 - t); } }
public static async Task <List <ListedItem> > ListEntries( string path, string returnformat, IntPtr hFile, WIN32_FIND_DATA findData, NamedPipeAsAppServiceConnection connection, CancellationToken cancellationToken, int countLimit, Func <List <ListedItem>, Task> intermediateAction ) { var sampler = new IntervalSampler(500); var tempList = new List <ListedItem>(); var hasNextFile = false; var count = 0; IUserSettingsService userSettingsService = Ioc.Default.GetService <IUserSettingsService>(); do { var isSystem = ((FileAttributes)findData.dwFileAttributes & FileAttributes.System) == FileAttributes.System; var isHidden = ((FileAttributes)findData.dwFileAttributes & FileAttributes.Hidden) == FileAttributes.Hidden; if (!isHidden || (userSettingsService.FilesAndFoldersSettingsService.AreHiddenItemsVisible && (!isSystem || !userSettingsService.FilesAndFoldersSettingsService.AreSystemItemsHidden))) { if (((FileAttributes)findData.dwFileAttributes & FileAttributes.Directory) != FileAttributes.Directory) { var file = await GetFile(findData, path, returnformat, connection, cancellationToken); if (file != null) { tempList.Add(file); ++count; } } else if (((FileAttributes)findData.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory) { if (findData.cFileName != "." && findData.cFileName != "..") { var folder = await GetFolder(findData, path, returnformat, cancellationToken); if (folder != null) { tempList.Add(folder); ++count; } } } } if (cancellationToken.IsCancellationRequested || count == countLimit) { break; } hasNextFile = FindNextFile(hFile, out findData); if (intermediateAction != null && (count == 32 || sampler.CheckNow())) { await intermediateAction(tempList); // clear the temporary list every time we do an intermediate action tempList.Clear(); } } while (hasNextFile); FindClose(hFile); return(tempList); }