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 cc55d07
Show file tree
Hide file tree
Showing 29 changed files with 32 additions and 118 deletions.
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;
}
}
}
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public Microsoft_Data_Analysis(ActivationMethod activationMethod)
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

// This only works for data with exactly 25 columns.
// You must either provide the column types, or the types will be
Expand All @@ -38,17 +37,15 @@ public Microsoft_Data_Analysis(ActivationMethod activationMethod)
catch(FormatException e)
{
if (e.Message == "Empty file")
return allRecords;
yield break;
throw;
}
foreach (var row in frame.Rows)
{
var record = activate();
record.Read(i => row[i].ToString());
allRecords.Add(record);
yield return record;
}

return allRecords;
}
}
}
5 changes: 1 addition & 4 deletions NCsvPerf/CsvReadable/Implementations/Microsoft_ML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public Microsoft_ML(ActivationMethod activationMethod)
}

var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();
var mlc = new MLContext();

using (var reader = new StreamReader(stream))
Expand All @@ -55,11 +54,9 @@ public Microsoft_ML(ActivationMethod activationMethod)
{
var record = activate();
record.Read(i => { ReadOnlyMemory<char> s = null; getters[i](ref s); return s.ToString(); });
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
Loading

0 comments on commit cc55d07

Please sign in to comment.