-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from creeperlv/develop
Added a few utilities for some collections.
- Loading branch information
Showing
4 changed files
with
85 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters