Skip to content

Commit

Permalink
use yield return instead of List
Browse files Browse the repository at this point in the history
  • Loading branch information
leandromoh committed Dec 26, 2021
1 parent 20cd041 commit 4ff58c1
Show file tree
Hide file tree
Showing 31 changed files with 36 additions and 128 deletions.
4 changes: 1 addition & 3 deletions NCsvPerf/CsvReadable/Implementations/Angara_Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@ public override FSharpOption<Type> Invoke(Tuple<int, string> func)
{
var cols = new FSharpOption<FSharpFunc<Tuple<int, string>, FSharpOption<Type>>>(new Types());
var table = Table.Load(reader, new ReadSettings(Delimiter.Comma, false, false, FSharpOption<int>.None, cols));
var allRecords = new List<T>(table.RowsCount);
for (int r = 0; r < table.RowsCount; r++)
{
var item = activate();
item.Read(i => table[i].Rows.Item(r).AsString);
allRecords.Add(item);
yield return item;
}
return allRecords;
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions NCsvPerf/CsvReadable/Implementations/CSVFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public CSVFile(ActivationMethod activationMethod)
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

var config = new global::CSVFile.CSVSettings
{
Expand All @@ -32,11 +31,9 @@ public CSVFile(ActivationMethod activationMethod)
{
var record = activate();
record.Read(i => row[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
10 changes: 3 additions & 7 deletions NCsvPerf/CsvReadable/Implementations/ChoEtl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ public ChoETL(ActivationMethod _)

public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
return GetAssets(stream) as List<T>;
return GetAssets(stream) as IEnumerable<T>;
}

List<PackageAsset> GetAssets(MemoryStream stream)
IEnumerable<PackageAsset> GetAssets(MemoryStream stream)
{
var allRecords = new List<PackageAsset>();

var config = new global::ChoETL.ChoCSVRecordConfiguration
{
FileHeaderConfiguration = new global::ChoETL.ChoCSVFileHeaderConfiguration
Expand All @@ -37,10 +35,8 @@ List<PackageAsset> GetAssets(MemoryStream stream)
{
var asset = new PackageAsset();
asset.Read(i => record.GetString(i));
allRecords.Add(asset);
yield return asset;
}

return allRecords;
}
}
}
5 changes: 1 addition & 4 deletions NCsvPerf/CsvReadable/Implementations/CommonLibrary_Net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public CommonLibrary_NET(ActivationMethod activationMethod)
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

string text;
using (var reader = new StreamReader(stream))
Expand All @@ -37,11 +36,9 @@ public CommonLibrary_NET(ActivationMethod activationMethod)
{
var record = activate();
record.Read(i => row[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
5 changes: 1 addition & 4 deletions NCsvPerf/CsvReadable/Implementations/Csv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public Csv(ActivationMethod activationMethod)
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using (var reader = new StreamReader(stream))
{
Expand All @@ -33,11 +32,9 @@ public Csv(ActivationMethod activationMethod)
{
var record = activate();
record.Read(i => row[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
5 changes: 1 addition & 4 deletions NCsvPerf/CsvReadable/Implementations/CsvHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public CsvHelper(ActivationMethod activationMethod)
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using (var reader = new StreamReader(stream))
{
Expand All @@ -33,11 +32,9 @@ public CsvHelper(ActivationMethod activationMethod)
{
var record = activate();
record.Read(i => csvParser[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
5 changes: 1 addition & 4 deletions NCsvPerf/CsvReadable/Implementations/CsvTextFieldParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public CsvTextFieldParser(ActivationMethod activationMethod)
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using (var reader = new StreamReader(stream))
{
Expand All @@ -29,11 +28,9 @@ public CsvTextFieldParser(ActivationMethod activationMethod)
var fields = parser.ReadFields();
var record = activate();
record.Read(i => fields[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
7 changes: 2 additions & 5 deletions NCsvPerf/CsvReadable/Implementations/CsvTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public CsvTools(ActivationMethod activationMethod)
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

if (stream.Length > 0)
{
Expand All @@ -30,17 +29,15 @@ public CsvTools(ActivationMethod activationMethod)
// Read header as a row.
var r = activate();
r.Read(i => table.ColumnNames.ElementAt(i));
allRecords.Add(r);
yield return r;

foreach (var row in table.Rows)
{
var record = activate();
record.Read(i => row.Values[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
5 changes: 1 addition & 4 deletions NCsvPerf/CsvReadable/Implementations/Ctl_Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public Ctl_Data(ActivationMethod activationMethod)
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using (var streamReader = new StreamReader(stream))
{
Expand All @@ -32,11 +31,9 @@ public Ctl_Data(ActivationMethod activationMethod)
// Empty fields are returned as null by this library. Convert that to empty string to be more
// consistent with other libraries.
record.Read(i => csvReader.CurrentRow[i].Value ?? string.Empty);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
5 changes: 1 addition & 4 deletions NCsvPerf/CsvReadable/Implementations/DSV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public Dsv(ActivationMethod activationMethod)
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using (var reader = new StreamReader(stream))
{
Expand All @@ -30,11 +29,9 @@ public Dsv(ActivationMethod activationMethod)
{
var record = activate();
record.Read(i => row[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}

static IEnumerable<string> EnumerateLines(TextReader r)
Expand Down
5 changes: 1 addition & 4 deletions NCsvPerf/CsvReadable/Implementations/FastCsvParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@ public FastCsvParser(ActivationMethod activationMethod)
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using (var parser = new global::FastCsvParser.CsvReader(stream, Encoding.UTF8))
{
while (parser.MoveNext())
{
var record = activate();
record.Read(i => parser.Current[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
5 changes: 1 addition & 4 deletions NCsvPerf/CsvReadable/Implementations/FileHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public FileHelpers(ActivationMethod activationMethod)
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using (var reader = new StreamReader(stream))
{
Expand All @@ -36,12 +35,10 @@ public FileHelpers(ActivationMethod activationMethod)
// bind directly to the PackageAsset.
var record = activate();
record.Read(i => item.GetString(i));
allRecords.Add(record);
yield return record;
}
}
}

return allRecords;
}
[global::FileHelpers.DelimitedRecord(",")]
public class PackageAssetData
Expand Down
5 changes: 1 addition & 4 deletions NCsvPerf/CsvReadable/Implementations/FlatFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public FlatFiles(ActivationMethod activationMethod)
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using (var reader = new StreamReader(stream))
{
Expand All @@ -30,11 +29,9 @@ public FlatFiles(ActivationMethod activationMethod)
var values = csvReader.GetValues();
var record = activate();
record.Read(i => values[i]?.ToString() ?? string.Empty);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
5 changes: 1 addition & 4 deletions NCsvPerf/CsvReadable/Implementations/FluentCsv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public FluentCSV(ActivationMethod activationMethod)
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using (var reader = new StreamReader(stream))
{
Expand All @@ -30,11 +29,9 @@ public FluentCSV(ActivationMethod activationMethod)
var row = splitter.SplitColumns(line, ",");
var record = activate();
record.Read(i => row[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
5 changes: 1 addition & 4 deletions NCsvPerf/CsvReadable/Implementations/HomeGrown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public HomeGrown(ActivationMethod activationMethod)
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();
var fields = new List<string>();
var builder = new StringBuilder();

Expand All @@ -31,11 +30,9 @@ public HomeGrown(ActivationMethod activationMethod)
{
var record = activate();
record.Read(i => fields[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
5 changes: 1 addition & 4 deletions NCsvPerf/CsvReadable/Implementations/KBCsv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public KBCsv(ActivationMethod activationMethod)
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();
var stringPool = new StringPool(128);

using (var reader = new StreamReader(stream))
Expand All @@ -33,11 +32,9 @@ public KBCsv(ActivationMethod activationMethod)

var record = activate();
record.Read(i => row[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
5 changes: 1 addition & 4 deletions NCsvPerf/CsvReadable/Implementations/LinqToCsv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public LinqToCsv(ActivationMethod activationMethod)
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using (var reader = new StreamReader(stream))
{
Expand All @@ -33,11 +32,9 @@ public LinqToCsv(ActivationMethod activationMethod)
{
var record = activate();
record.Read(i => row[i].Value ?? string.Empty);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}

private class DataRow : List<DataRowItem>, IDataRow
Expand Down
5 changes: 1 addition & 4 deletions NCsvPerf/CsvReadable/Implementations/LumenWorksCsvReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public LumenWorksCsvReader(ActivationMethod activationMethod)
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using (var reader = new StreamReader(stream))
using (var csvReader = new LumenWorks.Framework.IO.Csv.CsvReader(reader, hasHeaders: false))
Expand All @@ -28,11 +27,9 @@ public LumenWorksCsvReader(ActivationMethod activationMethod)
{
var record = activate();
record.Read(i => csvReader[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
Loading

0 comments on commit 4ff58c1

Please sign in to comment.