-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an input iterator which support reading from multiple data source
- Loading branch information
1 parent
a3ffce6
commit 61c569b
Showing
7 changed files
with
325 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace LibNavigate.Converter | ||
{ | ||
/// <summary> | ||
/// Function which combine inputs to create new data type | ||
/// </summary> | ||
/// <typeparam name="Input1"></typeparam> | ||
/// <typeparam name="Input2"></typeparam> | ||
/// <typeparam name="Output"></typeparam> | ||
public interface IZipper<Input1,Input2,Output> | ||
{ | ||
|
||
Output Zip(Data.Nullable<Input1> input1, | ||
Data.Nullable<Input2> input2); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace LibNavigate.Data | ||
{ | ||
public sealed class Nullable<T> | ||
{ | ||
private readonly T value; | ||
|
||
public readonly bool HasValue; | ||
|
||
public T Value | ||
{ | ||
get | ||
{ | ||
if(!HasValue) | ||
{ | ||
throw new InvalidOperationException("Does not have value"); | ||
} | ||
|
||
return value; | ||
} | ||
} | ||
public Nullable(T value) | ||
{ | ||
this.value = value; | ||
|
||
HasValue = true; | ||
} | ||
|
||
private Nullable() | ||
{ | ||
this.value = default(T); | ||
|
||
HasValue = false; | ||
} | ||
|
||
public static Nullable<T> From(T value) | ||
{ | ||
return new Nullable<T>(value); | ||
} | ||
|
||
public static Nullable<T> Empty() | ||
{ | ||
return new Nullable<T>(); | ||
} | ||
|
||
public T GetValueOrDefault(T defaultValue) | ||
{ | ||
return HasValue ? value : defaultValue; | ||
} | ||
|
||
public override bool Equals(object other) | ||
{ | ||
if (!HasValue) return other == null; | ||
if (other == null) return false; | ||
return value.Equals(other); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return HasValue ? value.GetHashCode() : 0; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return HasValue ? value.ToString() : ""; | ||
} | ||
|
||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using LibNavigate.Converter; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace LibNavigate.Iterator.Extend | ||
{ | ||
/// <summary> | ||
/// Input iterator which support reading from multiple data source | ||
/// </summary> | ||
/// <typeparam name="Input1"></typeparam> | ||
/// <typeparam name="Input2"></typeparam> | ||
/// <typeparam name="Output"></typeparam> | ||
public sealed class MultipleInputIterator<Input1, Input2, Output> : IInputIterator<Output> | ||
{ | ||
private IInputIterator<Input1> inputIterator1; | ||
|
||
private IInputIterator<Input2> inputIterator2; | ||
|
||
private IZipper<Input1, Input2, Output> zipper; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="inputIterator1"></param> | ||
/// <param name="inputIterator2"></param> | ||
/// <param name="zipper">Function which create new data type</param> | ||
public MultipleInputIterator(IInputIterator<Input1> inputIterator1, | ||
IInputIterator<Input2> inputIterator2, | ||
IZipper<Input1, Input2, Output> zipper) | ||
{ | ||
|
||
this.inputIterator1 = inputIterator1; | ||
|
||
this.inputIterator2 = inputIterator2; | ||
|
||
this.zipper = zipper; | ||
} | ||
|
||
public void Begin() | ||
{ | ||
inputIterator1.Begin(); | ||
|
||
inputIterator2.Begin(); | ||
|
||
} | ||
|
||
public void Dispose() | ||
{ | ||
inputIterator1.Dispose(); | ||
|
||
inputIterator2.Dispose(); | ||
} | ||
|
||
public void End() | ||
{ | ||
inputIterator1.End(); | ||
|
||
inputIterator2.End(); | ||
} | ||
|
||
public bool IsEnd() | ||
{ | ||
return inputIterator1.IsEnd() && | ||
inputIterator2.IsEnd(); | ||
} | ||
|
||
public void MoveNext() | ||
{ | ||
if(!inputIterator1.IsEnd()) | ||
{ | ||
inputIterator1.MoveNext(); | ||
} | ||
|
||
if(!inputIterator2.IsEnd()) | ||
{ | ||
inputIterator2.MoveNext(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// </summary> | ||
/// <returns>Output which is create from inputs</returns> | ||
public Output Read() | ||
{ | ||
Data.Nullable<Input1> input1 = Data.Nullable<Input1>.Empty(); | ||
|
||
if(!inputIterator1.IsEnd()) | ||
{ | ||
input1 = Data.Nullable<Input1>.From(inputIterator1.Read()); | ||
} | ||
|
||
Data.Nullable<Input2> input2 = Data.Nullable<Input2>.Empty(); | ||
|
||
if (!inputIterator2.IsEnd()) | ||
{ | ||
input2 = Data.Nullable<Input2>.From(inputIterator2.Read()); | ||
} | ||
|
||
return zipper.Zip(input1,input2); | ||
} | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using LibNavigate.Converter; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using LibNavigate.Data; | ||
|
||
namespace LibNavigateTests | ||
{ | ||
public class SumZipper : IZipper<int, int, int> | ||
{ | ||
public int Zip(LibNavigate.Data.Nullable<int> input1, | ||
LibNavigate.Data.Nullable<int> input2) | ||
{ | ||
if(!input1.HasValue) | ||
{ | ||
return input2.Value; | ||
} | ||
|
||
if(!input2.HasValue) | ||
{ | ||
return input1.Value; | ||
} | ||
|
||
return input1.Value + input2.Value; | ||
} | ||
} | ||
} |