/// <summary> /// Determines whether this instance is ever true during a specified time period. /// </summary> public Tbool IsEverTrue(Tdate start, Tdate end) { Tbool equalsVal = this == true; Tbool isDuringInterval = Time.IsBetween(start, end); Tbool isOverlap = equalsVal & isDuringInterval; return(isOverlap.IsEverTrue()); }
/// <summary> /// Returns a Tvar when its associated Tbool is true. /// </summary> /// <remarks> /// Similar in principle to a C# switch statement, just temporal. /// Sample usage: Switch(Tbool1, Tvar1, Tbool2, Tvar2, ..., defaultTvar). /// Returns Tvar1 if Tbool2 is true, else Tvar2 if Tbool2 is true, etc., else defaultTvar. /// </remarks> public static T Switch <T>(params Func <Tvar>[] arguments) where T : Tvar { // Default result Hval h = new Hval(null, Hstate.Null); T result = (T)Util.ReturnProperTvar <T>(h); // Analyze each condition-value pair...and keep going // until all intervals of the result Tvar are defined... int len = (int)arguments.Length; for (int arg = 0; arg < len - 1; arg += 2) { // Get value of the condition Tbool newCondition = Util.ConvertToTvar <Tbool>(arguments[arg].Invoke()); // Identify the intervals when the new condition is neither false nor true // Falsehood causes it to fall through to next condition. Truth causes the // result to assume the value during that interval. Tbool newConditionIsUnknown = Util.HasUnknownState(newCondition); // Merge these 'unknown' intervals in new condition into the result. result = Util.MergeTvars <T>(result, Util.ConditionalAssignment <T>(newConditionIsUnknown, newCondition)); // Identify the intervals when the new condition is true. // Ignore irrelevant periods when result is already determined. // During these intervals, "result" takes on the value of its conclusion. Tbool newConditionIsTrueAndResultIsNull = newCondition && Util.IsNull(result); // If new true segments are found, accumulate the values during those intervals if (newConditionIsTrueAndResultIsNull.IsEverTrue()) { T val = (T)Util.ConvertToTvar <T>(arguments[arg + 1].Invoke()); result = Util.MergeTvars <T>(result, Util.ConditionalAssignment <T>(newConditionIsTrueAndResultIsNull, val)); } if (!Util.HasUndefinedIntervals(result)) { return(result.LeanTvar <T>()); } } T defaultVal = (T)Util.ConvertToTvar <T>(arguments[len - 1].Invoke()); result = Util.MergeTvars <T>(result, defaultVal); return(result.LeanTvar <T>()); }