Content-Length: 681619 | pFad | http://github.com/nuscien/trivial/commit/83ca1d65944050e07d7fd49f11e31fcad7a52892

68 Update maths library. · nuscien/trivial@83ca1d6 · GitHub
Skip to content

Commit

Permalink
Update maths library.
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcean committed Nov 12, 2024
1 parent d030fbe commit 83ca1d6
Show file tree
Hide file tree
Showing 34 changed files with 3,550 additions and 796 deletions.
73 changes: 73 additions & 0 deletions Core/Collection/List/ListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,43 @@ public static int IndexOf<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TVal
return i;
}

//github.com/ <summary>
//github.com/ Returns the first element of a sequence, or a null if the sequence contains no elements.
//github.com/ </summary>
//github.com/ <typeparam name="T">The type of the elements of source.</typeparam>
//github.com/ <param name="col">The collection to return the first element of.</param>
//github.com/ <returns>The first element in source; or null, if empty.</returns>
public static T? FirstOrNull<T>(IEnumerable<T> col) where T : struct
{
if (col is null) return null;
foreach (var item in col)
{
return item;
}

return null;
}

//github.com/ <summary>
//github.com/ Returns the last element of a sequence, or a null if the sequence contains no elements.
//github.com/ </summary>
//github.com/ <typeparam name="T">The type of the elements of source.</typeparam>
//github.com/ <param name="col">The collection to return the last element of.</param>
//github.com/ <returns>The last element in source; or null, if empty.</returns>
public static T? LastOrNull<T>(IEnumerable<T> col) where T : struct
{
if (col is null) return null;
if (col is ICollection<T> col2) return col2.Count > 0 ? col2.Last() : null;
if (col is T[] col3) return col3.Length > 0 ? col3.Last() : null;
col = col.Reverse();
foreach (var item in col)
{
return item;
}

return null;
}

//github.com/ <summary>
//github.com/ Searches for the specified object and returns the zero-based index array of the all occurrence within the entire key value pairs.
//github.com/ </summary>
Expand Down Expand Up @@ -1315,4 +1352,40 @@ public static IEnumerable<ServerSentEventInfo> On(this IEnumerable<ServerSentEve
yield return item;
}
}

internal static IEnumerable<TResult> Select<TItem, TResult>(IEnumerable<TItem> leftValue, IEnumerable<TItem> rightValue, TItem padding, Func<TItem, bool, TItem, bool, int, TResult> callback)
{
var a = leftValue.GetEnumerator();
var b = rightValue.GetEnumerator();
var hasA = true;
var hasB = true;
var i = -1;
while (true)
{
i++;
var vA = padding;
if (hasA)
{
hasA = a.MoveNext();
if (hasA) vA = a.Current;
}
else
{
if (!hasB) yield break;
}

var vB = padding;
if (hasB)
{
hasB = b.MoveNext();
if (hasB) vB = b.Current;
}
else
{
if (!hasA) yield break;
}

yield return callback(vA, hasA, vB, hasB, i);
}
}
}
2 changes: 1 addition & 1 deletion Core/Collection/Search/QueryPredication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public QueryPredication(IQueryable<T> source, QueryData q)
{
Data = source;
OriginalSource = source;
Q = q ?? new QueryData();
Q = q ?? new();
}

//github.com/ <summary>
Expand Down
61 changes: 22 additions & 39 deletions Core/Data/Query/SimpleCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,9 @@ public abstract class SimpleCondition : ISimpleCondition
//github.com/ <param name="value">A simple interval instance.</param>
//github.com/ <returns>A comparing operator.</returns>
public static DbCompareOperator GetLeftOperator<T>(ISimpleInterval<T> value)
{
if (value == null)
throw new ArgumentNullException("value");
return value.LeftOpen ? DbCompareOperator.Greater : DbCompareOperator.GreaterOrEqual;
}
=> value == null
? throw new ArgumentNullException(nameof(value))
: (value.LeftOpen ? DbCompareOperator.Greater : DbCompareOperator.GreaterOrEqual);

//github.com/ <summary>
//github.com/ Gets a right comparing operator from a simple interval.
Expand All @@ -151,54 +149,46 @@ public static DbCompareOperator GetLeftOperator<T>(ISimpleInterval<T> value)
//github.com/ <param name="value">A simple interval instance.</param>
//github.com/ <returns>A comparing operator.</returns>
public static DbCompareOperator GetRightOperator<T>(ISimpleInterval<T> value)
{
if (value == null)
throw new ArgumentNullException("value");
return value.RightOpen ? DbCompareOperator.Less : DbCompareOperator.LessOrEqual;
}
=> value == null
? throw new ArgumentNullException(nameof(value))
: (value.RightOpen ? DbCompareOperator.Less : DbCompareOperator.LessOrEqual);

//github.com/ <summary>
//github.com/ Gets the list about valid comparing operator for null or boolean value.
//github.com/ </summary>
public static ICollection<DbCompareOperator> GetBasicValidOperators()
{
return _validNullValueOp ?? (_validNullValueOp = new List<DbCompareOperator>
=> _validNullValueOp ??= new List<DbCompareOperator>
{
DbCompareOperator.Equal,
DbCompareOperator.NotEqual
});
}
};

//github.com/ <summary>
//github.com/ Gets the list about valid comparing operator for literal value.
//github.com/ </summary>
public static ICollection<DbCompareOperator> GetLiteralValidOperators()
{
return _validLiteralOp ?? (_validLiteralOp = new List<DbCompareOperator>
=> _validLiteralOp ??= new List<DbCompareOperator>
{
DbCompareOperator.Equal,
DbCompareOperator.NotEqual,
DbCompareOperator.Contains,
DbCompareOperator.EndsWith,
DbCompareOperator.StartsWith
});
}
};

//github.com/ <summary>
//github.com/ Gets the list about valid comparing operator for comparable value.
//github.com/ </summary>
public static ICollection<DbCompareOperator> GetComparableValidOperators()
{
return _validComparableOp ?? (_validComparableOp = new List<DbCompareOperator>
=> _validComparableOp ??= new List<DbCompareOperator>
{
DbCompareOperator.Equal,
DbCompareOperator.NotEqual,
DbCompareOperator.Greater,
DbCompareOperator.Less,
DbCompareOperator.GreaterOrEqual,
DbCompareOperator.LessOrEqual
});
}
};

//github.com/ <summary>
//github.com/ Converts operation string.
Expand All @@ -207,23 +197,16 @@ public static ICollection<DbCompareOperator> GetComparableValidOperators()
//github.com/ <returns>A string that represents the operation.</returns>
public static string ToString(DbCompareOperator op)
{
switch (op)
return op switch
{
case DbCompareOperator.Equal:
return BooleanSymbols.EqualSign;
case DbCompareOperator.Greater:
return BooleanSymbols.GreaterSign;
case DbCompareOperator.GreaterOrEqual:
return BooleanSymbols.GreaterOrEqualSign;
case DbCompareOperator.Less:
return BooleanSymbols.LessSign;
case DbCompareOperator.LessOrEqual:
return BooleanSymbols.LessOrEqualSign;
case DbCompareOperator.NotEqual:
return BooleanSymbols.NotEqualSign;
default:
return op.ToString();
}
DbCompareOperator.Equal => BooleanOperations.EqualSign,
DbCompareOperator.Greater => BooleanOperations.GreaterSign,
DbCompareOperator.GreaterOrEqual => BooleanOperations.GreaterOrEqualSign,
DbCompareOperator.Less => BooleanOperations.LessSign,
DbCompareOperator.LessOrEqual => BooleanOperations.LessOrEqualSign,
DbCompareOperator.NotEqual => BooleanOperations.NotEqualSign,
_ => op.ToString(),
};
}

//github.com/ <summary>
Expand All @@ -232,7 +215,7 @@ public static string ToString(DbCompareOperator op)
//github.com/ <returns>A string that represents the current object.</returns>
public override string ToString()
{
return SimpleCondition.ToString(Operator) + " " + (Value != null ? Value.ToString() : "null");
return ToString(Operator) + " " + (Value != null ? Value.ToString() : "null");
}
}

Expand Down
116 changes: 102 additions & 14 deletions Core/Data/Result/ConciseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Xml.Linq;
using Trivial.Collection;
using Trivial.Reflection;
using Trivial.Text;
Expand Down Expand Up @@ -136,7 +137,7 @@ public ConciseModel(Guid id, string title, IEnumerable<string> keywords, string
//github.com/ <param name="copy">The model to copy.</param>
public ConciseModel(IConciseModel copy)
{
if (copy == null) return;
if (copy is null) return;
Id = copy.Id;
Title = copy.Title;
Link = copy.Link;
Expand All @@ -145,7 +146,6 @@ public ConciseModel(IConciseModel copy)
var keywords = copy.Keywords;
if (keywords != null) Keywords = [.. keywords];
if (copy is not ConciseModel model) return;
Raw = model.Raw;
Tag = model.Tag;
}

Expand All @@ -160,6 +160,12 @@ public string Id
set => SetCurrentProperty(value);
}

//github.com/ <summary>
//github.com/ Tests if the name is not null, empty nor consists only of white-space characters.
//github.com/ </summary>
[JsonIgnore]
public bool HasId => !string.IsNullOrWhiteSpace(Id);

//github.com/ <summary>
//github.com/ Gets or sets the title.
//github.com/ </summary>
Expand All @@ -171,6 +177,12 @@ public string Title
set => SetCurrentProperty(value);
}

//github.com/ <summary>
//github.com/ Tests if the title is not null, empty nor consists only of white-space characters.
//github.com/ </summary>
[JsonIgnore]
public bool HasTitle => !string.IsNullOrWhiteSpace(Title);

//github.com/ <summary>
//github.com/ Gets or sets the description.
//github.com/ </summary>
Expand Down Expand Up @@ -249,18 +261,6 @@ public int KeywordCount
}
}

//github.com/ <summary>
//github.com/ Gets or sets the raw data.
//github.com/ </summary>
[JsonPropertyName("raw")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[Description("The source object.")]
public object Raw
{
get => GetCurrentProperty<object>();
set => SetCurrentProperty(value);
}

//github.com/ <summary>
//github.com/ Gets or sets the optional additional object.
//github.com/ </summary>
Expand Down Expand Up @@ -378,3 +378,91 @@ public override string ToString()
return col.Count < 1 ? (Id ?? string.Concat('(', GetType().Name, ')')) : string.Join(Environment.NewLine, col);
}
}

//github.com/ <summary>
//github.com/ The concise model content.
//github.com/ </summary>
//github.com/ <typeparam name="T">The type of raw object.</typeparam>
public class ConciseModel<T> : ConciseModel
{
//github.com/ <summary>
//github.com/ Initializes a new instance of the ConciseModel class.
//github.com/ </summary>
public ConciseModel()
: base()
{
}

//github.com/ <summary>
//github.com/ Initializes a new instance of the ConciseModel class.
//github.com/ </summary>
//github.com/ <param name="id">The identifier of the video.</param>
//github.com/ <param name="title">The title of the video.</param>
//github.com/ <param name="link">The URL of the video to play.</param>
//github.com/ <param name="description">The video description.</param>
//github.com/ <param name="keywords">The keywords of the video.</param>
public ConciseModel(string id, string title, string link = null, string description = null, IEnumerable<string> keywords = null)
: base(id, title, link, description, keywords)
{
}

//github.com/ <summary>
//github.com/ Initializes a new instance of the BaseConciseModel class.
//github.com/ </summary>
//github.com/ <param name="id">The identifier of the video.</param>
//github.com/ <param name="title">The title of the video.</param>
//github.com/ <param name="link">The URL of the video to play.</param>
//github.com/ <param name="description">The video description.</param>
//github.com/ <param name="keywords">The keywords of the video.</param>
public ConciseModel(Guid id, string title, string link = null, string description = null, IEnumerable<string> keywords = null)
: base(id, title, link, description, keywords)
{
}

//github.com/ <summary>
//github.com/ Initializes a new instance of the ConciseModel class.
//github.com/ </summary>
//github.com/ <param name="id">The identifier of the video.</param>
//github.com/ <param name="title">The title of the video.</param>
//github.com/ <param name="keywords">The keywords of the video.</param>
//github.com/ <param name="link">The URL of the video to play.</param>
public ConciseModel(string id, string title, IEnumerable<string> keywords, string link = null)
: base(id, title, keywords, link)
{
}

//github.com/ <summary>
//github.com/ Initializes a new instance of the ConciseModel class.
//github.com/ </summary>
//github.com/ <param name="id">The identifier of the video.</param>
//github.com/ <param name="title">The title of the video.</param>
//github.com/ <param name="keywords">The keywords of the video.</param>
//github.com/ <param name="link">The URL of the video to play.</param>
public ConciseModel(Guid id, string title, IEnumerable<string> keywords, string link = null)
: base(id, title, keywords, link)
{
}

//github.com/ <summary>
//github.com/ Initializes a new instance of the ConciseModel class.
//github.com/ </summary>
//github.com/ <param name="copy">The model to copy.</param>
public ConciseModel(IConciseModel copy)
: base(copy)
{
if (copy is not ConciseModel<T> model) return;
Tag = model.Tag;
}

//github.com/ <summary>
//github.com/ Gets or sets the raw data.
//github.com/ </summary>
[JsonPropertyName("raw")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[Description("The source object.")]
public T Raw
{
get => GetCurrentProperty<T>();
set => SetCurrentProperty(value);
}
}
1 change: 1 addition & 0 deletions Core/Drawing/Color/ColorParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ internal static Color ParseValue(ref Utf8JsonReader reader)
{
try
{
JsonValues.SkipComments(ref reader);
switch (reader.TokenType)
{
case JsonTokenType.Null:
Expand Down
Loading

0 comments on commit 83ca1d6

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/nuscien/trivial/commit/83ca1d65944050e07d7fd49f11e31fcad7a52892

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy