Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Commit

Permalink
Added new LINQ method - ReplaceMultiple
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Dobrzyński committed Oct 31, 2017
1 parent 7023b4b commit 40a7ea7
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 44 deletions.
18 changes: 12 additions & 6 deletions KD.Linq.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2005
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KD.Linq", "KD.Linq\KD.Linq.csproj", "{3B4D83B5-AABA-483D-BAAC-C2C4D5D0AD9E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KD.Linq", "KD.Linq\KD.Linq.csproj", "{3B4D83B5-AABA-483D-BAAC-C2C4D5D0AD9E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{F3E40302-F1ED-41D4-87C6-F397A96446C4}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test_ReplaceAt", "Test_ReplaceAt\Test_ReplaceAt.csproj", "{EFA95432-08D7-40D6-9AD3-C12A4BB03C39}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test_ReplaceMultiple", "Test_ReplaceMultiple\Test_ReplaceMultiple.csproj", "{B271100D-B46E-47AE-A7E0-6054E8C73D32}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -17,10 +19,14 @@ Global
{3B4D83B5-AABA-483D-BAAC-C2C4D5D0AD9E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B4D83B5-AABA-483D-BAAC-C2C4D5D0AD9E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B4D83B5-AABA-483D-BAAC-C2C4D5D0AD9E}.Release|Any CPU.Build.0 = Release|Any CPU
{F3E40302-F1ED-41D4-87C6-F397A96446C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F3E40302-F1ED-41D4-87C6-F397A96446C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3E40302-F1ED-41D4-87C6-F397A96446C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3E40302-F1ED-41D4-87C6-F397A96446C4}.Release|Any CPU.Build.0 = Release|Any CPU
{EFA95432-08D7-40D6-9AD3-C12A4BB03C39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EFA95432-08D7-40D6-9AD3-C12A4BB03C39}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EFA95432-08D7-40D6-9AD3-C12A4BB03C39}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EFA95432-08D7-40D6-9AD3-C12A4BB03C39}.Release|Any CPU.Build.0 = Release|Any CPU
{B271100D-B46E-47AE-A7E0-6054E8C73D32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B271100D-B46E-47AE-A7E0-6054E8C73D32}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B271100D-B46E-47AE-A7E0-6054E8C73D32}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B271100D-B46E-47AE-A7E0-6054E8C73D32}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
25 changes: 23 additions & 2 deletions KD.Linq/ByIndexIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public ByIndexIterator(IEnumerable<TValue> source, int index)

public override bool MoveNext()
{
while (this.Enumerator.MoveNext())
var moved = this.Enumerator.MoveNext();
if (moved)
{
if (this.currentIndex == this.Index)
{
Expand All @@ -40,10 +41,30 @@ public override bool MoveNext()
{
OnWrongIndexHit();
}
currentIndex++;
this.currentIndex++;
return true;
}
return false;

//var localIndex = 0;
//while (this.Enumerator.MoveNext())
//{
// if (localIndex == this.currentIndex)
// {
// if (this.currentIndex == this.Index)
// {
// OnWantedIndexHit();
// }
// else
// {
// OnWrongIndexHit();
// }
// currentIndex++;
// return true;
// }
// localIndex++;
//}
//return false;
}

/// <summary>
Expand Down
9 changes: 8 additions & 1 deletion KD.Linq/Enumerables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ public static IEnumerable<TValue> ReplaceAt<TValue>(this IEnumerable<TValue> sou
return new ReplaceAtIterator<TValue>(source, index, newValue);
}

//public static IEnumerable<TValue> ReplaceMultiple<TValue>
/// <summary>
/// Replaces multiple values by given selector.
/// If the selector is null
/// </summary>
public static IEnumerable<TValue> ReplaceMultiple<TValue>(this IEnumerable<TValue> source, TValue newValue, Func<TValue, bool> selector = null)
{
return new ReplaceMultipleIterator<TValue>(source, newValue, selector);
}
}
}
10 changes: 3 additions & 7 deletions KD.Linq/ReplaceAtIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@ namespace KD.Linq
/// <summary>
/// Iterator used for ReplaceAt method.
/// </summary>
public class ReplaceAtIterator<TValue> : ByIndexIterator<TValue>
public class ReplaceAtIterator<TValue> : ReplaceIterator<TValue>
{
// Values needed for replace
private TValue newValue;

public ReplaceAtIterator(IEnumerable<TValue> source, int index, TValue newValue) :
base(source, index)
base(source, index, newValue)
{
this.newValue = newValue;
}

public override void OnWantedIndexHit()
{
this.Current = this.newValue;
this.Current = this.NewValue;
}

public override void OnWrongIndexHit()
Expand Down
25 changes: 25 additions & 0 deletions KD.Linq/ReplaceIterator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Generic;

namespace KD.Linq
{
/// <summary>
/// Class used when replacement is needed.
/// </summary>
public abstract class ReplaceIterator<TValue> : ByIndexIterator<TValue>
{
/// <summary>
/// New value for which the selected one will be replaced.
/// </summary>
public TValue NewValue { get; }

public ReplaceIterator(IEnumerable<TValue> source, int index, TValue newValue) :
base(source, index)
{
this.NewValue = newValue;
}

public override abstract void OnWantedIndexHit();

public override abstract void OnWrongIndexHit();
}
}
40 changes: 40 additions & 0 deletions KD.Linq/ReplaceMultipleIterator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;

namespace KD.Linq
{
internal class ReplaceMultipleIterator<TValue> : ReplaceIterator<TValue>
{
public Func<TValue, bool> Selector { get; }

public ReplaceMultipleIterator(IEnumerable<TValue> source, TValue newValue, Func<TValue, bool> selector) :
base(source, -1, newValue) // -1 will prevent from any hit
{
this.Selector = selector;
}

/// <summary>
/// This should never be hit.
/// </summary>
public override void OnWantedIndexHit()
{
throw new NotImplementedException();
}

/// <summary>
/// All logic should be here.
/// </summary>
public override void OnWrongIndexHit()
{
var canReplace = this.Selector(this.Enumerator.Current);
if (this.Selector != null && canReplace)
{
this.Current = this.NewValue;
}
else
{
this.Current = this.Enumerator.Current;
}
}
}
}
28 changes: 28 additions & 0 deletions Test_ReplaceAt/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using KD.Linq;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Test_ReplaceAt
{
class Program
{
static void Main(string[] args)
{
var list = new List<int>() { 1, 2, 1, 3, 1, 4 };
list.ForEach(value => Console.Write(value + ", "));

Console.WriteLine();

var list2 = list.ReplaceAt(1, 9).ToList();
list2.ForEach(value => Console.Write(value + ", "));

Console.WriteLine();

var list3 = list2.ReplaceAt(2, 7).ToList();
list3.ForEach(value => Console.Write(value + ", "));

Console.ReadKey();
}
}
}
File renamed without changes.
30 changes: 30 additions & 0 deletions Test_ReplaceMultiple/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using KD.Linq;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Test_ReplaceMultiple
{
class Program
{
static void Main(string[] args)
{
var list = new List<int>() { 1, 2, 1, 3, 1, 4 };
list.ForEach(value => Console.Write(value + ", "));

Console.WriteLine();

// Replace value at each index where value equals 1 to new value which is 9
var list2 = list.ReplaceMultiple(9, value => value == 1).ToList();
list2.ForEach(value => Console.Write(value + ", "));

Console.WriteLine();

// Replace value at each index where value equals 2 to new value which is 8
var list3 = list2.ReplaceMultiple(8, value => value == 2).ToList();
list3.ForEach(value => Console.Write(value + ", "));

Console.ReadKey();
}
}
}
12 changes: 12 additions & 0 deletions Test_ReplaceMultiple/Test_ReplaceMultiple.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\KD.Linq\KD.Linq.csproj" />
</ItemGroup>

</Project>
28 changes: 0 additions & 28 deletions Tests/Program.cs

This file was deleted.

0 comments on commit 40a7ea7

Please sign in to comment.