Skip to content

Instantly share code, notes, and snippets.

@Mooophy
Last active September 29, 2016 20:28
Show Gist options
  • Save Mooophy/bebd8f28969395c0c507680a78c53bb1 to your computer and use it in GitHub Desktop.
Save Mooophy/bebd8f28969395c0c507680a78c53bb1 to your computer and use it in GitHub Desktop.
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()}");
}
}
}
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)));
}
}
@Mooophy
Copy link
Author

Mooophy commented Sep 29, 2016

<Height = 1,    Value = 0x1,    IsLeaf = False>
<Height = 2,    Value = 0x5,    IsLeaf = False> <Height = 2,    Value = 0x9,    IsLeaf = False>
<Height = 3,    Value = 0x15,   IsLeaf = False> <Height = 3,    Value = 0x25,   IsLeaf = False> <Height = 3,    Value = 0x19,   IsLeaf = False> <Height = 3,    Value = 0x29,   IsLeaf = False>
<Height = 4,    Value = 0x55,   IsLeaf = False> <Height = 4,    Value = 0x95,   IsLeaf = False> <Height = 4,    Value = 0x65,   IsLeaf = False> <Height = 4,    Value = 0xa5,   IsLeaf = False> <Height = 4,    Value = 0x59,   IsLeaf = False> <Height = 4,    Value = 0x99,   IsLeaf = False> <Height = 4,    Value = 0x69,   IsLeaf = False> <Height = 4,    Value = 0xa9,   IsLeaf = False>
<Height = 5,    Value = 0x155,  IsLeaf = False> <Height = 5,    Value = 0x255,  IsLeaf = False> <Height = 5,    Value = 0x195,  IsLeaf = False> <Height = 5,    Value = 0x295,  IsLeaf = False> <Height = 5,    Value = 0x165,  IsLeaf = False> <Height = 5,    Value = 0x265,  IsLeaf = False> <Height = 5,    Value = 0x1a5,  IsLeaf = False> <Height = 5,    Value = 0x2a5,  IsLeaf = False> <Height = 5,    Value = 0x159,  IsLeaf = False> <Height = 5,    Value = 0x259,  IsLeaf = False> <Height = 5,    Value = 0x199,  IsLeaf = False> <Height = 5,    Value = 0x299,  IsLeaf = False> <Height = 5,    Value = 0x169,  IsLeaf = False> <Height = 5,    Value = 0x269,  IsLeaf = False> <Height = 5,    Value = 0x1a9,  IsLeaf = False> <Height = 5,    Value = 0x2a9,  IsLeaf = False>
<Height = 6,    Value = 0x555,  IsLeaf = False> <Height = 6,    Value = 0x955,  IsLeaf = False> <Height = 6,    Value = 0x655,  IsLeaf = False> <Height = 6,    Value = 0xa55,  IsLeaf = False> <Height = 6,    Value = 0x595,  IsLeaf = False> <Height = 6,    Value = 0x995,  IsLeaf = False> <Height = 6,    Value = 0x695,  IsLeaf = False> <Height = 6,    Value = 0xa95,  IsLeaf = False> <Height = 6,    Value = 0x565,  IsLeaf = False> <Height = 6,    Value = 0x965,  IsLeaf = False> <Height = 6,    Value = 0x665,  IsLeaf = False> <Height = 6,    Value = 0xa65,  IsLeaf = False> <Height = 6,    Value = 0x5a5,  IsLeaf = False> <Height = 6,    Value = 0x9a5,  IsLeaf = False> <Height = 6,    Value = 0x6a5,  IsLeaf = False> <Height = 6,    Value = 0xaa5,  IsLeaf = False> <Height = 6,    Value = 0x559,  IsLeaf = False> <Height = 6,    Value = 0x959,  IsLeaf = False> <Height = 6,    Value = 0x659,  IsLeaf = False> <Height = 6,    Value = 0xa59,  IsLeaf = False> <Height = 6,    Value = 0x599,  IsLeaf = False> <Height = 6,    Value = 0x999,  IsLeaf = False> <Height = 6,    Value = 0x699,  IsLeaf = False> <Height = 6,    Value = 0xa99,  IsLeaf = False> <Height = 6,    Value = 0x569,  IsLeaf = False> <Height = 6,    Value = 0x969,  IsLeaf = False> <Height = 6,    Value = 0x669,  IsLeaf = False> <Height = 6,    Value = 0xa69,  IsLeaf = False> <Height = 6,    Value = 0x5a9,  IsLeaf = False> <Height = 6,    Value = 0x9a9,  IsLeaf = False> <Height = 6,    Value = 0x6a9,  IsLeaf = False> <Height = 6,    Value = 0xaa9,  IsLeaf = False>
<Height = 7,    Value = 0x1555, IsLeaf = True>  <Height = 7,    Value = 0x2555, IsLeaf = True>  <Height = 7,    Value = 0x1955, IsLeaf = True>  <Height = 7,    Value = 0x2955, IsLeaf = True>  <Height = 7,    Value = 0x1655, IsLeaf = True>  <Height = 7,    Value = 0x2655, IsLeaf = True>  <Height = 7,    Value = 0x1a55, IsLeaf = True>  <Height = 7,    Value = 0x2a55, IsLeaf = True>  <Height = 7,    Value = 0x1595, IsLeaf = True>  <Height = 7,    Value = 0x2595, IsLeaf = True>  <Height = 7,    Value = 0x1995, IsLeaf = True>  <Height = 7,    Value = 0x2995, IsLeaf = True>  <Height = 7,    Value = 0x1695, IsLeaf = True>  <Height = 7,    Value = 0x2695, IsLeaf = True>  <Height = 7,    Value = 0x1a95, IsLeaf = True>  <Height = 7,    Value = 0x2a95, IsLeaf = True>  <Height = 7,    Value = 0x1565, IsLeaf = True>  <Height = 7,    Value = 0x2565, IsLeaf = True>  <Height = 7,    Value = 0x1965, IsLeaf = True>  <Height = 7,    Value = 0x2965, IsLeaf = True>  <Height = 7,    Value = 0x1665, IsLeaf = True>  <Height = 7,    Value = 0x2665, IsLeaf = True>  <Height = 7,    Value = 0x1a65, IsLeaf = True>  <Height = 7,    Value = 0x2a65, IsLeaf = True>  <Height = 7,    Value = 0x15a5, IsLeaf = True>  <Height = 7,    Value = 0x25a5, IsLeaf = True>  <Height = 7,    Value = 0x19a5, IsLeaf = True>  <Height = 7,    Value = 0x29a5, IsLeaf = True>  <Height = 7,    Value = 0x16a5, IsLeaf = True>  <Height = 7,    Value = 0x26a5, IsLeaf = True>  <Height = 7,    Value = 0x1aa5, IsLeaf = True>  <Height = 7,    Value = 0x2aa5, IsLeaf = True>  <Height = 7,    Value = 0x1559, IsLeaf = True>  <Height = 7,    Value = 0x2559, IsLeaf = True>  <Height = 7,    Value = 0x1959, IsLeaf = True>  <Height = 7,    Value = 0x2959, IsLeaf = True>  <Height = 7,    Value = 0x1659, IsLeaf = True>  <Height = 7,    Value = 0x2659, IsLeaf = True>  <Height = 7,    Value = 0x1a59, IsLeaf = True>  <Height = 7,    Value = 0x2a59, IsLeaf = True>  <Height = 7,    Value = 0x1599, IsLeaf = True>  <Height = 7,    Value = 0x2599, IsLeaf = True>  <Height = 7,    Value = 0x1999, IsLeaf = True>  <Height = 7,    Value = 0x2999, IsLeaf = True>  <Height = 7,    Value = 0x1699, IsLeaf = True>  <Height = 7,    Value = 0x2699, IsLeaf = True>  <Height = 7,    Value = 0x1a99, IsLeaf = True>  <Height = 7,    Value = 0x2a99, IsLeaf = True>  <Height = 7,    Value = 0x1569, IsLeaf = True>  <Height = 7,    Value = 0x2569, IsLeaf = True>  <Height = 7,    Value = 0x1969, IsLeaf = True>  <Height = 7,    Value = 0x2969, IsLeaf = True>  <Height = 7,    Value = 0x1669, IsLeaf = True>  <Height = 7,    Value = 0x2669, IsLeaf = True>  <Height = 7,    Value = 0x1a69, IsLeaf = True>  <Height = 7,    Value = 0x2a69, IsLeaf = True>  <Height = 7,    Value = 0x15a9, IsLeaf = True>  <Height = 7,    Value = 0x25a9, IsLeaf = True>  <Height = 7,    Value = 0x19a9, IsLeaf = True>  <Height = 7,    Value = 0x29a9, IsLeaf = True>  <Height = 7,    Value = 0x16a9, IsLeaf = True>  <Height = 7,    Value = 0x26a9, IsLeaf = True>  <Height = 7,    Value = 0x1aa9, IsLeaf = True>  <Height = 7,    Value = 0x2aa9, IsLeaf = True>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment