-
Notifications
You must be signed in to change notification settings - Fork 72
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 #234 from Boddlnagg/projectsystem
New project system & Cargo based build system
- Loading branch information
Showing
569 changed files
with
9,795 additions
and
57,401 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
; Top-most EditorConfig file | ||
root = true | ||
|
||
; 4-column space indentation | ||
[*.cs] | ||
indent_style = space | ||
indent_size = 4 |
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,38 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Microsoft.Common.Core { | ||
public static class DictionaryExtension { | ||
public static IDictionary<string, object> FromAnonymousObject(object data) { | ||
IDictionary<string, object> dict; | ||
if (data != null) { | ||
dict = data as IDictionary<string, object>; | ||
if (dict == null) { | ||
var attr = BindingFlags.Public | BindingFlags.Instance; | ||
dict = new Dictionary<string, object>(); | ||
foreach (var property in data.GetType().GetProperties(attr)) { | ||
if (property.CanRead) { | ||
dict.Add(property.Name, property.GetValue(data, null)); | ||
} | ||
} | ||
} | ||
} | ||
else { | ||
dict = new Dictionary<string, object>(); | ||
} | ||
return dict; | ||
} | ||
|
||
public static void RemoveWhere<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, Func<KeyValuePair<TKey, TValue>, bool> predicate) { | ||
var toRemove = dictionary.Where(predicate).ToList(); | ||
foreach (var item in toRemove) { | ||
dictionary.Remove(item.Key); | ||
} | ||
} | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
Microsoft.Common.Core/Collections/EnumerableExtensions.cs
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,113 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Microsoft.Common.Core { | ||
public static class EnumerableExtensions { | ||
public static List<T> AsList<T>(this IEnumerable<T> source) { | ||
return source as List<T> ?? source.ToList(); | ||
} | ||
|
||
public static T[] AsArray<T>(this IEnumerable<T> source) { | ||
return source as T[] ?? source.ToArray(); | ||
} | ||
|
||
public static IEnumerable<T> Append<T>(this IEnumerable<T> source, T item) { | ||
foreach (T sourceItem in source) { | ||
yield return sourceItem; | ||
} | ||
|
||
yield return item; | ||
} | ||
|
||
public static void Split<T>(this IEnumerable<T> source, Func<T, bool> predicate, out IList<T> first, out IList<T> second) { | ||
first = new List<T>(); | ||
second = new List<T>(); | ||
foreach (var item in source) { | ||
if (predicate(item)) { | ||
first.Add(item); | ||
} else { | ||
second.Add(item); | ||
} | ||
} | ||
} | ||
|
||
public static void Split<TIn, TOut>(this IEnumerable<TIn> source, Func<TIn, bool> predicate, Func<TIn, TOut> converter, out IList<TOut> first, out IList<TOut> second) { | ||
first = new List<TOut>(); | ||
second = new List<TOut>(); | ||
foreach (var item in source) { | ||
if (predicate(item)) { | ||
first.Add(converter(item)); | ||
} else { | ||
second.Add(converter(item)); | ||
} | ||
} | ||
} | ||
|
||
public static IEnumerable<IReadOnlyCollection<T>> Split<T>(this IEnumerable<T> source, int chunkSize) { | ||
var index = 0; | ||
var items = new T[chunkSize]; | ||
foreach (var item in source) { | ||
items[index] = item; | ||
index++; | ||
|
||
if (index == chunkSize) { | ||
index = 0; | ||
yield return items; | ||
items = new T[chunkSize]; | ||
} | ||
} | ||
|
||
if (index > 0) { | ||
T[] lastItems = new T[index]; | ||
Array.Copy(items, 0, lastItems, 0, lastItems.Length); | ||
yield return lastItems; | ||
} | ||
} | ||
|
||
public static IEnumerable<int> IndexWhere<T>(this IEnumerable<T> source, Func<T, bool> predicate) { | ||
var i = 0; | ||
foreach (var item in source) { | ||
if (predicate(item)) { | ||
yield return i; | ||
} | ||
|
||
i++; | ||
} | ||
} | ||
|
||
public static IEnumerable<T> TraverseBreadthFirst<T>(this T root, Func<T, IEnumerable<T>> selectChildren) { | ||
Queue<T> items = new Queue<T>(); | ||
items.Enqueue(root); | ||
while (items.Count > 0) { | ||
var item = items.Dequeue(); | ||
yield return item; | ||
|
||
IEnumerable<T> childen = selectChildren(item); | ||
if (childen == null) { | ||
continue; | ||
} | ||
|
||
foreach (var child in childen) { | ||
items.Enqueue(child); | ||
} | ||
} | ||
} | ||
|
||
public static IEnumerable<T> TraverseDepthFirst<T>(this T root, Func<T, IEnumerable<T>> selectChildren) { | ||
yield return root; | ||
|
||
var children = selectChildren(root); | ||
if (children != null) { | ||
foreach (T child in children) { | ||
foreach (T t in TraverseDepthFirst(child, selectChildren)) { | ||
yield return t; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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,76 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Microsoft.Common.Core.Collections { | ||
public static class ListExtensions { | ||
public static IList<T> AddIf<T>(this IList<T> list, bool condition, T value) { | ||
if (condition) { | ||
list.Add(value); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
public static void RemoveWhere<T>(this IList<T> list, Func<T, bool> predicate) { | ||
for (var i = list.Count - 1; i >= 0; i--) { | ||
if (predicate(list[i])) { | ||
list.RemoveAt(i); | ||
} | ||
} | ||
} | ||
|
||
public static bool AddSorted<T>(this IList<T> list, T value, IComparer<T> comparer = null) { | ||
var index = list.BinarySearch(value, comparer); | ||
if (index >= 0) { | ||
return false; | ||
} | ||
|
||
list.Insert(~index, value); | ||
return true; | ||
} | ||
|
||
public static bool RemoveSorted<T>(this IList<T> list, T value, IComparer<T> comparer = null) { | ||
var index = list.BinarySearch(value, comparer); | ||
if (index < 0) { | ||
return false; | ||
} | ||
|
||
list.RemoveAt(index); | ||
return true; | ||
} | ||
|
||
public static int BinarySearch<T>(this IList<T> list, T value, IComparer<T> comparer = null) { | ||
if (list == null) { | ||
throw new ArgumentNullException(nameof(list)); | ||
} | ||
|
||
comparer = comparer ?? Comparer<T>.Default; | ||
|
||
int low = 0; | ||
int high = list.Count - 1; | ||
|
||
while (low <= high) { | ||
int mid = low + (high - low) / 2; | ||
int comparisonResult = comparer.Compare(list[mid], value); | ||
|
||
if (comparisonResult < 0) { | ||
low = mid + 1; | ||
} else if (comparisonResult > 0) { | ||
high = mid - 1; | ||
} else { | ||
return mid; | ||
} | ||
} | ||
|
||
return ~low; | ||
} | ||
|
||
public static bool Equals<T, TOther>(this IList<T> source, IList<TOther> other, Func<T, TOther, bool> predicate) { | ||
return source.Count == other.Count && !source.Where((t, i) => !predicate(t, other[i])).Any(); | ||
} | ||
} | ||
} |
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,8 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
namespace Microsoft.Common.Core.Composition { | ||
public interface INamedExport { | ||
string Name { get; } | ||
} | ||
} |
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,22 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.Composition; | ||
using System.Linq; | ||
|
||
namespace Microsoft.Common.Core.Composition { | ||
public sealed class NamedExportLocator<TExport> { | ||
[ImportMany] | ||
private IEnumerable<Lazy<TExport, INamedExport>> Exports { get; set; } | ||
|
||
public NamedExportLocator(ICompositionService cs) { | ||
cs.SatisfyImportsOnce(this); | ||
} | ||
|
||
public TExport GetExport(string name) { | ||
return Exports.FirstOrDefault(e => e.Metadata.Name.EqualsOrdinal(name)).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using System; | ||
|
||
namespace Microsoft.Common.Core.Diagnostics { | ||
public static class Check { | ||
public static void ArgumentNull(string argumentName, object argument) { | ||
if (argument == null) { | ||
throw new ArgumentNullException(argumentName); | ||
} | ||
} | ||
|
||
public static void ArgumentStringNullOrEmpty(string argumentName, string argument) { | ||
Check.ArgumentNull(argumentName, argument); | ||
|
||
if (string.IsNullOrEmpty(argument)) { | ||
throw new ArgumentException(argumentName); | ||
} | ||
} | ||
|
||
public static void ArgumentOutOfRange(string argumentName, Func<bool> predicate) { | ||
if (predicate()) { | ||
throw new ArgumentOutOfRangeException(argumentName); | ||
} | ||
} | ||
|
||
public static void InvalidOperation(Func<bool> predicate) { | ||
if (predicate()) { | ||
throw new InvalidOperationException(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.