namespace View_by_Distance.Property.Models.Stateless; public static class A_Property { public static string DateTimeFormat() => "yyyy:MM:dd HH:mm:ss"; public static (int Season, string seasonName) GetSeason(int dayOfYear) { (int Season, string seasonName) result = dayOfYear switch { < 78 => new(0, "Winter"), < 171 => new(1, "Spring"), < 264 => new(2, "Summer"), < 354 => new(3, "Fall"), _ => new(4, "Winter") }; return result; } public static List Get(Models.Configuration configuration, PropertyLogic propertyLogic) { List results; long ticks = DateTime.Now.Ticks; List exceptionsDirectories = new(); results = Container.GetContainers(configuration, propertyLogic); propertyLogic.ParallelWork(ticks, results, firstPass: false); if (exceptionsDirectories.Any()) throw new Exception(); return results; } public static int GetDeterministicHashCode(byte[] value) { int result; unchecked { int hash1 = (5381 << 16) + 5381; int hash2 = hash1; for (int i = 0; i < value.Length; i += 2) { hash1 = ((hash1 << 5) + hash1) ^ value[i]; if (i == value.Length - 1) break; hash2 = ((hash2 << 5) + hash2) ^ value[i + 1]; } result = hash1 + (hash2 * 1566083941); } return result; } public static int GetDeterministicHashCode(string value) { int result; unchecked { int hash1 = (5381 << 16) + 5381; int hash2 = hash1; for (int i = 0; i < value.Length; i += 2) { hash1 = ((hash1 << 5) + hash1) ^ value[i]; if (i == value.Length - 1) break; hash2 = ((hash2 << 5) + hash2) ^ value[i + 1]; } result = hash1 + (hash2 * 1566083941); } return result; } public static (bool?, string[]) IsWrongYear(string[] segments, string year) { bool? result; string[] results = ( from l in segments where l?.Length > 2 && ( l[..2] is "19" or "20" || (l.Length == 5 && l.Substring(1, 2) is "19" or "20" && (l[0] is '~' or '=' or '-' or '^' or '#')) || (l.Length == 6 && l[..2] is "19" or "20" && l[4] == '.') || (l.Length == 7 && l.Substring(1, 2) is "19" or "20" && l[5] == '.') ) select l ).ToArray(); string[] matches = ( from l in results where l == year || (l.Length == 5 && l.Substring(1, 4) == year && (l[0] is '~' or '=' or '-' or '^' or '#')) || (l.Length == 6 && l[..4] == year && l[4] == '.') || (l.Length == 7 && l.Substring(1, 4) == year && l[5] == '.') select l ).ToArray(); if (!results.Any()) result = null; else result = !matches.Any(); return new(result, results); } public static List GetDateTimes(DateTime creationTime, DateTime lastWriteTime, DateTime? dateTime, DateTime? dateTimeDigitized, DateTime? dateTimeOriginal, DateTime? gpsDateStamp) { List results = new() { creationTime, lastWriteTime }; if (dateTime.HasValue) results.Add(dateTime.Value); if (dateTimeDigitized.HasValue) results.Add(dateTimeDigitized.Value); if (dateTimeOriginal.HasValue) results.Add(dateTimeOriginal.Value); if (gpsDateStamp.HasValue) results.Add(gpsDateStamp.Value); return results; } public static DateTime GetDateTime(Models.A_Property? property) { DateTime result; if (property is null) result = DateTime.MinValue; else { List dateTimes = new() { property.CreationTime, property.LastWriteTime }; if (property.DateTime.HasValue) dateTimes.Add(property.DateTime.Value); if (property.DateTimeDigitized.HasValue) dateTimes.Add(property.DateTimeDigitized.Value); if (property.DateTimeOriginal.HasValue) dateTimes.Add(property.DateTimeOriginal.Value); if (property.GPSDateStamp.HasValue) dateTimes.Add(property.GPSDateStamp.Value); result = dateTimes.Min(); } return result; } public static List GetDateTimes(Models.A_Property property) => GetDateTimes(property.CreationTime, property.LastWriteTime, property.DateTime, property.DateTimeDigitized, property.DateTimeOriginal, property.GPSDateStamp); public static DateTime GetMinimumDateTime(Models.A_Property? property) { DateTime result; List dateTimes; if (property is null) result = DateTime.MinValue; else { dateTimes = GetDateTimes(property); result = dateTimes.Min(); } return result; } public static string GetDiffRootDirectory(string diffPropertyDirectory) { string result = string.Empty; string results = " - Results"; string? checkDirectory = diffPropertyDirectory; for (int i = 0; i < int.MaxValue; i++) { checkDirectory = Path.GetDirectoryName(checkDirectory); if (string.IsNullOrEmpty(checkDirectory)) break; if (checkDirectory.EndsWith(results)) { result = checkDirectory[..^results.Length]; break; } } return result; } public static double GetStandardDeviation(IEnumerable values, double average) { double result = 0; if (!values.Any()) throw new Exception("Collection must have at least one value!"); double sum = values.Sum(l => (l - average) * (l - average)); result = Math.Sqrt(sum / values.Count()); return result; } public static TimeSpan GetThreeStandardDeviationHigh(int minimum, Models.Container container) { TimeSpan result; DateTime? minimumDateTime; List ticksCollection = new(); foreach (Item item in container.Items) { if (item.Property is null) continue; minimumDateTime = GetMinimumDateTime(item.Property); if (minimumDateTime is null) continue; ticksCollection.Add(minimumDateTime.Value.Ticks); } long threeStandardDeviationHigh; long min; if (!ticksCollection.Any()) min = 0; else min = ticksCollection.Min(); if (ticksCollection.Count < minimum) threeStandardDeviationHigh = long.MaxValue; else { ticksCollection = (from l in ticksCollection select l - min).ToList(); double sum = ticksCollection.Sum(); double average = sum / ticksCollection.Count; double standardDeviation = GetStandardDeviation(ticksCollection, average); threeStandardDeviationHigh = (long)Math.Ceiling(average + min + (standardDeviation * 3)); } result = new TimeSpan(threeStandardDeviationHigh - min); return result; } public static (int, List, List) Get(Models.Container container, TimeSpan threeStandardDeviationHigh, int i) { List results = new(); int j = i; Item item; long? ticks; Item nextItem; TimeSpan timeSpan; DateTime? minimumDateTime; DateTime? nextMinimumDateTime; List dateTimes = new(); for (; j < container.Items.Count; j++) { ticks = null; item = container.Items[j]; if (item.Property is null) continue; minimumDateTime = GetMinimumDateTime(item.Property); if (minimumDateTime is null) continue; for (int k = j + 1; k < container.Items.Count; k++) { nextItem = container.Items[k]; if (nextItem.Property is null) continue; nextMinimumDateTime = GetMinimumDateTime(nextItem.Property); if (nextMinimumDateTime is null) continue; ticks = nextMinimumDateTime.Value.Ticks; break; } results.Add(item); dateTimes.Add(minimumDateTime.Value); if (ticks.HasValue) { timeSpan = new(ticks.Value - minimumDateTime.Value.Ticks); if (timeSpan > threeStandardDeviationHigh) break; } } return new(j, dateTimes, results); } public static bool Any(List propertyHolderCollections) { bool result = false; foreach (Models.Container container in propertyHolderCollections) { if (!container.Items.Any()) continue; if ((from l in container.Items where l.Any() select true).Any()) { result = true; break; } } return result; } }