This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #70 from techno-dwarf-works/dev
Add ListExtensions.cs
- Loading branch information
Showing
4 changed files
with
80 additions
and
1 deletion.
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
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,75 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Better.Extensions.Runtime | ||
{ | ||
public static class ListExtensions | ||
{ | ||
public static bool RemoveRange<T>(this List<T> self, IEnumerable<T> range) | ||
{ | ||
if (self == null) | ||
{ | ||
DebugUtility.LogException<ArgumentNullException>(nameof(self)); | ||
return false; | ||
} | ||
|
||
if (range == null) | ||
{ | ||
DebugUtility.LogException<ArgumentNullException>(nameof(range)); | ||
return false; | ||
} | ||
|
||
var anyRemoved = false; | ||
foreach (var item in range) | ||
{ | ||
if (self.Remove(item)) | ||
{ | ||
anyRemoved = true; | ||
} | ||
} | ||
|
||
return anyRemoved; | ||
} | ||
|
||
public static void AddRangeDistinct<T>(this List<T> self, IEnumerable<T> range) | ||
{ | ||
if (self == null) | ||
{ | ||
DebugUtility.LogException<ArgumentNullException>(nameof(self)); | ||
return; | ||
} | ||
|
||
if (range == null) | ||
{ | ||
DebugUtility.LogException<ArgumentNullException>(nameof(range)); | ||
return; | ||
} | ||
|
||
foreach (var item in range) | ||
{ | ||
if (self.Contains(item)) continue; | ||
self.Add(item); | ||
} | ||
} | ||
|
||
public static bool TrimToCount<T>(this List<T> self, int count) | ||
{ | ||
if (self == null) | ||
{ | ||
DebugUtility.LogException<ArgumentNullException>(nameof(self)); | ||
return false; | ||
} | ||
|
||
count = Math.Max(count, 0); | ||
|
||
if (self.Count > count) | ||
{ | ||
var difference = self.Count - count; | ||
self.RemoveRange(count, difference); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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