Skip to content

Commit

Permalink
Merge pull request #234 from Boddlnagg/projectsystem
Browse files Browse the repository at this point in the history
New project system & Cargo based build system
  • Loading branch information
Boddlnagg authored Oct 21, 2016
2 parents 17d0eac + a41e41a commit 6e505ce
Show file tree
Hide file tree
Showing 569 changed files with 9,795 additions and 57,401 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.suo
*.user
*.sln.docstates
.vs

# Build results

Expand Down Expand Up @@ -98,6 +99,9 @@ publish/
# NuGet Packages Directory
packages/

# NuGet lock files
project.lock.json

# Windows Azure Build Output
csx
*.build.csdef
Expand Down
38 changes: 38 additions & 0 deletions Microsoft.Common.Core/Collections/DictionaryExtension.cs
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 Microsoft.Common.Core/Collections/EnumerableExtensions.cs
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;
}
}
}
}
}
}
76 changes: 76 additions & 0 deletions Microsoft.Common.Core/Collections/ListExtensions.cs
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();
}
}
}
8 changes: 8 additions & 0 deletions Microsoft.Common.Core/Composition/INamedExport.cs
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; }
}
}
22 changes: 22 additions & 0 deletions Microsoft.Common.Core/Composition/NamedExportLocator.cs
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;
}
}
}
34 changes: 34 additions & 0 deletions Microsoft.Common.Core/Diagnostics/Check.cs
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();
}
}
}
}
Loading

0 comments on commit 6e505ce

Please sign in to comment.