Skip to content

Commit

Permalink
Added Combinations method for collection module and for list module, …
Browse files Browse the repository at this point in the history
…added tests for all of the added methods
  • Loading branch information
konkked committed Jan 25, 2016
1 parent 8bfe3c9 commit 8f4b7d1
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 30 deletions.
Binary file modified Underscore.Test/Collection/CreationTest.cs
Binary file not shown.
Binary file modified Underscore.Test/Collection/PartitionTest.cs
Binary file not shown.
2 changes: 1 addition & 1 deletion Underscore.Test/Function/SynchTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ public async Task FunctionThrottle()
timer.Stop( );
Thread.MemoryBarrier( );
Assert.AreEqual(3, cnt);
Assert.IsTrue(timer.ElapsedMilliseconds >= waiting, "Elapsed time : {0} < Waiting: {1}", timer.ElapsedMilliseconds, waiting);
Assert.IsTrue(Math.Abs( timer.ElapsedMilliseconds - waiting ) < 5, "Elapsed time : {0} < Waiting: {1}", timer.ElapsedMilliseconds, waiting);
Thread.MemoryBarrier( );

continuing.Clear();
Expand Down
Binary file modified Underscore.Test/List/PartitionTest.cs
Binary file not shown.
2 changes: 2 additions & 0 deletions Underscore.cs/Collection/Contract/Creation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ public interface ICreationComponent
/// creates a function that always returns a copy of the passed collection at the time it was called
/// </summary>
Func<IEnumerable<T>> Snapshot<T>( IEnumerable<T> collection );


}
}
8 changes: 8 additions & 0 deletions Underscore.cs/Collection/Contract/Partition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public interface IPartitionComponent
/// Breaks collection into two seperate parts
/// </summary>
Tuple<IEnumerable<T>, IEnumerable<T>> Partition<T>( IEnumerable<T> collection, Func<T,bool> on );

/// <summary>
/// Returns all combinations of the collection being passed
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collection"></param>
/// <returns></returns>
IEnumerable<IEnumerable<T>> Combinations<T>(IEnumerable<T> collection);
}

}
5 changes: 3 additions & 2 deletions Underscore.cs/Collection/Implementation/Creation.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Runtime.CompilerServices;

namespace Underscore.Collection
{


public class CreationComponent : ICreationComponent
{


/// <summary>
/// creates a function that always returns a copy of the passed collection at the time it was called
/// </summary>
Expand Down
42 changes: 19 additions & 23 deletions Underscore.cs/Collection/Implementation/Partition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ namespace Underscore.Collection

public class PartitionComponent : IPartitionComponent
{
private readonly List.IPartitionComponent _partitionComponent;

public PartitionComponent(List.IPartitionComponent partitionComponent)
{
_partitionComponent = partitionComponent;
}

private IEnumerable<T> Segment<T>( IEnumerator<T> iter, int size, out bool cont )
{
Expand Down Expand Up @@ -55,25 +61,6 @@ public IEnumerable<IEnumerable<T>> Chunk<T>( IEnumerable<T> collection, int size

}

private IEnumerable<T> Segment<T>( IEnumerator<T> iter, Func<T, bool> on, out bool cont )
{
var ret = new List<T>( );
cont = true;

while ( iter.MoveNext( ) )
{
if ( on( iter.Current ) )
return ret;
else
ret.Add( iter.Current );

return ret;
}

cont = false;
return ret;
}

/// <summary>
/// Breaks the collection into smaller chunks
/// </summary>
Expand All @@ -90,8 +77,7 @@ public IEnumerable<IEnumerable<T>> Chunk<T>( IEnumerable<T> collection, Func<T,
if ( on( iter.Current ) && retv.Count!=0)
{
yield return retv;
retv = new List<T>( );
retv.Add( iter.Current );
retv = new List<T> {iter.Current};
}
else
{
Expand Down Expand Up @@ -144,8 +130,8 @@ public Tuple<IEnumerable<T>, IEnumerable<T>> Partition<T>( IEnumerable<T> collec
}

return Tuple.Create(
left as IEnumerable<T>,
right as IEnumerable<T>
(IEnumerable<T>) left,
(IEnumerable<T>) right
);
}

Expand Down Expand Up @@ -192,5 +178,15 @@ right as IEnumerable<T>
);
}




public IEnumerable<IEnumerable<T>> Combinations<T>(IEnumerable<T> collection)
{
if(collection == null) throw new ArgumentNullException("collection");
var ls = collection as IList<T> ?? collection.ToList();
return _partitionComponent.Combinations(ls);
}

}
}
9 changes: 8 additions & 1 deletion Underscore.cs/List/Contract/Partition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ public interface IPartitionComponent
/// </summary>
Tuple<IList<T>, IList<T>> Split<T>( IList<T> list );


/// <summary>
/// Returns a
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="applying"></param>
/// <returns></returns>
IEnumerable<IEnumerable<T>> Combinations<T>(IList<T> list);



Expand Down
23 changes: 23 additions & 0 deletions Underscore.cs/List/Implementation/Partition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,28 @@ public Tuple<IList<T>, IList<T>> Split<T>( IList<T> list )
);
}

public IEnumerable<IEnumerable<T>> Combinations<T>(IList<T> list)
{
if(list== null) throw new ArgumentNullException("list");

yield return new T[] {};

foreach (var value in NonEmptyPermutate(list))
yield return value;
}


private IEnumerable<IEnumerable<T>> NonEmptyPermutate<T>(IList<T> collection, int index = -1)
{
if (index <= -1)
index = collection.Count - 1;

if (index == 0)
return new List<IEnumerable<T>> { new[] { collection[0] } };

var permutations = NonEmptyPermutate(collection, index - 1).ToList();
return permutations.Concat(permutations.Select(a => a.Concat(new[] { collection[index] })).Concat(new[] { new[] { collection[index] } }));
}

}
}
6 changes: 6 additions & 0 deletions Underscore.cs/Module/Collection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public Func<IEnumerable<T>> Snapshot<T>( IEnumerable<T> collection )
{
return _creator.Snapshot( collection );
}


public IEnumerable<IEnumerable<T>> Chunk<T>( IEnumerable<T> collection, int size )
{
Expand All @@ -47,5 +48,10 @@ public Tuple<IEnumerable<T>, IEnumerable<T>> Partition<T>( IEnumerable<T> collec
{
return _partitioner.Partition( collection, on );
}

public IEnumerable<IEnumerable<T>> Combinations<T>(IEnumerable<T> collection)
{
return _partitioner.Combinations(collection);
}
}
}
5 changes: 5 additions & 0 deletions Underscore.cs/Module/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public Tuple<IList<T>, IList<T>> Split<T>( IList<T> list )
return _partitioner.Split( list );
}

public IEnumerable<IEnumerable<T>> Combinations<T>(IList<T> list)
{
return _partitioner.Combinations(list);
}

public IEnumerable<IEnumerable<T>> Chunk<T>( IList<T> list, int size )
{
return _partitioner.Chunk( list, size );
Expand Down
6 changes: 3 additions & 3 deletions Underscore.cs/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
[assembly: AssemblyTitle("Underscore.cs")]
[assembly: AssemblyDescription("Unified Utility Library Underscore.cs")]
[assembly: AssemblyProduct("Underscore.cs")]
[assembly: AssemblyCopyright("Copyright © 2014-2015 Chip Keyser")]
[assembly: AssemblyCopyright("Copyright © 2014-2016 Chip Keyser")]
[assembly: ComVisible(false)]
[assembly: Guid("b96a9e7d-de48-4b0d-8397-d98a7a734880")]
[assembly: AssemblyVersion("0.1.3.9")]
[assembly: AssemblyFileVersion("0.1.3.9")]
[assembly: AssemblyVersion("0.1.3.12")]
[assembly: AssemblyFileVersion("0.1.3.12")]
1 change: 1 addition & 0 deletions Underscore.cs/Setup/Collection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public void Load(Kernel kernel)
{
kernel.Register<ICreationComponent,CreationComponent>();
kernel.Register<IPartitionComponent, PartitionComponent>();
kernel.Register<List.IPartitionComponent,List.PartitionComponent>();
kernel.Register<Module.Collection>();
}
}
Expand Down

0 comments on commit 8f4b7d1

Please sign in to comment.