Last active
September 29, 2016 20:28
-
-
Save Mooophy/bebd8f28969395c0c507680a78c53bb1 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class DecisionTree : IEnumerable<DecisionTree>, IEnumerable | |
{ | |
public int Height { get; private set; } | |
public int Value { get; private set; } | |
public DecisionTree Left { get; set; } | |
public DecisionTree Right { get; set; } | |
public bool IsLeaf | |
=> Left == null && Right == null; | |
/// <summary> | |
/// Return the leaf. | |
/// </summary> | |
/// <returns></returns> | |
public DecisionTree ToLeaf() | |
{ | |
Left = Right = null; | |
return this; | |
} | |
/// <summary> | |
/// Return the root. | |
/// </summary> | |
/// <param name="height"></param> | |
/// <returns></returns> | |
public DecisionTree ToLeaves(int height) | |
{ | |
this | |
.Where(t => t.Height == height) | |
.ToList() | |
.ForEach(t => t.ToLeaf()); | |
return this; | |
} | |
/// <summary> | |
/// Return the root. | |
/// </summary> | |
/// <param name="values"></param> | |
/// <returns></returns> | |
public DecisionTree ToLeaves(IEnumerable<int> values) | |
=> values | |
.Aggregate( | |
this, | |
(t, v) => | |
{ | |
t.FirstOrDefault(node => node.Value == v)?.ToLeaf(); | |
return t; | |
} | |
); | |
public static int Limit { get; set; } = 7; | |
public static DecisionTree Create(int height, int value) | |
=> height == Limit | |
? null | |
: new DecisionTree | |
{ | |
Height = height, | |
Value = value, | |
Left = Create(height + 1, value + (1 << (2 * height + 0))), | |
Right = Create(height + 1, value + (1 << (2 * height + 1))) | |
}; | |
/// <summary> | |
/// Implemented with Breadth-first search. | |
/// </summary> | |
/// <returns></returns> | |
public IEnumerator<DecisionTree> GetEnumerator() | |
{ | |
var queue = new Queue<DecisionTree>(); | |
queue.Enqueue(this); | |
while (queue.Count != 0) | |
{ | |
var current = queue.Dequeue(); | |
if (current.Left != null) | |
queue.Enqueue(current.Left); | |
if (current.Right != null) | |
queue.Enqueue(current.Right); | |
yield return current; | |
} | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
public override string ToString() | |
=> $"<Height = {Height},\tValue = 0x{Value:x},\tIsLeaf = {IsLeaf}>"; | |
static void Main(string[] args) | |
{ | |
Limit = 7; | |
{ | |
var tree = Create(1, 1); | |
tree | |
.ToLeaves(values: new[] { 1, 2, 3 }) | |
.ToList() | |
.ForEach(Console.WriteLine); | |
Console.WriteLine($"Count = {tree.Count()}"); | |
} | |
{ | |
var tree = Create(1, 1); | |
tree | |
.ToLeaves(height: 3) | |
.ToList() | |
.ForEach(Console.WriteLine); | |
Console.WriteLine($"Count = {tree.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
using System; | |
using System.Reflection; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class Program | |
{ | |
[AttributeUsage(AttributeTargets.All)] | |
public class StandardOperationAttribute : Attribute | |
{ | |
public bool ReadOnly { get; set; } | |
} | |
public class Foo | |
{ | |
[StandardOperation(ReadOnly = true)] | |
private static Func<int, string> UpdateDraftReference => i => nameof(UpdateDraftReference); | |
[StandardOperation(ReadOnly = false)] | |
private static Func<int, string> CheckSyntax => i => nameof(CheckSyntax); | |
[StandardOperation(ReadOnly = true)] | |
private static Func<int, string> Upgrade => i => nameof(Upgrade); | |
public IEnumerable<Func<int, string>> Run(bool readOnly) | |
=> GetType() | |
.GetProperties(BindingFlags.NonPublic | BindingFlags.Static) | |
.Where(readOnly ? IsStandardOperationReadOnly : IsStandardOperation) | |
.Select(p => p.GetValue(this)) | |
.Select(o => o as Func<int, string>); | |
private Func<PropertyInfo, bool> IsStandardOperationReadOnly | |
=> (PropertyInfo property) => IsStandardOperation(property) && property.GetCustomAttributes(false).OfType<StandardOperationAttribute>().FirstOrDefault().ReadOnly; | |
private Func<PropertyInfo, bool> IsStandardOperation | |
=> (PropertyInfo property) => Attribute.IsDefined(property, typeof(StandardOperationAttribute)); | |
} | |
static void Main(string[] args) | |
{ | |
new Foo() | |
.Run(true) | |
.ToList() | |
.ForEach(f => Console.WriteLine(f(42))); | |
new Foo() | |
.Run(false) | |
.ToList() | |
.ForEach(f => Console.WriteLine(f(42))); | |
} | |
} |
Author
Mooophy
commented
Sep 29, 2016
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment