Skip to content

Commit

Permalink
Merge pull request #23 from creeperlv/develop
Browse files Browse the repository at this point in the history
Added a few utilities for some collections.
  • Loading branch information
creeperlv authored Aug 6, 2023
2 parents 4631705 + c23d2c6 commit 27d6823
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 20 deletions.
67 changes: 48 additions & 19 deletions LibCLCC.NET/Collections/ArrayTools.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,49 @@
namespace LibCLCC.NET.Collections {
/// <summary>
/// Tools for arrays.
/// </summary>
public static class ArrayTools {
/// <summary>
/// Is an array contains an element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="arr"></param>
/// <param name="t"></param>
/// <returns></returns>
public static bool Contains<T>(T[] arr,T t) {
foreach (var item in arr) {
if(item.Equals(t)) return true;
}
return false;
}
}
using System;
using System.Collections.Generic;

namespace LibCLCC.NET.Collections
{
/// <summary>
/// Tools for arrays.
/// </summary>
public static class ArrayTools
{
/// <summary>
/// Is an array contains an element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="arr"></param>
/// <param name="t"></param>
/// <returns></returns>
public static bool Contains<T>(T [ ] arr , T t)
{
foreach (var item in arr)
{
if (item.Equals(t)) return true;
}
return false;
}
/// <summary>
/// Randomly pick one in the array.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="arr"></param>
/// <param name="random"></param>
/// <returns></returns>
public static T PickOne<T>(this T [ ] arr , Random random)
{
return arr [ random.Next(arr.Length) ];
}
/// <summary>
/// Randomly pick on in the list.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="arr"></param>
/// <param name="random"></param>
/// <returns></returns>
public static T PickOne<T>(this List<T> arr , Random random)
{
return arr [ random.Next(arr.Count) ];
}
}
}
35 changes: 35 additions & 0 deletions LibCLCC.NET/Collections/DictionaryTools.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace LibCLCC.NET.Collections
{
/// <summary>
/// Some tool methods for Dictionaries.
/// </summary>
public static class DictionaryTools
{
/// <summary>
/// Merge from DataSource to Target. The DataSource will remain unchanged.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="V"></typeparam>
/// <param name="Target"></param>
/// <param name="DataSource"></param>
/// <param name="ReplaceSource"></param>
public static void Merge<T,V>(this Dictionary<T,V> Target,Dictionary<T,V> DataSource,bool ReplaceSource=true)
{
foreach (var item in DataSource)
{
if (Target.ContainsKey(item.Key))
{
if (ReplaceSource) Target [ item.Key ] = item.Value;
}
else
{
Target.Add(item.Key, item.Value);
}
}
}
}
}
2 changes: 1 addition & 1 deletion LibCLCC.NET/LibCLCC.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>$(VersionPrefix)1.18.19.0</Version>
<Version>$(VersionPrefix)1.19.20.0</Version>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Also, the library is aimed on providing functions in pure C# and compatible with
| Name | Description |
| --- | --- |
|ConnectableList | Help concatenate two lists |
|ArrayTools, DictionaryTools| Some tools for array, dictionary and list. |
|ChainAction | A Chain Action|
|BreakableFunc | A breakable function chain, returning `true` will break the chain|
|ObservableData | A wrapper of data that will invoke breakable functions when the data is changed |
Expand Down

0 comments on commit 27d6823

Please sign in to comment.