Skip to content

Commit c3b549f

Browse files
authored
Merge pull request #16 from Crequency/dev=main
[Pull Request] Improvements under `Math` and `Graphics` namespace
2 parents d8893fc + 78d7c75 commit c3b549f

9 files changed

Lines changed: 174 additions & 67 deletions

File tree

.github/FUNDING.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# These are supported funding model platforms
22

3-
github: [Crequency, Dynesshely] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
3+
# github: [Crequency, Dynesshely] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
44

55
# patreon: # Replace with a single Patreon username
66
# open_collective: # Replace with a single Open Collective username

.github/workflows/auto-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050
uses: softprops/action-gh-release@v1
5151
with:
5252
tag_name: v${{ env.version }}
53-
name: Daily Release v${{ env.version }}
53+
name: Weekly Release v${{ env.version }}
5454
body: Auto release by Actions.
5555
draft: false
5656
prerelease: true

.github/workflows/build.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,18 @@ jobs:
4848
dotnet test -c Release
4949
5050
- name: Add to GitHub Repo
51-
if: ${{ matrix.os == 'ubuntu-latest' }}
51+
if: ${{ matrix.os == 'ubuntu-latest' && github.event_name != 'pull_request' }}
5252
run: |
5353
nuget sources add -name github -Source https://nuget.pkg.github.com/Crequency/index.json -Username Crequency -Password ${{ secrets.GitHubToken }}
5454
5555
- name: Install NuGet
56-
if: ${{ matrix.os == 'ubuntu-latest' }}
56+
if: ${{ matrix.os == 'ubuntu-latest' && github.event_name != 'pull_request' }}
5757
uses: nuget/setup-nuget@v1
5858
with:
5959
nuget-version: "6.x"
6060

6161
- name: Publish Package to GitHub and NuGet
62-
if: ${{ matrix.os == 'ubuntu-latest' }}
62+
if: ${{ matrix.os == 'ubuntu-latest' && github.event_name != 'pull_request' }}
6363
run: |
64-
nuget push ./Common.BasicHelper/bin/Release/*.nupkg -Source https://api.nuget.org/v3/index.json -SkipDuplicate -ApiKey ${{ secrets.NugetKey }} -NoSymbol
65-
nuget push ./Common.BasicHelper/bin/Release/*.nupkg -Source github -SkipDuplicate
64+
nuget push ./Common.BasicHelper/bin/Release/*.nupkg -Source github -SkipDuplicate -Verbosity detailed
65+
nuget push ./Common.BasicHelper/bin/Release/*.nupkg -Source https://api.nuget.org/v3/index.json -SkipDuplicate -ApiKey ${{ secrets.NugetKey }} -NoSymbol -Verbosity detailed

.gitlab/merge_request_templates/default.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## What does this PR do?
1+
## What does this MR do?
22

33
<!-- Briefly describe what this PR is about -->
44

Common.BasicHelper.Samples/Program.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Common.BasicHelper.Core.Shell;
2+
using Common.BasicHelper.Graphics.Screen;
23
using Common.BasicHelper.Utils.Extensions;
34
using System.Web;
45

@@ -20,19 +21,32 @@
2021

2122
app.UseHttpsRedirection();
2223

24+
static bool parameterNullCheck(string? param, string? name = null)
25+
{
26+
return param == "," || param == $"{{{name}}}" || param.IsNullOrWhiteSpace();
27+
}
28+
29+
app.MapGet("/", () => Results.Redirect("/swagger/index.html"));
30+
31+
app.MapGet("/Graphics/Screen/Resolution/Parse/{resolution}.{descr?}",
32+
(string resolution, string? descr) => Resolution.Parse(
33+
resolution, parameterNullCheck(descr, nameof(descr)) ? null : descr
34+
)
35+
)
36+
.WithName("ParseSolution")
37+
.WithOpenApi();
38+
2339
app.MapGet("/Utils/Extensions/StringHelper/ExecuteAsCommand/{cmd}.{args?}",
2440
(string cmd, string? args) => cmd.ExecuteAsCommand(
25-
args == "," || args == "{args}" || args.IsNullOrWhiteSpace()
26-
? null : HttpUtility.UrlDecode(args)
41+
parameterNullCheck(args, nameof(args)) ? null : HttpUtility.UrlDecode(args)
2742
)
2843
)
2944
.WithName("ExecuteAsCommand")
3045
.WithOpenApi();
3146

3247
app.MapGet("/Utils/Extensions/StringHelper/ExecuteAsCommandAsync/{cmd}.{args?}",
3348
async (string cmd, string? args) => await cmd.ExecuteAsCommandAsync(
34-
args == "," || args == "{args}" || args.IsNullOrWhiteSpace()
35-
? null : HttpUtility.UrlDecode(args)
49+
parameterNullCheck(args, nameof(args)) ? null : HttpUtility.UrlDecode(args)
3650
)
3751
)
3852
.WithName("ExecuteAsCommandAsync")

Common.BasicHelper.Test/Math/Expression_Tests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,49 @@ public void Test_Expression()
4747

4848
Assert.AreEqual(expr, tree.Result);
4949
}
50+
51+
[TestMethod()]
52+
public void Test_ToString()
53+
{
54+
var expr = "(39 / 3) + (16 * 5) - ((24 ** 2) + 5 + 7)";
55+
56+
var tree = new Expression()
57+
{
58+
Type = CalculationType.Add,
59+
Left = new()
60+
{
61+
Type = CalculationType.Division,
62+
Left = Expression.FromValue(39),
63+
Right = Expression.FromValue(3)
64+
},
65+
Right = new()
66+
{
67+
Type = CalculationType.Substraction,
68+
Left = new()
69+
{
70+
Type = CalculationType.Multiply,
71+
Left = Expression.FromValue(16),
72+
Right = Expression.FromValue(5),
73+
},
74+
Right = new()
75+
{
76+
Type = CalculationType.Add,
77+
Left = new()
78+
{
79+
Type = CalculationType.Power,
80+
Left = Expression.FromValue(24),
81+
Right = Expression.FromValue(2)
82+
},
83+
Right = new()
84+
{
85+
Type = CalculationType.Add,
86+
Left = Expression.FromValue(5),
87+
Right = Expression.FromValue(7)
88+
}
89+
}
90+
}
91+
};
92+
93+
Assert.AreEqual(expr, tree.ToString());
94+
}
5095
}

Common.BasicHelper/Graphics/Screen/Resolution.cs

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Common.BasicHelper.Utils.Extensions;
33
using System;
44
using System.Collections.Generic;
5+
using System.Linq;
56

67
namespace Common.BasicHelper.Graphics.Screen;
78

@@ -50,10 +51,10 @@ public class Resolution
5051

5152
public double? AspectRatio => Width / Height;
5253

53-
public string Description { get; set; } = string.Empty;
54+
public string? Description { get; set; }
5455

5556
/// <summary>
56-
/// 宽高整数化
57+
/// Integerize width and height
5758
/// </summary>
5859
public Resolution Integerization()
5960
{
@@ -65,10 +66,10 @@ public Resolution Integerization()
6566
}
6667

6768
/// <summary>
68-
/// 根据字符串返回分辨率对象
69+
/// Returns a resolution object based on a string
6970
/// </summary>
70-
/// <param name="input">字符串: 宽x高@刷新率</param>
71-
/// <returns>分辨率对象</returns>
71+
/// <param name="input">Format: {Width}x{Height}@{FPS}</param>
72+
/// <returns>Resolution object</returns>
7273
public static Resolution Parse(string input)
7374
{
7475
var res_fps = input.Split('@');
@@ -77,7 +78,7 @@ public static Resolution Parse(string input)
7778
var resolution = new Resolution
7879
{
7980
Width = Convert.ToDouble(res[0]),
80-
Height = Convert.ToDouble(res[1])
81+
Height = Convert.ToDouble(res[1]),
8182
};
8283

8384
if (res_fps.Length == 2) resolution.FramePerSecond = Convert.ToDouble(res_fps[1]);
@@ -87,27 +88,30 @@ public static Resolution Parse(string input)
8788
}
8889

8990
/// <summary>
90-
/// 根据字符串返回分辨率对象
91+
/// Returns a resolution object based on a string
9192
/// </summary>
92-
/// <param name="input">字符串: 宽x高@刷新率</param>
93-
/// <param name="descr">字符串: 描述信息</param>
94-
/// <returns>分辨率对象</returns>
95-
public static Resolution Parse(string input, string descr)
93+
/// <param name="input">Format: {Width}x{Height}@{FPS}</param>
94+
/// <param name="descr">Description</param>
95+
/// <returns>Resolution object</returns>
96+
public static Resolution Parse(string input, string? descr = null)
9697
{
9798
var resolution = Parse(input);
9899

99-
resolution.Description = descr;
100+
if (descr is null)
101+
resolution.Description = resolutions.FirstOrDefault((x) => x.Equals(resolution))?.Description;
102+
else
103+
resolution.Description = descr;
100104

101105
return resolution;
102106
}
103107

104108
/// <summary>
105-
/// 建议分辨率
109+
/// Suggest content resolution
106110
/// </summary>
107-
/// <param name="screen">理想屏幕</param>
108-
/// <param name="content">理想内容</param>
109-
/// <param name="actual">实际屏幕</param>
110-
/// <returns>建议的内容尺寸</returns>
111+
/// <param name="screen">Target screen resolution</param>
112+
/// <param name="content">Target content resolution</param>
113+
/// <param name="actual">Actual screen resolution</param>
114+
/// <returns>Suggested content resolution</returns>
111115
public static Resolution Suggest(Resolution screen, Resolution content, Resolution actual)
112116
{
113117
// 理想内容面积与理想屏幕面积之比
@@ -145,20 +149,8 @@ public static Resolution Suggest(Resolution screen, Resolution content, Resoluti
145149
return suggest;
146150
}
147151

148-
/// <summary>
149-
/// 重载运算符: 除法
150-
/// </summary>
151-
/// <param name="a">分辨率1</param>
152-
/// <param name="b">分辨率2</param>
153-
/// <returns>面积之比</returns>
154152
public static double? operator /(Resolution a, Resolution b) => a.Area / b.Area;
155153

156-
/// <summary>
157-
/// 重载运算符: 加法
158-
/// </summary>
159-
/// <param name="a">分辨率1</param>
160-
/// <param name="b">分辨率2</param>
161-
/// <returns>分辨率</returns>
162154
public static Resolution operator +(Resolution a, Resolution b) => new()
163155
{
164156
Width = a.Width + b.Width,
@@ -167,40 +159,19 @@ public static Resolution Suggest(Resolution screen, Resolution content, Resoluti
167159
Description = $"{a.Description}\nPlus\n{b.Description}"
168160
};
169161

170-
/// <summary>
171-
/// 重写 ToString() 方法
172-
/// </summary>
173-
/// <returns>表示分辨率及刷新率的字符串</returns>
174162
public override string ToString() =>
175163
$"{Width}x{Height}{(FramePerSecond is null ? "" : "@")}{FramePerSecond}";
176164

177-
/// <summary>
178-
/// 重写 Equals() 方法
179-
/// </summary>
180-
/// <param name="obj">目标比较分辨率</param>
181-
/// <returns>是否相等</returns>
182165
public override bool Equals(object obj)
183166
{
184167
if (obj is not Resolution)
185168
ErrorCodes.CB0017.BuildMessage(parameterName: nameof(Equals)).Throw<ArgumentException>();
186169

187-
return GetHashCode() == obj.GetHashCode();
170+
var target = obj as Resolution;
188171

189-
//if (obj is not Resolution res) return false;
190-
191-
//if (Width.Equals(res.Width) && Height.Equals(res.Height))
192-
// if (FramePerSecond != null && res.FramePerSecond != null)
193-
// if (FramePerSecond.Equals(res.FramePerSecond))
194-
// return true;
195-
// else return false;
196-
// else return true;
197-
//else return false;
172+
return Width == target?.Width && Height == target?.Height && FramePerSecond == target?.FramePerSecond;
198173
}
199174

200-
/// <summary>
201-
/// 重写 GetHashCode() 方法
202-
/// </summary>
203-
/// <returns>哈希值</returns>
204175
public override int GetHashCode() => (int)(
205176
0
206177
+ (Area.GetHashCode() ^ AspectRatio.GetHashCode())

Common.BasicHelper/Math/Expression.cs

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Collections.Generic;
2+
13
namespace Common.BasicHelper.Math;
24

35
public enum CalculationType
@@ -16,9 +18,33 @@ public class Expression
1618

1719
public CalculationType Type { get => type; set => type = value; }
1820

19-
public Expression? Left { get; set; }
21+
public Expression? Parent { get; set; }
22+
23+
private Expression? left;
2024

21-
public Expression? Right { get; set; }
25+
public Expression? Left
26+
{
27+
get => left;
28+
set
29+
{
30+
left = value;
31+
if (left is not null)
32+
left!.Parent = this;
33+
}
34+
}
35+
36+
private Expression? right;
37+
38+
public Expression? Right
39+
{
40+
get => right;
41+
set
42+
{
43+
right = value;
44+
if (right is not null)
45+
right!.Parent = this;
46+
}
47+
}
2248

2349
private double? givenValue = null;
2450

@@ -31,13 +57,49 @@ public double Result
3157
}
3258
}
3359

60+
public static Dictionary<CalculationType, string> CalculationTypeStrings = new()
61+
{
62+
{ CalculationType.Unknown, "?" },
63+
{ CalculationType.Add, "+" },
64+
{ CalculationType.Substraction, "-" },
65+
{ CalculationType.Multiply, "*" },
66+
{ CalculationType.Division, "/" },
67+
{ CalculationType.Power, "**" },
68+
};
69+
70+
public static List<CalculationType> CalculationTypeOrder = new()
71+
{
72+
CalculationType.Power,
73+
CalculationType.Multiply,
74+
CalculationType.Division,
75+
CalculationType.Add,
76+
CalculationType.Substraction,
77+
CalculationType.Unknown,
78+
};
79+
3480
public static Expression FromValue(double value)
3581
{
3682
return new()
3783
{
3884
Result = value
3985
};
4086
}
87+
88+
public override string ToString()
89+
{
90+
if (type == CalculationType.Unknown && givenValue is not null)
91+
{
92+
return givenValue.ToString();
93+
}
94+
95+
var shouldAddBrackets = true
96+
&& Parent is not null
97+
&& (CalculationTypeOrder.IndexOf(type) < CalculationTypeOrder.IndexOf(Parent.type));
98+
99+
var baseStr = $"{Left?.ToString()} {CalculationTypeStrings[type]} {Right?.ToString()}";
100+
101+
return $"{(shouldAddBrackets ? "(" : "")}{baseStr}{(shouldAddBrackets ? ")" : "")}";
102+
}
41103
}
42104

43105

0 commit comments

Comments
 (0)